Skip to content

Commit 889b710

Browse files
committed
fix(47597): ignore commented imports following template expression
1 parent 3206df8 commit 889b710

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/services/preProcess.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,23 @@ namespace ts {
345345
break;
346346
}
347347

348+
if (scanner.getToken() === SyntaxKind.TemplateHead) {
349+
while (true) {
350+
if (scanner.getToken() === SyntaxKind.EndOfFileToken) {
351+
break;
352+
}
353+
if (scanner.getToken() === SyntaxKind.ImportKeyword) {
354+
tryConsumeImport();
355+
}
356+
if (scanner.getToken() === SyntaxKind.CloseBraceToken) {
357+
scanner.reScanTemplateToken(/*isTaggedTemplate*/ false);
358+
break;
359+
}
360+
scanner.scan();
361+
}
362+
nextToken();
363+
}
364+
348365
// check if at least one of alternative have moved scanner forward
349366
if (tryConsumeDeclare() ||
350367
tryConsumeImport() ||

src/testRunner/unittests/services/preProcessFile.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,74 @@ describe("unittests:: services:: PreProcessFile:", () => {
176176
});
177177
});
178178

179+
it("Correctly ignore commented imports following template expression", () => {
180+
/* eslint-disable no-template-curly-in-string */
181+
test("/**" + "\n" +
182+
" * Before" + "\n" +
183+
" * ```" + "\n" +
184+
" * import * as a from \"a\";" + "\n" +
185+
" * ```" + "\n" +
186+
" */" + "\n" +
187+
"type Foo = `${string}`;" + "\n" +
188+
"/**" + "\n" +
189+
" * After" + "\n" +
190+
" * ```" + "\n" +
191+
" * import { B } from \"b\";" + "\n" +
192+
" * import * as c from \"c\";" + "\n" +
193+
" * ```" + "\n" +
194+
" */",
195+
/*readImportFile*/ true,
196+
/*detectJavaScriptImports*/ true,
197+
{
198+
referencedFiles: [],
199+
typeReferenceDirectives: [],
200+
libReferenceDirectives: [],
201+
importedFiles: [],
202+
ambientExternalModules: undefined,
203+
isLibFile: false
204+
});
205+
/* eslint-enable no-template-curly-in-string */
206+
});
207+
208+
it("Correctly returns imports after a template expression", () => {
209+
/* eslint-disable no-template-curly-in-string */
210+
test("`${foo}`; import \"./foo\";",
211+
/*readImportFile*/ true,
212+
/*detectJavaScriptImports*/ true,
213+
{
214+
referencedFiles: [],
215+
typeReferenceDirectives: [],
216+
libReferenceDirectives: [],
217+
importedFiles: [
218+
{ fileName: "./foo", pos: 17, end: 22 }
219+
],
220+
ambientExternalModules: undefined,
221+
isLibFile: false
222+
});
223+
/* eslint-enable no-template-curly-in-string */
224+
});
225+
226+
it("Correctly returns dynamic imports from template expression", () => {
227+
/* eslint-disable no-template-curly-in-string */
228+
test("`${(<div>Text `` ${} text {} " + "\n" +
229+
"${import(\"a\")} {import(\"b\")} " + "\n" +
230+
"${/* A comment */} ${/* import(\"ignored\") */} </div>)}`",
231+
/*readImportFile*/ true,
232+
/*detectJavaScriptImports*/ true,
233+
{
234+
referencedFiles: [],
235+
typeReferenceDirectives: [],
236+
libReferenceDirectives: [],
237+
importedFiles: [
238+
{ fileName: "a", pos: 39, end: 40 },
239+
{ fileName: "b", pos: 53, end: 54 }
240+
],
241+
ambientExternalModules: undefined,
242+
isLibFile: false
243+
});
244+
/* eslint-enable no-template-curly-in-string */
245+
});
246+
179247
it("Correctly return ES6 exports", () => {
180248
test("export * from \"m1\";" + "\n" +
181249
"export {a} from \"m2\";" + "\n" +

0 commit comments

Comments
 (0)