fix(es/parser): Fix assert after imports (#1513)

swc_ecma_parser:
 - Check for newline while parsing import assertions. (#1512)
This commit is contained in:
강동윤 2021-03-29 11:30:00 +09:00 committed by GitHub
parent 8eae00900f
commit 3ddf229c18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 308 additions and 3 deletions

View File

@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "examples/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_parser"
repository = "https://github.com/swc-project/swc.git"
version = "0.51.0"
version = "0.51.1"
[features]
default = []

View File

@ -141,7 +141,11 @@ impl<'a, I: Tokens> Parser<I> {
src
};
let asserts = if self.input.syntax().import_assertions() && eat!(self, "assert") {
let _ = cur!(self, false);
let asserts = if self.input.syntax().import_assertions()
&& !self.input.had_line_break_before_cur()
&& eat!(self, "assert")
{
match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(v),
_ => unreachable!(),
@ -577,7 +581,11 @@ impl<'a, I: Tokens> Parser<I> {
},
_ => unexpected!(self, "a string literal"),
};
let asserts = if self.input.syntax().import_assertions() && eat!(self, "assert") {
let _ = cur!(self, false);
let asserts = if self.input.syntax().import_assertions()
&& !self.input.had_line_break_before_cur()
&& eat!(self, "assert")
{
match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(v),
_ => unreachable!(),

View File

@ -0,0 +1,5 @@
import assert from './index'
assert<123>(123)
assert<123>(123, 123)
assert<123>(123, 123, 123)

View File

@ -0,0 +1,292 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 95,
"ctxt": 0
},
"body": [
{
"type": "ImportDeclaration",
"span": {
"start": 0,
"end": 28,
"ctxt": 0
},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"span": {
"start": 7,
"end": 13,
"ctxt": 0
},
"local": {
"type": "Identifier",
"span": {
"start": 7,
"end": 13,
"ctxt": 0
},
"value": "assert",
"optional": false
}
}
],
"source": {
"type": "StringLiteral",
"span": {
"start": 19,
"end": 28,
"ctxt": 0
},
"value": "./index",
"hasEscape": false,
"kind": {
"type": "normal",
"containsQuote": true
}
},
"typeOnly": false,
"asserts": null
},
{
"type": "ExpressionStatement",
"span": {
"start": 30,
"end": 46,
"ctxt": 0
},
"expression": {
"type": "CallExpression",
"span": {
"start": 30,
"end": 46,
"ctxt": 0
},
"callee": {
"type": "Identifier",
"span": {
"start": 30,
"end": 36,
"ctxt": 0
},
"value": "assert",
"optional": false
},
"arguments": [
{
"spread": null,
"expression": {
"type": "NumericLiteral",
"span": {
"start": 42,
"end": 45,
"ctxt": 0
},
"value": 123.0
}
}
],
"typeArguments": {
"type": "TsTypeParameterInstantiation",
"span": {
"start": 36,
"end": 41,
"ctxt": 0
},
"params": [
{
"type": "TsLiteralType",
"span": {
"start": 37,
"end": 40,
"ctxt": 0
},
"literal": {
"type": "NumericLiteral",
"span": {
"start": 37,
"end": 40,
"ctxt": 0
},
"value": 123.0
}
}
]
}
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 47,
"end": 68,
"ctxt": 0
},
"expression": {
"type": "CallExpression",
"span": {
"start": 47,
"end": 68,
"ctxt": 0
},
"callee": {
"type": "Identifier",
"span": {
"start": 47,
"end": 53,
"ctxt": 0
},
"value": "assert",
"optional": false
},
"arguments": [
{
"spread": null,
"expression": {
"type": "NumericLiteral",
"span": {
"start": 59,
"end": 62,
"ctxt": 0
},
"value": 123.0
}
},
{
"spread": null,
"expression": {
"type": "NumericLiteral",
"span": {
"start": 64,
"end": 67,
"ctxt": 0
},
"value": 123.0
}
}
],
"typeArguments": {
"type": "TsTypeParameterInstantiation",
"span": {
"start": 53,
"end": 58,
"ctxt": 0
},
"params": [
{
"type": "TsLiteralType",
"span": {
"start": 54,
"end": 57,
"ctxt": 0
},
"literal": {
"type": "NumericLiteral",
"span": {
"start": 54,
"end": 57,
"ctxt": 0
},
"value": 123.0
}
}
]
}
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 69,
"end": 95,
"ctxt": 0
},
"expression": {
"type": "CallExpression",
"span": {
"start": 69,
"end": 95,
"ctxt": 0
},
"callee": {
"type": "Identifier",
"span": {
"start": 69,
"end": 75,
"ctxt": 0
},
"value": "assert",
"optional": false
},
"arguments": [
{
"spread": null,
"expression": {
"type": "NumericLiteral",
"span": {
"start": 81,
"end": 84,
"ctxt": 0
},
"value": 123.0
}
},
{
"spread": null,
"expression": {
"type": "NumericLiteral",
"span": {
"start": 86,
"end": 89,
"ctxt": 0
},
"value": 123.0
}
},
{
"spread": null,
"expression": {
"type": "NumericLiteral",
"span": {
"start": 91,
"end": 94,
"ctxt": 0
},
"value": 123.0
}
}
],
"typeArguments": {
"type": "TsTypeParameterInstantiation",
"span": {
"start": 75,
"end": 80,
"ctxt": 0
},
"params": [
{
"type": "TsLiteralType",
"span": {
"start": 76,
"end": 79,
"ctxt": 0
},
"literal": {
"type": "NumericLiteral",
"span": {
"start": 76,
"end": 79,
"ctxt": 0
},
"value": 123.0
}
}
]
}
}
}
],
"interpreter": null
}