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 Jan 29, 2022
1 parent 3206df8 commit 889b710
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/services/preProcess.ts
Expand Up @@ -345,6 +345,23 @@ namespace ts {
break;
}

if (scanner.getToken() === SyntaxKind.TemplateHead) {
while (true) {
if (scanner.getToken() === SyntaxKind.EndOfFileToken) {
break;
}
if (scanner.getToken() === SyntaxKind.ImportKeyword) {
tryConsumeImport();
}
if (scanner.getToken() === SyntaxKind.CloseBraceToken) {
scanner.reScanTemplateToken(/*isTaggedTemplate*/ false);
break;
}
scanner.scan();
}
nextToken();
}

// check if at least one of alternative have moved scanner forward
if (tryConsumeDeclare() ||
tryConsumeImport() ||
Expand Down
68 changes: 68 additions & 0 deletions src/testRunner/unittests/services/preProcessFile.ts
Expand Up @@ -176,6 +176,74 @@ 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 return ES6 exports", () => {
test("export * from \"m1\";" + "\n" +
"export {a} from \"m2\";" + "\n" +
Expand Down

0 comments on commit 889b710

Please sign in to comment.