From 375774d31e690142faeb8422fed4cc996bf1de16 Mon Sep 17 00:00:00 2001 From: Austaras Date: Wed, 13 Jul 2022 18:29:03 +0800 Subject: [PATCH] fix(es): Check the syntax context of `arguments` (#5174) --- crates/swc/src/builder.rs | 15 +- crates/swc_ecma_parser/src/error.rs | 4 + crates/swc_ecma_parser/src/lib.rs | 2 + .../src/parser/class_and_fn.rs | 11 +- crates/swc_ecma_parser/src/parser/expr.rs | 23 +- crates/swc_ecma_parser/src/parser/ident.rs | 8 +- crates/swc_ecma_parser/src/parser/object.rs | 3 + crates/swc_ecma_parser/src/parser/pat.rs | 47 ++- crates/swc_ecma_parser/src/parser/util.rs | 12 +- .../tests/errors/arguments/input.js | 9 + .../tests/errors/arguments/input.js.stderr | 12 + .../errors/strict-mode/argument/input.js | 3 + .../strict-mode/argument/input.js.stderr | 12 + crates/swc_ecma_preset_env/src/lib.rs | 17 +- .../tests/es2015_function_name.rs | 31 +- .../src/bugfixes/async_arrows_in_class.rs | 19 +- .../src/bugfixes/mod.rs | 6 +- .../src/es2015/arrow.rs | 9 +- .../src/es2015/block_scoping/mod.rs | 7 +- .../src/es2015/mod.rs | 8 +- .../src/es2015/parameters.rs | 12 +- .../src/es2017/async_to_generator.rs | 16 +- .../src/es2017/mod.rs | 5 +- .../tests/es2015_arrow.rs | 63 ++-- .../tests/es2015_block_scoping.rs | 95 +++--- .../tests/es2015_classes.rs | 27 +- .../tests/es2015_destructuring.rs | 54 ++-- .../tests/es2015_new_target.rs | 4 +- .../tests/es2015_parameters.rs | 25 +- .../tests/es2015_regenerator.rs | 72 +++-- .../tests/es2015_spread.rs | 12 +- .../tests/es2017_async_to_generator.rs | 288 +++++++++--------- .../tests/es2022_class_properties.rs | 71 ++--- .../tests/simplify.rs | 2 +- .../src/jsx/tests.rs | 2 +- .../benches/compat.rs | 16 +- .../tests/strip.rs | 17 +- .../src/function/fn_env_hoister.rs | 33 +- 38 files changed, 652 insertions(+), 420 deletions(-) create mode 100644 crates/swc_ecma_parser/tests/errors/arguments/input.js create mode 100644 crates/swc_ecma_parser/tests/errors/arguments/input.js.stderr create mode 100644 crates/swc_ecma_parser/tests/errors/strict-mode/argument/input.js create mode 100644 crates/swc_ecma_parser/tests/errors/strict-mode/argument/input.js.stderr diff --git a/crates/swc/src/builder.rs b/crates/swc/src/builder.rs index 823ce103e2d..9175c5e949d 100644 --- a/crates/swc/src/builder.rs +++ b/crates/swc/src/builder.rs @@ -258,12 +258,15 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { should_enable(self.target, EsVersion::Es2018) ), Optional::new( - compat::es2017(compat::es2017::Config { - async_to_generator: compat::es2017::async_to_generator::Config { - ignore_function_name: assumptions.ignore_function_name, - ignore_function_length: assumptions.ignore_function_length - } - }), + compat::es2017( + compat::es2017::Config { + async_to_generator: compat::es2017::async_to_generator::Config { + ignore_function_name: assumptions.ignore_function_name, + ignore_function_length: assumptions.ignore_function_length + }, + }, + self.unresolved_mark + ), should_enable(self.target, EsVersion::Es2017) ), Optional::new( diff --git a/crates/swc_ecma_parser/src/error.rs b/crates/swc_ecma_parser/src/error.rs index ce884a425a1..dd956ebcaec 100644 --- a/crates/swc_ecma_parser/src/error.rs +++ b/crates/swc_ecma_parser/src/error.rs @@ -98,6 +98,7 @@ pub enum SyntaxError { InvalidIdentInStrict, /// 'eval' and 'arguments' are invalid identifier in strict mode. EvalAndArgumentsInStrict, + ArgumentsInClassField, IllegalLanguageModeDirective, UnaryInExp { left: String, @@ -325,6 +326,9 @@ impl SyntaxError { SyntaxError::EvalAndArgumentsInStrict => "'eval' and 'arguments' cannot be used as a \ binding identifier in strict mode" .into(), + SyntaxError::ArgumentsInClassField => { + "'arguments' is only allowed in functions and class methods".into() + } SyntaxError::IllegalLanguageModeDirective => { "Illegal 'use strict' directive in function with non-simple parameter list.".into() } diff --git a/crates/swc_ecma_parser/src/lib.rs b/crates/swc_ecma_parser/src/lib.rs index adafa81dced..89b817bfa71 100644 --- a/crates/swc_ecma_parser/src/lib.rs +++ b/crates/swc_ecma_parser/src/lib.rs @@ -361,6 +361,8 @@ pub struct Context { in_class: bool, + in_class_field: bool, + in_function: bool, /// This indicates current scope or the scope out of arrow function is diff --git a/crates/swc_ecma_parser/src/parser/class_and_fn.rs b/crates/swc_ecma_parser/src/parser/class_and_fn.rs index d4635dc50c1..a66995bae4b 100644 --- a/crates/swc_ecma_parser/src/parser/class_and_fn.rs +++ b/crates/swc_ecma_parser/src/parser/class_and_fn.rs @@ -519,7 +519,12 @@ impl Parser { } fn parse_static_block(&mut self, start: BytePos) -> PResult { - let body = self.parse_block(false)?; + let body = self + .with_ctx(Context { + in_class_field: true, + ..self.ctx() + }) + .parse_block(false)?; let span = span!(self, start); Ok(StaticBlock { span, body }.into()) @@ -991,6 +996,7 @@ impl Parser { let ctx = Context { include_in_expr: true, + in_class_field: true, ..self.ctx() }; self.with_ctx(ctx).parse_with(|p| { @@ -1082,6 +1088,7 @@ impl Parser { in_async: is_async, in_generator: is_generator, allow_direct_super: false, + in_class_field: false, ..self.ctx() }) .parse_maybe_opt_binding_ident(is_ident_required)? @@ -1089,6 +1096,7 @@ impl Parser { // function declaration does not change context for `BindingIdentifier`. self.with_ctx(Context { allow_direct_super: false, + in_class_field: false, ..self.ctx() }) .parse_maybe_opt_binding_ident(is_ident_required)? @@ -1096,6 +1104,7 @@ impl Parser { self.with_ctx(Context { allow_direct_super: false, + in_class_field: false, ..self.ctx() }) .parse_with(|p| { diff --git a/crates/swc_ecma_parser/src/parser/expr.rs b/crates/swc_ecma_parser/src/parser/expr.rs index 63feec5b18e..9624cd8bfd7 100644 --- a/crates/swc_ecma_parser/src/parser/expr.rs +++ b/crates/swc_ecma_parser/src/parser/expr.rs @@ -165,13 +165,12 @@ impl Parser { { self.emit_err(cond.span(), SyntaxError::NotSimpleAssign) } - let is_eval_or_arguments = match *cond { - Expr::Ident(ref i) => { - i.sym == js_word!("eval") || i.sym == js_word!("arguments") - } - _ => false, - }; - if self.input.syntax().typescript() && is_eval_or_arguments { + if self.input.syntax().typescript() + && cond + .as_ident() + .map(|i| i.is_reserved_in_strict_bind()) + .unwrap_or(false) + { self.emit_strict_mode_err(cond.span(), SyntaxError::TS1100); } @@ -375,7 +374,8 @@ impl Parser { || (self.input.syntax().typescript() && is_one_of!(self, IdentRef, "await")) || is!(self, IdentRef) { - let id = self.parse_ident_name()?; + let ctx = self.ctx(); + let id = self.parse_ident(!ctx.in_generator, !ctx.in_async)?; if id.is_reserved_in_strict_mode(self.ctx().module && !self.ctx().in_declare) { self.emit_strict_mode_err( self.input.prev_span(), @@ -418,6 +418,9 @@ impl Parser { type_params: None, }))); } else if can_be_arrow && !self.input.had_line_break_before_cur() && eat!(self, "=>") { + if self.ctx().strict && id.is_reserved_in_strict_bind() { + self.emit_strict_mode_err(id.span, SyntaxError::EvalAndArgumentsInStrict) + } let params = vec![id.into()]; let body = self.parse_fn_body(false, false, true, params.is_simple_parameter_list())?; @@ -1928,8 +1931,8 @@ impl Parser { // We follow behavior of tsc if self.input.syntax().typescript() && self.syntax().early_errors() { - let is_eval_or_arguments = match *expr { - Expr::Ident(ref i) => i.sym == js_word!("eval") || i.sym == js_word!("arguments"), + let is_eval_or_arguments = match expr { + Expr::Ident(i) => i.is_reserved_in_strict_bind(), _ => false, }; diff --git a/crates/swc_ecma_parser/src/parser/ident.rs b/crates/swc_ecma_parser/src/parser/ident.rs index df258ae80e8..e0fe44e52cd 100644 --- a/crates/swc_ecma_parser/src/parser/ident.rs +++ b/crates/swc_ecma_parser/src/parser/ident.rs @@ -132,7 +132,6 @@ impl Parser { // Spec: // It is a Syntax Error if StringValue of IdentifierName is the same String // value as the StringValue of any ReservedWord except for yield or await. - match w { Word::Keyword(Keyword::Await) if p.input.syntax().typescript() => { Ok(js_word!("await")) @@ -147,7 +146,12 @@ impl Parser { Ok(js_word!("this")) } Word::Keyword(Keyword::Let) => Ok(js_word!("let")), - Word::Ident(ident) => Ok(ident), + Word::Ident(ident) => { + if ident == js_word!("arguments") && p.ctx().in_class_field { + p.emit_err(p.input.prev_span(), SyntaxError::ArgumentsInClassField) + } + Ok(ident) + } Word::Keyword(Keyword::Yield) if incl_yield => Ok(js_word!("yield")), Word::Keyword(Keyword::Await) if incl_await => Ok(js_word!("await")), Word::Keyword(..) | Word::Null | Word::True | Word::False => { diff --git a/crates/swc_ecma_parser/src/parser/object.rs b/crates/swc_ecma_parser/src/parser/object.rs index f5d4b90fb1c..ab49b2ef63b 100644 --- a/crates/swc_ecma_parser/src/parser/object.rs +++ b/crates/swc_ecma_parser/src/parser/object.rs @@ -160,6 +160,7 @@ impl ParseObject> for Parser { return self .with_ctx(Context { allow_direct_super: true, + in_class_field: false, ..self.ctx() }) .parse_fn_args_body( @@ -216,6 +217,7 @@ impl ParseObject> for Parser { return self .with_ctx(Context { allow_direct_super: true, + in_class_field: false, ..self.ctx() }) .parse_fn_args_body( @@ -276,6 +278,7 @@ impl ParseObject> for Parser { let key_span = key.span(); self.with_ctx(Context { allow_direct_super: true, + in_class_field: false, ..self.ctx() }) .parse_with(|parser| { diff --git a/crates/swc_ecma_parser/src/parser/pat.rs b/crates/swc_ecma_parser/src/parser/pat.rs index 13adcc39d12..0242645111e 100644 --- a/crates/swc_ecma_parser/src/parser/pat.rs +++ b/crates/swc_ecma_parser/src/parser/pat.rs @@ -33,7 +33,7 @@ impl Parser { // "yield" and "await" is **lexically** accepted. let ident = self.parse_ident(true, true)?; - if ident.sym == js_word!("arguments") || ident.sym == js_word!("eval") { + if ident.is_reserved_in_strict_bind() { self.emit_strict_mode_err(ident.span, SyntaxError::EvalAndArgumentsInStrict); } if self.ctx().in_async && ident.sym == js_word!("await") { @@ -867,8 +867,53 @@ impl Parser { }; params.push(last); + if self.ctx().strict { + for param in params.iter() { + self.pat_is_valid_argument_in_strict(param) + } + } + Ok(params) } + + /// argument of arrow is pattern, although idents in pattern is already + /// checked if is a keyword, it should also be checked if is arguments or + /// eval + fn pat_is_valid_argument_in_strict(&self, pat: &Pat) { + match pat { + Pat::Ident(i) => { + if i.id.is_reserved_in_strict_bind() { + self.emit_strict_mode_err(i.id.span, SyntaxError::EvalAndArgumentsInStrict) + } + } + Pat::Array(arr) => { + for pat in arr.elems.iter().flatten() { + self.pat_is_valid_argument_in_strict(pat) + } + } + Pat::Rest(r) => self.pat_is_valid_argument_in_strict(&*r.arg), + Pat::Object(obj) => { + for prop in obj.props.iter() { + match prop { + ObjectPatProp::KeyValue(KeyValuePatProp { value, .. }) + | ObjectPatProp::Rest(RestPat { arg: value, .. }) => { + self.pat_is_valid_argument_in_strict(&**value) + } + ObjectPatProp::Assign(AssignPatProp { key, .. }) => { + if key.is_reserved_in_strict_bind() { + self.emit_strict_mode_err( + key.span, + SyntaxError::EvalAndArgumentsInStrict, + ) + } + } + } + } + } + Pat::Assign(a) => self.pat_is_valid_argument_in_strict(&*a.left), + Pat::Invalid(_) | Pat::Expr(_) => (), + } + } } #[cfg(test)] diff --git a/crates/swc_ecma_parser/src/parser/util.rs b/crates/swc_ecma_parser/src/parser/util.rs index 593b2957be6..86aec6959a6 100644 --- a/crates/swc_ecma_parser/src/parser/util.rs +++ b/crates/swc_ecma_parser/src/parser/util.rs @@ -249,9 +249,9 @@ pub(super) trait ExprExt { /// "IsValidSimpleAssignmentTarget" from spec. fn is_valid_simple_assignment_target(&self, strict: bool) -> bool { - match *self.as_expr() { - Expr::Ident(Ident { ref sym, .. }) => { - if strict && (sym == "arguments" || sym == "eval") { + match self.as_expr() { + Expr::Ident(ident) => { + if strict && ident.is_reserved_in_strict_bind() { return false; } true @@ -265,11 +265,9 @@ pub(super) trait ExprExt { | Expr::Class(..) | Expr::Tpl(..) | Expr::TaggedTpl(..) => false, - Expr::Paren(ParenExpr { ref expr, .. }) => { - expr.is_valid_simple_assignment_target(strict) - } + Expr::Paren(ParenExpr { expr, .. }) => expr.is_valid_simple_assignment_target(strict), - Expr::Member(MemberExpr { ref obj, .. }) => match obj.as_ref() { + Expr::Member(MemberExpr { obj, .. }) => match obj.as_ref() { Expr::Member(..) => obj.is_valid_simple_assignment_target(strict), Expr::OptChain(..) => false, _ => true, diff --git a/crates/swc_ecma_parser/tests/errors/arguments/input.js b/crates/swc_ecma_parser/tests/errors/arguments/input.js new file mode 100644 index 00000000000..fced34d90dd --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/arguments/input.js @@ -0,0 +1,9 @@ +class A { + static { + class B { + [arguments] + } + } + + foo = arguments +} \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/errors/arguments/input.js.stderr b/crates/swc_ecma_parser/tests/errors/arguments/input.js.stderr new file mode 100644 index 00000000000..ae333013bf4 --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/arguments/input.js.stderr @@ -0,0 +1,12 @@ + + x 'arguments' is only allowed in functions and class methods + ,-[$DIR/tests/errors/arguments/input.js:4:13] + 4 | [arguments] + : ^^^^^^^^^ + `---- + + x 'arguments' is only allowed in functions and class methods + ,-[$DIR/tests/errors/arguments/input.js:8:5] + 8 | foo = arguments + : ^^^^^^^^^ + `---- diff --git a/crates/swc_ecma_parser/tests/errors/strict-mode/argument/input.js b/crates/swc_ecma_parser/tests/errors/strict-mode/argument/input.js new file mode 100644 index 00000000000..09e28342b5d --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/strict-mode/argument/input.js @@ -0,0 +1,3 @@ +export function f0() { + return Array.from(arguments, arguments => arguments + 1, (arguments, b) => arguments + b); +} \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/errors/strict-mode/argument/input.js.stderr b/crates/swc_ecma_parser/tests/errors/strict-mode/argument/input.js.stderr new file mode 100644 index 00000000000..0889094e0bf --- /dev/null +++ b/crates/swc_ecma_parser/tests/errors/strict-mode/argument/input.js.stderr @@ -0,0 +1,12 @@ + + x 'eval' and 'arguments' cannot be used as a binding identifier in strict mode + ,-[$DIR/tests/errors/strict-mode/argument/input.js:2:5] + 2 | return Array.from(arguments, arguments => arguments + 1, (arguments, b) => arguments + b); + : ^^^^^^^^^ + `---- + + x 'eval' and 'arguments' cannot be used as a binding identifier in strict mode + ,-[$DIR/tests/errors/strict-mode/argument/input.js:2:5] + 2 | return Array.from(arguments, arguments => arguments + 1, (arguments, b) => arguments + b); + : ^^^^^^^^^ + `---- diff --git a/crates/swc_ecma_preset_env/src/lib.rs b/crates/swc_ecma_preset_env/src/lib.rs index e88c9647fc1..fbfcf0ddb4b 100644 --- a/crates/swc_ecma_preset_env/src/lib.rs +++ b/crates/swc_ecma_preset_env/src/lib.rs @@ -179,10 +179,13 @@ where let pass = add!( pass, AsyncToGenerator, - es2017::async_to_generator(es2017::async_to_generator::Config { - ignore_function_name: loose || assumptions.ignore_function_name, - ignore_function_length: loose || assumptions.ignore_function_length, - }) + es2017::async_to_generator( + es2017::async_to_generator::Config { + ignore_function_name: loose || assumptions.ignore_function_name, + ignore_function_length: loose || assumptions.ignore_function_length, + }, + global_mark + ) ); // ES2016 @@ -220,7 +223,7 @@ where ); let pass = add!(pass, ObjectSuper, es2015::object_super()); let pass = add!(pass, FunctionName, es2015::function_name()); - let pass = add!(pass, ArrowFunctions, es2015::arrow()); + let pass = add!(pass, ArrowFunctions, es2015::arrow(global_mark)); let pass = add!(pass, DuplicateKeys, es2015::duplicate_keys()); let pass = add!(pass, StickyRegex, es2015::sticky_regex()); // TODO: InstanceOf, @@ -262,7 +265,7 @@ where es2015::regenerator(Default::default(), global_mark), true ); - let pass = add!(pass, BlockScoping, es2015::block_scoping(), true); + let pass = add!(pass, BlockScoping, es2015::block_scoping(global_mark), true); let pass = add!(pass, NewTarget, es2015::new_target(), true); @@ -290,7 +293,7 @@ where let pass = add!( pass, BugfixAsyncArrowsInClass, - bugfixes::async_arrows_in_class() + bugfixes::async_arrows_in_class(global_mark) ); let pass = add!( pass, diff --git a/crates/swc_ecma_transforms/tests/es2015_function_name.rs b/crates/swc_ecma_transforms/tests/es2015_function_name.rs index 9219ba12ead..e0fb11241df 100644 --- a/crates/swc_ecma_transforms/tests/es2015_function_name.rs +++ b/crates/swc_ecma_transforms/tests/es2015_function_name.rs @@ -28,7 +28,7 @@ fn tr() -> impl Fold { chain!( resolver(unresolved_mark, top_level_mark, false), function_name(), - block_scoping() + block_scoping(unresolved_mark) ) } @@ -246,7 +246,7 @@ export const y = function y() {}; test!( ignore, syntax(), - |_| chain!(arrow(), function_name()), + |_| chain!(arrow(Mark::new()), function_name()), function_name_with_arrow_functions_transform, r#" const x = () => x; @@ -381,17 +381,20 @@ var g = function g() { test!( ignore, syntax(), - |t| chain!( - arrow(), - shorthand(), - function_name(), - common_js( - Mark::fresh(Mark::root()), - Default::default(), - Default::default(), - Some(t.comments.clone()) + |t| { + let unresolved_mark = Mark::new(); + chain!( + arrow(unresolved_mark), + shorthand(), + function_name(), + common_js( + unresolved_mark, + Default::default(), + Default::default(), + Some(t.comments.clone()) + ) ) - ), + }, function_name_export_default_arrow_renaming, r#" export default (a) => { @@ -557,7 +560,7 @@ _f = null; test!( ignore, syntax(), - |_| chain!(arrow(), function_name()), + |_| chain!(arrow(Mark::new()), function_name()), function_name_with_arrow_functions_transform_spec, r#" // These are actually handled by transform-arrow-functions @@ -625,7 +628,7 @@ test!( test!( ignore, syntax(), - |_| chain!(arrow(), shorthand(), function_name()), + |_| chain!(arrow(Mark::new()), shorthand(), function_name()), function_name_export_default_arrow_renaming_module_es6, r#" export default (a) => { diff --git a/crates/swc_ecma_transforms_compat/src/bugfixes/async_arrows_in_class.rs b/crates/swc_ecma_transforms_compat/src/bugfixes/async_arrows_in_class.rs index 30f2a4fa128..0a816bcf328 100644 --- a/crates/swc_ecma_transforms_compat/src/bugfixes/async_arrows_in_class.rs +++ b/crates/swc_ecma_transforms_compat/src/bugfixes/async_arrows_in_class.rs @@ -1,4 +1,4 @@ -use swc_common::{util::take::Take, DUMMY_SP}; +use swc_common::{util::take::Take, Mark, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::prepend_stmt; use swc_ecma_visit::{noop_fold_type, Fold, FoldWith, InjectVars}; @@ -12,12 +12,16 @@ use crate::es2015::arrow; /// converting arrow functions in class methods into equivalent function /// expressions. See https://bugs.webkit.org/show_bug.cgi?id=166879 #[tracing::instrument(level = "info", skip_all)] -pub fn async_arrows_in_class() -> impl Fold { - AsyncArrowsInClass::default() +pub fn async_arrows_in_class(unresolved_mark: Mark) -> impl Fold { + AsyncArrowsInClass { + unresolved_mark, + ..Default::default() + } } #[derive(Default, Clone)] struct AsyncArrowsInClass { in_class_method: bool, + unresolved_mark: Mark, vars: Vec, } @@ -49,7 +53,7 @@ impl Fold for AsyncArrowsInClass { match n { Expr::Arrow(ref a) => { if a.is_async { - let mut v = arrow(); + let mut v = arrow(self.unresolved_mark); let n = n.fold_with(&mut v); self.vars.extend(v.take_vars()); n @@ -98,16 +102,17 @@ impl Fold for AsyncArrowsInClass { #[cfg(test)] mod tests { - use swc_common::{chain, Mark}; + use swc_common::chain; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_testing::test; use super::*; fn tr() -> impl Fold { + let unresolved = Mark::new(); chain!( - resolver(Mark::new(), Mark::new(), false), - async_arrows_in_class() + resolver(unresolved, Mark::new(), false), + async_arrows_in_class(unresolved) ) } diff --git a/crates/swc_ecma_transforms_compat/src/bugfixes/mod.rs b/crates/swc_ecma_transforms_compat/src/bugfixes/mod.rs index 68dab2d8082..2ae52d2db39 100644 --- a/crates/swc_ecma_transforms_compat/src/bugfixes/mod.rs +++ b/crates/swc_ecma_transforms_compat/src/bugfixes/mod.rs @@ -1,4 +1,4 @@ -use swc_common::chain; +use swc_common::{chain, Mark}; use swc_ecma_visit::Fold; pub use self::{ @@ -13,9 +13,9 @@ mod safari_id_destructuring_collision_in_function_expression; mod template_literal_caching; #[tracing::instrument(level = "info", skip_all)] -pub fn bugfixes() -> impl Fold { +pub fn bugfixes(unresolved_mark: Mark) -> impl Fold { chain!( - async_arrows_in_class(), + async_arrows_in_class(unresolved_mark), edge_default_param(), template_literal_caching(), safari_id_destructuring_collision_in_function_expression() diff --git a/crates/swc_ecma_transforms_compat/src/es2015/arrow.rs b/crates/swc_ecma_transforms_compat/src/es2015/arrow.rs index 126e08ebaf3..cd9d3e2fbd1 100644 --- a/crates/swc_ecma_transforms_compat/src/es2015/arrow.rs +++ b/crates/swc_ecma_transforms_compat/src/es2015/arrow.rs @@ -1,6 +1,6 @@ use std::mem; -use swc_common::{util::take::Take, DUMMY_SP}; +use swc_common::{util::take::Take, Mark, SyntaxContext, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_utils::{ function::{init_this, FnEnvHoister}, @@ -58,8 +58,11 @@ use swc_trace_macro::swc_trace; /// console.log(bob.printFriends()); /// ``` #[tracing::instrument(level = "info", skip_all)] -pub fn arrow() -> impl Fold + VisitMut + InjectVars { - as_folder(Arrow::default()) +pub fn arrow(unresolved_mark: Mark) -> impl Fold + VisitMut + InjectVars { + as_folder(Arrow { + in_subclass: false, + hoister: FnEnvHoister::new(SyntaxContext::empty().apply_mark(unresolved_mark)), + }) } #[derive(Default)] diff --git a/crates/swc_ecma_transforms_compat/src/es2015/block_scoping/mod.rs b/crates/swc_ecma_transforms_compat/src/es2015/block_scoping/mod.rs index fbbbcdef98b..903f240ead9 100644 --- a/crates/swc_ecma_transforms_compat/src/es2015/block_scoping/mod.rs +++ b/crates/swc_ecma_transforms_compat/src/es2015/block_scoping/mod.rs @@ -36,10 +36,11 @@ mod vars; /// } /// ``` #[tracing::instrument(level = "info", skip_all)] -pub fn block_scoping() -> impl VisitMut + Fold { +pub fn block_scoping(unresolved_mark: Mark) -> impl VisitMut + Fold { as_folder(chain!( self::vars::block_scoped_vars(), BlockScoping { + unresolved_mark, scope: Default::default(), vars: vec![], var_decl_kind: VarDeclKind::Var, @@ -67,6 +68,7 @@ enum ScopeKind { } struct BlockScoping { + unresolved_mark: Mark, scope: ScopeStack, vars: Vec, var_decl_kind: VarDeclKind, @@ -152,7 +154,8 @@ impl BlockScoping { return; } - let mut env_hoister = FnEnvHoister::default(); + let mut env_hoister = + FnEnvHoister::new(SyntaxContext::empty().apply_mark(self.unresolved_mark)); body_stmt.visit_mut_with(&mut env_hoister); self.vars.extend(env_hoister.to_decl()); diff --git a/crates/swc_ecma_transforms_compat/src/es2015/mod.rs b/crates/swc_ecma_transforms_compat/src/es2015/mod.rs index 966b11af881..f37ee7c2fc6 100644 --- a/crates/swc_ecma_transforms_compat/src/es2015/mod.rs +++ b/crates/swc_ecma_transforms_compat/src/es2015/mod.rs @@ -34,9 +34,9 @@ pub mod template_literal; mod typeof_symbol; #[tracing::instrument(level = "info", skip_all)] -fn exprs() -> impl Fold { +fn exprs(unresolved_mark: Mark) -> impl Fold { chain!( - arrow(), + arrow(unresolved_mark), duplicate_keys(), sticky_regex(), instance_of(), @@ -77,14 +77,14 @@ where Optional::new(object_super(), !c.typescript), shorthand(), function_name(), - exprs(), + exprs(unresolved_mark), for_of(c.for_of), // Should come before parameters // See: https://github.com/swc-project/swc/issues/1036 parameters(c.parameters, unresolved_mark), computed_properties(c.computed_props), destructuring(c.destructuring), - block_scoping(), + block_scoping(unresolved_mark), regenerator(c.regenerator, unresolved_mark), ) } diff --git a/crates/swc_ecma_transforms_compat/src/es2015/parameters.rs b/crates/swc_ecma_transforms_compat/src/es2015/parameters.rs index 6341e4a5c76..f39298f7746 100644 --- a/crates/swc_ecma_transforms_compat/src/es2015/parameters.rs +++ b/crates/swc_ecma_transforms_compat/src/es2015/parameters.rs @@ -3,7 +3,7 @@ use std::mem; use arrayvec::ArrayVec; use serde::Deserialize; use swc_atoms::js_word; -use swc_common::{util::take::Take, Mark, Spanned, DUMMY_SP}; +use swc_common::{util::take::Take, Mark, Spanned, SyntaxContext, DUMMY_SP}; use swc_ecma_ast::*; // use swc_ecma_transforms_base::perf::Parallel; // use swc_ecma_transforms_macros::parallel; @@ -17,9 +17,11 @@ use tracing::trace; #[tracing::instrument(level = "info", skip_all)] pub fn parameters(c: Config, unresolved_mark: Mark) -> impl 'static + Fold { + let unresolved_ctxt = SyntaxContext::empty().apply_mark(unresolved_mark); as_folder(Params { c, - unresolved_mark, + unresolved_ctxt, + hoister: FnEnvHoister::new(unresolved_ctxt), ..Default::default() }) } @@ -29,7 +31,7 @@ struct Params { /// Used to store `this, in case if `arguments` is used and we should /// transform an arrow expression to a function expression. hoister: FnEnvHoister, - unresolved_mark: Mark, + unresolved_ctxt: SyntaxContext, in_subclass: bool, in_prop: bool, c: Config, @@ -302,7 +304,7 @@ impl Params { span, callee: Box::new( quote_ident!( - DUMMY_SP.apply_mark(self.unresolved_mark), + DUMMY_SP.with_ctxt(self.unresolved_ctxt), "Array" ) .into(), @@ -534,7 +536,7 @@ impl VisitMut for Params { if !self.in_prop { f.visit_mut_children_with(&mut self.hoister) } else { - let mut hoister = FnEnvHoister::default(); + let mut hoister = FnEnvHoister::new(self.unresolved_ctxt); f.visit_mut_children_with(&mut hoister); local_vars = hoister.to_stmt(); } diff --git a/crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs b/crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs index 7f76bdc5661..fb9055b9ddb 100644 --- a/crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs +++ b/crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs @@ -1,7 +1,7 @@ use std::iter; use serde::Deserialize; -use swc_common::{util::take::Take, Spanned, DUMMY_SP}; +use swc_common::{util::take::Take, Mark, Spanned, SyntaxContext, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_transforms_base::{helper, helper_expr, perf::Check}; use swc_ecma_transforms_macros::fast_path; @@ -36,8 +36,11 @@ use swc_trace_macro::swc_trace; /// }); /// ``` #[tracing::instrument(level = "info", skip_all)] -pub fn async_to_generator(c: Config) -> impl Fold + VisitMut { - as_folder(AsyncToGenerator { c }) +pub fn async_to_generator(c: Config, unresolved_mark: Mark) -> impl Fold + VisitMut { + as_folder(AsyncToGenerator { + c, + unresolved_ctxt: SyntaxContext::empty().apply_mark(unresolved_mark), + }) } #[derive(Debug, Clone, Copy, Default, Deserialize)] @@ -52,11 +55,13 @@ pub struct Config { #[derive(Default, Clone)] struct AsyncToGenerator { c: Config, + unresolved_ctxt: SyntaxContext, } struct Actual { c: Config, + unresolved_ctxt: SyntaxContext, extra_stmts: Vec, hoist_stmts: Vec, } @@ -87,6 +92,7 @@ impl AsyncToGenerator { for mut stmt in stmts.drain(..) { let mut actual = Actual { c: self.c, + unresolved_ctxt: self.unresolved_ctxt, extra_stmts: vec![], hoist_stmts: vec![], }; @@ -116,7 +122,7 @@ impl VisitMut for Actual { } let params = m.function.params.clone(); - let mut visitor = FnEnvHoister::default(); + let mut visitor = FnEnvHoister::new(self.unresolved_ctxt); m.function.params.clear(); m.function.body.visit_mut_with(&mut visitor); @@ -307,7 +313,7 @@ impl Actual { match expr { Expr::Arrow(arrow_expr @ ArrowExpr { is_async: true, .. }) => { - let mut state = FnEnvHoister::default(); + let mut state = FnEnvHoister::new(self.unresolved_ctxt); arrow_expr.visit_mut_with(&mut state); diff --git a/crates/swc_ecma_transforms_compat/src/es2017/mod.rs b/crates/swc_ecma_transforms_compat/src/es2017/mod.rs index 87e831885f1..89ce58ffd92 100644 --- a/crates/swc_ecma_transforms_compat/src/es2017/mod.rs +++ b/crates/swc_ecma_transforms_compat/src/es2017/mod.rs @@ -1,4 +1,5 @@ use serde::Deserialize; +use swc_common::Mark; use swc_ecma_visit::Fold; pub use self::async_to_generator::async_to_generator; @@ -6,8 +7,8 @@ pub use self::async_to_generator::async_to_generator; pub mod async_to_generator; #[tracing::instrument(level = "info", skip_all)] -pub fn es2017(c: Config) -> impl Fold { - async_to_generator(c.async_to_generator) +pub fn es2017(c: Config, unresolved_mark: Mark) -> impl Fold { + async_to_generator(c.async_to_generator, unresolved_mark) } #[derive(Debug, Clone, Copy, Default, Deserialize)] diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs b/crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs index c547488b8c9..0df02227c06 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_arrow.rs @@ -1,9 +1,18 @@ +use swc_common::{chain, Mark}; +use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_compat::es2015::arrow; use swc_ecma_transforms_testing::{compare_stdout, test}; +use swc_ecma_visit::Fold; + +fn tr() -> impl Fold { + let unresolved = Mark::new(); + let global = Mark::new(); + chain!(resolver(unresolved, global, false), arrow(unresolved)) +} test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), issue_233, "const foo = () => ({ x, ...y }) => y", "const foo = function() { @@ -15,7 +24,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), destructuring, r#"let foo = ({bar}) => undefined;"#, r#"let foo = function ({bar}) { @@ -25,7 +34,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), basic, r#"let echo = (bar) => bar"#, r#"let echo = function(bar) { @@ -35,7 +44,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), empty_arguments, r#"var t = () => 5 + 5;"#, r#"var t = function () { @@ -45,7 +54,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), expression, r#"arr.map(x => x * x);"#, r#"arr.map(function (x) { @@ -55,7 +64,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), inside_call, r#"arr.map(i => i + 1);"#, r#"arr.map(function (i) { @@ -65,7 +74,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), multiple_arguments, r#"var t = (i, x) => i * x;"#, r#"var t = function (i, x) { @@ -74,7 +83,7 @@ test!( ); // test!(::swc_ecma_parser::Syntax::default(), -// |_| arrow(), +// |_| arrow(Mark::new()), // nested, // r#"module.exports = { // init: function () { @@ -109,7 +118,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), paren_insertion, r#"var t = i => i * 5;"#, r#"var t = function (i) { @@ -119,7 +128,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), single_argument, r#"var t = (i) => i * 5;"#, r#"var t = function (i) { @@ -129,7 +138,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), statement, r#"nums.forEach(v => { if (v % 5 === 0) { @@ -145,7 +154,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), issue_413, r#" export const getBadgeBorderRadius = (text, color) => { @@ -161,7 +170,7 @@ export const getBadgeBorderRadius = function(text, color) { test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| tr(), arguments, r#" function test() { @@ -178,7 +187,7 @@ function test() { compare_stdout!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| tr(), arguments_nested_arrow, " function test() { @@ -197,7 +206,7 @@ compare_stdout!( compare_stdout!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| tr(), arguments_nested_fn, " function test() { @@ -218,7 +227,7 @@ compare_stdout!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), arguments_member, r#" function test() { @@ -236,7 +245,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), arguments_fn_expr, r#" function test() { @@ -254,7 +263,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), issue_2212_1, "const foo = () => this", " @@ -267,7 +276,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), issue_2212_2, " const foo = function (){ @@ -290,7 +299,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), fixture_this, r#" function b() { @@ -341,7 +350,7 @@ class Foo extends function () {} { test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| tr(), fixture_arguments, r#" function fn() { @@ -379,7 +388,7 @@ var baz = function () { test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| tr(), two_arrow, r#" let foo = () => this; @@ -406,7 +415,7 @@ let bar1 = function () { test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), computed_props, r#" var a = { @@ -426,7 +435,7 @@ var a = { test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| tr(), this_in_params, r#" export const getBadgeBorderRadius = (text = this, color = arguments) => { @@ -443,7 +452,7 @@ export const getBadgeBorderRadius = function(text = _this, color = _arguments) { test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), getter_setter, r#" const a = () => ({ @@ -477,7 +486,7 @@ const a = function () { test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), method_computed, r#" const a = () => ({ @@ -510,7 +519,7 @@ const b = function() { test!( ::swc_ecma_parser::Syntax::default(), - |_| arrow(), + |_| arrow(Mark::new()), chrome_46, "function foo() { const a = (a) => new.target diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_block_scoping.rs b/crates/swc_ecma_transforms_compat/tests/es2015_block_scoping.rs index 1dc07f0fb3a..e5bf171f87c 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_block_scoping.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_block_scoping.rs @@ -9,10 +9,19 @@ use swc_ecma_transforms_compat::{ es2017::async_to_generator, }; use swc_ecma_transforms_testing::{compare_stdout, test, test_exec, test_fixture, Tester}; +use swc_ecma_visit::Fold; + +fn tr() -> impl Fold { + let unresolved_mark = Mark::new(); + chain!( + resolver(unresolved_mark, Mark::new(), false), + block_scoping(unresolved_mark) + ) +} test!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), for_loop, "for (const key in obj) { const bar = obj[key]; @@ -48,7 +57,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), for_let_loop, "let functions = []; for (let i = 0; i < 10; i++) { @@ -73,7 +82,7 @@ functions[7](); test_exec!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), for_let_loop_exec, "let functions = []; for (let i = 0; i < 10; i++) { @@ -88,7 +97,7 @@ expect(functions[7]()).toBe(7); test_exec!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), for_let_of_exec, "let functions = []; for (let i of [1, 3, 5, 7, 9]) { @@ -103,7 +112,7 @@ expect(functions[1]()).toBe(3); test_exec!( ::swc_ecma_parser::Syntax::default(), - |_| chain!(for_of(Default::default()), block_scoping()), + |_| chain!(for_of(Default::default()), block_scoping(Mark::new())), issue_609_1, "let functions = []; for (let i of [1, 3, 5, 7, 9]) { @@ -118,7 +127,7 @@ expect(functions[1]()).toBe(3); test!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), issue_662, "function foo(parts) { let match = null; @@ -166,7 +175,7 @@ foo();" test!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), issue_686, "module.exports = function(values) { var vars = []; @@ -249,7 +258,7 @@ test!( test_exec!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), issue_723_1, "function foo() { const lod = { 0: { mig: 'bana' }}; @@ -489,7 +498,7 @@ test!( |Tester { comments, .. }| { let mark = Mark::fresh(Mark::root()); chain!( - async_to_generator(Default::default()), + async_to_generator(Default::default(), mark), es2015::es2015( mark, Some(comments.clone()), @@ -564,7 +573,7 @@ test_exec!( |Tester { comments, .. }| { let mark = Mark::fresh(Mark::root()); chain!( - async_to_generator(Default::default()), + async_to_generator(Default::default(), mark), es2015::es2015( mark, Some(comments.clone()), @@ -589,7 +598,7 @@ test_exec!( test!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), issue_1231_1, " function combineOverlappingMatches(matches) { @@ -640,7 +649,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), issue_1415_1, " export function test(items) { @@ -715,13 +724,16 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), |Tester { comments, .. }| { - let mark = Mark::fresh(Mark::root()); - es2015::es2015( - mark, - Some(comments.clone()), - es2015::Config { - ..Default::default() - }, + let mark = Mark::new(); + chain!( + resolver(mark, Mark::new(), false), + es2015::es2015( + mark, + Some(comments.clone()), + es2015::Config { + ..Default::default() + }, + ) ) }, arguments_loop, @@ -784,12 +796,15 @@ compare_stdout!( ::swc_ecma_parser::Syntax::default(), |Tester { comments, .. }| { let mark = Mark::fresh(Mark::root()); - es2015::es2015( - mark, - Some(comments.clone()), - es2015::Config { - ..Default::default() - }, + chain!( + resolver(mark, Mark::new(), false), + es2015::es2015( + mark, + Some(comments.clone()), + es2015::Config { + ..Default::default() + }, + ) ) }, arguments_arrow, @@ -838,7 +853,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| tr(), issue_1462_1, " export default function _objectSpread(target) { @@ -884,7 +899,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), issue_2027_1, " const keys = { @@ -960,7 +975,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), issue_2998_1, " let a = 5; @@ -980,7 +995,7 @@ for(var b = 0; b < a; b++){ test!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), issue_2998_2, " for (var a; ;) { } @@ -994,7 +1009,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), issue_4196, " for (let i = 0; i < 2; i++) { @@ -1025,7 +1040,7 @@ test!( test!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), labeled_break, " a: @@ -1063,7 +1078,7 @@ test!( test_exec!( ::swc_ecma_parser::Syntax::default(), - |_| block_scoping(), + |_| block_scoping(Mark::new()), issue_2998_3, "let a = 5; const expected = []; @@ -1080,7 +1095,13 @@ fn exec(input: PathBuf) { let input = read_to_string(&input).unwrap(); compare_stdout( Default::default(), - |_| chain!(resolver(Mark::new(), Mark::new(), false), block_scoping()), + |_| { + let unresolved_mark = Mark::new(); + chain!( + resolver(unresolved_mark, Mark::new(), false), + block_scoping(unresolved_mark) + ) + }, &input, ); } @@ -1091,7 +1112,13 @@ fn fixture(input: PathBuf) { test_fixture( Default::default(), - &|_| chain!(resolver(Mark::new(), Mark::new(), false), block_scoping()), + &|_| { + let unresolved_mark = Mark::new(); + chain!( + resolver(unresolved_mark, Mark::new(), false), + block_scoping(unresolved_mark) + ) + }, &input, &output, ); diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs b/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs index 1c12c1cda01..2a34521ab3a 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs @@ -21,13 +21,14 @@ fn tr(tester: &Tester) -> impl Fold { } fn spec_tr(tester: &Tester) -> impl Fold { + let unresolved_mark = Mark::new(); chain!( - resolver(Mark::new(), Mark::new(), false), + resolver(unresolved_mark, Mark::new(), false), classes(Some(tester.comments.clone()), Default::default()), spread(spread::Config { ..Default::default() }), - block_scoping(), + block_scoping(unresolved_mark), ) } @@ -566,7 +567,7 @@ expect(constructor).toBe(CustomElement); // regression_5817 test!( syntax(), - |t| chain!(tr(t), arrow()), + |t| chain!(tr(t), arrow(Mark::new())), regression_5817, r#" class A extends B { @@ -5251,7 +5252,7 @@ test_exec!( syntax(), |t| chain!( classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), extend_builtins_imported_babel_plugin_transform_builtin_classes_exec, r#" @@ -5555,7 +5556,7 @@ test_exec!( syntax(), |t| chain!( classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), extend_builtins_spec_exec, r#" @@ -5806,7 +5807,7 @@ test!( // TODO: Unignore this ignore, syntax(), - |t| chain!(tr(t), block_scoping()), + |t| chain!(tr(t), block_scoping(Mark::new())), regression_t7010, r#" class Foo { @@ -6095,7 +6096,7 @@ test_exec!( syntax(), |t| chain!( classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), extend_builtins_builtin_objects_throw_when_wrapped_exec, r#" @@ -6147,7 +6148,7 @@ test_exec!( syntax(), |t| chain!( classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), extend_builtins_overwritten_null_exec, r#" @@ -6171,7 +6172,7 @@ test_exec!( syntax(), |t| chain!( classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), extend_builtins_super_called_exec, r#" @@ -6543,15 +6544,15 @@ test!( test!( syntax(), |t| { - let global_mark = Mark::fresh(Mark::root()); + let unresolved_mark = Mark::fresh(Mark::root()); chain!( es2022::es2022(Some(t.comments.clone()), Default::default()), es2018::es2018(Default::default()), - es2017::es2017(Default::default()), + es2017::es2017(Default::default(), unresolved_mark), es2016::es2016(), es2015::es2015( - global_mark, + unresolved_mark, Some(t.comments.clone()), es2015::Config { ..Default::default() @@ -7350,7 +7351,7 @@ test!( syntax(), |t| chain!( classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), issue_5102, r#" diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs b/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs index cf7cd4091a1..0e1f879a9bd 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs @@ -123,7 +123,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -977,7 +977,7 @@ test!( |_| chain!( spread(Default::default()), destructuring(Default::default()), - block_scoping(), + block_scoping(Mark::new()), object_rest_spread(Default::default()), ), destructuring_for_of, @@ -1016,7 +1016,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1046,7 +1046,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1075,7 +1075,7 @@ test_exec!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1106,7 +1106,7 @@ test_exec!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1131,7 +1131,7 @@ test_exec!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1160,7 +1160,7 @@ test_exec!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), ) }, destructuring_object_rest_impure_computed_keys_exec, @@ -1215,7 +1215,7 @@ test_exec!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1246,7 +1246,7 @@ test_exec!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1335,7 +1335,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1406,7 +1406,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1491,7 +1491,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1522,7 +1522,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1587,7 +1587,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), ) }, destructuring_assignment_expression_pattern, @@ -1615,7 +1615,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), ) }, destructuring_object_advanced, @@ -1653,7 +1653,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), ) }, destructuring_spread, @@ -1693,7 +1693,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), ) }, destructuring_mixed, @@ -1721,7 +1721,7 @@ test!( |_| chain!( destructuring(Default::default()), spread(Default::default()), - block_scoping(), + block_scoping(Mark::new()), object_rest_spread(Default::default()) ), destructuring_assignment_statement_no_loose, @@ -1742,7 +1742,7 @@ test!( spread(spread::Config { ..Default::default() }), - block_scoping(), + block_scoping(Mark::new()), object_rest_spread(Default::default()) ), destructuring_assignment_statement, @@ -1767,7 +1767,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1805,7 +1805,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1833,7 +1833,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1896,7 +1896,7 @@ test!( |_| chain!( spread(Default::default()), destructuring(Default::default()), - block_scoping(), + block_scoping(Mark::new()), object_rest_spread(Default::default()), ), destructuring_for_in, @@ -1930,7 +1930,7 @@ test!( ..Default::default() }), destructuring(Config { loose: true }), - block_scoping(), + block_scoping(Mark::new()), object_rest_spread(Default::default()), ), destructuring_for_in_loose, @@ -1968,7 +1968,7 @@ test!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, @@ -1996,7 +1996,7 @@ test_exec!( spread(Default::default()), parameters(Default::default(), unresolved_mark), destructuring(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), object_rest_spread(Default::default()), ) }, diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_new_target.rs b/crates/swc_ecma_transforms_compat/tests/es2015_new_target.rs index c551b5ef249..08db59edb20 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_new_target.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_new_target.rs @@ -1,7 +1,7 @@ use std::{fs::read_to_string, path::PathBuf}; use serde::Deserialize; -use swc_common::chain; +use swc_common::{chain, Mark}; use swc_ecma_parser::{EsConfig, Syntax}; use swc_ecma_transforms_base::pass::noop; use swc_ecma_transforms_compat::{ @@ -80,7 +80,7 @@ fn fixture(input: PathBuf) { } "transform-arrow-functions" => { - pass = Box::new(chain!(pass, arrow())); + pass = Box::new(chain!(pass, arrow(Mark::new()))); } _ => { diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_parameters.rs b/crates/swc_ecma_transforms_compat/tests/es2015_parameters.rs index c9c453ad0a6..baf1dca1b77 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_parameters.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_parameters.rs @@ -22,7 +22,7 @@ fn tr(c: Config) -> impl Fold { resolver(unresolved_mark, top_level_mark, false), parameters(c, unresolved_mark), destructuring(destructuring::Config { loose: false }), - block_scoping(), + block_scoping(unresolved_mark), ) } @@ -763,7 +763,18 @@ function () { test!( syntax(), - |_| chain!(arrow(), tr(Default::default())), + |_| { + let unresolved_mark = Mark::new(); + let top_level_mark = Mark::new(); + + chain!( + arrow(unresolved_mark), + resolver(unresolved_mark, top_level_mark, false), + parameters(Default::default(), unresolved_mark), + destructuring(destructuring::Config { loose: false }), + block_scoping(unresolved_mark), + ) + }, rest_binding_deoptimisation, r#"const deepAssign = (...args) => args = []; "#, @@ -1561,8 +1572,8 @@ test!( let top_level_mark = Mark::new(); chain!( resolver(unresolved_mark, top_level_mark, false), - async_to_generator(Default::default()), - arrow(), + async_to_generator(Default::default(), unresolved_mark), + arrow(unresolved_mark), parameters(Default::default(), unresolved_mark), ) }, @@ -1597,8 +1608,8 @@ test!( let top_level_mark = Mark::new(); chain!( resolver(unresolved_mark, top_level_mark, false), - async_to_generator(Default::default()), - arrow(), + async_to_generator(Default::default(), unresolved_mark), + arrow(unresolved_mark), parameters(Default::default(), unresolved_mark), ) }, @@ -1673,7 +1684,7 @@ test!( chain!( resolver(unresolved_mark, top_level_mark, false), parameters(Default::default(), unresolved_mark), - block_scoping() + block_scoping(unresolved_mark) ) }, parameters_regression_4333, diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_regenerator.rs b/crates/swc_ecma_transforms_compat/tests/es2015_regenerator.rs index 33aa40be822..44d51073ff1 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_regenerator.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_regenerator.rs @@ -22,6 +22,16 @@ fn tr(_: ()) -> impl Fold { ) } +fn tr_with_async() -> impl Fold { + let unresolved_mark = Mark::new(); + let top_level_mark = Mark::new(); + chain!( + resolver(unresolved_mark, top_level_mark, false), + async_to_generator(Default::default(), unresolved_mark), + regenerator(Default::default(), unresolved_mark) + ) +} + // computed_properties_example test!( syntax(), @@ -931,15 +941,18 @@ expect(v.next()).toEqual({ done: true }); test_exec!( syntax(), - |t| chain!( - es2017(Default::default()), - es2016(), - es2015( - Mark::fresh(Mark::root()), - Some(t.comments.clone()), - Default::default() - ), - ), + |t| { + let unresolved_mark = Mark::new(); + chain!( + es2017(Default::default(), unresolved_mark), + es2016(), + es2015( + unresolved_mark, + Some(t.comments.clone()), + Default::default() + ), + ) + }, issue_600_full, "async function foo(b) { for (let a of b) { @@ -950,11 +963,14 @@ test_exec!( test_exec!( syntax(), - |_| chain!( - async_to_generator(Default::default()), - es2015::for_of(Default::default()), - es2015::regenerator(Default::default(), Mark::fresh(Mark::root())), - ), + |_| { + let unresolved_mark = Mark::new(); + chain!( + async_to_generator(Default::default(), unresolved_mark), + es2015::for_of(Default::default()), + es2015::regenerator(Default::default(), unresolved_mark), + ) + }, issue_600_exact_passes, "async function foo(b) { for (let a of b) { @@ -1076,7 +1092,7 @@ expect(v.next()).toEqual({ done: false, value: 'Error'}); test_exec!( Syntax::default(), - |_| chain!(async_to_generator(Default::default()), tr(())), + |_| tr_with_async(), issue_1036_1, " const x = async function() { @@ -1159,7 +1175,7 @@ test_exec!( test_exec!( Syntax::default(), - |_| chain!(async_to_generator(Default::default()), tr(())), + |_| tr_with_async(), issue_1125_1, " async function test() { @@ -1207,7 +1223,7 @@ function foo() { test_exec!( Syntax::default(), - |_| chain!(async_to_generator(Default::default()), tr(())), + |_| tr_with_async(), hoist_function_in_async_issue_2556_2, " async function foo() { @@ -1233,7 +1249,7 @@ return foo() test!( Syntax::default(), - |_| chain!(async_to_generator(Default::default()), tr(())), + |_| tr_with_async(), hoist_function_in_async_issue_2556_4, r#" function requester() { @@ -1301,7 +1317,7 @@ function requester() { test_exec!( Syntax::default(), - |_| chain!(async_to_generator(Default::default()), tr(())), + |_| tr_with_async(), hoist_function_in_async_issue_2556_5, r#" function requester() { @@ -1323,7 +1339,7 @@ function requester() { test_exec!( Syntax::default(), - |_| chain!(async_to_generator(Default::default()), tr(())), + |_| tr_with_async(), issue_2620, r#" async function main() { @@ -1379,7 +1395,7 @@ return main().then((results) => { test!( Syntax::default(), - |_| chain!(async_to_generator(Default::default()), tr(())), + |_| tr_with_async(), hoist_function_in_async_issue_2556_6, r#" async function foo(a) { @@ -1453,7 +1469,7 @@ foo(1) test_exec!( Syntax::default(), - |_| chain!(async_to_generator(Default::default()), tr(())), + |_| tr_with_async(), hoist_function_in_async_issue_2556_7, r#" async function foo(a) { @@ -1474,7 +1490,7 @@ return foo(1) test!( Syntax::default(), - |_| chain!(async_to_generator(Default::default()), tr(())), + |_| tr_with_async(), hoist_function_in_async_issue_2556_8, r#" var fib = function fib() { @@ -1711,7 +1727,7 @@ test!( test!( Syntax::default(), - |_| chain!(async_to_generator(Default::default()), tr(())), + |_| tr_with_async(), issue_1799_1, " export default function Foo() { @@ -1747,7 +1763,7 @@ test!( |_| { let mark = Mark::fresh(Mark::root()); chain!( - async_to_generator(Default::default()), + async_to_generator(Default::default(), mark), es2015::(mark, None, Default::default()) ) }, @@ -1786,7 +1802,7 @@ test!( |_| { let mark = Mark::fresh(Mark::root()); chain!( - async_to_generator(Default::default()), + async_to_generator(Default::default(), mark), es2016(), es2015::(mark, None, Default::default()), ) @@ -1829,7 +1845,7 @@ test!( es2022(Some(t.comments.clone()), Default::default()), es2021(), es2018(Default::default()), - es2017(Default::default()), + es2017(Default::default(), mark), es2016(), es2015::(mark, None, Default::default()), ) @@ -2089,7 +2105,7 @@ test_exec!( |_| { let mark = Mark::fresh(Mark::root()); chain!( - async_to_generator(Default::default()), + async_to_generator(Default::default(), mark), es2015::for_of(Default::default()), regenerator(Default::default(), mark) ) diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_spread.rs b/crates/swc_ecma_transforms_compat/tests/es2015_spread.rs index 9966c51d08c..e0d1366429e 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_spread.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_spread.rs @@ -633,7 +633,17 @@ test!( // Cost is too high. ignore, syntax(), - |_| chain!(tr(), block_scoping()), + |_| { + let unresolved_mark = Mark::new(); + let top_level_mark = Mark::new(); + + chain!( + resolver(unresolved_mark, top_level_mark, false), + parameters(Default::default(), unresolved_mark), + spread(Default::default()), + block_scoping(unresolved_mark) + ) + }, spread_known_rest, r#" function foo(...bar) { diff --git a/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs b/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs index 7133fb1886f..b45bff104f0 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs @@ -45,15 +45,24 @@ fn tr() -> impl Fold { chain!( resolver(unresolved_mark, top_level_mark, false), ParenRemover, - arrow(), + arrow(unresolved_mark), parameters(Default::default(), unresolved_mark), destructuring(destructuring::Config { loose: false }), function_name(), - async_to_generator(Default::default()), + async_to_generator(Default::default(), unresolved_mark), fixer(None) ) } +fn with_resolver() -> impl Fold { + let unresolved = Mark::new(); + let top_level = Mark::new(); + chain!( + resolver(unresolved, top_level, false), + async_to_generator(Default::default(), unresolved) + ) +} + test!( syntax(), |_| tr(), @@ -690,7 +699,7 @@ return (new B(20)).print().then(() => console.log('Done'));" test_exec!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_400_2, "class A { constructor() { @@ -720,14 +729,17 @@ return (new B(20)).print().then(() => console.log('Done'));" test_exec!( syntax(), - |t| chain!( - async_to_generator(Default::default()), - es2015( - Mark::fresh(Mark::root()), - Some(t.comments.clone()), - Default::default() + |t| { + let unresolved_mark = Mark::new(); + chain!( + async_to_generator(Default::default(), unresolved_mark), + es2015( + unresolved_mark, + Some(t.comments.clone()), + Default::default() + ) ) - ), + }, issue_400_3, "class A { constructor() { @@ -782,7 +794,7 @@ return (new A()).print();" test!( ignore, syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), bluebird_coroutines_named_expression, r#" var foo = async function bar() { @@ -815,7 +827,7 @@ test!( // TODO: Enable this test after implementing es6 module pass. ignore, syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), export_async_lone_export, r#" export async function foo () { } @@ -845,7 +857,7 @@ function _foo() { test!( ignore, syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), bluebird_coroutines_arrow_function, r#" (async () => { await foo(); })() @@ -866,9 +878,14 @@ _coroutine(function* () { // regression_t7194 test!( // Optimization - ignore, syntax(), - |_| chain!(async_to_generator(Default::default()), arrow()), + |_| { + let unresolved_mark = Mark::new(); + chain!( + async_to_generator(Default::default(), unresolved_mark), + arrow(unresolved_mark) + ) + }, regression_t7194, r#" function f() { @@ -901,12 +918,12 @@ function f() { /*#__PURE__*/ _asyncToGenerator(function* () { - var _this2 = this; + var _this = this; console.log('async wrapper:', this === 'foo'); (function () { - console.log('nested arrow:', _this2 === 'foo'); + console.log('nested arrow:', _this === 'foo'); })(); }).call('foo'); @@ -918,15 +935,7 @@ test!( // TODO: Unignore this ignore, syntax(), - |_| { - let unresolved_mark = Mark::new(); - let top_level_mark = Mark::new(); - - chain!( - resolver(unresolved_mark, top_level_mark, false), - async_to_generator(Default::default()) - ) - }, + |_| with_resolver(), async_to_generator_shadowed_promise, r#" let Promise; @@ -957,7 +966,7 @@ function foo() { // async_to_generator_object_method_with_arrows test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_to_generator_object_method_with_arrows, r#" class Class { @@ -1023,7 +1032,7 @@ class Class { // async_to_generator_object_method test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_to_generator_object_method, r#" let obj = { @@ -1053,7 +1062,7 @@ let obj = { test!( ignore, syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), bluebird_coroutines_class, r#" class Foo { @@ -1082,11 +1091,14 @@ class Foo { test!( ignore, syntax(), - |_| chain!( - async_to_generator(Default::default()), - //regenerator(), - arrow(), - ), + |_| { + let unresolved_mark = Mark::new(); + chain!( + async_to_generator(Default::default(), unresolved_mark), + //regenerator(), + arrow(unresolved_mark), + ) + }, async_to_generator_async_iife_with_regenerator, r#" (async function() { await 'ok' })(); @@ -1243,7 +1255,7 @@ regeneratorRuntime.mark(function _callee4() { // async_to_generator_named_expression test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_to_generator_named_expression, r#" var foo = async function bar() { @@ -1365,7 +1377,7 @@ function () { // async_to_generator_async_arrow_in_method test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_to_generator_async_arrow_in_method, r#" let TestClass = { @@ -1404,7 +1416,7 @@ let TestClass = { test!( ignore, syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), bluebird_coroutines_statement, r#" async function foo() { @@ -1437,7 +1449,7 @@ test!( chain!( resolver(unresolved_mark, top_level_mark, false), - async_to_generator(Default::default()), + async_to_generator(Default::default(), unresolved_mark), parameters(Default::default(), unresolved_mark), destructuring(destructuring::Config { loose: false }), ) @@ -1484,7 +1496,7 @@ test!( // TODO: Enable this test after implementing es6 module pass. ignore, syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), export_async_default_arrow_export, r#" export default async () => { return await foo(); } @@ -1512,7 +1524,7 @@ exports.default = _default; // async_to_generator_function_arity test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_to_generator_function_arity, r#" async function one(a, b = 1) {} @@ -1581,9 +1593,8 @@ function _six() { // async_to_generator_object_method_with_super test!( - ignore, syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_to_generator_object_method_with_super_caching, r#" class Foo extends class {} { @@ -1598,13 +1609,12 @@ class Foo extends class {} { r#" class Foo extends class {} { method() { - var _superprop_getMethod = () => super.method, - _this = this; + var _this = this, _superprop_get_method = () => super.method,; return _asyncToGenerator(function* () { - _superprop_getMethod().call(_this); + _superprop_get_method().call(_this); - var arrow = () => _superprop_getMethod().call(_this); + var arrow = () => _superprop_get_method().call(_this); })(); } @@ -1618,7 +1628,7 @@ test!( // TODO: Enable this test after implementing es6 module pass. ignore, syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), export_async_default_export, r#" export default async function myFunc() {} @@ -1647,7 +1657,7 @@ function _myFunc() { // async_to_generator_async test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_to_generator_async, r#" class Foo { @@ -1673,7 +1683,7 @@ class Foo { // regression_8783 test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), regression_8783, r#" (async function poll() { @@ -1701,7 +1711,7 @@ test!( // async_to_generator_deeply_nested_asyncs test!( syntax(), - |_| async_to_generator(Default::default()), + |_| with_resolver(), async_to_generator_deeply_nested_asyncs, r#" async function s(x, ...args) { @@ -1766,7 +1776,7 @@ test!( // TODO: Enable this test after implementing es6 module pass. ignore, syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), export_async_import_and_export, r#" import bar from 'bar'; @@ -1801,15 +1811,7 @@ test!( // TODO: Unignore this ignore, syntax(), - |_| { - let unresolved_mark = Mark::new(); - let top_level_mark = Mark::new(); - - chain!( - resolver(unresolved_mark, top_level_mark, false), - async_to_generator(Default::default()) - ) - }, + |_| with_resolver(), async_to_generator_shadowed_promise_nested, r#" let Promise; @@ -1861,7 +1863,7 @@ function foo() { // regression_4599 test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), regression_4599, r#" async () => await promise @@ -1886,7 +1888,7 @@ _asyncToGenerator(function* () { // regression_4943_exec test_exec!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), regression_4943_exec, r#" "use strict"; @@ -1909,7 +1911,7 @@ return foo().then(() => { // regression_8783_exec test_exec!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), regression_8783_exec, r#" let log = []; @@ -1934,7 +1936,7 @@ return main.then(() => { test!( ignore, syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), bluebird_coroutines_expression, r#" var foo = async function () { @@ -1963,7 +1965,7 @@ function () { // async_to_generator_expression test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_to_generator_expression, r#" var foo = async function () { @@ -2020,7 +2022,7 @@ function () { // async_to_generator_statement test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_to_generator_statement, r#" async function foo() { @@ -2046,15 +2048,7 @@ test!( // TODO: Unignore this ignore, syntax(), - |_| { - let unresolved_mark = Mark::new(); - let top_level_mark = Mark::new(); - - chain!( - resolver(unresolved_mark, top_level_mark, false), - async_to_generator(Default::default()) - ) - }, + |_| with_resolver(), async_to_generator_shadowed_promise_import, r#" import Promise from 'somewhere'; @@ -2084,7 +2078,7 @@ function foo() { // async_to_generator_parameters test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_to_generator_parameters, r#" async function foo(bar) { @@ -2108,7 +2102,7 @@ function _foo() { // regression_t6882_exec test_exec!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), regression_t6882_exec, r#" foo(); @@ -2121,7 +2115,7 @@ async function foo() {} // async_to_generator_parameters test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_600, r#" async function foo() { @@ -2144,7 +2138,7 @@ function _foo() { test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1036_1, " const x = async function() { @@ -2187,7 +2181,7 @@ test!( test_exec!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1036_2, " const x = async function() { @@ -2203,7 +2197,7 @@ test_exec!( test!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1216_1, " const source = Math.random() < 2 ? 'matilda' : 'fred'; @@ -2250,7 +2244,7 @@ test!( test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1125_1, " async function test() { @@ -2283,7 +2277,7 @@ test() test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1341_1, " class A { @@ -2325,7 +2319,7 @@ test_exec!( Syntax::default(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - async_to_generator(Default::default()) + async_to_generator(Default::default(), Mark::new()) ), issue_1341_1_exec, " @@ -2347,7 +2341,7 @@ test_exec!( test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1341_2, " class A { @@ -2381,7 +2375,7 @@ test_exec!( Syntax::default(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - async_to_generator(Default::default()) + async_to_generator(Default::default(), Mark::new()) ), issue_1341_2_exec, " @@ -2399,7 +2393,7 @@ test_exec!( test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1455_1, " const obj = { @@ -2442,7 +2436,7 @@ test!( test_exec!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1575_1, " const obj = { @@ -2460,7 +2454,7 @@ test_exec!( |_| { let mark = Mark::fresh(Mark::root()); chain!( - async_to_generator(Default::default()), + async_to_generator(Default::default(), mark), regenerator(Default::default(), mark) ) }, @@ -2478,7 +2472,7 @@ test_exec!( test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1722_1, " (async function main() { @@ -2502,7 +2496,7 @@ test!( test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1721_1, " async function main() { @@ -2549,7 +2543,7 @@ test!( test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1721_2_async_generator, " async function* lol() { @@ -2573,7 +2567,7 @@ test!( test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1684_1, " const cache = {} @@ -2610,10 +2604,10 @@ test!( test!( Syntax::default(), |_| { - let top_level_mark = Mark::fresh(Mark::root()); + let unresolved_mark = Mark::fresh(Mark::root()); chain!( - async_to_generator(Default::default()), - regenerator(Default::default(), top_level_mark) + async_to_generator(Default::default(), unresolved_mark), + regenerator(Default::default(), unresolved_mark) ) }, issue_1684_2, @@ -2671,7 +2665,7 @@ test!( test_exec!( syntax(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1752_1, " async function* generate() { @@ -2699,7 +2693,7 @@ test_exec!( test_exec!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_1918_1, " let counter = 0; @@ -2739,7 +2733,7 @@ test_exec!( test_exec!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_2402_1, " function MyClass(item) { @@ -2761,10 +2755,10 @@ expect(tmp.fun()).resolves.toEqual({ foo: 'bar' }); test!( Syntax::default(), |_| { - let top_level_mark = Mark::fresh(Mark::root()); + let unresolved_mark = Mark::fresh(Mark::root()); chain!( - async_to_generator(Default::default()), - regenerator(Default::default(), top_level_mark) + async_to_generator(Default::default(), unresolved_mark), + regenerator(Default::default(), unresolved_mark) ) }, issue_2402_2, @@ -2820,7 +2814,7 @@ tmp.fun().then((res)=>{ test_exec!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_2305_1, " function MyClass () {} @@ -2853,10 +2847,10 @@ expect(myclass.init(2)).resolves.toEqual(true); test!( Syntax::default(), |_| { - let top_level_mark = Mark::fresh(Mark::root()); + let unresolved_mark = Mark::fresh(Mark::root()); chain!( - async_to_generator(Default::default()), - regenerator(Default::default(), top_level_mark) + async_to_generator(Default::default(), unresolved_mark), + regenerator(Default::default(), unresolved_mark) ) }, issue_2305_2, @@ -2928,10 +2922,10 @@ myclass.handle(); test!( Syntax::default(), |_| { - let top_level_mark = Mark::fresh(Mark::root()); + let unresolved_mark = Mark::fresh(Mark::root()); chain!( - async_to_generator(Default::default()), - regenerator(Default::default(), top_level_mark) + async_to_generator(Default::default(), unresolved_mark), + regenerator(Default::default(), unresolved_mark) ) }, issue_2677_1, @@ -3006,10 +3000,10 @@ function _someCall() { test!( Syntax::default(), |_| { - let top_level_mark = Mark::fresh(Mark::root()); + let unresolved_mark = Mark::fresh(Mark::root()); chain!( - async_to_generator(Default::default()), - regenerator(Default::default(), top_level_mark) + async_to_generator(Default::default(), unresolved_mark), + regenerator(Default::default(), unresolved_mark) ) }, issue_2677_2, @@ -3061,7 +3055,7 @@ export default async function() { test_exec!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), function_parameters, " class A { @@ -3084,7 +3078,7 @@ expect(a.doTest()).resolves.toEqual(3); test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), function_length_issue_3135_1, r#" async function foo(x, y, ...z) { @@ -3106,7 +3100,7 @@ function _foo() { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), function_length_issue_3135_2, r#" async function* foo(x, y, ...z) { @@ -3128,7 +3122,7 @@ function _foo() { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), function_length_issue_3135_3, r#" const foo = async function (x, y, ...z) { @@ -3149,7 +3143,7 @@ const foo = /*#__PURE__*/ function () { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), function_length_issue_3135_4, r#" const foo = async function* (x, y, ...z) { @@ -3170,7 +3164,7 @@ const foo = /*#__PURE__*/ function () { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), function_length_issue_3135_5, r#" const foo = async function foo(x, y, ...z) { @@ -3198,7 +3192,7 @@ const foo = function () { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), function_length_issue_3135_6, r#" const foo = async function* foo(x, y, ...z) { @@ -3226,7 +3220,7 @@ const foo = function () { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), function_length_issue_3135_7, r#" const foo = async (x, y, ...z) => { @@ -3248,10 +3242,8 @@ const foo = /*#__PURE__*/ function () { ); test!( - // TODO: resolve bind issue - ignore, Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_wrap_this, r#" const foo = async (x, y, ...z) => { @@ -3259,23 +3251,22 @@ const foo = async (x, y, ...z) => { }; "#, r#" +var _this = this; + const foo = /*#__PURE__*/ function () { - var _ref = _asyncToGenerator((function* (x, y, ...z) { - return this; - }).bind(_this)).bind(_this); + var _ref = _asyncToGenerator(function* (x, y, ...z) { + return _this; + }); return function foo(x, y) { return _ref.apply(this, arguments); }; }(); -var _this = this; "# ); test!( - // TODO: arguments - ignore, Syntax::default(), - |_| async_to_generator(Default::default()), + |_| with_resolver(), async_wrap_arguments, r#" function foo() { @@ -3302,10 +3293,8 @@ function foo() { ); test!( - // TODO: super and new.target - ignore, Syntax::default(), - |_| async_to_generator(Default::default()), + |_| with_resolver(), async_wrap_super_and_new_target, r#" class Foo { @@ -3339,16 +3328,15 @@ class Foo { } hello() { - var _superprop_getHello = () => super.hello, - _this = this; + var _this = this, _superprop_get_hello = () => super.hello; const world = /*#__PURE__*/ function () { - var _ref2 = _asyncToGenerator(function* () { - return _superprop_getHello().call(_this); + var _ref = _asyncToGenerator(function* () { + return _superprop_get_hello().call(_this); }); return function world() { - return _ref2.apply(this, arguments); + return _ref.apply(this, arguments); }; }(); } @@ -3358,7 +3346,7 @@ class Foo { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), export_default_async_nested_1, " export default async function foo(x) { @@ -3390,7 +3378,7 @@ function _foo() { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), export_default_async_nested_2, " export default async function (x) { @@ -3429,7 +3417,7 @@ function _ref() { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_with_optional_params, " (async function (a = 10, ...rest) {})(); @@ -3443,7 +3431,7 @@ _asyncToGenerator(function*(a = 10, ...rest) {})(); test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| with_resolver(), issue_2895, " export class Quirk { @@ -3473,7 +3461,7 @@ export class Quirk { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), super_field_update, " class Foo { @@ -3503,7 +3491,7 @@ class Foo { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), microbundle_835, " class A extends B { @@ -3528,7 +3516,7 @@ class A extends B { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), super_update, " class A extends B { @@ -3568,7 +3556,7 @@ class A extends B { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), async_with_optional_params_2, " const Z = (f) => ((x) => f((y) => x(x)(y)))((x) => f((y) => x(x)(y))); @@ -3591,7 +3579,7 @@ const p = Z((f)=>_asyncToGenerator(function*(n = 0) { test!( Syntax::default(), - |_| async_to_generator(Default::default()), + |_| async_to_generator(Default::default(), Mark::new()), issue_4208, " function foo() { @@ -3627,7 +3615,7 @@ fn exec(input: PathBuf) { chain!( resolver(unresolved_mark, top_level_mark, false), class_properties(Some(t.comments.clone()), Default::default()), - async_to_generator(Default::default()) + async_to_generator(Default::default(), unresolved_mark) ) }, &input, @@ -3646,9 +3634,9 @@ fn exec_regenerator(input: PathBuf) { chain!( resolver(unresolved_mark, top_level_mark, false), class_properties(Some(t.comments.clone()), Default::default()), - async_to_generator(Default::default()), + async_to_generator(Default::default(), unresolved_mark), es2015::for_of(Default::default()), - block_scoping(), + block_scoping(unresolved_mark), regenerator(Default::default(), unresolved_mark) ) }, diff --git a/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs b/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs index ac2ae2abfd5..3647f79d8d1 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs @@ -30,7 +30,7 @@ fn tr(t: &Tester) -> impl Fold { function_name(), class_properties(Some(t.comments.clone()), Default::default()), classes(Some(t.comments.clone()), Default::default()), - block_scoping(), + block_scoping(unresolved_mark), reserved_words(false), ) } @@ -2867,7 +2867,7 @@ test!( chain!( resolver(unresolved_mark, top_level_mark, false), class_properties(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(unresolved_mark) ) }, issue_443, @@ -2896,7 +2896,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - async_to_generator(Default::default()) + async_to_generator(Default::default(), Mark::new()) ), public_regression_t7364, r#" @@ -2955,7 +2955,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_regression_t6719, r#" @@ -3042,7 +3042,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_reevaluated, r#" @@ -3106,7 +3106,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_static, r#" @@ -3156,7 +3156,7 @@ test!( |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_destructuring_object_pattern_1, r#" @@ -3196,7 +3196,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_static_inherited, r#" @@ -3305,7 +3305,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_static_undefined, r#" @@ -3348,7 +3348,7 @@ test!( |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_destructuring_array_pattern, r#" @@ -3383,7 +3383,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_regression_t2983, r#" @@ -3419,11 +3419,14 @@ export { _class1 as default } // private_regression_t7364 test!( syntax(), - |t| chain!( - class_properties(Some(t.comments.clone()), Default::default()), - async_to_generator(Default::default()), - block_scoping() - ), + |t| { + let unresolved_mark = Mark::new(); + chain!( + class_properties(Some(t.comments.clone()), Default::default()), + async_to_generator(Default::default(), unresolved_mark), + block_scoping(unresolved_mark) + ) + }, private_regression_t7364, r#" class MyClass { @@ -3493,7 +3496,7 @@ test!( |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_destructuring_array_pattern_1, r#" @@ -3719,7 +3722,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_static_export, r#" @@ -3804,7 +3807,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - arrow() + arrow(Mark::new()) ), regression_6153, r#" @@ -3912,7 +3915,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - arrow() + arrow(Mark::new()) ), regression_7371, r#" @@ -4184,7 +4187,7 @@ test!( |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_canonical, r#" @@ -4324,7 +4327,7 @@ test!( |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_destructuring_array_pattern_3, r#" @@ -4413,7 +4416,7 @@ test!( |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_destructuring_array_pattern_2, r#" @@ -4447,7 +4450,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_non_block_arrow_func, r#" @@ -4530,7 +4533,7 @@ test!( class_properties(Some(t.comments.clone()), Default::default()), exponentiation(), classes(Some(t.comments.clone()), Default::default()), - block_scoping(), + block_scoping(Mark::new()), ), private_instance, r#" @@ -4587,7 +4590,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), public_native_classes, r#" @@ -4614,7 +4617,7 @@ _defineProperty(Foo, "foo", "foo"); test!( // Emitting class properties is not supported yet. syntax(), - |_| arrow(), + |_| arrow(Mark::new()), public_arrow_static_this_without_transform, r#" class Foo { @@ -4641,7 +4644,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_static_infer_name, r#" @@ -4700,7 +4703,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), private_native_classes, r#" @@ -4751,7 +4754,7 @@ test!( |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), public_computed_without_block, r#" @@ -4803,7 +4806,7 @@ test!( |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), classes(Some(t.comments.clone()), Default::default()), - block_scoping() + block_scoping(Mark::new()) ), public_static_super, r#" @@ -5437,7 +5440,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - async_to_generator(Default::default()) + async_to_generator(Default::default(), Mark::new()) ), issue_1694_1, " @@ -5468,7 +5471,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - async_to_generator(Default::default()) + async_to_generator(Default::default(), Mark::new()) ), issue_1694_2, " @@ -5497,7 +5500,7 @@ test!( syntax(), |t| chain!( class_properties(Some(t.comments.clone()), Default::default()), - async_to_generator(Default::default()) + async_to_generator(Default::default(), Mark::new()) ), issue_1702_1, " diff --git a/crates/swc_ecma_transforms_optimization/tests/simplify.rs b/crates/swc_ecma_transforms_optimization/tests/simplify.rs index 6d372bceffe..497e3c68bc0 100644 --- a/crates/swc_ecma_transforms_optimization/tests/simplify.rs +++ b/crates/swc_ecma_transforms_optimization/tests/simplify.rs @@ -572,7 +572,7 @@ test!( class_properties(Some(t.comments.clone()), Default::default()), simplifier(unresolved_mark, Default::default()), es2018(Default::default()), - es2017(Default::default()), + es2017(Default::default(), unresolved_mark), es2016(), es2015( unresolved_mark, diff --git a/crates/swc_ecma_transforms_react/src/jsx/tests.rs b/crates/swc_ecma_transforms_react/src/jsx/tests.rs index a8d80ea72c2..cd191dcf166 100644 --- a/crates/swc_ecma_transforms_react/src/jsx/tests.rs +++ b/crates/swc_ecma_transforms_react/src/jsx/tests.rs @@ -28,7 +28,7 @@ fn tr(t: &mut Tester, options: Options, top_level_mark: Mark) -> impl Fold { ), display_name(), classes(Some(t.comments.clone()), Default::default()), - arrow(), + arrow(unresolved_mark), ) } diff --git a/crates/swc_ecma_transforms_typescript/benches/compat.rs b/crates/swc_ecma_transforms_typescript/benches/compat.rs index 1d694907944..5183c4f6d97 100644 --- a/crates/swc_ecma_transforms_typescript/benches/compat.rs +++ b/crates/swc_ecma_transforms_typescript/benches/compat.rs @@ -182,12 +182,14 @@ fn es2019_optional_catch_binding(b: &mut Bencher) { } fn es2017(b: &mut Bencher) { - run(b, || swc_ecma_transforms_compat::es2017(Default::default())); + run(b, || { + swc_ecma_transforms_compat::es2017(Default::default(), Mark::new()) + }); } fn es2017_async_to_generator(b: &mut Bencher) { run(b, || { - swc_ecma_transforms_compat::es2017::async_to_generator(Default::default()) + swc_ecma_transforms_compat::es2017::async_to_generator(Default::default(), Mark::new()) }); } @@ -210,7 +212,7 @@ fn es2015(b: &mut Bencher) { } fn es2015_arrow(b: &mut Bencher) { - run(b, swc_ecma_transforms_compat::es2015::arrow); + run(b, || swc_ecma_transforms_compat::es2015::arrow(Mark::new())); } fn es2015_block_scoped_fn(b: &mut Bencher) { @@ -220,7 +222,9 @@ fn es2015_block_scoped_fn(b: &mut Bencher) { } fn es2015_block_scoping(b: &mut Bencher) { - run(b, swc_ecma_transforms_compat::es2015::block_scoping); + run(b, || { + swc_ecma_transforms_compat::es2015::block_scoping(Mark::new()) + }); } fn es2015_classes(b: &mut Bencher) { @@ -303,7 +307,7 @@ fn full_es2016(b: &mut Bencher) { ), swc_ecma_transforms_compat::es2019(), swc_ecma_transforms_compat::es2018(Default::default()), - swc_ecma_transforms_compat::es2017(Default::default()), + swc_ecma_transforms_compat::es2017(Default::default(), Mark::new()), swc_ecma_transforms_compat::es2016(), ) }); @@ -318,7 +322,7 @@ fn full_es2017(b: &mut Bencher) { ), swc_ecma_transforms_compat::es2019(), swc_ecma_transforms_compat::es2018(Default::default()), - swc_ecma_transforms_compat::es2017(Default::default()), + swc_ecma_transforms_compat::es2017(Default::default(), Mark::new()), ) }); } diff --git a/crates/swc_ecma_transforms_typescript/tests/strip.rs b/crates/swc_ecma_transforms_typescript/tests/strip.rs index 86a1ec37079..b190835aab3 100644 --- a/crates/swc_ecma_transforms_typescript/tests/strip.rs +++ b/crates/swc_ecma_transforms_typescript/tests/strip.rs @@ -96,7 +96,7 @@ test!( unresolved_mark ), destructuring(destructuring::Config { loose: false }), - block_scoping(), + block_scoping(unresolved_mark), ) }, fn_len_default_assignment_with_types, @@ -3252,7 +3252,20 @@ test!( Syntax::Typescript(TsConfig { ..Default::default() }), - |_| chain!(tr(), async_to_generator(Default::default())), + |_| { + let unresolved_mark = Mark::new(); + let top_level_mark = Mark::new(); + let config = strip::Config { + no_empty_export: true, + ..Default::default() + }; + chain!( + Optional::new(decorators(Default::default()), false,), + resolver(unresolved_mark, top_level_mark, true), + strip_with_config(config, top_level_mark), + async_to_generator(Default::default(), unresolved_mark) + ) + }, issue_1235_1, " class Service { diff --git a/crates/swc_ecma_utils/src/function/fn_env_hoister.rs b/crates/swc_ecma_utils/src/function/fn_env_hoister.rs index 9e0523e1e44..f2727e40429 100644 --- a/crates/swc_ecma_utils/src/function/fn_env_hoister.rs +++ b/crates/swc_ecma_utils/src/function/fn_env_hoister.rs @@ -1,6 +1,8 @@ +use std::mem; + use indexmap::IndexMap; use swc_atoms::{js_word, JsWord}; -use swc_common::{util::take::Take, Span, Spanned, DUMMY_SP}; +use swc_common::{util::take::Take, Span, Spanned, SyntaxContext, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; @@ -17,6 +19,7 @@ struct SuperField { #[derive(Default)] pub struct FnEnvHoister { + unresolved_ctxt: SyntaxContext, this: Option, args: Option, new_target: Option, @@ -28,6 +31,24 @@ pub struct FnEnvHoister { } impl FnEnvHoister { + pub fn new(unresolved_ctxt: SyntaxContext) -> Self { + Self { + unresolved_ctxt, + ..Default::default() + } + } + + pub fn take(&mut self) -> Self { + let mut new = Self { + unresolved_ctxt: self.unresolved_ctxt, + ..Default::default() + }; + + mem::swap(self, &mut new); + + new + } + pub fn to_decl(self) -> Vec { let Self { this, @@ -188,18 +209,14 @@ impl FnEnvHoister { } } -impl Take for FnEnvHoister { - fn dummy() -> Self { - Self::default() - } -} - impl VisitMut for FnEnvHoister { noop_visit_mut_type!(); fn visit_mut_expr(&mut self, e: &mut Expr) { match e { - Expr::Ident(id) if id.sym == js_word!("arguments") => { + Expr::Ident(Ident { span, sym, .. }) + if *sym == js_word!("arguments") && span.ctxt == self.unresolved_ctxt => + { let arguments = self .args .get_or_insert_with(|| private_ident!("_arguments"));