@@ -17,23 +17,28 @@ function changeTsToTsx(str: string) {
17
17
return str . replace ( / \. t s / g, '.tsx' ) ;
18
18
}
19
19
20
- function fixRuleNames ( rule : any , name : string ) {
21
- if ( typeof rule [ name ] === 'string' ) {
22
- rule [ name ] = changeTsToTsx ( rule [ name ] ) ;
20
+ function transformGrammarRule ( rule : any , propertyNames : string [ ] , transformProperty : ( ruleProperty : string ) => string ) {
21
+ for ( const propertyName of propertyNames ) {
22
+ if ( typeof rule [ propertyName ] === 'string' ) {
23
+ rule [ propertyName ] = transformProperty ( rule [ propertyName ] ) ;
24
+ }
23
25
}
24
- }
25
26
26
- function fixGrammarScopeNames ( rule : any ) {
27
- fixRuleNames ( rule , "name" ) ;
28
- fixRuleNames ( rule , "contentName" ) ;
29
- for ( var property in rule ) {
30
- var value = rule [ property ] ;
27
+ for ( var propertyName in rule ) {
28
+ var value = rule [ propertyName ] ;
31
29
if ( typeof value === 'object' ) {
32
- fixGrammarScopeNames ( value ) ;
30
+ transformGrammarRule ( value , propertyNames , transformProperty ) ;
33
31
}
34
32
}
35
33
}
36
34
35
+ function transformGrammarRepository ( grammar : any , propertyNames : string [ ] , transformProperty : ( ruleProperty : string ) => string ) {
36
+ const repository = grammar . repository ;
37
+ for ( let key in repository ) {
38
+ transformGrammarRule ( repository [ key ] , propertyNames , transformProperty ) ;
39
+ }
40
+ }
41
+
37
42
function changeTsToTsxGrammar ( grammar : any ) {
38
43
const tsxUpdates = readYaml ( "../TypeScriptReact.YAML-tmLanguage" ) ;
39
44
@@ -45,12 +50,10 @@ function changeTsToTsxGrammar(grammar: any) {
45
50
}
46
51
47
52
// Update scope names to .tsx
48
- const repository = grammar . repository ;
49
- for ( let key in repository ) {
50
- fixGrammarScopeNames ( repository [ key ] ) ;
51
- }
53
+ transformGrammarRepository ( grammar , [ "name" , "contentName" ] , changeTsToTsx ) ;
52
54
53
55
// Add repository items
56
+ const repository = grammar . repository ;
54
57
const updatesRepository = tsxUpdates . repository ;
55
58
for ( let key in updatesRepository ) {
56
59
switch ( key ) {
@@ -67,8 +70,34 @@ function changeTsToTsxGrammar(grammar: any) {
67
70
return grammar ;
68
71
}
69
72
73
+ function replacePatternVariables ( pattern : string , variableReplacers : VariableReplacer [ ] ) {
74
+ let result = pattern ;
75
+ for ( const [ variableName , value ] of variableReplacers ) {
76
+ result = result . replace ( variableName , value ) ;
77
+ }
78
+ return result ;
79
+ }
80
+
81
+ type VariableReplacer = [ RegExp , string ] ;
82
+ function updateGrammarVariables ( grammar : any ) {
83
+ if ( grammar . variables !== undefined ) {
84
+ const variables = grammar . variables ;
85
+ delete grammar . variables ;
86
+ const variableReplacers : VariableReplacer [ ] = [ ] ;
87
+ for ( const variableName in variables ) {
88
+ variableReplacers . push ( [ new RegExp ( `{{${ variableName } }}` , "gim" ) , variables [ variableName ] ] ) ;
89
+ }
90
+ transformGrammarRepository (
91
+ grammar ,
92
+ [ "begin" , "end" , "match" ] ,
93
+ pattern => replacePatternVariables ( pattern , variableReplacers )
94
+ ) ;
95
+ }
96
+ return grammar ;
97
+ }
98
+
70
99
function buildGrammar ( ) {
71
- const tsGrammar = readYaml ( "../TypeScript.YAML-tmLanguage" ) ;
100
+ const tsGrammar = updateGrammarVariables ( readYaml ( "../TypeScript.YAML-tmLanguage" ) ) ;
72
101
73
102
// Write TypeScript.tmLanguage
74
103
writePlistFile ( tsGrammar , "../TypeScript.tmLanguage" ) ;
0 commit comments