Skip to content

Commit c9c9bc7

Browse files
committed
Fix tests to use latest vscode-textmate extension
1 parent e6a877d commit c9c9bc7

File tree

8 files changed

+99
-46
lines changed

8 files changed

+99
-46
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: node_js
22

33
node_js:
44
- 'stable'
5-
- '8'
5+
- '10'
66

77
sudo: false
88

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Test against this version of Node.js
22
environment:
3-
nodejs_version: "8"
3+
nodejs_version: "10"
44

55
# Install scripts. (runs after repo cloning)
66
install:

azure-pipelines.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ jobs:
66

77
# All tasks on Linux
88
- job: build_all_linux_legacy
9-
displayName: Build all tasks using node 8 (Linux)
9+
displayName: Build all tasks using node 10 (Linux)
1010
variables:
11-
node_version: '8.x'
11+
node_version: '10.x'
1212
pool:
1313
vmImage: 'Ubuntu 16.04'
1414
steps:
@@ -18,7 +18,7 @@ jobs:
1818
- job: build_all_windows
1919
displayName: Build all tasks (Windows)
2020
variables:
21-
node_version: '11.x'
21+
node_version: '12.x'
2222
pool:
2323
vmImage: vs2017-win2016
2424
steps:
@@ -28,7 +28,7 @@ jobs:
2828
- job: build_all_linux
2929
displayName: Build all tasks (Linux)
3030
variables:
31-
node_version: '11.x'
31+
node_version: '12.x'
3232
pool:
3333
vmImage: 'Ubuntu 16.04'
3434
steps:
@@ -38,7 +38,7 @@ jobs:
3838
- job: build_all_darwin
3939
displayName: Build all tasks (macOS)
4040
variables:
41-
node_version: '11.x'
41+
node_version: '12.x'
4242
pool:
4343
vmImage: macos-10.13
4444
steps:

build/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function getTsxGrammar() {
6666
}
6767
return variables;
6868
});
69-
const tsxUpdates = updateGrammarVariables(tsxUpdatesBeforeTransformation, variables);
69+
const tsxUpdates = updateGrammarVariables(tsxUpdatesBeforeTransformation, variables!);
7070

7171
// Update name, file types, scope name and uuid
7272
grammar.name = tsxUpdates.name;

