Repository: swc-project/swc (rust)

Issue:
Import token followed by square bracket panics
### Describe the bug

`import[expr]` within an expression panics.

### Input code

```typescript
const str = "meta";

console.log(import[str]);
```


### Config

_No response_

### Playground link

_No response_

### Expected behavior

Syntax error

### Actual behavior

```
thread 'main' panicked at 'internal error: entered unreachable code', /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/swc_ecma_parser-0.104.0/src/parser/expr.rs:1186:43
```

### Version

swc_ecma_parser 0.104.0

### Additional context

_No response_

Pull request:
fix(es/parser): Invalid Meta Property
<!-- Note: CI script will automatically rebase your PR so please do not rebase unless required -->

<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. -->

**Description:**

<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->

**BREAKING CHANGE:**

<!--
If this PR introduces a breaking change, it must contain a notice for it to be included in the CHANGELOG. Add description or remove entirely if not breaking.

You may need to update `MIGRATION.md` for the breaking changes.
-->

**Related issue (if exists):**
- fix #4991 

Patch:
diff --git a/crates/swc_ecma_parser/src/parser/expr.rs b/crates/swc_ecma_parser/src/parser/expr.rs
index 165153a232..50e8f04d09 100644
--- a/crates/swc_ecma_parser/src/parser/expr.rs
+++ b/crates/swc_ecma_parser/src/parser/expr.rs
@@ -1155,6 +1155,10 @@ impl<I: Tokens> Parser<I> {
             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, '?');


Test patch:
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 0000000000..21eea8d675
--- /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 0000000000..3b6b067e64
--- /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 0000000000..3b9ded9dc3
--- /dev/null
+++ b/crates/swc_ecma_parser/tests/errors/issue-4991/2/input.js
@@ -0,0 +1,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 0000000000..7e4b36806d
--- /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 0000000000..9ad30177b7
--- /dev/null
+++ b/crates/swc_ecma_parser/tests/errors/issue-4991/3/input.js
@@ -0,0 +1,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 0000000000..82ead0b76d
--- /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 0000000000..b3193bd488
--- /dev/null
+++ b/crates/swc_ecma_parser/tests/errors/issue-4991/4/input.js
@@ -0,0 +1,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 0000000000..7d5fb6cb37
--- /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]);
+   :                    ^
+   `----