Merge pull request #3354 from mermaid-js/sidv/tsAnnotation

Add nested test for parseGenericTypes
This commit is contained in:
Knut Sveidqvist
2022-09-01 16:21:46 +02:00
committed by GitHub
4 changed files with 28 additions and 14 deletions

View File

@@ -347,10 +347,9 @@ const buildMethodDisplay = function (parsedText) {
}; };
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 don't have any match, use old format to parse text
let displayText = ''; 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(')');
@@ -370,26 +369,27 @@ const buildLegacyDisplay = function (text) {
methodName = text.substring(1, methodStart).trim(); methodName = text.substring(1, methodStart).trim();
} }
let parameters = text.substring(methodStart + 1, methodEnd); const parameters = text.substring(methodStart + 1, methodEnd);
let classifier = text.substring(methodEnd + 1, 1); const classifier = text.substring(methodEnd + 1, methodEnd + 2);
cssStyle = parseClassifier(classifier); cssStyle = parseClassifier(classifier);
displayText = visibility + methodName + '(' + parseGenericTypes(parameters.trim()) + ')'; displayText = visibility + methodName + '(' + parseGenericTypes(parameters.trim()) + ')';
if (methodEnd < memberText.length) { if (methodEnd <= text.length) {
returnType = text.substring(methodEnd + 2).trim(); returnType = text.substring(methodEnd + 2).trim();
if (returnType !== '') { if (returnType !== '') {
returnType = ' : ' + parseGenericTypes(returnType); returnType = ' : ' + parseGenericTypes(returnType);
displayText += returnType;
} }
} else {
// finally - if all else fails, just send the text back as written (other than parsing for generic types)
displayText = parseGenericTypes(text);
} }
} else {
// finally - if all else fails, just send the text back as written (other than parsing for generic types)
displayText = parseGenericTypes(text);
} }
return { return {
displayText: displayText, displayText,
cssStyle: cssStyle, cssStyle,
}; };
}; };

View File

@@ -137,6 +137,14 @@ describe('class member Renderer, ', function () {
expect(actual.displayText).toBe('+foo(List<int> ids) : List<Item>'); expect(actual.displayText).toBe('+foo(List<int> ids) : List<Item>');
expect(actual.cssStyle).toBe('font-style:italic;'); expect(actual.cssStyle).toBe('font-style:italic;');
}); });
it('should handle method declaration with nested markup', function () {
const str = '+foo ( List~List~int~~ ids )* List~List~Item~~';
let actual = svgDraw.parseMember(str);
expect(actual.displayText).toBe('+foo(List<List<int>> ids) : List<List<Item>>');
expect(actual.cssStyle).toBe('font-style:italic;');
});
}); });
describe('when parsing text to build field display string', function () { describe('when parsing text to build field display string', function () {

View File

@@ -196,8 +196,8 @@ export const parseGenericTypes = function (text) {
let cleanedText = text; let cleanedText = text;
if (text.indexOf('~') != -1) { if (text.indexOf('~') != -1) {
cleanedText = cleanedText.replace('~', '<'); cleanedText = cleanedText.replace(/~([^~].*)/, '<$1');
cleanedText = cleanedText.replace('~', '>'); cleanedText = cleanedText.replace(/~([^~]*)$/, '>$1');
return parseGenericTypes(cleanedText); return parseGenericTypes(cleanedText);
} else { } else {

View File

@@ -106,7 +106,13 @@ describe('Sanitize text', function () {
describe('generic parser', function () { describe('generic parser', function () {
it('should parse generic types', function () { it('should parse generic types', function () {
const result = parseGenericTypes('test~T~'); expect(parseGenericTypes('test~T~')).toEqual('test<T>');
expect(result).toEqual('test<T>'); expect(parseGenericTypes('test~Array~Array~string~~~')).toEqual('test<Array<Array<string>>>');
expect(parseGenericTypes('test~Array~Array~string[]~~~')).toEqual(
'test<Array<Array<string[]>>>'
);
expect(parseGenericTypes('test ~Array~Array~string[]~~~')).toEqual(
'test <Array<Array<string[]>>>'
);
}); });
}); });