Skip to content

Commit 50104a7

Browse files
committed
Add more types to grammar and theme reading
1 parent 9426f0c commit 50104a7

File tree

5 files changed

+134
-79
lines changed

5 files changed

+134
-79
lines changed

build/build.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function file(language: Language, extension: Extension) {
1919
return path.join(__dirname, '..', `${language}.${extension}`);
2020
}
2121

22-
function writePlistFile(grammar: any, fileName: string) {
22+
function writePlistFile(grammar: TmGrammar | TmTheme, fileName: string) {
2323
const text = plist.build(grammar);
2424
fs.writeFileSync(fileName, text);
2525
}
@@ -35,29 +35,30 @@ function changeTsToTsx(str: string) {
3535

3636
function transformGrammarRule(rule: any, propertyNames: string[], transformProperty: (ruleProperty: string) => string) {
3737
for (const propertyName of propertyNames) {
38-
if (typeof rule[propertyName] === 'string') {
39-
rule[propertyName] = transformProperty(rule[propertyName]);
38+
const value = rule[propertyName];
39+
if (typeof value === 'string') {
40+
rule[propertyName] = transformProperty(value);
4041
}
4142
}
4243

4344
for (var propertyName in rule) {
44-
var value = rule[propertyName];
45+
const value = rule[propertyName];
4546
if (typeof value === 'object') {
4647
transformGrammarRule(value, propertyNames, transformProperty);
4748
}
4849
}
4950
}
5051

51-
function transformGrammarRepository(grammar: any, propertyNames: string[], transformProperty: (ruleProperty: string) => string) {
52+
function transformGrammarRepository(grammar: TmGrammar, propertyNames: string[], transformProperty: (ruleProperty: string) => string) {
5253
const repository = grammar.repository;
5354
for (let key in repository) {
5455
transformGrammarRule(repository[key], propertyNames, transformProperty);
5556
}
5657
}
5758

5859
function getTsxGrammar() {
59-
let variables: any;
60-
const tsxUpdatesBeforeTransformation = readYaml(file(Language.TypeScriptReact, Extension.YamlTmLangauge));
60+
let variables: MapLike<string>;
61+
const tsxUpdatesBeforeTransformation = readYaml(file(Language.TypeScriptReact, Extension.YamlTmLangauge)) as TmGrammar;
6162
const grammar = getTsGrammar(tsGrammarVariables => {
6263
variables = tsGrammarVariables;
6364
for (const variableName in tsxUpdatesBeforeTransformation.variables) {
@@ -68,11 +69,10 @@ function getTsxGrammar() {
6869
const tsxUpdates = updateGrammarVariables(tsxUpdatesBeforeTransformation, variables);
6970

7071
// Update name, file types, scope name and uuid
71-
for (let key in tsxUpdates) {
72-
if (key !== "repository") {
73-
grammar[key] = tsxUpdates[key];
74-
}
75-
}
72+
grammar.name = tsxUpdates.name;
73+
grammar.scopeName = tsxUpdates.scopeName;
74+
grammar.fileTypes = tsxUpdates.fileTypes;
75+
grammar.uuid = tsxUpdates.uuid;
7676

7777
// Update scope names to .tsx
7878
transformGrammarRepository(grammar, ["name", "contentName"], changeTsToTsx);
@@ -84,7 +84,7 @@ function getTsxGrammar() {
8484
switch(key) {
8585
case "expressionWithoutIdentifiers":
8686
// Update expression
87-
repository[key].patterns.unshift(updatesRepository[key].patterns[0]);
87+
(repository[key] as TmGrammarRepositoryPatterns).patterns.unshift((updatesRepository[key] as TmGrammarRepositoryPatterns).patterns[0]);
8888
break;
8989
default:
9090
// Add jsx
@@ -95,8 +95,8 @@ function getTsxGrammar() {
9595
return grammar;
9696
}
9797

98-
function getTsGrammar(getVariables: (tsGrammarVariables: any) => any) {
99-
const tsGrammarBeforeTransformation = readYaml(file(Language.TypeScript, Extension.YamlTmLangauge));
98+
function getTsGrammar(getVariables: (tsGrammarVariables: MapLike<string>) => MapLike<string>) {
99+
const tsGrammarBeforeTransformation = readYaml(file(Language.TypeScript, Extension.YamlTmLangauge)) as TmGrammar;
100100
return updateGrammarVariables(tsGrammarBeforeTransformation, getVariables(tsGrammarBeforeTransformation.variables));
101101
}
102102

@@ -109,7 +109,7 @@ function replacePatternVariables(pattern: string, variableReplacers: VariableRep
109109
}
110110

111111
type VariableReplacer = [RegExp, string];
112-
function updateGrammarVariables(grammar: any, variables: any) {
112+
function updateGrammarVariables(grammar: TmGrammar, variables: MapLike<string>) {
113113
delete grammar.variables;
114114
const variableReplacers: VariableReplacer[] = [];
115115
for (const variableName in variables) {
@@ -136,15 +136,12 @@ function buildGrammar() {
136136
writePlistFile(tsxGrammar, file(Language.TypeScriptReact, Extension.TmLanguage));
137137
}
138138

139-
function changeTsToTsxTheme(theme: any) {
140-
const tsxUpdates = readYaml(file(Language.TypeScriptReact, Extension.YamlTmTheme));
139+
function changeTsToTsxTheme(theme: TmTheme) {
140+
const tsxUpdates = readYaml(file(Language.TypeScriptReact, Extension.YamlTmTheme)) as TmTheme;
141141

142142
// Update name, uuid
143-
for (let key in tsxUpdates) {
144-
if (key !== "settings") {
145-
theme[key] = tsxUpdates[key];
146-
}
147-
}
143+
theme.name = tsxUpdates.name;
144+
theme.uuid = tsxUpdates.uuid;
148145

149146
// Update scope names to .tsx
150147
const settings = theme.settings;
@@ -159,7 +156,7 @@ function changeTsToTsxTheme(theme: any) {
159156
}
160157

161158
function buildTheme() {
162-
const tsTheme = readYaml(file(Language.TypeScript, Extension.YamlTmTheme));
159+
const tsTheme = readYaml(file(Language.TypeScript, Extension.YamlTmTheme)) as TmTheme;
163160

164161
// Write TypeScript.tmTheme
165162
writePlistFile(tsTheme, file(Language.TypeScript, Extension.TmTheme));

build/index.d.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
1-
declare module "plist" {
2-
export function build(json: any): string;
1+
declare module "plist" {
2+
export function build(json: any): string;
3+
}
4+
5+
declare interface MapLike<T> {
6+
[s: string]: T;
7+
}
8+
9+
declare interface TmGrammarRuleName {
10+
name: string;
11+
}
12+
13+
declare interface TmGrammarRule {
14+
name?: string;
15+
}
16+
declare interface TmGrammarMatchRule extends TmGrammarRule {
17+
match: string;
18+
captures: MapLike<TmGrammarRuleName>;
19+
}
20+
declare interface TmGrammarBeginEndRule extends TmGrammarRule {
21+
contentName?: string;
22+
begin: string;
23+
end: string;
24+
beginCaptures?: MapLike<TmGrammarRuleName>;
25+
endCaptures?: MapLike<TmGrammarRuleName>;
26+
patterns: AnyTmGrammarRule[];
27+
}
28+
declare interface TmGrammarIncludeRule extends TmGrammarRule {
29+
include: string;
30+
}
31+
declare type AnyTmGrammarRule = TmGrammarMatchRule | TmGrammarBeginEndRule | TmGrammarIncludeRule;
32+
declare interface TmGrammarRepositoryPatterns {
33+
patterns: AnyTmGrammarRule[];
34+
}
35+
declare type TmGrammarRepositaryRule = AnyTmGrammarRule | TmGrammarRepositoryPatterns;
36+
declare interface TmGrammar {
37+
name: string;
38+
scopeName: string;
39+
fileTypes: string[];
40+
uuid: string;
41+
variables: MapLike<string>;
42+
patterns?: AnyTmGrammarRule[];
43+
repository: MapLike<TmGrammarRepositaryRule>;
44+
}
45+
46+
declare interface TmThemeSetting {
47+
scope: string;
48+
settings: { vsclassificationtype: string; };
49+
}
50+
declare interface TmTheme {
51+
name: string;
52+
uuid: string;
53+
settings: TmThemeSetting[];
354
}

build/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"compilerOptions": {
33
"target": "ES5",
44
"module": "commonjs",
5-
"noImplicitAny": true
5+
"noImplicitAny": true,
6+
"skipLibCheck": true
67
},
78
"exclude": [
89
"node_modules"

package-lock.json

Lines changed: 56 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"compilerOptions": {
33
"target": "ES5",
44
"module": "commonjs",
5-
"noImplicitAny": true
5+
"noImplicitAny": true,
6+
"skipLibCheck": true
67
},
78
"exclude": [
89
"cases",

0 commit comments

Comments
 (0)