Skip to content

Commit

Permalink
fix(47597): ignore commented imports following template expression
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Feb 19, 2022
1 parent 3206df8 commit 213dbe1
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/services/preProcess.ts
Expand Up @@ -345,6 +345,42 @@ namespace ts {
break;
}

if (scanner.getToken() === SyntaxKind.TemplateHead) {
const stack = [scanner.getToken()];
loop: while (true) {
const token = scanner.getToken();
switch (token) {
case SyntaxKind.EndOfFileToken:
break loop;
case SyntaxKind.ImportKeyword:
tryConsumeImport();
break;
case SyntaxKind.TemplateHead:
stack.push(token);
break;
case SyntaxKind.OpenBraceToken:
if (stack.length > 0) {
stack.push(token);
}
break;
case SyntaxKind.CloseBraceToken:
if (stack.length > 0) {
if (lastOrUndefined(stack) === SyntaxKind.TemplateHead) {
if (scanner.reScanTemplateToken(/* isTaggedTemplate */ false) === SyntaxKind.TemplateTail) {
stack.pop();
}
}
else {
stack.pop();
}
}
break;
}
scanner.scan();
}
nextToken();
}

// check if at least one of alternative have moved scanner forward
if (tryConsumeDeclare() ||
tryConsumeImport() ||
Expand Down
111 changes: 111 additions & 0 deletions src/testRunner/unittests/services/preProcessFile.ts
Expand Up @@ -176,6 +176,117 @@ describe("unittests:: services:: PreProcessFile:", () => {
});
});

it("Correctly ignore commented imports following template expression", () => {
/* eslint-disable no-template-curly-in-string */
test("/**" + "\n" +
" * Before" + "\n" +
" * ```" + "\n" +
" * import * as a from \"a\";" + "\n" +
" * ```" + "\n" +
" */" + "\n" +
"type Foo = `${string}`;" + "\n" +
"/**" + "\n" +
" * After" + "\n" +
" * ```" + "\n" +
" * import { B } from \"b\";" + "\n" +
" * import * as c from \"c\";" + "\n" +
" * ```" + "\n" +
" */",
/*readImportFile*/ true,
/*detectJavaScriptImports*/ true,
{
referencedFiles: [],
typeReferenceDirectives: [],
libReferenceDirectives: [],
importedFiles: [],
ambientExternalModules: undefined,
isLibFile: false
});
/* eslint-enable no-template-curly-in-string */
});

it("Correctly returns imports after a template expression", () => {
/* eslint-disable no-template-curly-in-string */
test("`${foo}`; import \"./foo\";",
/*readImportFile*/ true,
/*detectJavaScriptImports*/ true,
{
referencedFiles: [],
typeReferenceDirectives: [],
libReferenceDirectives: [],
importedFiles: [
{ fileName: "./foo", pos: 17, end: 22 }
],
ambientExternalModules: undefined,
isLibFile: false
});
/* eslint-enable no-template-curly-in-string */
});

it("Correctly returns dynamic imports from template expression", () => {
/* eslint-disable no-template-curly-in-string */
test("`${(<div>Text `` ${} text {} " + "\n" +
"${import(\"a\")} {import(\"b\")} " + "\n" +
"${/* A comment */} ${/* import(\"ignored\") */} </div>)}`",
/*readImportFile*/ true,
/*detectJavaScriptImports*/ true,
{
referencedFiles: [],
typeReferenceDirectives: [],
libReferenceDirectives: [],
importedFiles: [
{ fileName: "a", pos: 39, end: 40 },
{ fileName: "b", pos: 53, end: 54 }
],
ambientExternalModules: undefined,
isLibFile: false
});
/* eslint-enable no-template-curly-in-string */
});

it("Correctly returns dynamic imports from nested template expression", () => {
/* eslint-disable no-template-curly-in-string */
test("`${foo(`${bar(`${import(\"a\")} ${import(\"b\")}`, `${baz(`${import(\"c\") ${import(\"d\")}`)}`)}`)}`",
/*readImportFile*/ true,
/*detectJavaScriptImports*/ true,
{
referencedFiles: [],
typeReferenceDirectives: [],
libReferenceDirectives: [],
importedFiles: [
{ fileName: "a", pos: 24, end: 25 },
{ fileName: "b", pos: 39, end: 40 },
{ fileName: "c", pos: 64, end: 65 },
{ fileName: "d", pos: 78, end: 79 },
],
ambientExternalModules: undefined,
isLibFile: false
});
/* eslint-enable no-template-curly-in-string */
});

it("Correctly returns dynamic imports from tagged template expression", () => {
/* eslint-disable no-template-curly-in-string */
test("foo`${ fn({ a: 100 }, import(\"a\"), `${import(\"b\")}`, import(\"c\"), `${import(\"d\")} foo`, import(\"e\")) }`",
/*readImportFile*/ true,
/*detectJavaScriptImports*/ true,
{
referencedFiles: [],
typeReferenceDirectives: [],
libReferenceDirectives: [],
importedFiles: [
{ fileName: "a", pos: 29, end: 30 },
{ fileName: "b", pos: 45, end: 46 },
{ fileName: "c", pos: 60, end: 61 },
{ fileName: "d", pos: 76, end: 77 },
{ fileName: "e", pos: 95, end: 96 },
],
ambientExternalModules: undefined,
isLibFile: false
});
/* eslint-enable no-template-curly-in-string */
});

it("Correctly return ES6 exports", () => {
test("export * from \"m1\";" + "\n" +
"export {a} from \"m2\";" + "\n" +
Expand Down

0 comments on commit 213dbe1

Please sign in to comment.