fix(es/parser): Use correct error message (#2025)

swc_ecma_parser:
 - Use correct error message for import statements in a script.
This commit is contained in:
Sosuke Suzuki 2021-08-07 16:43:23 +09:00 committed by GitHub
parent 4cdb45ff2e
commit 8a39c1db97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 244 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.66.0"
version = "0.66.1"
[package.metadata.docs.rs]
all-features = true

View File

@ -232,6 +232,9 @@ impl<'a, I: Tokens> Parser<I> {
tok!("import") => {
let import = self.parse_ident_name()?;
if self.input.syntax().import_meta() && is!(self, '.') {
if !self.ctx().module {
syntax_error!(self, SyntaxError::ImportMetaInScript);
}
return self
.parse_import_meta_prop(import)
.map(Expr::MetaProp)

View File

@ -1148,7 +1148,7 @@ pub(super) trait StmtLikeParser<'a, Type: IsDirective> {
impl<'a, I: Tokens> StmtLikeParser<'a, Stmt> for Parser<I> {
fn handle_import_export(&mut self, top_level: bool, _: Vec<Decorator>) -> PResult<Stmt> {
let start = cur_pos!(self);
if self.input.syntax().dynamic_import() && is!(self, "import") {
if self.input.syntax().dynamic_import() && is!(self, "import") && peeked_is!(self, '(') {
let expr = self.parse_expr()?;
eat!(self, ';');
@ -1161,10 +1161,6 @@ impl<'a, I: Tokens> StmtLikeParser<'a, Stmt> for Parser<I> {
}
if self.input.syntax().import_meta() && is!(self, "import") && peeked_is!(self, '.') {
if !self.ctx().module {
syntax_error!(self, SyntaxError::ImportMetaInScript);
}
let expr = self.parse_expr()?;
eat!(self, ';');
@ -1798,7 +1794,7 @@ export default function waitUntil(callback, options = {}) {
#[test]
#[should_panic(expected = "'import.meta' cannot be used outside of module code.")]
fn import_meta_in_script() {
let src = "import.meta.url;";
let src = "const foo = import.meta.url;";
test_parser(
src,
Syntax::Es(EsConfig {
@ -1808,4 +1804,18 @@ export default function waitUntil(callback, options = {}) {
|p| p.parse_script(),
);
}
#[test]
#[should_panic(expected = "'import', and 'export' cannot be used outside of module code")]
fn import_statement_in_script() {
let src = "import 'foo';";
test_parser(
src,
Syntax::Es(EsConfig {
dynamic_import: true,
..Default::default()
}),
|p| p.parse_script(),
);
}
}

View File

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

View File

@ -1,170 +0,0 @@
{
"type": "Script",
"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",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "MemberExpression",
"span": {
"start": 13,
"end": 33,
"ctxt": 0
},
"object": {
"type": "MetaProperty",
"meta": {
"type": "Identifier",
"span": {
"start": 13,
"end": 19,
"ctxt": 0
},
"value": "import",
"optional": false
},
"property": {
"type": "Identifier",
"span": {
"start": 20,
"end": 24,
"ctxt": 0
},
"value": "meta",
"optional": false
}
},
"property": {
"type": "Identifier",
"span": {
"start": 25,
"end": 33,
"ctxt": 0
},
"value": "someProp",
"optional": false
},
"computed": false
},
"definite": false
}
]
},
{
"type": "FunctionDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 45,
"end": 49,
"ctxt": 0
},
"value": "func",
"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",
"optional": false
},
"property": {
"type": "Identifier",
"span": {
"start": 65,
"end": 69,
"ctxt": 0
},
"value": "meta",
"optional": false
}
},
"property": {
"type": "Identifier",
"span": {
"start": 70,
"end": 78,
"ctxt": 0
},
"value": "someProp",
"optional": false
},
"computed": false
}
}
]
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": null
}
],
"interpreter": null
}

View File

@ -1 +0,0 @@
import.meta.someProp;

View File

@ -1,61 +0,0 @@
{
"type": "Script",
"span": {
"start": 0,
"end": 21,
"ctxt": 0
},
"body": [
{
"type": "ExpressionStatement",
"span": {
"start": 0,
"end": 21,
"ctxt": 0
},
"expression": {
"type": "MemberExpression",
"span": {
"start": 0,
"end": 20,
"ctxt": 0
},
"object": {
"type": "MetaProperty",
"meta": {
"type": "Identifier",
"span": {
"start": 0,
"end": 6,
"ctxt": 0
},
"value": "import",
"optional": false
},
"property": {
"type": "Identifier",
"span": {
"start": 7,
"end": 11,
"ctxt": 0
},
"value": "meta",
"optional": false
}
},
"property": {
"type": "Identifier",
"span": {
"start": 12,
"end": 20,
"ctxt": 0
},
"value": "someProp",
"optional": false
},
"computed": false
}
}
],
"interpreter": null
}