Skip to content

Commit cfe0dcf

Browse files
committed
Validate scope extensions..
1 parent 863fe48 commit cfe0dcf

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

tests/baselines/Issue337.baseline.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@ Grammar: TypeScript.tmLanguage
99
^^
1010
source.ts comment.line.shebang.ts punctuation.definition.comment.ts
1111
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
source.ts comment.line.shebang.ts
12+
source.ts comment.line.shebang.ts
13+
14+
15+
Grammar: TypeScriptReact.tmLanguage
16+
-----------------------------------
17+
>#!/usr/bin/env node -r babel-register
18+
^^
19+
source.tsx comment.line.shebang.ts punctuation.definition.comment.ts INCORRECT_SCOPE_EXTENSION
20+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
source.tsx comment.line.shebang.ts INCORRECT_SCOPE_EXTENSION

tests/build.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ export function generateScopes(text: string, parsedFileName: path.ParsedPath) {
124124
));
125125
}
126126

127+
function validateTokenScopeExtension(grammar: Grammar, token: vt.IToken) {
128+
return !token.scopes.some(scope => !isValidScopeExtension(grammar, scope));
129+
}
130+
131+
function isValidScopeExtension(grammar: Grammar, scope: string) {
132+
return scope.endsWith(grammar.kind === GrammarKind.ts ? ".ts" : ".tsx") ||
133+
scope.endsWith(".jsdoc") ||
134+
scope.endsWith(".regexp");
135+
}
136+
127137
function generateScopesWorker(mainGrammar: Grammar, otherGrammar: Grammar | undefined, oriLines: string[]): string {
128138
let cleanLines: string[] = [];
129139
let baselineLines: string[] = [];
@@ -140,15 +150,16 @@ function generateScopesWorker(mainGrammar: Grammar, otherGrammar: Grammar | unde
140150
otherBaselines.push(">" + line);
141151

142152
for (let token of mainLineTokens) {
143-
writeTokenLine(token, "", "", baselineLines);
153+
writeTokenLine(mainGrammar, token, baselineLines);
144154
}
145155

146156
if (otherGrammar) {
147157
const otherLineTokens = tokenizeLine(otherGrammar, line);
148-
if (hasDiff(mainLineTokens, otherLineTokens, hasDiffLineToken)) {
158+
if (otherLineTokens.some(token => !validateTokenScopeExtension(otherGrammar, token)) ||
159+
hasDiff(mainLineTokens, otherLineTokens, hasDiffLineToken)) {
149160
foundDiff = true;
150161
for (let token of otherLineTokens) {
151-
writeTokenLine(token, "", "", otherBaselines);
162+
writeTokenLine(otherGrammar, token, otherBaselines);
152163
}
153164
}
154165
}
@@ -158,7 +169,7 @@ function generateScopesWorker(mainGrammar: Grammar, otherGrammar: Grammar | unde
158169
return getInputFile(cleanLines) + getBaseline(mainGrammar, baselineLines) + otherDiffBaseline;
159170
}
160171

161-
function writeTokenLine(token: vt.IToken, preTextForToken: string, postTextForToken: string, outputLines: string[]) {
172+
function writeTokenLine(grammar: Grammar, token: vt.IToken, outputLines: string[]) {
162173
let startingSpaces = " ";
163174
for (let j = 0; j < token.startIndex; j++) {
164175
startingSpaces += " ";
@@ -169,5 +180,5 @@ function writeTokenLine(token: vt.IToken, preTextForToken: string, postTextForTo
169180
locatingString += "^";
170181
}
171182
outputLines.push(startingSpaces + locatingString);
172-
outputLines.push(startingSpaces + preTextForToken + token.scopes.join(' ') + postTextForToken);
183+
outputLines.push(`${startingSpaces}${token.scopes.join(' ')}${validateTokenScopeExtension(grammar, token) ? "" : " INCORRECT_SCOPE_EXTENSION"}`);
173184
}

0 commit comments

Comments
 (0)