Skip to content

Commit

Permalink
fix(es/parser): Recover from import.meta in scripts (#2042)
Browse files Browse the repository at this point in the history
swc_ecma_parser:
 - Recover from `import.meta` in scripts. (#2041)
 - Allow `import.meta` when using `parse_program`.
  • Loading branch information
kdy1 authored Aug 9, 2021
1 parent 4ead801 commit 8cbbddb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecmascript/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.1"
version = "0.66.2"

[package.metadata.docs.rs]
all-features = true
Expand Down
21 changes: 11 additions & 10 deletions ecmascript/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ impl Syntax {
}
}

pub fn optional_chaining(self) -> bool {
pub const fn optional_chaining(self) -> bool {
true
}

pub fn dynamic_import(self) -> bool {
pub const fn dynamic_import(self) -> bool {
true
}

Expand All @@ -175,7 +175,7 @@ impl Syntax {
}
}

pub fn num_sep(self) -> bool {
pub const fn num_sep(self) -> bool {
true
}

Expand All @@ -191,15 +191,15 @@ impl Syntax {
}
}

pub fn class_private_methods(self) -> bool {
pub const fn class_private_methods(self) -> bool {
true
}

pub fn class_private_props(self) -> bool {
pub const fn class_private_props(self) -> bool {
true
}

pub fn class_props(self) -> bool {
pub const fn class_props(self) -> bool {
true
}

Expand Down Expand Up @@ -240,20 +240,20 @@ impl Syntax {
}

/// `true`
pub fn export_namespace_from(self) -> bool {
pub const fn export_namespace_from(self) -> bool {
true
}

/// `true`
pub fn nullish_coalescing(self) -> bool {
pub const fn nullish_coalescing(self) -> bool {
true
}

pub fn import_meta(self) -> bool {
pub const fn import_meta(self) -> bool {
true
}

pub fn top_level_await(self) -> bool {
pub const fn top_level_await(self) -> bool {
true
}

Expand Down Expand Up @@ -369,6 +369,7 @@ pub struct EsConfig {
pub struct Context {
/// Is in module code?
module: bool,
can_be_module: bool,
strict: bool,
include_in_expr: bool,
/// If true, await expression is parsed, and "await" is treated as a
Expand Down
6 changes: 4 additions & 2 deletions ecmascript/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,10 @@ 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);
self.state.found_module_item = true;
if !self.ctx().can_be_module {
let span = span!(self, start);
self.emit_err(span, SyntaxError::ImportMetaInScript);
}
return self
.parse_import_meta_prop(import)
Expand Down
29 changes: 23 additions & 6 deletions ecmascript/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ struct State {
labels: Vec<JsWord>,
/// Start position of an assignment expression.
potential_arrow_start: Option<BytePos>,

found_module_item: bool,
}

impl<'a, I: Input> Parser<Lexer<'a, I>> {
Expand Down Expand Up @@ -127,15 +129,21 @@ impl<I: Tokens> Parser<I> {
pub fn parse_program(&mut self) -> PResult<Program> {
let start = cur_pos!(self);
let shebang = self.parse_shebang()?;
let ctx = Context {
can_be_module: true,
..self.ctx()
};

let body: Vec<ModuleItem> = self.parse_block_body(true, true, None)?;
let has_module_item = body.iter().any(|item| match item {
ModuleItem::ModuleDecl(..) => true,
_ => false,
});
let body: Vec<ModuleItem> = self.with_ctx(ctx).parse_block_body(true, true, None)?;
let has_module_item = self.state.found_module_item
|| body.iter().any(|item| match item {
ModuleItem::ModuleDecl(..) => true,
_ => false,
});
if has_module_item && !self.ctx().module {
let ctx = Context {
module: true,
can_be_module: true,
strict: true,
..self.ctx()
};
Expand Down Expand Up @@ -168,6 +176,7 @@ impl<I: Tokens> Parser<I> {
pub fn parse_module(&mut self) -> PResult<Module> {
let ctx = Context {
module: true,
can_be_module: true,
strict: true,
..self.ctx()
};
Expand Down Expand Up @@ -239,12 +248,20 @@ where
let lexer = Lexer::new(syntax, JscTarget::Es2019, input, None);
let mut p = Parser::new_from(lexer);
let ret = f(&mut p);
let mut error = false;

for err in p.take_errors() {
error = true;
err.into_diagnostic(handler).emit();
}

ret.map_err(|err| err.into_diagnostic(handler).emit())
let res = ret.map_err(|err| err.into_diagnostic(handler).emit())?;

if error {
return Err(());
}

Ok(res)
})
.unwrap_or_else(|output| panic!("test_parser(): failed to parse \n{}\n{}", s, output))
}
Expand Down
13 changes: 13 additions & 0 deletions ecmascript/parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,19 @@ export default function waitUntil(callback, options = {}) {
);
}

#[test]
fn import_meta_in_program() {
let src = "const foo = import.meta.url;";
test_parser(
src,
Syntax::Es(EsConfig {
import_meta: true,
..Default::default()
}),
|p| p.parse_program(),
);
}

#[test]
#[should_panic(expected = "'import', and 'export' cannot be used outside of module code")]
fn import_statement_in_script() {
Expand Down

0 comments on commit 8cbbddb

Please sign in to comment.