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:
Tom PERRILLAT-COLLOMB
2022-12-16 22:17:39 +00:00
parent 33f78a5429
commit 5705515483
3 changed files with 24 additions and 4 deletions

View File

@@ -68,5 +68,6 @@ describe('generic parser', function () {
expect(parseGenericTypes('test ~Array~Array~string[]~~~')).toEqual(
'test <Array<Array<string[]>>>'
);
expect(parseGenericTypes('~test')).toEqual('~test');
});
});

View File

@@ -154,11 +154,17 @@ export const evaluate = (val?: string | boolean): boolean =>
export const parseGenericTypes = function (text: string): string {
let cleanedText = text;
if (text.includes('~')) {
cleanedText = cleanedText.replace(/~([^~].*)/, '<$1');
cleanedText = cleanedText.replace(/~([^~]*)$/, '>$1');
if (text.split('~').length - 1 >= 2) {
let newCleanedText = cleanedText;
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 {
return cleanedText;
}