Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

keywords are not allowed as local specifier in imports #307

Merged
merged 1 commit into from
Feb 10, 2017
Merged
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
9 changes: 4 additions & 5 deletions src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -1018,14 +1018,13 @@ pp.parseExprListItem = function (allowEmpty, refShorthandDefaultPos) {

pp.parseIdentifier = function (liberal) {
const node = this.startNode();
if (!liberal) {
this.checkReservedWord(this.state.value, this.state.start, !!this.state.type.keyword, false);
}

if (this.match(tt.name)) {
if (!liberal) {
this.checkReservedWord(this.state.value, this.state.start, false, false);
}

node.name = this.state.value;
} else if (liberal && this.state.type.keyword) {
} else if (this.state.type.keyword) {
node.name = this.state.type.keyword;
} else {
this.unexpected();
Expand Down
7 changes: 6 additions & 1 deletion src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,12 @@ pp.parseImportSpecifiers = function (node) {
pp.parseImportSpecifier = function (node) {
const specifier = this.startNode();
specifier.imported = this.parseIdentifier(true);
specifier.local = this.eatContextual("as") ? this.parseIdentifier() : specifier.imported.__clone();
if (this.eatContextual("as")) {
specifier.local = this.parseIdentifier();
} else {
this.checkReservedWord(specifier.imported.name, specifier.start, true, true);
specifier.local = specifier.imported.__clone();
}
this.checkLVal(specifier.local, true, undefined, "import specifier");
node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
};
Expand Down
26 changes: 13 additions & 13 deletions src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -1221,9 +1221,10 @@ export default function (instance) {
specifierTypeKind = "typeof";
}

let isBinding = false;
if (this.isContextual("as")) {
const as_ident = this.parseIdentifier(true);
if (specifierTypeKind !== null && !this.match(tt.name)) {
if (specifierTypeKind !== null && !this.match(tt.name) && !this.state.type.keyword) {
// `import {type as ,` or `import {type as }`
specifier.imported = as_ident;
specifier.importKind = specifierTypeKind;
Expand All @@ -1232,23 +1233,20 @@ export default function (instance) {
// `import {type as foo`
specifier.imported = firstIdent;
specifier.importKind = null;
specifier.local = this.parseIdentifier(false);
specifier.local = this.parseIdentifier();
}
} else if (specifierTypeKind !== null && this.match(tt.name)) {
} else if (specifierTypeKind !== null && (this.match(tt.name) || this.state.type.keyword)) {
// `import {type foo`
specifier.imported = this.parseIdentifier(true);
specifier.importKind = specifierTypeKind;
specifier.local =
this.eatContextual("as")
? this.parseIdentifier(false)
: specifier.imported.__clone();
} else {
if (firstIdent.name === "typeof") {
this.unexpected(
firstIdentLoc,
"Cannot import a variable named `typeof`"
);
if (this.eatContextual("as")) {
specifier.local = this.parseIdentifier();
} else {
isBinding = true;
specifier.local = specifier.imported.__clone();
}
} else {
isBinding = true;
specifier.imported = firstIdent;
specifier.importKind = null;
specifier.local = specifier.imported.__clone();
Expand All @@ -1261,6 +1259,8 @@ export default function (instance) {
this.raise(firstIdentLoc, "`The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements`");
}

if (isBinding) this.checkReservedWord(specifier.local.name, specifier.start, true, true);

this.checkLVal(specifier.local, true, undefined, "import specifier");
node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { default } from "foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": ["flow"],
"throws": "default is a reserved word (1:9)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { typeof } from "foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": ["flow"],
"throws": "typeof is a reserved word (1:9)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { typeof } from "foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "typeof is a reserved word (1:9)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { debugger } from "foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "debugger is a reserved word (1:9)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:26)"
}
"throws": "yield is a reserved word (1:26)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:25)"
}
"throws": "yield is a reserved word (1:25)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:46)"
}
"throws": "yield is a reserved word (1:46)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:29)"
"throws": "yield is a reserved word (1:29)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:28)"
}
"throws": "yield is a reserved word (1:28)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { type as debugger } from "foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "debugger is a reserved word (1:17)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { type debugger } from "foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "debugger is a reserved word (1:9)"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"throws": "Unexpected token (1:14)",
"throws": "delete is a reserved word (1:14)",
"plugins": ["flow", "jsx"]
}