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]
name = "swc_ecma_parser"
version = "0.24.2"
version = "0.24.3"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
license = "Apache-2.0/MIT"
repository = "https://github.com/swc-project/swc.git"

View File

@ -60,8 +60,13 @@ impl<'a, I: Tokens> Parser<'a, I> {
.map(ModuleItem::from);
}
let type_only_span = self.input.cur_span();
let type_only = self.syntax().typescript() && eat!("type");
let type_only = self.input.syntax().typescript()
&& is!("type")
&& (peeked_is!('{') || !peeked_is!("from") && !peeked_is!(','));
if type_only {
assert_and_bump!("type");
}
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()?;
Ok(ModuleDecl::Import(ImportDecl {

View File

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

View File

@ -2,7 +2,7 @@
"type": "Module",
"span": {
"start": 0,
"end": 27,
"end": 51,
"ctxt": 0
},
"body": [
@ -45,6 +45,46 @@
"hasEscape": false
},
"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

View File

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

View File

@ -2,7 +2,7 @@
"type": "Module",
"span": {
"start": 0,
"end": 31,
"end": 58,
"ctxt": 0
},
"body": [
@ -46,6 +46,26 @@
"hasEscape": false
},
"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