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:
Kai Cataldo 2020-03-20 20:38:07 -04:00 committed by GitHub
parent 469e8ed591
commit 7ca814489a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 548 additions and 7 deletions

View File

@ -1862,15 +1862,26 @@ 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)) {
const ahead = this.lookahead();
if (this.match(tt.name) && ahead.type === tt.eq) {
return this.tsParseImportEqualsDeclaration(node); return this.tsParseImportEqualsDeclaration(node);
} }
if (this.eatContextual("type")) { 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"; node.importKind = "type";
this.next();
} else { } else {
node.importKind = "value"; node.importKind = "value";
} }
}
const importNode = super.parseImport(node); const importNode = super.parseImport(node);
/*:: invariant(importNode.type !== "TSImportEqualsDeclaration") */ /*:: invariant(importNode.type !== "TSImportEqualsDeclaration") */

View File

@ -0,0 +1 @@
import type, { bar } from 'foo';

View File

@ -0,0 +1,6 @@
{
"sourceType": "module",
"plugins": [
"typescript"
]
}

View 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": []
}
}

View File

@ -0,0 +1 @@
import type from 'foo';

View File

@ -0,0 +1,6 @@
{
"sourceType": "module",
"plugins": [
"typescript"
]
}

View 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": []
}
}

View File

@ -0,0 +1 @@
import { foo } from "bar";

View 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": []
}
}

View File

@ -0,0 +1 @@
import * as a from "a";

View 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": []
}
}

View File

@ -0,0 +1 @@
import typ\u{65} typescript from "typescript";

View File

@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "Unexpected token (1:17)"
}

View File

@ -0,0 +1 @@
import type, { bar } from 'foo';

View File

@ -0,0 +1,8 @@
{
"plugins": [[
"transform-typescript",
{
"onlyRemoveTypeImports": true
}
]]
}

View File

@ -0,0 +1 @@
import type, { bar } from 'foo';

View File

@ -0,0 +1 @@
import type from 'foo';

View File

@ -0,0 +1,8 @@
{
"plugins": [[
"transform-typescript",
{
"onlyRemoveTypeImports": true
}
]]
}

View File

@ -0,0 +1 @@
import type from 'foo';