keywords are not allowed as local specifier (#307)
Also fix some error messages to be more specific
This commit is contained in:
parent
e049ec3456
commit
4bd682e90b
@ -1039,14 +1039,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();
|
||||
|
||||
@ -1074,7 +1074,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"));
|
||||
};
|
||||
|
||||
@ -1267,9 +1267,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;
|
||||
@ -1278,23 +1279,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();
|
||||
@ -1307,6 +1305,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"));
|
||||
};
|
||||
|
||||
1
test/fixtures/es2015/modules/import-invalid-keyword-flow/actual.js
vendored
Normal file
1
test/fixtures/es2015/modules/import-invalid-keyword-flow/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import { default } from "foo";
|
||||
4
test/fixtures/es2015/modules/import-invalid-keyword-flow/options.json
vendored
Normal file
4
test/fixtures/es2015/modules/import-invalid-keyword-flow/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"plugins": ["flow"],
|
||||
"throws": "default is a reserved word (1:9)"
|
||||
}
|
||||
1
test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/actual.js
vendored
Normal file
1
test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import { typeof } from "foo";
|
||||
4
test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/options.json
vendored
Normal file
4
test/fixtures/es2015/modules/import-invalid-keyword-typeof-flow/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"plugins": ["flow"],
|
||||
"throws": "typeof is a reserved word (1:9)"
|
||||
}
|
||||
1
test/fixtures/es2015/modules/import-invalid-keyword-typeof/actual.js
vendored
Normal file
1
test/fixtures/es2015/modules/import-invalid-keyword-typeof/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import { typeof } from "foo";
|
||||
3
test/fixtures/es2015/modules/import-invalid-keyword-typeof/options.json
vendored
Normal file
3
test/fixtures/es2015/modules/import-invalid-keyword-typeof/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "typeof is a reserved word (1:9)"
|
||||
}
|
||||
1
test/fixtures/es2015/modules/import-invalid-keyword/actual.js
vendored
Normal file
1
test/fixtures/es2015/modules/import-invalid-keyword/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import { debugger } from "foo";
|
||||
3
test/fixtures/es2015/modules/import-invalid-keyword/options.json
vendored
Normal file
3
test/fixtures/es2015/modules/import-invalid-keyword/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "debugger is a reserved word (1:9)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:26)"
|
||||
}
|
||||
"throws": "yield is a reserved word (1:26)"
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:25)"
|
||||
}
|
||||
"throws": "yield is a reserved word (1:25)"
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:46)"
|
||||
}
|
||||
"throws": "yield is a reserved word (1:46)"
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:29)"
|
||||
"throws": "yield is a reserved word (1:29)"
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:28)"
|
||||
}
|
||||
"throws": "yield is a reserved word (1:28)"
|
||||
}
|
||||
|
||||
1
test/fixtures/flow/type-imports/invalid-import-type-as/actual.js
vendored
Normal file
1
test/fixtures/flow/type-imports/invalid-import-type-as/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import { type as debugger } from "foo";
|
||||
3
test/fixtures/flow/type-imports/invalid-import-type-as/options.json
vendored
Normal file
3
test/fixtures/flow/type-imports/invalid-import-type-as/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "debugger is a reserved word (1:17)"
|
||||
}
|
||||
1
test/fixtures/flow/type-imports/invalid-import-type/actual.js
vendored
Normal file
1
test/fixtures/flow/type-imports/invalid-import-type/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import { type debugger } from "foo";
|
||||
3
test/fixtures/flow/type-imports/invalid-import-type/options.json
vendored
Normal file
3
test/fixtures/flow/type-imports/invalid-import-type/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "debugger is a reserved word (1:9)"
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:14)",
|
||||
"throws": "delete is a reserved word (1:14)",
|
||||
"plugins": ["flow", "jsx"]
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user