From 53a8cb12094b15aed1667f844abfefea1420d3d2 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Sat, 18 Jun 2022 18:00:52 +0800 Subject: [PATCH] fix(es/parser): Don't panic on an invalid import meta property (#4994) --- crates/swc_ecma_parser/src/parser/expr.rs | 4 ++++ crates/swc_ecma_parser/tests/errors/issue-4991/1/input.js | 3 +++ .../tests/errors/issue-4991/1/input.js.stderr | 6 ++++++ crates/swc_ecma_parser/tests/errors/issue-4991/2/input.js | 1 + .../tests/errors/issue-4991/2/input.js.stderr | 6 ++++++ crates/swc_ecma_parser/tests/errors/issue-4991/3/input.js | 1 + .../tests/errors/issue-4991/3/input.js.stderr | 6 ++++++ crates/swc_ecma_parser/tests/errors/issue-4991/4/input.js | 1 + .../tests/errors/issue-4991/4/input.js.stderr | 6 ++++++ 9 files changed, 34 insertions(+) create mode 100644 crates/swc_ecma_parser/tests/errors/issue-4991/1/input.js create mode 100644 crates/swc_ecma_parser/tests/errors/issue-4991/1/input.js.stderr create mode 100644 crates/swc_ecma_parser/tests/errors/issue-4991/2/input.js create mode 100644 crates/swc_ecma_parser/tests/errors/issue-4991/2/input.js.stderr create mode 100644 crates/swc_ecma_parser/tests/errors/issue-4991/3/input.js create mode 100644 crates/swc_ecma_parser/tests/errors/issue-4991/3/input.js.stderr create mode 100644 crates/swc_ecma_parser/tests/errors/issue-4991/4/input.js create mode 100644 crates/swc_ecma_parser/tests/errors/issue-4991/4/input.js.stderr diff --git a/crates/swc_ecma_parser/src/parser/expr.rs b/crates/swc_ecma_parser/src/parser/expr.rs index 165153a2329..50e8f04d09e 100644 --- a/crates/swc_ecma_parser/src/parser/expr.rs +++ b/crates/swc_ecma_parser/src/parser/expr.rs @@ -1155,6 +1155,10 @@ impl Parser { None }; + if obj.is_import() && !is_one_of!(self, '.', '(') { + unexpected!(self, "`.` or `(`") + } + let question_dot_token = if is!(self, '?') && peeked_is!(self, '.') { let start = cur_pos!(self); eat!(self, '?'); diff --git a/crates/swc_ecma_parser/tests/errors/issue-4991/1/input.js b/crates/swc_ecma_parser/tests/errors/issue-4991/1/input.js new file mode 100644 index 00000000000..21eea8d6756 --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/issue-4991/1/input.js @@ -0,0 +1,3 @@ +const str = "meta"; + +console.log(import[str]); \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/errors/issue-4991/1/input.js.stderr b/crates/swc_ecma_parser/tests/errors/issue-4991/1/input.js.stderr new file mode 100644 index 00000000000..3b6b067e64c --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/issue-4991/1/input.js.stderr @@ -0,0 +1,6 @@ + + x Unexpected token `[`. Expected `.` or `(` + ,-[$DIR/tests/errors/issue-4991/1/input.js:3:1] + 3 | console.log(import[str]); + : ^ + `---- diff --git a/crates/swc_ecma_parser/tests/errors/issue-4991/2/input.js b/crates/swc_ecma_parser/tests/errors/issue-4991/2/input.js new file mode 100644 index 00000000000..3b9ded9dc33 --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/issue-4991/2/input.js @@ -0,0 +1 @@ +console.log(import?.[meta]); \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/errors/issue-4991/2/input.js.stderr b/crates/swc_ecma_parser/tests/errors/issue-4991/2/input.js.stderr new file mode 100644 index 00000000000..7e4b36806de --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/issue-4991/2/input.js.stderr @@ -0,0 +1,6 @@ + + x Unexpected token `?`. Expected `.` or `(` + ,-[$DIR/tests/errors/issue-4991/2/input.js:1:1] + 1 | console.log(import?.[meta]); + : ^ + `---- diff --git a/crates/swc_ecma_parser/tests/errors/issue-4991/3/input.js b/crates/swc_ecma_parser/tests/errors/issue-4991/3/input.js new file mode 100644 index 00000000000..9ad30177b72 --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/issue-4991/3/input.js @@ -0,0 +1 @@ +console.log(import?.meta); \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/errors/issue-4991/3/input.js.stderr b/crates/swc_ecma_parser/tests/errors/issue-4991/3/input.js.stderr new file mode 100644 index 00000000000..82ead0b76dd --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/issue-4991/3/input.js.stderr @@ -0,0 +1,6 @@ + + x Unexpected token `?`. Expected `.` or `(` + ,-[$DIR/tests/errors/issue-4991/3/input.js:1:1] + 1 | console.log(import?.meta); + : ^ + `---- diff --git a/crates/swc_ecma_parser/tests/errors/issue-4991/4/input.js b/crates/swc_ecma_parser/tests/errors/issue-4991/4/input.js new file mode 100644 index 00000000000..b3193bd4886 --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/issue-4991/4/input.js @@ -0,0 +1 @@ +console.log(import.[meta]); \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/errors/issue-4991/4/input.js.stderr b/crates/swc_ecma_parser/tests/errors/issue-4991/4/input.js.stderr new file mode 100644 index 00000000000..7d5fb6cb37a --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/issue-4991/4/input.js.stderr @@ -0,0 +1,6 @@ + + x Expected ident + ,-[$DIR/tests/errors/issue-4991/4/input.js:1:1] + 1 | console.log(import.[meta]); + : ^ + `----