Skip to content

Commit e390ae4

Browse files
committed
fix: 🐛 fix v2 upgrade script
1 parent 73b4dd3 commit e390ae4

File tree

1 file changed

+36
-32
lines changed
  • schematics/src/upgrade

1 file changed

+36
-32
lines changed

schematics/src/upgrade/v2.ts

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ export function run(path) {
99
const htmlFiles = glob.sync(`${path}.html`);
1010
const templateRegex = /<ng-template[^>]*transloco[^>]*>[^]+?<\/ng-template>/g;
1111
const structuralRegex = /<([a-zA-Z-]*)[^*>]*\*transloco=('|")\s*let\s+(?<varName>\w*)[^>]*\2>[^]+?<\/\1\s*>/g;
12-
const templateKey = varName =>
13-
new RegExp(`(?<rawKey>${varName}(?:(?:\\[(?:'|"))|\\.)(?:[^}|:]*))(?<param>[\\s\\w|:{}'"$&!?%@#^)(]*)}}`, 'g');
12+
const coreKeyRegex = varName =>
13+
`(?<rawKey>${varName}(?:(?:\\[('|").+\\1\\])|(?:\\.\\w+))+)(?<param>[\\s\\w|:{}'"$&!?%@#^)(]*)`;
14+
const bindingKey = varName => new RegExp(`("|')\\s*${coreKeyRegex(varName)}\\1`, 'g');
15+
const templateKey = varName => new RegExp(`{{\\s*${coreKeyRegex(varName)}}}`, 'g');
1416
for (const file of htmlFiles) {
1517
let str = fs.readFileSync(file).toString('utf8');
1618
if (!str.includes('transloco')) continue;
@@ -19,39 +21,41 @@ export function run(path) {
1921
while (containerSearch) {
2022
const [matchedStr] = containerSearch;
2123
let { varName } = index === 0 ? containerSearch.groups : matchedStr.match(/let-(?<varName>\w*)/).groups;
22-
const keyRegex = templateKey(varName);
2324
let newStructuralStr = matchedStr;
24-
let keySearch = keyRegex.exec(matchedStr);
25-
let hasMatches = !!keySearch;
26-
while (keySearch) {
27-
const { rawKey, param } = keySearch.groups;
28-
/** The raw key may contain square braces we need to align it to '.' */
29-
let [key, ...inner] = rawKey
30-
.trim()
31-
.replace(/\[/g, '.')
32-
.replace(/'|"|\]|\?/g, '')
33-
.replace(`${varName}.`, '')
34-
.split('.');
35-
let callEnd = ')';
36-
const pipes = (param && param.split('|')) || [];
37-
const [paramsPipe] = pipes.filter(pipe => pipe.includes('translocoParams'));
38-
if (paramsPipe) {
39-
const paramValue = paramsPipe.substring(paramsPipe.indexOf('{'), paramsPipe.lastIndexOf('}') + 1);
40-
if (paramValue) {
41-
callEnd = `, ${paramValue})`;
25+
let hasMatches;
26+
[templateKey(varName), bindingKey(varName)].forEach(keyRegex => {
27+
let keySearch = keyRegex.exec(matchedStr);
28+
hasMatches = hasMatches || !!keySearch;
29+
while (keySearch) {
30+
const { rawKey, param } = keySearch.groups;
31+
/** The raw key may contain square braces we need to align it to '.' */
32+
let [key, ...inner] = rawKey
33+
.trim()
34+
.replace(/\[/g, '.')
35+
.replace(/'|"|\]|\?/g, '')
36+
.replace(`${varName}.`, '')
37+
.split('.');
38+
let callEnd = ')';
39+
const pipes = (param && param.split('|')) || [];
40+
const [paramsPipe] = pipes.filter(pipe => pipe.includes('translocoParams'));
41+
if (paramsPipe) {
42+
const paramValue = paramsPipe.substring(paramsPipe.indexOf('{'), paramsPipe.lastIndexOf('}') + 1);
43+
if (paramValue) {
44+
callEnd = `, ${paramValue})`;
45+
}
4246
}
47+
if (inner.length) {
48+
key = `${key}.${inner.join('.')}`;
49+
}
50+
newStructuralStr = paramsPipe
51+
? newStructuralStr.replace(
52+
`${rawKey}${param}`,
53+
`${varName}('${key}'${callEnd} ${pipes.filter(pipe => !pipe.includes('translocoParams')).join('|')}`
54+
)
55+
: newStructuralStr.replace(rawKey, `${varName}('${key}'${callEnd}`);
56+
keySearch = keyRegex.exec(matchedStr);
4357
}
44-
if (inner.length) {
45-
key = `${key}.${inner.join('.')}`;
46-
}
47-
newStructuralStr = paramsPipe
48-
? newStructuralStr.replace(
49-
`${rawKey}${param}`,
50-
`${varName}('${key}'${callEnd} ${pipes.filter(pipe => !pipe.includes('translocoParams')).join('|')}`
51-
)
52-
: newStructuralStr.replace(rawKey, `${varName}('${key}'${callEnd}`);
53-
keySearch = keyRegex.exec(matchedStr);
54-
}
58+
});
5559
if (hasMatches) {
5660
str = str.replace(matchedStr, newStructuralStr);
5761
}

0 commit comments

Comments
 (0)