Skip to content

Commit

Permalink
Ensure a comma between imported default binding and named imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
ariya committed Jul 3, 2015
1 parent a87270b commit 6bd50a2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 33 deletions.
63 changes: 32 additions & 31 deletions esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -5199,52 +5199,53 @@
}

function parseImportDeclaration() {
var specifiers, src, node = new Node();
var specifiers = [], src, node = new Node();

if (state.inFunctionBody) {
throwError(Messages.IllegalImportDeclaration);
}

expectKeyword('import');
specifiers = [];

if (lookahead.type === Token.StringLiteral) {
// covers:
// import 'foo';
src = parseModuleSpecifier();
consumeSemicolon();
return node.finishImportDeclaration(specifiers, src);
}
} else {

if (!matchKeyword('default') && isIdentifierName(lookahead)) {
// covers:
// import foo
// import foo, ...
specifiers.push(parseImportDefaultSpecifier());
if (match(',')) {
lex();
if (match('{')) {
// import {bar}
specifiers = specifiers.concat(parseNamedImports());
} else if (match('*')) {
// import * as foo
specifiers.push(parseImportNamespaceSpecifier());
} else if (isIdentifierName(lookahead) && !matchKeyword('default')) {
// import foo
specifiers.push(parseImportDefaultSpecifier());
if (match(',')) {
lex();
if (match('*')) {
// import foo, * as foo
specifiers.push(parseImportNamespaceSpecifier());
} else if (match('{')) {
// import foo, {bar}
specifiers = specifiers.concat(parseNamedImports());
} else {
throwUnexpectedToken(lookahead);
}
}
} else {
throwUnexpectedToken(lex());
}
}
if (match('*')) {
// covers:
// import foo, * as foo
// import * as foo
specifiers.push(parseImportNamespaceSpecifier());
} else if (match('{')) {
// covers:
// import foo, {bar}
// import {bar}
specifiers = specifiers.concat(parseNamedImports());
}

if (!matchContextualKeyword('from')) {
throwError(lookahead.value ?
Messages.UnexpectedToken : Messages.MissingFromClause, lookahead.value);
if (!matchContextualKeyword('from')) {
throwError(lookahead.value ?
Messages.UnexpectedToken : Messages.MissingFromClause, lookahead.value);
}
lex();
src = parseModuleSpecifier();
}
lex();
src = parseModuleSpecifier();
consumeSemicolon();

consumeSemicolon();
return node.finishImportDeclaration(specifiers, src);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"index": 6,
"index": 7,
"lineNumber": 1,
"column": 7,
"column": 8,
"message": "Error: Line 1: Unexpected token default",
"description": "Unexpected token default"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import foo { bar } from "bar";
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"index": 10,
"lineNumber": 1,
"column": 11,
"message": "Error: Line 1: Unexpected token {",
"description":"Unexpected token {"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import foo, from "bar";
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"index": 12,
"lineNumber": 1,
"column": 13,
"message": "Error: Line 1: Unexpected identifier",
"description": "Unexpected identifier"
}

0 comments on commit 6bd50a2

Please sign in to comment.