Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/services/preProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,31 @@ namespace ts {
let lastToken: SyntaxKind;
let currentToken: SyntaxKind;
let braceNesting = 0;
let templateNesting = 0;
// assume that text represent an external module if it contains at least one top level import/export
// ambient modules that are found inside external modules are interpreted as module augmentations
let externalModule = false;

function nextToken() {
lastToken = currentToken;
currentToken = scanner.scan();
if (currentToken === SyntaxKind.OpenBraceToken) {
if (currentToken === SyntaxKind.CloseBraceToken && templateNesting > 0) {
currentToken = scanner.reScanTemplateToken(/* isTaggedTemplate */ true);
}

if (currentToken === SyntaxKind.TemplateHead) {
Copy link
Member

@weswigham weswigham Jan 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This technically needs to track more than one scanning context. For example,

`${(<div>This is JSX text so these: `` ${} mean nothing, and in fact, the {} are
(empty) interpolation areas! So ${import("mod")} and {import("mod")} are dynamic imports! Also,
you could have ${/*A comment*/} or ${/*import("ignored")*/} a commented module </div>)}`;

The textmate grammar chokes on that one, too, since it involves multiple nested scanning contexts, but it is parsed correctly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit unfamiliar with how this works, but I will add these test cases and see if I can figure it out. Thanks!

templateNesting++;
}
else if (currentToken === SyntaxKind.TemplateTail) {
templateNesting--;
}
else if (currentToken === SyntaxKind.OpenBraceToken) {
braceNesting++;
}
else if (currentToken === SyntaxKind.CloseBraceToken) {
braceNesting--;
}

return currentToken;
}

Expand Down
44 changes: 44 additions & 0 deletions src/testRunner/unittests/services/preProcessFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,5 +659,49 @@ describe("unittests:: services:: PreProcessFile:", () => {
isLibFile: false
});
});

it("Correctly ignores ES6 imports in string templates", () => {
test("`import def from 'm1'`;",
/*readImportFile*/ true,
/*detectJavaScriptImports*/ false,
{
referencedFiles: [],
typeReferenceDirectives: [],
libReferenceDirectives: [],
importedFiles: [],
ambientExternalModules: undefined,
isLibFile: false
});
});
it("Correctly ignores ES6 imports in string templates following another template", () => {
// eslint-disable-next-line no-template-curly-in-string
test("`${foo}`;\n`import def from 'm1'`;",
/*readImportFile*/ true,
/*detectJavaScriptImports*/ false,
{
referencedFiles: [],
typeReferenceDirectives: [],
libReferenceDirectives: [],
importedFiles: [],
ambientExternalModules: undefined,
isLibFile: false
});
});
it("Correctly recognizes ES6 imports after template", () => {
// eslint-disable-next-line no-template-curly-in-string
test("`${foo}`;\nimport def from 'm1';",
/*readImportFile*/ true,
/*detectJavaScriptImports*/ false,
{
referencedFiles: [],
typeReferenceDirectives: [],
libReferenceDirectives: [],
importedFiles: [
{ fileName: "m1", pos: 26, end: 28 },
],
ambientExternalModules: undefined,
isLibFile: false
});
});
});
});