fix: parse value imports named type as values (#11296)
* fix: parse value imports named type as values * Address feedback * Add plugin tests * Add isContextual() check * Remove importKind: value from extraneous nodes * Ensure importKind is correct for more nodes * Add additional test * Address feedback * Revert formatting * Fix tests
This commit is contained in:
parent
469e8ed591
commit
7ca814489a
@ -1862,14 +1862,25 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
checkDuplicateExports() {}
|
checkDuplicateExports() {}
|
||||||
|
|
||||||
parseImport(node: N.Node): N.AnyImport {
|
parseImport(node: N.Node): N.AnyImport {
|
||||||
if (this.match(tt.name) && this.lookahead().type === tt.eq) {
|
if (this.match(tt.name) || this.match(tt.star) || this.match(tt.braceL)) {
|
||||||
return this.tsParseImportEqualsDeclaration(node);
|
const ahead = this.lookahead();
|
||||||
}
|
|
||||||
|
|
||||||
if (this.eatContextual("type")) {
|
if (this.match(tt.name) && ahead.type === tt.eq) {
|
||||||
node.importKind = "type";
|
return this.tsParseImportEqualsDeclaration(node);
|
||||||
} else {
|
}
|
||||||
node.importKind = "value";
|
|
||||||
|
if (
|
||||||
|
this.isContextual("type") &&
|
||||||
|
// import type, { a } from "b";
|
||||||
|
ahead.type !== tt.comma &&
|
||||||
|
// import type from "a";
|
||||||
|
!(ahead.type === tt.name && ahead.value === "from")
|
||||||
|
) {
|
||||||
|
node.importKind = "type";
|
||||||
|
this.next();
|
||||||
|
} else {
|
||||||
|
node.importKind = "value";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const importNode = super.parseImport(node);
|
const importNode = super.parseImport(node);
|
||||||
|
|||||||
@ -0,0 +1 @@
|
|||||||
|
import type, { bar } from 'foo';
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": [
|
||||||
|
"typescript"
|
||||||
|
]
|
||||||
|
}
|
||||||
154
packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/output.json
vendored
Normal file
154
packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/output.json
vendored
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start": 0,
|
||||||
|
"end": 32,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 32
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start": 0,
|
||||||
|
"end": 32,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 32
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ImportDeclaration",
|
||||||
|
"start": 0,
|
||||||
|
"end": 32,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 32
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"importKind": "value",
|
||||||
|
"specifiers": [
|
||||||
|
{
|
||||||
|
"type": "ImportDefaultSpecifier",
|
||||||
|
"start": 7,
|
||||||
|
"end": 11,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 7
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 11
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 7,
|
||||||
|
"end": 11,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 7
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 11
|
||||||
|
},
|
||||||
|
"identifierName": "type"
|
||||||
|
},
|
||||||
|
"name": "type"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ImportSpecifier",
|
||||||
|
"start": 15,
|
||||||
|
"end": 18,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 15
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 18
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"imported": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 15,
|
||||||
|
"end": 18,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 15
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 18
|
||||||
|
},
|
||||||
|
"identifierName": "bar"
|
||||||
|
},
|
||||||
|
"name": "bar"
|
||||||
|
},
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 15,
|
||||||
|
"end": 18,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 15
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 18
|
||||||
|
},
|
||||||
|
"identifierName": "bar"
|
||||||
|
},
|
||||||
|
"name": "bar"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start": 26,
|
||||||
|
"end": 31,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 26
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 31
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "foo",
|
||||||
|
"raw": "'foo'"
|
||||||
|
},
|
||||||
|
"value": "foo"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
import type from 'foo';
|
||||||
6
packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/options.json
vendored
Normal file
6
packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/options.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": [
|
||||||
|
"typescript"
|
||||||
|
]
|
||||||
|
}
|
||||||
105
packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/output.json
vendored
Normal file
105
packages/babel-parser/test/fixtures/typescript/import/import-default-id-type/output.json
vendored
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start": 0,
|
||||||
|
"end": 23,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 23
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start": 0,
|
||||||
|
"end": 23,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 23
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ImportDeclaration",
|
||||||
|
"start": 0,
|
||||||
|
"end": 23,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 23
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"importKind": "value",
|
||||||
|
"specifiers": [
|
||||||
|
{
|
||||||
|
"type": "ImportDefaultSpecifier",
|
||||||
|
"start": 7,
|
||||||
|
"end": 11,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 7
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 11
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 7,
|
||||||
|
"end": 11,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 7
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 11
|
||||||
|
},
|
||||||
|
"identifierName": "type"
|
||||||
|
},
|
||||||
|
"name": "type"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start": 17,
|
||||||
|
"end": 22,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 17
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 22
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "foo",
|
||||||
|
"raw": "'foo'"
|
||||||
|
},
|
||||||
|
"value": "foo"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/babel-parser/test/fixtures/typescript/import/import-named/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/import/import-named/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
import { foo } from "bar";
|
||||||
122
packages/babel-parser/test/fixtures/typescript/import/import-named/output.json
vendored
Normal file
122
packages/babel-parser/test/fixtures/typescript/import/import-named/output.json
vendored
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start": 0,
|
||||||
|
"end": 26,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 26
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start": 0,
|
||||||
|
"end": 26,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 26
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ImportDeclaration",
|
||||||
|
"start": 0,
|
||||||
|
"end": 26,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 26
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"importKind": "value",
|
||||||
|
"specifiers": [
|
||||||
|
{
|
||||||
|
"type": "ImportSpecifier",
|
||||||
|
"start": 9,
|
||||||
|
"end": 12,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 9
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 12
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"imported": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 9,
|
||||||
|
"end": 12,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 9
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 12
|
||||||
|
},
|
||||||
|
"identifierName": "foo"
|
||||||
|
},
|
||||||
|
"name": "foo"
|
||||||
|
},
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 9,
|
||||||
|
"end": 12,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 9
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 12
|
||||||
|
},
|
||||||
|
"identifierName": "foo"
|
||||||
|
},
|
||||||
|
"name": "foo"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start": 20,
|
||||||
|
"end": 25,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 20
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 25
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "bar",
|
||||||
|
"raw": "\"bar\""
|
||||||
|
},
|
||||||
|
"value": "bar"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/babel-parser/test/fixtures/typescript/import/import-star-as/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/import/import-star-as/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
import * as a from "a";
|
||||||
105
packages/babel-parser/test/fixtures/typescript/import/import-star-as/output.json
vendored
Normal file
105
packages/babel-parser/test/fixtures/typescript/import/import-star-as/output.json
vendored
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start": 0,
|
||||||
|
"end": 23,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 23
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start": 0,
|
||||||
|
"end": 23,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 23
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ImportDeclaration",
|
||||||
|
"start": 0,
|
||||||
|
"end": 23,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 23
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"importKind": "value",
|
||||||
|
"specifiers": [
|
||||||
|
{
|
||||||
|
"type": "ImportNamespaceSpecifier",
|
||||||
|
"start": 7,
|
||||||
|
"end": 13,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 7
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 13
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"local": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 12,
|
||||||
|
"end": 13,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 12
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 13
|
||||||
|
},
|
||||||
|
"identifierName": "a"
|
||||||
|
},
|
||||||
|
"name": "a"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start": 19,
|
||||||
|
"end": 22,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 19
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 22
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "a",
|
||||||
|
"raw": "\"a\""
|
||||||
|
},
|
||||||
|
"value": "a"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/babel-parser/test/fixtures/typescript/types/import-type-escaped-error/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/types/import-type-escaped-error/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
import typ\u{65} typescript from "typescript";
|
||||||
7
packages/babel-parser/test/fixtures/typescript/types/import-type-escaped-error/options.json
vendored
Normal file
7
packages/babel-parser/test/fixtures/typescript/types/import-type-escaped-error/options.json
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": [
|
||||||
|
"typescript"
|
||||||
|
],
|
||||||
|
"throws": "Unexpected token (1:17)"
|
||||||
|
}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
import type, { bar } from 'foo';
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"plugins": [[
|
||||||
|
"transform-typescript",
|
||||||
|
{
|
||||||
|
"onlyRemoveTypeImports": true
|
||||||
|
}
|
||||||
|
]]
|
||||||
|
}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
import type, { bar } from 'foo';
|
||||||
1
packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type/input.ts
vendored
Normal file
1
packages/babel-plugin-transform-typescript/test/fixtures/imports/import-named-type/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
import type from 'foo';
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"plugins": [[
|
||||||
|
"transform-typescript",
|
||||||
|
{
|
||||||
|
"onlyRemoveTypeImports": true
|
||||||
|
}
|
||||||
|
]]
|
||||||
|
}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
import type from 'foo';
|
||||||
Loading…
x
Reference in New Issue
Block a user