Fix parsing of imports with module string name in flow plugin (#12224)
This commit is contained in:
parent
136bf231ea
commit
f1bc314c79
@ -2496,13 +2496,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
parseImportSpecifier(node: N.ImportDeclaration): void {
|
parseImportSpecifier(node: N.ImportDeclaration): void {
|
||||||
const specifier = this.startNode();
|
const specifier = this.startNode();
|
||||||
const firstIdentLoc = this.state.start;
|
const firstIdentLoc = this.state.start;
|
||||||
const firstIdent = this.parseIdentifier(true);
|
const firstIdent = this.parseModuleExportName();
|
||||||
|
|
||||||
let specifierTypeKind = null;
|
let specifierTypeKind = null;
|
||||||
if (firstIdent.name === "type") {
|
if (firstIdent.type === "Identifier") {
|
||||||
specifierTypeKind = "type";
|
if (firstIdent.name === "type") {
|
||||||
} else if (firstIdent.name === "typeof") {
|
specifierTypeKind = "type";
|
||||||
specifierTypeKind = "typeof";
|
} else if (firstIdent.name === "typeof") {
|
||||||
|
specifierTypeKind = "typeof";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let isBinding = false;
|
let isBinding = false;
|
||||||
@ -2537,6 +2539,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
specifier.local = specifier.imported.__clone();
|
specifier.local = specifier.imported.__clone();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (firstIdent.type === "StringLiteral") {
|
||||||
|
throw this.raise(
|
||||||
|
specifier.start,
|
||||||
|
Errors.ImportBindingIsString,
|
||||||
|
firstIdent.value,
|
||||||
|
);
|
||||||
|
}
|
||||||
isBinding = true;
|
isBinding = true;
|
||||||
specifier.imported = firstIdent;
|
specifier.imported = firstIdent;
|
||||||
specifier.importKind = null;
|
specifier.importKind = null;
|
||||||
|
|||||||
1
packages/babel-parser/test/fixtures/flow/module-string-names/default-import/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/flow/module-string-names/default-import/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
import {"default" as quotation} from "Confucius";
|
||||||
48
packages/babel-parser/test/fixtures/flow/module-string-names/default-import/output.json
vendored
Normal file
48
packages/babel-parser/test/fixtures/flow/module-string-names/default-import/output.json
vendored
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":49}},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":49}},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ImportDeclaration",
|
||||||
|
"start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":49}},
|
||||||
|
"specifiers": [
|
||||||
|
{
|
||||||
|
"type": "ImportSpecifier",
|
||||||
|
"start":8,"end":30,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":30}},
|
||||||
|
"imported": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":8,"end":17,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":17}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "default",
|
||||||
|
"raw": "\"default\""
|
||||||
|
},
|
||||||
|
"value": "default"
|
||||||
|
},
|
||||||
|
"importKind": null,
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":21,"end":30,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":30},"identifierName":"quotation"},
|
||||||
|
"name": "quotation"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"importKind": "value",
|
||||||
|
"source": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":37,"end":48,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":48}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "Confucius",
|
||||||
|
"raw": "\"Confucius\""
|
||||||
|
},
|
||||||
|
"value": "Confucius"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/babel-parser/test/fixtures/flow/module-string-names/import-local-is-string/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/flow/module-string-names/import-local-is-string/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
import { "foo" } from "baz";
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": [
|
||||||
|
"moduleStringNames",
|
||||||
|
"flow"
|
||||||
|
],
|
||||||
|
"throws": "A string literal cannot be used as an imported binding.\n- Did you mean `import { \"foo\" as foo }`? (1:9)"
|
||||||
|
}
|
||||||
2
packages/babel-parser/test/fixtures/flow/module-string-names/mixed/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/flow/module-string-names/mixed/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
import { "foo" as bar, "default" as qux } from "module-a";
|
||||||
|
export * as "foo", { default as "quux" } from "module-b";
|
||||||
115
packages/babel-parser/test/fixtures/flow/module-string-names/mixed/output.json
vendored
Normal file
115
packages/babel-parser/test/fixtures/flow/module-string-names/mixed/output.json
vendored
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":116,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":57}},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":116,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":57}},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ImportDeclaration",
|
||||||
|
"start":0,"end":58,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":58}},
|
||||||
|
"specifiers": [
|
||||||
|
{
|
||||||
|
"type": "ImportSpecifier",
|
||||||
|
"start":9,"end":21,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":21}},
|
||||||
|
"imported": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":9,"end":14,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":14}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "foo",
|
||||||
|
"raw": "\"foo\""
|
||||||
|
},
|
||||||
|
"value": "foo"
|
||||||
|
},
|
||||||
|
"importKind": null,
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":18,"end":21,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":21},"identifierName":"bar"},
|
||||||
|
"name": "bar"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ImportSpecifier",
|
||||||
|
"start":23,"end":39,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":39}},
|
||||||
|
"imported": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":23,"end":32,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":32}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "default",
|
||||||
|
"raw": "\"default\""
|
||||||
|
},
|
||||||
|
"value": "default"
|
||||||
|
},
|
||||||
|
"importKind": null,
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":36,"end":39,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":39},"identifierName":"qux"},
|
||||||
|
"name": "qux"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"importKind": "value",
|
||||||
|
"source": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":47,"end":57,"loc":{"start":{"line":1,"column":47},"end":{"line":1,"column":57}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "module-a",
|
||||||
|
"raw": "\"module-a\""
|
||||||
|
},
|
||||||
|
"value": "module-a"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ExportNamedDeclaration",
|
||||||
|
"start":59,"end":116,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":57}},
|
||||||
|
"specifiers": [
|
||||||
|
{
|
||||||
|
"type": "ExportNamespaceSpecifier",
|
||||||
|
"start":66,"end":76,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":17}},
|
||||||
|
"exported": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":71,"end":76,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":17}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "foo",
|
||||||
|
"raw": "\"foo\""
|
||||||
|
},
|
||||||
|
"value": "foo"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ExportSpecifier",
|
||||||
|
"start":80,"end":97,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":38}},
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":80,"end":87,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":28},"identifierName":"default"},
|
||||||
|
"name": "default"
|
||||||
|
},
|
||||||
|
"exported": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":91,"end":97,"loc":{"start":{"line":2,"column":32},"end":{"line":2,"column":38}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "quux",
|
||||||
|
"raw": "\"quux\""
|
||||||
|
},
|
||||||
|
"value": "quux"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":105,"end":115,"loc":{"start":{"line":2,"column":46},"end":{"line":2,"column":56}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "module-b",
|
||||||
|
"raw": "\"module-b\""
|
||||||
|
},
|
||||||
|
"value": "module-b"
|
||||||
|
},
|
||||||
|
"declaration": null,
|
||||||
|
"exportKind": "value"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/babel-parser/test/fixtures/flow/module-string-names/named-imports/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/flow/module-string-names/named-imports/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
import {"學而時習之,不亦說乎?" as quotation} from "Confucius";
|
||||||
48
packages/babel-parser/test/fixtures/flow/module-string-names/named-imports/output.json
vendored
Normal file
48
packages/babel-parser/test/fixtures/flow/module-string-names/named-imports/output.json
vendored
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":53}},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":53}},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ImportDeclaration",
|
||||||
|
"start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":53}},
|
||||||
|
"specifiers": [
|
||||||
|
{
|
||||||
|
"type": "ImportSpecifier",
|
||||||
|
"start":8,"end":34,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":34}},
|
||||||
|
"imported": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":8,"end":21,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":21}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "學而時習之,不亦說乎?",
|
||||||
|
"raw": "\"學而時習之,不亦說乎?\""
|
||||||
|
},
|
||||||
|
"value": "學而時習之,不亦說乎?"
|
||||||
|
},
|
||||||
|
"importKind": null,
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":25,"end":34,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":34},"identifierName":"quotation"},
|
||||||
|
"name": "quotation"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"importKind": "value",
|
||||||
|
"source": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":41,"end":52,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":52}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "Confucius",
|
||||||
|
"raw": "\"Confucius\""
|
||||||
|
},
|
||||||
|
"value": "Confucius"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
4
packages/babel-parser/test/fixtures/flow/module-string-names/options.json
vendored
Normal file
4
packages/babel-parser/test/fixtures/flow/module-string-names/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": ["flow", "moduleStringNames"]
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user