Repository: swc-project/swc (rust)

Issue:
using `parse_file_as_expr` to parse `import.meta` will throw `SyntaxError::ImportMetaInScript`
### Describe the bug

![image](https://user-images.githubusercontent.com/49502170/211561872-2d9d83b7-7cf5-4304-b70b-5413a3527e66.png)

![image](https://user-images.githubusercontent.com/49502170/211562435-8298bc13-6662-4722-89f1-f8e82781fde5.png)


### Input code

_No response_

### Config

_No response_

### Playground link

_No response_

### Expected behavior

Should parse successfully.

### Actual behavior

_No response_

### Version

newest

### Additional context

Just doing some expression replacements such as `import.meta` => `true`.

Pull request:
fix(es/parser): parsing `import.meta` should not throw error using `parse_file_as_expr`
<!-- 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 #6781.

Patch:
diff --git a/crates/swc_ecma_parser/src/lib.rs b/crates/swc_ecma_parser/src/lib.rs
index b523edfabb..3ad332067e 100644
--- a/crates/swc_ecma_parser/src/lib.rs
+++ b/crates/swc_ecma_parser/src/lib.rs
@@ -442,7 +442,11 @@ macro_rules! expose {
     };
 }
 
-expose!(parse_file_as_expr, Box<Expr>, |p| { p.parse_expr() });
+expose!(parse_file_as_expr, Box<Expr>, |p| {
+    // This allow to parse `import.meta`
+    p.input().ctx.can_be_module = true;
+    p.parse_expr()
+});
 expose!(parse_file_as_module, Module, |p| { p.parse_module() });
 expose!(parse_file_as_script, Script, |p| { p.parse_script() });
 expose!(parse_file_as_program, Program, |p| { p.parse_program() });


Test patch:
diff --git a/crates/swc_ecma_parser/src/parser/expr/tests.rs b/crates/swc_ecma_parser/src/parser/expr/tests.rs
index 5e650e87fb..f98f473ed6 100644
--- a/crates/swc_ecma_parser/src/parser/expr/tests.rs
+++ b/crates/swc_ecma_parser/src/parser/expr/tests.rs
@@ -2,12 +2,12 @@ extern crate test;
 
 use std::hint::black_box;
 
-use swc_common::DUMMY_SP as span;
+use swc_common::{FileName, SourceMap, DUMMY_SP as span};
 use swc_ecma_visit::assert_eq_ignore_span;
 use test::Bencher;
 
 use super::*;
-use crate::EsConfig;
+use crate::{parse_file_as_expr, EsConfig};
 
 fn syntax() -> Syntax {
     Syntax::Es(EsConfig {
@@ -519,6 +519,22 @@ fn issue_5947() {
     );
 }
 
+#[test]
+fn issue_6781() {
+    let cm = SourceMap::default();
+    let fm = cm.new_source_file(FileName::Anon, "import.meta.env".to_string());
+    let mut errors = vec![];
+    let expr = parse_file_as_expr(
+        &fm,
+        Default::default(),
+        Default::default(),
+        None,
+        &mut errors,
+    );
+    assert!(expr.is_ok());
+    assert!(errors.is_empty());
+}
+
 #[bench]
 fn bench_new_expr_ts(b: &mut Bencher) {
     bench_parser(