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 9b93410
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/services/preProcess.ts
Expand Up @@ -345,6 +345,20 @@ namespace ts {
break;
}

if (scanner.getToken() === SyntaxKind.TemplateHead) {
while (true) {
if (scanner.getToken() === SyntaxKind.EndOfFileToken) {
break;
}
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
44 changes: 44 additions & 0 deletions src/testRunner/unittests/services/preProcessFile.ts
Expand Up @@ -176,6 +176,50 @@ describe("unittests:: services:: PreProcessFile:", () => {
});
});

it("Correctly ignore commented imports following template expression", () => {
test("/**" + "\n" +
" * Before" + "\n" +
" * ```" + "\n" +
" * import * as a from \"a\";" + "\n" +
" * ```" + "\n" +
" */" + "\n" +
"type Foo = `${string}`;" + "\n" + // eslint-disable-line no-template-curly-in-string
"/**" + "\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
});
});

it("Correctly returns imports after a template expression", () => {
// eslint-disable-next-line 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
});
});

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

0 comments on commit 9b93410

Please sign in to comment.