Fix type-only import declaration with empty named imports being parsed as having a default import specifier (#817)

Co-authored-by: 강동윤 <kdy1997.dev@gmail.com>
This commit is contained in:
David Sherret 2020-06-03 02:41:24 -04:00 committed by GitHub
parent d7bb5c18ae
commit 99c164b628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 15 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "swc_ecma_parser" name = "swc_ecma_parser"
version = "0.24.2" version = "0.24.3"
authors = ["강동윤 <kdy1997.dev@gmail.com>"] authors = ["강동윤 <kdy1997.dev@gmail.com>"]
license = "Apache-2.0/MIT" license = "Apache-2.0/MIT"
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"

View File

@ -60,8 +60,13 @@ impl<'a, I: Tokens> Parser<'a, I> {
.map(ModuleItem::from); .map(ModuleItem::from);
} }
let type_only_span = self.input.cur_span(); let type_only = self.input.syntax().typescript()
let type_only = self.syntax().typescript() && eat!("type"); && is!("type")
&& (peeked_is!('{') || !peeked_is!("from") && !peeked_is!(','));
if type_only {
assert_and_bump!("type");
}
let mut specifiers = vec![]; let mut specifiers = vec![];
@ -101,14 +106,6 @@ impl<'a, I: Tokens> Parser<'a, I> {
} }
} }
// import type from './a';
if type_only && specifiers.is_empty() {
specifiers.push(ImportSpecifier::Default(ImportDefaultSpecifier {
span: span!(start),
local: Ident::new(js_word!("type"), type_only_span),
}));
}
let src = self.parse_from_clause_and_semi()?; let src = self.parse_from_clause_and_semi()?;
Ok(ModuleDecl::Import(ImportDecl { Ok(ModuleDecl::Import(ImportDecl {

View File

@ -1 +1,2 @@
import type Foo from 'foo'; import type Foo from 'foo';
import type from './a';

View File

@ -2,7 +2,7 @@
"type": "Module", "type": "Module",
"span": { "span": {
"start": 0, "start": 0,
"end": 27, "end": 51,
"ctxt": 0 "ctxt": 0
}, },
"body": [ "body": [
@ -45,6 +45,46 @@
"hasEscape": false "hasEscape": false
}, },
"typeOnly": true "typeOnly": true
},
{
"type": "ImportDeclaration",
"span": {
"start": 28,
"end": 51,
"ctxt": 0
},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"span": {
"start": 35,
"end": 39,
"ctxt": 0
},
"local": {
"type": "Identifier",
"span": {
"start": 35,
"end": 39,
"ctxt": 0
},
"value": "type",
"typeAnnotation": null,
"optional": false
}
}
],
"source": {
"type": "StringLiteral",
"span": {
"start": 45,
"end": 50,
"ctxt": 0
},
"value": "./a",
"hasEscape": false
},
"typeOnly": false
} }
], ],
"interpreter": null "interpreter": null

View File

@ -1 +1,2 @@
import type { Foo } from 'foo'; import type { Foo } from 'foo';
import type {} from 'foo';

View File

@ -2,7 +2,7 @@
"type": "Module", "type": "Module",
"span": { "span": {
"start": 0, "start": 0,
"end": 31, "end": 58,
"ctxt": 0 "ctxt": 0
}, },
"body": [ "body": [
@ -46,6 +46,26 @@
"hasEscape": false "hasEscape": false
}, },
"typeOnly": true "typeOnly": true
},
{
"type": "ImportDeclaration",
"span": {
"start": 32,
"end": 58,
"ctxt": 0
},
"specifiers": [],
"source": {
"type": "StringLiteral",
"span": {
"start": 52,
"end": 57,
"ctxt": 0
},
"value": "foo",
"hasEscape": false
},
"typeOnly": true
} }
], ],
"interpreter": null "interpreter": null