mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-11-08 06:44:12 +01:00
fix(generic): fix generic type regex
instead of looking for single tildes, we are now looking for pairs, which avoid bugs when there is an odd number of tildes
This commit is contained in:
@@ -138,7 +138,20 @@ title: Demo Class Diagram
|
|||||||
Pineapple : -int leafCount()
|
Pineapple : -int leafCount()
|
||||||
Pineapple : -int spikeCount()
|
Pineapple : -int spikeCount()
|
||||||
</pre>
|
</pre>
|
||||||
|
<hr />
|
||||||
|
|
||||||
|
<pre class="mermaid">
|
||||||
|
classDiagram
|
||||||
|
class Person {
|
||||||
|
+Id : Guid
|
||||||
|
+FirstName : string
|
||||||
|
+LastName : string
|
||||||
|
-privateProperty : string
|
||||||
|
#ProtectedProperty : string
|
||||||
|
~InternalProperty : string
|
||||||
|
~AnotherInternalProperty : List~MyType~
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
<hr />
|
<hr />
|
||||||
|
|
||||||
<script src="./mermaid.js"></script>
|
<script src="./mermaid.js"></script>
|
||||||
|
|||||||
@@ -68,5 +68,6 @@ describe('generic parser', function () {
|
|||||||
expect(parseGenericTypes('test ~Array~Array~string[]~~~')).toEqual(
|
expect(parseGenericTypes('test ~Array~Array~string[]~~~')).toEqual(
|
||||||
'test <Array<Array<string[]>>>'
|
'test <Array<Array<string[]>>>'
|
||||||
);
|
);
|
||||||
|
expect(parseGenericTypes('~test')).toEqual('~test');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -154,11 +154,17 @@ export const evaluate = (val?: string | boolean): boolean =>
|
|||||||
export const parseGenericTypes = function (text: string): string {
|
export const parseGenericTypes = function (text: string): string {
|
||||||
let cleanedText = text;
|
let cleanedText = text;
|
||||||
|
|
||||||
if (text.includes('~')) {
|
if (text.split('~').length - 1 >= 2) {
|
||||||
cleanedText = cleanedText.replace(/~([^~].*)/, '<$1');
|
let newCleanedText = cleanedText;
|
||||||
cleanedText = cleanedText.replace(/~([^~]*)$/, '>$1');
|
|
||||||
|
|
||||||
return parseGenericTypes(cleanedText);
|
// use a do...while loop instead of replaceAll to detect recursion
|
||||||
|
// e.g. Array~Array~T~~
|
||||||
|
do {
|
||||||
|
cleanedText = newCleanedText;
|
||||||
|
newCleanedText = cleanedText.replace(/~([^\s,:;]+)~/, '<$1>');
|
||||||
|
} while (newCleanedText != cleanedText);
|
||||||
|
|
||||||
|
return parseGenericTypes(newCleanedText);
|
||||||
} else {
|
} else {
|
||||||
return cleanedText;
|
return cleanedText;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user