Skip to content

Commit

Permalink
Parse import ModuleSpecifier;
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Jan 28, 2015
1 parent 5bd8271 commit 4f1b908
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,6 @@ module ts {
return token === SyntaxKind.GreaterThanToken || token === SyntaxKind.OpenParenToken;
case ParsingContext.HeritageClauses:
return token === SyntaxKind.OpenBraceToken || token === SyntaxKind.CloseBraceToken;

}
}

Expand Down Expand Up @@ -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);

This comment has been minimized.

Copy link
@JsonFreeman

JsonFreeman Jan 28, 2015

Contributor

How does this type check? You are returning an ImportStatement, when the stated return type is ImportEqualsDeclaration? Seems incorrect

This comment has been minimized.

Copy link
@sheetalkamat

sheetalkamat Jan 28, 2015

Author Member

Because return type is ImportStatement | ImportEqualsDeclaration.

This comment has been minimized.

Copy link
@JsonFreeman

JsonFreeman Jan 28, 2015

Contributor

Oh, didn't see it because of github

}

var node = <ImportEqualsDeclaration>createNode(SyntaxKind.ImportEqualsDeclaration, fullStart);
setModifiers(node, modifiers);
parseExpected(SyntaxKind.ImportKeyword);
node.name = parseIdentifier();
parseExpected(SyntaxKind.EqualsToken);
node.moduleReference = parseModuleReference();
Expand Down Expand Up @@ -4556,6 +4559,30 @@ module ts {
return finishNode(node);
}

function parseImportStatement(fullStart: number, modifiers: ModifiersArray): ImportStatement {
var node = <ImportStatement>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 = <StringLiteralExpression>parseLiteralNode(/*internName*/ true);

This comment has been minimized.

Copy link
@CyrusNajmabadi

CyrusNajmabadi Jan 28, 2015

Contributor

just reutrn this.

return moduleSpecifier;
}

parseErrorAtCurrentToken(Diagnostics.String_literal_expected);
}

function parseExportAssignmentTail(fullStart: number, modifiers: ModifiersArray): ExportAssignment {
var node = <ExportAssignment>createNode(SyntaxKind.ExportAssignment, fullStart);
setModifiers(node, modifiers);
Expand All @@ -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);
Expand Down Expand Up @@ -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");
}
Expand Down
8 changes: 8 additions & 0 deletions tests/baselines/reference/es6ImportParseErrors.errors.txt
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions tests/baselines/reference/es6ImportWithoutFromClause.js
Original file line number Diff line number Diff line change
@@ -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]
8 changes: 8 additions & 0 deletions tests/baselines/reference/es6ImportWithoutFromClause.types
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions tests/baselines/reference/es6ImportWithoutFromClauseInEs5.js
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions tests/cases/compiler/es6ImportParseErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @target: es6
// @module: commonjs

import 10;
8 changes: 8 additions & 0 deletions tests/cases/compiler/es6ImportWithoutFromClause.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @target: es6
// @module: commonjs

// @filename: es6ImportWithoutFromClause_0.ts
export var a = 10;

// @filename: es6ImportWithoutFromClause_1.ts
import "es6ImportWithoutFromClause_0";
8 changes: 8 additions & 0 deletions tests/cases/compiler/es6ImportWithoutFromClauseInEs5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @target: es5
// @module: commonjs

// @filename: es6ImportWithoutFromClauseInEs5_0.ts
export var a = 10;

// @filename: es6ImportWithoutFromClauseInEs5_1.ts
import "es6ImportWithoutFromClauseInEs5_0";

0 comments on commit 4f1b908

Please sign in to comment.