Ensure semi-colon is eaten for dynamic imports and import meta found at the start of expression statements (#595)

Fixes #594.

Also fixes span for "imports" identifier.
This commit is contained in:
dsherret 2020-01-18 04:51:20 +00:00
parent a55fced20b
commit d4fa2a6a96
9 changed files with 343 additions and 37 deletions

View File

@ -213,7 +213,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
.map(Box::new);
}
return self.parse_dynamic_import(start);
return self.parse_dynamic_import(start, import);
}
if is!("async") {
@ -1443,19 +1443,19 @@ impl<'a, I: Tokens> Parser<'a, I> {
Ok(v)
}
pub(super) fn parse_dynamic_import(&mut self, start: BytePos) -> PResult<'a, Box<Expr>> {
pub(super) fn parse_dynamic_import(
&mut self,
start: BytePos,
import_ident: Ident,
) -> PResult<'a, Box<Expr>> {
if !self.input.syntax().dynamic_import() {
syntax_error!(span!(start), SyntaxError::DynamicImport);
}
let args = self.parse_args(true)?;
let import = Box::new(Expr::Call(CallExpr {
span: span!(start),
callee: ExprOrSuper::Expr(Box::new(Expr::Ident(Ident {
span: span!(start),
sym: js_word!("import"),
type_ann: None,
optional: false,
}))),
callee: ExprOrSuper::Expr(Box::new(Expr::Ident(import_ident))),
args,
type_args: Default::default(),
}));

View File

@ -1080,6 +1080,8 @@ impl<'a, I: Tokens> StmtLikeParser<'a, Stmt> for Parser<'a, I> {
if self.input.syntax().dynamic_import() && is!("import") {
let expr = self.parse_expr()?;
eat!(';');
return Ok(ExprStmt {
span: span!(start),
expr,
@ -1090,6 +1092,8 @@ impl<'a, I: Tokens> StmtLikeParser<'a, Stmt> for Parser<'a, I> {
if self.input.syntax().import_meta() && is!("import") && peeked_is!('.') {
let expr = self.parse_expr()?;
eat!(';');
return Ok(ExprStmt {
span: span!(start),
expr,

View File

@ -7,19 +7,22 @@ impl<'a, I: Tokens> Parser<'a, I> {
let start = cur_pos!();
if self.input.syntax().import_meta() && peeked_is!('.') {
return self
.parse_expr()
.map(|expr| ExprStmt {
span: span!(start),
expr,
})
.map(Stmt::Expr)
.map(ModuleItem::Stmt);
let expr = self.parse_expr()?;
eat!(';');
return Ok(Stmt::Expr(ExprStmt {
span: span!(start),
expr,
})
.into());
}
if self.input.syntax().dynamic_import() && peeked_is!('(') {
let expr = self.parse_expr()?;
eat!(';');
return Ok(Stmt::Expr(ExprStmt {
span: span!(start),
expr,

View File

@ -0,0 +1,5 @@
const test = import('foo').from();
function func() {
import('foo').from();
}

View File

@ -0,0 +1,217 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 81,
"ctxt": 0
},
"body": [
{
"type": "VariableDeclaration",
"span": {
"start": 0,
"end": 34,
"ctxt": 0
},
"kind": "const",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 6,
"end": 33,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 6,
"end": 10,
"ctxt": 0
},
"value": "test",
"typeAnnotation": null,
"optional": false
},
"init": {
"type": "CallExpression",
"span": {
"start": 13,
"end": 33,
"ctxt": 0
},
"callee": {
"type": "MemberExpression",
"span": {
"start": 13,
"end": 31,
"ctxt": 0
},
"object": {
"type": "CallExpression",
"span": {
"start": 13,
"end": 26,
"ctxt": 0
},
"callee": {
"type": "Identifier",
"span": {
"start": 13,
"end": 19,
"ctxt": 0
},
"value": "import",
"typeAnnotation": null,
"optional": false
},
"arguments": [
{
"spread": null,
"expression": {
"type": "StringLiteral",
"span": {
"start": 20,
"end": 25,
"ctxt": 0
},
"value": "foo",
"hasEscape": false
}
}
],
"typeArguments": null
},
"property": {
"type": "Identifier",
"span": {
"start": 27,
"end": 31,
"ctxt": 0
},
"value": "from",
"typeAnnotation": null,
"optional": false
},
"computed": false
},
"arguments": [],
"typeArguments": null
},
"definite": false
}
]
},
{
"type": "FunctionDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 45,
"end": 49,
"ctxt": 0
},
"value": "func",
"typeAnnotation": null,
"optional": false
},
"declare": false,
"params": [],
"decorators": [],
"span": {
"start": 36,
"end": 81,
"ctxt": 0
},
"body": {
"type": "BlockStatement",
"span": {
"start": 52,
"end": 81,
"ctxt": 0
},
"stmts": [
{
"type": "ExpressionStatement",
"span": {
"start": 58,
"end": 79,
"ctxt": 0
},
"expression": {
"type": "CallExpression",
"span": {
"start": 58,
"end": 78,
"ctxt": 0
},
"callee": {
"type": "MemberExpression",
"span": {
"start": 58,
"end": 76,
"ctxt": 0
},
"object": {
"type": "CallExpression",
"span": {
"start": 58,
"end": 71,
"ctxt": 0
},
"callee": {
"type": "Identifier",
"span": {
"start": 58,
"end": 64,
"ctxt": 0
},
"value": "import",
"typeAnnotation": null,
"optional": false
},
"arguments": [
{
"spread": null,
"expression": {
"type": "StringLiteral",
"span": {
"start": 65,
"end": 70,
"ctxt": 0
},
"value": "foo",
"hasEscape": false
}
}
],
"typeArguments": null
},
"property": {
"type": "Identifier",
"span": {
"start": 72,
"end": 76,
"ctxt": 0
},
"value": "from",
"typeAnnotation": null,
"optional": false
},
"computed": false
},
"arguments": [],
"typeArguments": null
}
}
]
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": null
}
],
"interpreter": null
}

View File

@ -10,7 +10,7 @@
"type": "ExpressionStatement",
"span": {
"start": 0,
"end": 20,
"end": 21,
"ctxt": 0
},
"expression": {
@ -38,7 +38,7 @@
"type": "Identifier",
"span": {
"start": 0,
"end": 13,
"end": 6,
"ctxt": 0
},
"value": "import",
@ -78,14 +78,6 @@
"arguments": [],
"typeArguments": null
}
},
{
"type": "EmptyStatement",
"span": {
"start": 20,
"end": 21,
"ctxt": 0
}
}
],
"interpreter": null

View File

@ -1 +1,5 @@
const test = import.meta.someProp;
const test = import.meta.someProp;
function func() {
import.meta.someProp;
}

View File

@ -2,7 +2,7 @@
"type": "Module",
"span": {
"start": 0,
"end": 34,
"end": 81,
"ctxt": 0
},
"body": [
@ -82,6 +82,95 @@
"definite": false
}
]
},
{
"type": "FunctionDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 45,
"end": 49,
"ctxt": 0
},
"value": "func",
"typeAnnotation": null,
"optional": false
},
"declare": false,
"params": [],
"decorators": [],
"span": {
"start": 36,
"end": 81,
"ctxt": 0
},
"body": {
"type": "BlockStatement",
"span": {
"start": 52,
"end": 81,
"ctxt": 0
},
"stmts": [
{
"type": "ExpressionStatement",
"span": {
"start": 58,
"end": 79,
"ctxt": 0
},
"expression": {
"type": "MemberExpression",
"span": {
"start": 58,
"end": 78,
"ctxt": 0
},
"object": {
"type": "MetaProperty",
"meta": {
"type": "Identifier",
"span": {
"start": 58,
"end": 64,
"ctxt": 0
},
"value": "import",
"typeAnnotation": null,
"optional": false
},
"property": {
"type": "Identifier",
"span": {
"start": 65,
"end": 69,
"ctxt": 0
},
"value": "meta",
"typeAnnotation": null,
"optional": false
}
},
"property": {
"type": "Identifier",
"span": {
"start": 70,
"end": 78,
"ctxt": 0
},
"value": "someProp",
"typeAnnotation": null,
"optional": false
},
"computed": false
}
}
]
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": null
}
],
"interpreter": null

View File

@ -10,7 +10,7 @@
"type": "ExpressionStatement",
"span": {
"start": 0,
"end": 20,
"end": 21,
"ctxt": 0
},
"expression": {
@ -58,14 +58,6 @@
},
"computed": false
}
},
{
"type": "EmptyStatement",
"span": {
"start": 20,
"end": 21,
"ctxt": 0
}
}
],
"interpreter": null