build/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"target": "ES5",
44
"module": "commonjs",
5-
"noImplicitAny": true,
5+
"strict": true,
66
"skipLibCheck": true
77
},
88
"exclude": [

tests/build.ts

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,52 @@
11
import * as vt from 'vscode-textmate/release/main';
22
import path = require('path');
3+
import fs = require('fs');
34

4-
const tsGrammarFileName = "TypeScript.tmLanguage"
5-
const tsReactGrammarFileName = "TypeScriptReact.tmLanguage"
5+
enum GrammarKind {
6+
ts = 'source.ts',
7+
tsx = 'source.tsx'
8+
}
9+
const grammarFileNames: Record<GrammarKind, string> = {
10+
[GrammarKind.ts]: "TypeScript.tmLanguage",
11+
[GrammarKind.tsx]: "TypeScriptReact.tmLanguage"
12+
};
13+
function grammarPath(kind: GrammarKind) {
14+
return path.join(__dirname, '..', grammarFileNames[kind]);
15+
}
16+
const grammarPaths = {
17+
[GrammarKind.ts]: grammarPath(GrammarKind.ts),
18+
[GrammarKind.tsx]: grammarPath(GrammarKind.tsx)
19+
};
20+
21+
const registery = new vt.Registry({
22+
loadGrammar: function (scopeName: GrammarKind) {
23+
const path = grammarPaths[scopeName];
24+
if (path) {
25+
return new Promise((resolve, reject) => {
26+
fs.readFile(path, (error, content) => {
27+
if (error) {
28+
reject(error);
29+
} else {
30+
const rawGrammar = vt.parseRawGrammar(content.toString(), path);
31+
resolve(rawGrammar);
32+
}
33+
});
34+
});
35+
}
636

7-
const register = new vt.Registry();
8-
const tsGrammar = register.loadGrammarFromPathSync(path.join(__dirname, '..', tsGrammarFileName));
9-
const tsReactGrammar = register.loadGrammarFromPathSync(path.join(__dirname, '..', tsReactGrammarFileName));
37+
return Promise.resolve(null);
38+
}
39+
});
40+
41+
interface ThenableGrammar {
42+
kind: GrammarKind;
43+
grammar: vt.Thenable<vt.IGrammar>;
44+
}
45+
function thenableGrammar(kind: GrammarKind): ThenableGrammar {
46+
return { kind, grammar: registery.loadGrammar(kind) };
47+
}
48+
const tsGrammar = thenableGrammar(GrammarKind.ts);
49+
const tsReactGrammar = thenableGrammar(GrammarKind.tsx);
1050

1151
const marker = '^^';
1252

@@ -31,24 +71,21 @@ function getInputFile(oriLines: string[]): string {
3171
"\n-----------------------------------\n\n";
3272
}
3373

34-
function getGrammarInfo(grammarFileName: string) {
35-
return "Grammar: " + grammarFileName + "\n-----------------------------------\n";
74+
function getGrammarInfo(kind: GrammarKind) {
75+
return "Grammar: " + grammarFileNames[kind] + "\n-----------------------------------\n";
3676
}
3777

38-
const tsGrammarInfo = getGrammarInfo(tsGrammarFileName);
39-
const tsReactGrammarInfo = getGrammarInfo(tsReactGrammarFileName);
40-
4178
interface Grammar {
79+
kind: GrammarKind;
4280
grammar: vt.IGrammar;
4381
ruleStack?: vt.StackElement;
4482
}
45-
46-
function initGrammar(grammar: vt.IGrammar): Grammar {
47-
return { grammar };
83+
function initGrammar(kind: GrammarKind, grammar: vt.IGrammar): Grammar {
84+
return { kind, grammar };
4885
}
4986

5087
function tokenizeLine(grammar: Grammar, line: string) {
51-
const lineTokens = grammar.grammar.tokenizeLine(line, grammar.ruleStack);
88+
const lineTokens = grammar.grammar.tokenizeLine(line, grammar.ruleStack!);
5289
grammar.ruleStack = lineTokens.ruleStack;
5390
return lineTokens.tokens;
5491
}
@@ -82,20 +119,29 @@ function hasDiffLineToken(first: vt.IToken, second: vt.IToken) {
82119
}
83120

84121
function getBaseline(grammar: Grammar, outputLines: string[]) {
85-
const grammarInfo = grammar.grammar === tsGrammar ? tsGrammarInfo : tsReactGrammarInfo;
86-
return grammarInfo + outputLines.join('\n');
122+
return getGrammarInfo(grammar.kind) + outputLines.join('\n');
87123
}
88124

89-
export function generateScopes(text: string, parsedFileName: path.ParsedPath): { markerScopes: string, wholeBaseline: string } {
90-
const grammar = parsedFileName.ext === '.tsx' ? tsReactGrammar : tsGrammar;
125+
export function generateScopes(text: string, parsedFileName: path.ParsedPath) {
126+
const mainGrammar = parsedFileName.ext === '.tsx' ? tsReactGrammar : tsGrammar;
91127
const oriLines = text.split(/\r\n|\r|\n/);
128+
const otherGrammar = oriLines[0].search(/\/\/\s*@onlyOwnGrammar/i) < 0 ?
129+
mainGrammar === tsGrammar ? tsReactGrammar : tsGrammar :
130+
undefined;
131+
132+
return Promise.all([
133+
mainGrammar.grammar,
134+
otherGrammar ?
135+
otherGrammar.grammar :
136+
Promise.resolve(undefined)
137+
]).then(([mainIGrammar, otherIGrammar]) => generateScopesWorker(
138+
initGrammar(mainGrammar.kind, mainIGrammar),
139+
otherIGrammar && initGrammar(otherGrammar!.kind, otherIGrammar),
140+
oriLines
141+
));
142+
}
92143

93-
let mainGrammar = initGrammar(grammar);
94-
let otherGrammar: Grammar = null;
95-
if (oriLines[0].search(/\/\/\s*@onlyOwnGrammar/i) < 0) {
96-
otherGrammar = initGrammar(grammar === tsGrammar ? tsReactGrammar : tsGrammar);
97-
}
98-
144+
function generateScopesWorker(mainGrammar: Grammar, otherGrammar: Grammar | undefined, oriLines: string[]): { markerScopes: string | undefined, wholeBaseline: string } {
99145
let outputLines: string[] = [];
100146
let cleanLines: string[] = [];
101147
let baselineLines: string[] = [];
@@ -136,9 +182,9 @@ export function generateScopes(text: string, parsedFileName: path.ParsedPath): {
136182
}
137183
}
138184

139-
const otherDiffBaseline = foundDiff ? "\n\n\n" + getBaseline(otherGrammar, otherBaselines) : "";
185+
const otherDiffBaseline = foundDiff ? "\n\n\n" + getBaseline(otherGrammar!, otherBaselines) : "";
140186
return {
141-
markerScopes: markers ? (getInputFile(oriLines) + getBaseline(mainGrammar, outputLines)) : null,
187+
markerScopes: markers ? (getInputFile(oriLines) + getBaseline(mainGrammar, outputLines)) : undefined,
142188
wholeBaseline: getInputFile(cleanLines) + getBaseline(mainGrammar, baselineLines) + otherDiffBaseline
143189
};
144190
}

tests/test.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,24 @@ ensureCleanGeneratedFolder();
2222

2323
// Generate the new baselines
2424
for (const fileName of fs.readdirSync(casesFolder)) {
25-
const text = fs.readFileSync(path.join(casesFolder, fileName), 'utf8');
26-
const parsedFileName = path.parse(fileName);
27-
28-
let wholeBaseline: string;
29-
let markerScopes: string;
30-
3125
describe("Generating baseline for " + fileName, () => {
32-
before(() => {
33-
const result = build.generateScopes(text, parsedFileName)
34-
wholeBaseline = result.wholeBaseline;
35-
markerScopes = result.markerScopes;
26+
let wholeBaseline: string;
27+
let markerScopes: string | undefined;
28+
let parsedFileName: path.ParsedPath;
29+
30+
before(done => {
31+
const text = fs.readFileSync(path.join(casesFolder, fileName), 'utf8');
32+
parsedFileName = path.parse(fileName);
33+
build.generateScopes(text, parsedFileName).then(result => {
34+
wholeBaseline = result.wholeBaseline;
35+
markerScopes = result.markerScopes;
36+
done();
37+
})
38+
});
39+
after(() => {
40+
wholeBaseline = undefined!;
41+
markerScopes = undefined!;
42+
parsedFileName = undefined!;
3643
});
3744

3845
it('Comparing whole baseline', () => {

tests/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"target": "ES5",
44
"module": "commonjs",
5-
"noImplicitAny": true,
5+
"strict": true,
66
"skipLibCheck": true
77
},
88
"exclude": [

0 commit comments

Comments
 (0)