@@ -9,8 +9,10 @@ export function run(path) {
9
9
const htmlFiles = glob . sync ( `${ path } .html` ) ;
10
10
const templateRegex = / < n g - t e m p l a t e [ ^ > ] * t r a n s l o c o [ ^ > ] * > [ ^ ] + ?< \/ n g - t e m p l a t e > / g;
11
11
const structuralRegex = / < ( [ a - z A - Z - ] * ) [ ^ * > ] * \* t r a n s l o c o = ( ' | " ) \s * l e t \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' ) ;
14
16
for ( const file of htmlFiles ) {
15
17
let str = fs . readFileSync ( file ) . toString ( 'utf8' ) ;
16
18
if ( ! str . includes ( 'transloco' ) ) continue ;
@@ -19,39 +21,41 @@ export function run(path) {
19
21
while ( containerSearch ) {
20
22
const [ matchedStr ] = containerSearch ;
21
23
let { varName } = index === 0 ? containerSearch . groups : matchedStr . match ( / l e t - (?< varName > \w * ) / ) . groups ;
22
- const keyRegex = templateKey ( varName ) ;
23
24
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
+ }
42
46
}
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 ) ;
43
57
}
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
+ } ) ;
55
59
if ( hasMatches ) {
56
60
str = str . replace ( matchedStr , newStructuralStr ) ;
57
61
}
0 commit comments