Merge pull request #1446 from jgreywolf/1378-AdditionalQualifiersBugWithMultipleArguments

1378 additional qualifiers bug with multiple arguments
This commit is contained in:
Knut Sveidqvist
2020-06-10 18:57:40 +02:00
committed by GitHub
4 changed files with 66 additions and 38 deletions

View File

@@ -71,9 +71,9 @@ describe('Class diagram', () => {
classDiagram classDiagram
Class01 <|-- AveryLongClass : Cool Class01 <|-- AveryLongClass : Cool
&lt;&lt;interface&gt;&gt; Class01 &lt;&lt;interface&gt;&gt; Class01
Class01 : -int privateMethod() Class01 : -privateMethod()
Class01 : +int publicMethod() Class01 : +publicMethod()
Class01 : #int protectedMethod() Class01 : #protectedMethod()
Class01 : -int privateChimp Class01 : -int privateChimp
Class01 : +int publicGorilla Class01 : +int publicGorilla
Class01 : #int protectedMarmoset Class01 : #int protectedMarmoset

View File

@@ -278,13 +278,13 @@ export const drawClass = function(elem, classDef, conf) {
}; };
export const parseMember = function(text) { export const parseMember = function(text) {
const fieldRegEx = /^(\+|-|~|#)?(\w+)(~\w+~|\[\])?\s+(\w+)$/; const fieldRegEx = /(\+|-|~|#)?(\w+)(~\w+~|\[\])?\s+(\w+)/;
const methodRegEx = /^(\+|-|~|#)?(\w+)\s?\(\s*(\w+(~\w+~|\[\])?\s*(\w+)?)?\s*\)\s?([*|$])?\s?(\w+(~\w+~|\[\])?)?\s*$/; const methodRegEx = /^([+|\-|~|#])?(\w+) *\( *(.*)\) *(\*|\$)? *(\w*[~|[\]]*\s*\w*~?)$/;
let fieldMatch = text.match(fieldRegEx); let fieldMatch = text.match(fieldRegEx);
let methodMatch = text.match(methodRegEx); let methodMatch = text.match(methodRegEx);
if (fieldMatch) { if (fieldMatch && !methodMatch) {
return buildFieldDisplay(fieldMatch); return buildFieldDisplay(fieldMatch);
} else if (methodMatch) { } else if (methodMatch) {
return buildMethodDisplay(methodMatch); return buildMethodDisplay(methodMatch);
@@ -294,56 +294,78 @@ export const parseMember = function(text) {
}; };
const buildFieldDisplay = function(parsedText) { const buildFieldDisplay = function(parsedText) {
let displayText = '';
try {
let visibility = parsedText[1] ? parsedText[1].trim() : ''; let visibility = parsedText[1] ? parsedText[1].trim() : '';
let fieldType = parsedText[2] ? parsedText[2].trim() : ''; let fieldType = parsedText[2] ? parsedText[2].trim() : '';
let genericType = parsedText[3] ? parseGenericTypes(parsedText[3]) : ''; let genericType = parsedText[3] ? parseGenericTypes(parsedText[3].trim()) : '';
let fieldName = parsedText[4] ? parsedText[4].trim() : ''; let fieldName = parsedText[4] ? parsedText[4].trim() : '';
displayText = visibility + fieldType + genericType + ' ' + fieldName;
} catch (err) {
displayText = parsedText;
}
return { return {
displayText: visibility + fieldType + genericType + ' ' + fieldName, displayText: displayText,
cssStyle: '' cssStyle: ''
}; };
}; };
const buildMethodDisplay = function(parsedText) { const buildMethodDisplay = function(parsedText) {
let cssStyle = ''; let cssStyle = '';
let displayText = parsedText; let displayText = '';
try {
let visibility = parsedText[1] ? parsedText[1].trim() : ''; let visibility = parsedText[1] ? parsedText[1].trim() : '';
let methodName = parsedText[2] ? parsedText[2].trim() : ''; let methodName = parsedText[2] ? parsedText[2].trim() : '';
let parameters = parsedText[3] ? parseGenericTypes(parsedText[3]) : ''; let parameters = parsedText[3] ? parseGenericTypes(parsedText[3].trim()) : '';
let classifier = parsedText[6] ? parsedText[6].trim() : ''; let classifier = parsedText[4] ? parsedText[4].trim() : '';
let returnType = parsedText[7] ? ' : ' + parseGenericTypes(parsedText[7]).trim() : ''; let returnType = parsedText[5] ? ' : ' + parseGenericTypes(parsedText[5]).trim() : '';
displayText = visibility + methodName + '(' + parameters + ')' + returnType; displayText = visibility + methodName + '(' + parameters + ')' + returnType;
cssStyle = parseClassifier(classifier); cssStyle = parseClassifier(classifier);
} catch (err) {
displayText = parsedText;
}
let member = { return {
displayText: displayText, displayText: displayText,
cssStyle: cssStyle cssStyle: cssStyle
}; };
return member;
}; };
const buildLegacyDisplay = function(text) { const buildLegacyDisplay = function(text) {
// if for some reason we dont have any match, use old format to parse text // if for some reason we dont have any match, use old format to parse text
let memberText = ''; let displayText = '';
let cssStyle = ''; let cssStyle = '';
let memberText = '';
let returnType = ''; let returnType = '';
let methodStart = text.indexOf('('); let methodStart = text.indexOf('(');
let methodEnd = text.indexOf(')'); let methodEnd = text.indexOf(')');
if (methodStart > 1 && methodEnd > methodStart && methodEnd <= text.length) { if (methodStart > 1 && methodEnd > methodStart && methodEnd <= text.length) {
let parsedText = text.match(/(\+|-|~|#)?(\w+)/); let visibility = '';
let visibility = parsedText[1] ? parsedText[1].trim() : ''; let methodName = '';
let methodName = parsedText[2];
let firstChar = text.substring(0, 1);
if (firstChar.match(/\w/)) {
methodName = text.substring(0, methodStart).trim();
} else {
if (firstChar.match(/\+|-|~|#/)) {
visibility = firstChar;
}
methodName = text.substring(1, methodStart).trim();
}
let parameters = text.substring(methodStart + 1, methodEnd); let parameters = text.substring(methodStart + 1, methodEnd);
let classifier = text.substring(methodEnd, methodEnd + 1); let classifier = text.substring(methodEnd + 1, 1);
cssStyle = parseClassifier(classifier); cssStyle = parseClassifier(classifier);
memberText = visibility + methodName + '(' + parseGenericTypes(parameters.trim()) + ')'; displayText = visibility + methodName + '(' + parseGenericTypes(parameters.trim()) + ')';
if (methodEnd < memberText.length) { if (methodEnd < memberText.length) {
returnType = text.substring(methodEnd + 2).trim(); returnType = text.substring(methodEnd + 2).trim();
@@ -353,15 +375,13 @@ const buildLegacyDisplay = function(text) {
} }
} else { } else {
// finally - if all else fails, just send the text back as written (other than parsing for generic types) // finally - if all else fails, just send the text back as written (other than parsing for generic types)
memberText = parseGenericTypes(text); displayText = parseGenericTypes(text);
} }
let member = { return {
displayText: memberText + returnType, displayText: displayText,
cssStyle: cssStyle cssStyle: cssStyle
}; };
return member;
}; };
const addTspan = function(textEl, txt, isFirst, conf) { const addTspan = function(textEl, txt, isFirst, conf) {

View File

@@ -83,6 +83,14 @@ describe('class member Renderer, ', function () {
expect(actual.cssStyle).toBe(''); expect(actual.cssStyle).toBe('');
}); });
it('should handle simple method declaration with multiple parameters', function () {
const str = 'foo(int id, object thing)';
let actual = svgDraw.parseMember(str);
expect(actual.displayText).toBe('foo(int id, object thing)');
expect(actual.cssStyle).toBe('');
});
it('should handle simple method declaration with single item in parameters', function () { it('should handle simple method declaration with single item in parameters', function () {
const str = 'foo(id)'; const str = 'foo(id)';
let actual = svgDraw.parseMember(str); let actual = svgDraw.parseMember(str);