From e88dfb151a8535c1cb6e7564b13b3479d26f5075 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 4 Dec 2015 11:41:11 -0800 Subject: [PATCH] parse module specifier as string (old logic is kept for better error recovery) --- src/compiler/parser.ts | 17 +++++++++-------- .../missingSemicolonInModuleSpecifier.js | 16 ++++++++++++++++ .../missingSemicolonInModuleSpecifier.symbols | 10 ++++++++++ .../missingSemicolonInModuleSpecifier.types | 16 ++++++++++++++++ .../missingSemicolonInModuleSpecifier.ts | 8 ++++++++ 5 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 tests/baselines/reference/missingSemicolonInModuleSpecifier.js create mode 100644 tests/baselines/reference/missingSemicolonInModuleSpecifier.symbols create mode 100644 tests/baselines/reference/missingSemicolonInModuleSpecifier.types create mode 100644 tests/cases/compiler/missingSemicolonInModuleSpecifier.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 3a3350f2166..ee942a17390 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5310,16 +5310,17 @@ namespace ts { } function parseModuleSpecifier(): Expression { - // We allow arbitrary expressions here, even though the grammar only allows string - // literals. We check to ensure that it is only a string literal later in the grammar - // walker. - const result = parseExpression(); - // Ensure the string being required is in our 'identifier' table. This will ensure - // that features like 'find refs' will look inside this file when search for its name. - if (result.kind === SyntaxKind.StringLiteral) { + if (token === SyntaxKind.StringLiteral) { + const result = parseLiteralNode(); internIdentifier((result).text); + return result; + } + else { + // We allow arbitrary expressions here, even though the grammar only allows string + // literals. We check to ensure that it is only a string literal later in the grammar + // check pass. + return parseExpression(); } - return result; } function parseNamespaceImport(): NamespaceImport { diff --git a/tests/baselines/reference/missingSemicolonInModuleSpecifier.js b/tests/baselines/reference/missingSemicolonInModuleSpecifier.js new file mode 100644 index 00000000000..069849ffb83 --- /dev/null +++ b/tests/baselines/reference/missingSemicolonInModuleSpecifier.js @@ -0,0 +1,16 @@ +//// [tests/cases/compiler/missingSemicolonInModuleSpecifier.ts] //// + +//// [a.ts] + +export const x = 1; + +//// [b.ts] +import {x} from "./a" +(function() { return 1; }()) + +//// [a.js] +"use strict"; +exports.x = 1; +//// [b.js] +"use strict"; +(function () { return 1; }()); diff --git a/tests/baselines/reference/missingSemicolonInModuleSpecifier.symbols b/tests/baselines/reference/missingSemicolonInModuleSpecifier.symbols new file mode 100644 index 00000000000..695779002ac --- /dev/null +++ b/tests/baselines/reference/missingSemicolonInModuleSpecifier.symbols @@ -0,0 +1,10 @@ +=== tests/cases/compiler/a.ts === + +export const x = 1; +>x : Symbol(x, Decl(a.ts, 1, 12)) + +=== tests/cases/compiler/b.ts === +import {x} from "./a" +>x : Symbol(x, Decl(b.ts, 0, 8)) + +(function() { return 1; }()) diff --git a/tests/baselines/reference/missingSemicolonInModuleSpecifier.types b/tests/baselines/reference/missingSemicolonInModuleSpecifier.types new file mode 100644 index 00000000000..b68c68b62e0 --- /dev/null +++ b/tests/baselines/reference/missingSemicolonInModuleSpecifier.types @@ -0,0 +1,16 @@ +=== tests/cases/compiler/a.ts === + +export const x = 1; +>x : number +>1 : number + +=== tests/cases/compiler/b.ts === +import {x} from "./a" +>x : number + +(function() { return 1; }()) +>(function() { return 1; }()) : number +>function() { return 1; }() : number +>function() { return 1; } : () => number +>1 : number + diff --git a/tests/cases/compiler/missingSemicolonInModuleSpecifier.ts b/tests/cases/compiler/missingSemicolonInModuleSpecifier.ts new file mode 100644 index 00000000000..c3db4389504 --- /dev/null +++ b/tests/cases/compiler/missingSemicolonInModuleSpecifier.ts @@ -0,0 +1,8 @@ +// @module: commonjs + +// @filename: a.ts +export const x = 1; + +// @filename: b.ts +import {x} from "./a" +(function() { return 1; }()) \ No newline at end of file