Fix parse error when using named import "as" with flow parser (#13659)
This commit is contained in:
parent
d48a5cb55c
commit
1229336fea
@ -2660,19 +2660,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
specifier.importKind = null;
|
specifier.importKind = null;
|
||||||
specifier.local = this.parseIdentifier();
|
specifier.local = this.parseIdentifier();
|
||||||
}
|
}
|
||||||
} else if (
|
} else {
|
||||||
|
if (
|
||||||
specifierTypeKind !== null &&
|
specifierTypeKind !== null &&
|
||||||
(this.match(tt.name) || this.state.type.keyword)
|
(this.match(tt.name) || this.state.type.keyword)
|
||||||
) {
|
) {
|
||||||
// `import {type foo`
|
// `import {type foo`
|
||||||
specifier.imported = this.parseIdentifier(true);
|
specifier.imported = this.parseIdentifier(true);
|
||||||
specifier.importKind = specifierTypeKind;
|
specifier.importKind = specifierTypeKind;
|
||||||
if (this.eatContextual("as")) {
|
|
||||||
specifier.local = this.parseIdentifier();
|
|
||||||
} else {
|
|
||||||
isBinding = true;
|
|
||||||
specifier.local = cloneIdentifier(specifier.imported);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (firstIdentIsString) {
|
if (firstIdentIsString) {
|
||||||
/*:: invariant(firstIdent instanceof N.StringLiteral) */
|
/*:: invariant(firstIdent instanceof N.StringLiteral) */
|
||||||
@ -2683,11 +2678,17 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
/*:: invariant(firstIdent instanceof N.Node) */
|
/*:: invariant(firstIdent instanceof N.Node) */
|
||||||
isBinding = true;
|
|
||||||
specifier.imported = firstIdent;
|
specifier.imported = firstIdent;
|
||||||
specifier.importKind = null;
|
specifier.importKind = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.eatContextual("as")) {
|
||||||
|
specifier.local = this.parseIdentifier();
|
||||||
|
} else {
|
||||||
|
isBinding = true;
|
||||||
specifier.local = cloneIdentifier(specifier.imported);
|
specifier.local = cloneIdentifier(specifier.imported);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const nodeIsTypeImport = hasTypeImportKind(node);
|
const nodeIsTypeImport = hasTypeImportKind(node);
|
||||||
const specifierIsTypeImport = hasTypeImportKind(specifier);
|
const specifierIsTypeImport = hasTypeImportKind(specifier);
|
||||||
|
|||||||
4
packages/babel-parser/test/fixtures/flow/imports/imports-with-as-identifiers/input.js
vendored
Normal file
4
packages/babel-parser/test/fixtures/flow/imports/imports-with-as-identifiers/input.js
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
// @flow
|
||||||
|
import { foo as as } from "foo";
|
||||||
|
import { as as as } from "foo";
|
||||||
|
import { as as foo } from "foo";
|
||||||
123
packages/babel-parser/test/fixtures/flow/imports/imports-with-as-identifiers/output.json
vendored
Normal file
123
packages/babel-parser/test/fixtures/flow/imports/imports-with-as-identifiers/output.json
vendored
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":106,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":32}},
|
||||||
|
"errors": [
|
||||||
|
"SyntaxError: Identifier 'as' has already been declared. (3:15)"
|
||||||
|
],
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":106,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":32}},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ImportDeclaration",
|
||||||
|
"start":9,"end":41,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":32}},
|
||||||
|
"specifiers": [
|
||||||
|
{
|
||||||
|
"type": "ImportSpecifier",
|
||||||
|
"start":18,"end":27,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":18}},
|
||||||
|
"imported": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":18,"end":21,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12},"identifierName":"foo"},
|
||||||
|
"name": "foo"
|
||||||
|
},
|
||||||
|
"importKind": null,
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":25,"end":27,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":18},"identifierName":"as"},
|
||||||
|
"name": "as"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"importKind": "value",
|
||||||
|
"source": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":35,"end":40,"loc":{"start":{"line":2,"column":26},"end":{"line":2,"column":31}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "foo",
|
||||||
|
"raw": "\"foo\""
|
||||||
|
},
|
||||||
|
"value": "foo"
|
||||||
|
},
|
||||||
|
"leadingComments": [
|
||||||
|
{
|
||||||
|
"type": "CommentLine",
|
||||||
|
"value": " @flow",
|
||||||
|
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ImportDeclaration",
|
||||||
|
"start":42,"end":73,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":31}},
|
||||||
|
"specifiers": [
|
||||||
|
{
|
||||||
|
"type": "ImportSpecifier",
|
||||||
|
"start":51,"end":59,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":17}},
|
||||||
|
"imported": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":51,"end":53,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":11},"identifierName":"as"},
|
||||||
|
"name": "as"
|
||||||
|
},
|
||||||
|
"importKind": null,
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":57,"end":59,"loc":{"start":{"line":3,"column":15},"end":{"line":3,"column":17},"identifierName":"as"},
|
||||||
|
"name": "as"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"importKind": "value",
|
||||||
|
"source": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":67,"end":72,"loc":{"start":{"line":3,"column":25},"end":{"line":3,"column":30}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "foo",
|
||||||
|
"raw": "\"foo\""
|
||||||
|
},
|
||||||
|
"value": "foo"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ImportDeclaration",
|
||||||
|
"start":74,"end":106,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":32}},
|
||||||
|
"specifiers": [
|
||||||
|
{
|
||||||
|
"type": "ImportSpecifier",
|
||||||
|
"start":83,"end":92,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":18}},
|
||||||
|
"imported": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":83,"end":85,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":11},"identifierName":"as"},
|
||||||
|
"name": "as"
|
||||||
|
},
|
||||||
|
"importKind": null,
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":89,"end":92,"loc":{"start":{"line":4,"column":15},"end":{"line":4,"column":18},"identifierName":"foo"},
|
||||||
|
"name": "foo"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"importKind": "value",
|
||||||
|
"source": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":100,"end":105,"loc":{"start":{"line":4,"column":26},"end":{"line":4,"column":31}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "foo",
|
||||||
|
"raw": "\"foo\""
|
||||||
|
},
|
||||||
|
"value": "foo"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
},
|
||||||
|
"comments": [
|
||||||
|
{
|
||||||
|
"type": "CommentLine",
|
||||||
|
"value": " @flow",
|
||||||
|
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
1
packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand-2/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand-2/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
import {type foo as as} from "foo";
|
||||||
44
packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand-2/output.json
vendored
Normal file
44
packages/babel-parser/test/fixtures/flow/type-imports/import-type-shorthand-2/output.json
vendored
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":35}},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":35}},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ImportDeclaration",
|
||||||
|
"start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":35}},
|
||||||
|
"specifiers": [
|
||||||
|
{
|
||||||
|
"type": "ImportSpecifier",
|
||||||
|
"start":8,"end":22,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":22}},
|
||||||
|
"imported": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":13,"end":16,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":16},"identifierName":"foo"},
|
||||||
|
"name": "foo"
|
||||||
|
},
|
||||||
|
"importKind": "type",
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":20,"end":22,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":22},"identifierName":"as"},
|
||||||
|
"name": "as"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"importKind": "value",
|
||||||
|
"source": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":29,"end":34,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":34}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "foo",
|
||||||
|
"raw": "\"foo\""
|
||||||
|
},
|
||||||
|
"value": "foo"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user