From 4f1b9082124c1616c9dcac2d0e2766810e087b8f Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 27 Jan 2015 15:42:36 -0800 Subject: [PATCH] Parse import ModuleSpecifier; --- src/compiler/parser.ts | 37 ++++++++++++++++--- .../reference/es6ImportParseErrors.errors.txt | 8 ++++ .../reference/es6ImportWithoutFromClause.js | 12 ++++++ .../es6ImportWithoutFromClause.types | 8 ++++ .../es6ImportWithoutFromClauseInEs5.js | 12 ++++++ .../es6ImportWithoutFromClauseInEs5.types | 8 ++++ tests/cases/compiler/es6ImportParseErrors.ts | 4 ++ .../compiler/es6ImportWithoutFromClause.ts | 8 ++++ .../es6ImportWithoutFromClauseInEs5.ts | 8 ++++ 9 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/es6ImportParseErrors.errors.txt create mode 100644 tests/baselines/reference/es6ImportWithoutFromClause.js create mode 100644 tests/baselines/reference/es6ImportWithoutFromClause.types create mode 100644 tests/baselines/reference/es6ImportWithoutFromClauseInEs5.js create mode 100644 tests/baselines/reference/es6ImportWithoutFromClauseInEs5.types create mode 100644 tests/cases/compiler/es6ImportParseErrors.ts create mode 100644 tests/cases/compiler/es6ImportWithoutFromClause.ts create mode 100644 tests/cases/compiler/es6ImportWithoutFromClauseInEs5.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 7453e5597f..2e6132287d 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1511,7 +1511,6 @@ module ts { return token === SyntaxKind.GreaterThanToken || token === SyntaxKind.OpenParenToken; case ParsingContext.HeritageClauses: return token === SyntaxKind.OpenBraceToken || token === SyntaxKind.CloseBraceToken; - } } @@ -4519,10 +4518,14 @@ module ts { return nextToken() === SyntaxKind.OpenParenToken; } - function parseImportEqualsDeclaration(fullStart: number, modifiers: ModifiersArray): ImportEqualsDeclaration { + function parseImportDeclarationOrStatement(fullStart: number, modifiers: ModifiersArray): ImportEqualsDeclaration | ImportStatement { + parseExpected(SyntaxKind.ImportKeyword); + if (token === SyntaxKind.StringLiteral) { + return parseImportStatement(fullStart, modifiers); + } + var node = createNode(SyntaxKind.ImportEqualsDeclaration, fullStart); setModifiers(node, modifiers); - parseExpected(SyntaxKind.ImportKeyword); node.name = parseIdentifier(); parseExpected(SyntaxKind.EqualsToken); node.moduleReference = parseModuleReference(); @@ -4556,6 +4559,30 @@ module ts { return finishNode(node); } + function parseImportStatement(fullStart: number, modifiers: ModifiersArray): ImportStatement { + var node = createNode(SyntaxKind.ImportStatement, fullStart); + setModifiers(node, modifiers); + + // ImportDeclaration: + // import ModuleSpecifier; + node.moduleSpecifier = parseModuleSpecifier(); + parseSemicolon(); + return finishNode(node); + } + + function parseModuleSpecifier(): StringLiteralExpression { + // ModuleSpecifier: + // StringLiteral + if (token === SyntaxKind.StringLiteral) { + // 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. + var moduleSpecifier = parseLiteralNode(/*internName*/ true); + return moduleSpecifier; + } + + parseErrorAtCurrentToken(Diagnostics.String_literal_expected); + } + function parseExportAssignmentTail(fullStart: number, modifiers: ModifiersArray): ExportAssignment { var node = createNode(SyntaxKind.ExportAssignment, fullStart); setModifiers(node, modifiers); @@ -4581,10 +4608,10 @@ module ts { case SyntaxKind.ClassKeyword: case SyntaxKind.InterfaceKeyword: case SyntaxKind.EnumKeyword: - case SyntaxKind.ImportKeyword: case SyntaxKind.TypeKeyword: // Not true keywords so ensure an identifier follows return lookAhead(nextTokenIsIdentifierOrKeyword); + case SyntaxKind.ImportKeyword: case SyntaxKind.ModuleKeyword: // Not a true keyword so ensure an identifier or string literal follows return lookAhead(nextTokenIsIdentifierOrKeywordOrStringLiteral); @@ -4653,7 +4680,7 @@ module ts { case SyntaxKind.ModuleKeyword: return parseModuleDeclaration(fullStart, modifiers); case SyntaxKind.ImportKeyword: - return parseImportEqualsDeclaration(fullStart, modifiers); + return parseImportDeclarationOrStatement(fullStart, modifiers); default: Debug.fail("Mismatch between isDeclarationStart and parseDeclaration"); } diff --git a/tests/baselines/reference/es6ImportParseErrors.errors.txt b/tests/baselines/reference/es6ImportParseErrors.errors.txt new file mode 100644 index 0000000000..d1e1cb7d00 --- /dev/null +++ b/tests/baselines/reference/es6ImportParseErrors.errors.txt @@ -0,0 +1,8 @@ +tests/cases/compiler/es6ImportParseErrors.ts(2,1): error TS1128: Declaration or statement expected. + + +==== tests/cases/compiler/es6ImportParseErrors.ts (1 errors) ==== + + import 10; + ~~~~~~ +!!! error TS1128: Declaration or statement expected. \ No newline at end of file diff --git a/tests/baselines/reference/es6ImportWithoutFromClause.js b/tests/baselines/reference/es6ImportWithoutFromClause.js new file mode 100644 index 0000000000..aa5e25bcd6 --- /dev/null +++ b/tests/baselines/reference/es6ImportWithoutFromClause.js @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/es6ImportWithoutFromClause.ts] //// + +//// [es6ImportWithoutFromClause_0.ts] + +export var a = 10; + +//// [es6ImportWithoutFromClause_1.ts] +import "es6ImportWithoutFromClause_0"; + +//// [es6ImportWithoutFromClause_0.js] +exports.a = 10; +//// [es6ImportWithoutFromClause_1.js] diff --git a/tests/baselines/reference/es6ImportWithoutFromClause.types b/tests/baselines/reference/es6ImportWithoutFromClause.types new file mode 100644 index 0000000000..1cd7df9962 --- /dev/null +++ b/tests/baselines/reference/es6ImportWithoutFromClause.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/es6ImportWithoutFromClause_0.ts === + +export var a = 10; +>a : number + +=== tests/cases/compiler/es6ImportWithoutFromClause_1.ts === +import "es6ImportWithoutFromClause_0"; +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/es6ImportWithoutFromClauseInEs5.js b/tests/baselines/reference/es6ImportWithoutFromClauseInEs5.js new file mode 100644 index 0000000000..3782987c2f --- /dev/null +++ b/tests/baselines/reference/es6ImportWithoutFromClauseInEs5.js @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/es6ImportWithoutFromClauseInEs5.ts] //// + +//// [es6ImportWithoutFromClauseInEs5_0.ts] + +export var a = 10; + +//// [es6ImportWithoutFromClauseInEs5_1.ts] +import "es6ImportWithoutFromClauseInEs5_0"; + +//// [es6ImportWithoutFromClauseInEs5_0.js] +exports.a = 10; +//// [es6ImportWithoutFromClauseInEs5_1.js] diff --git a/tests/baselines/reference/es6ImportWithoutFromClauseInEs5.types b/tests/baselines/reference/es6ImportWithoutFromClauseInEs5.types new file mode 100644 index 0000000000..3d674f9c22 --- /dev/null +++ b/tests/baselines/reference/es6ImportWithoutFromClauseInEs5.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/es6ImportWithoutFromClauseInEs5_0.ts === + +export var a = 10; +>a : number + +=== tests/cases/compiler/es6ImportWithoutFromClauseInEs5_1.ts === +import "es6ImportWithoutFromClauseInEs5_0"; +No type information for this code. \ No newline at end of file diff --git a/tests/cases/compiler/es6ImportParseErrors.ts b/tests/cases/compiler/es6ImportParseErrors.ts new file mode 100644 index 0000000000..2cc21dad74 --- /dev/null +++ b/tests/cases/compiler/es6ImportParseErrors.ts @@ -0,0 +1,4 @@ +// @target: es6 +// @module: commonjs + +import 10; \ No newline at end of file diff --git a/tests/cases/compiler/es6ImportWithoutFromClause.ts b/tests/cases/compiler/es6ImportWithoutFromClause.ts new file mode 100644 index 0000000000..70a15a9bc5 --- /dev/null +++ b/tests/cases/compiler/es6ImportWithoutFromClause.ts @@ -0,0 +1,8 @@ +// @target: es6 +// @module: commonjs + +// @filename: es6ImportWithoutFromClause_0.ts +export var a = 10; + +// @filename: es6ImportWithoutFromClause_1.ts +import "es6ImportWithoutFromClause_0"; \ No newline at end of file diff --git a/tests/cases/compiler/es6ImportWithoutFromClauseInEs5.ts b/tests/cases/compiler/es6ImportWithoutFromClauseInEs5.ts new file mode 100644 index 0000000000..339880ee09 --- /dev/null +++ b/tests/cases/compiler/es6ImportWithoutFromClauseInEs5.ts @@ -0,0 +1,8 @@ +// @target: es5 +// @module: commonjs + +// @filename: es6ImportWithoutFromClauseInEs5_0.ts +export var a = 10; + +// @filename: es6ImportWithoutFromClauseInEs5_1.ts +import "es6ImportWithoutFromClauseInEs5_0"; \ No newline at end of file