Make Helper scoped thread local (#134)

This allows injecting helpers per file without any multi threading issue.
This commit is contained in:
강동윤 2019-02-05 14:15:38 +09:00 committed by GitHub
parent a1629d3997
commit 4c6c4bc595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
58 changed files with 1164 additions and 1658 deletions

View File

@ -21,6 +21,7 @@ serde = { version = "1", features = ["derive"] }
indexmap = "1"
ordered-float = "1.0.1"
Inflector = { version = "0.11.4", default-features = false }
scoped-tls = { version = "0.1", features = ["nightly"] }
[dev-dependencies]
testing = { path ="../../testing" }

View File

@ -12,7 +12,6 @@ extern crate swc_ecma_transforms;
extern crate test;
extern crate testing;
use std::sync::Arc;
use swc_common::{FileName, FoldWith};
use swc_ecma_parser::{Parser, Session, SourceFileInput, Syntax};
use swc_ecma_transforms::{compat, helpers};
@ -109,81 +108,82 @@ macro_rules! tr {
()
})
.unwrap();
let helpers = Arc::new(helpers::Helpers::default());
let mut tr = $tr(helpers);
helpers::HELPERS.set(&Default::default(), || {
let mut tr = $tr();
$b.iter(|| {
let module = module.clone();
test::black_box(module.fold_with(&mut tr))
});
Ok(())
$b.iter(|| {
let module = module.clone();
test::black_box(module.fold_with(&mut tr))
});
Ok(())
})
});
};
}
#[bench]
fn all(b: &mut Bencher) {
tr!(b, |helpers| chain!(
compat::es2017(&helpers),
tr!(b, || chain!(
compat::es2017(),
compat::es2016(),
compat::es2015(&helpers),
compat::es2015(),
compat::es3(),
));
}
#[bench]
fn es2018(b: &mut Bencher) {
tr!(b, |helpers| compat::es2018(&helpers));
tr!(b, || compat::es2018());
}
#[bench]
fn es2018_object_rest_spread(b: &mut Bencher) {
tr!(b, |helpers| compat::es2018::object_rest_spread(helpers));
tr!(b, || compat::es2018::object_rest_spread());
}
#[bench]
fn es2017(b: &mut Bencher) {
tr!(b, |helpers| compat::es2017(&helpers));
tr!(b, || compat::es2017());
}
#[bench]
fn es2017_async_to_generator(b: &mut Bencher) {
tr!(b, |helpers| compat::es2017::async_to_generator(helpers));
tr!(b, || compat::es2017::async_to_generator());
}
#[bench]
fn es2016(b: &mut Bencher) {
tr!(b, |_| compat::es2016());
tr!(b, || compat::es2016());
}
#[bench]
fn es2016_exponentation(b: &mut Bencher) {
tr!(b, |_| compat::es2016::exponentation());
tr!(b, || compat::es2016::exponentation());
}
#[bench]
fn es2015(b: &mut Bencher) {
tr!(b, |helpers| compat::es2015(&helpers));
tr!(b, || compat::es2015());
}
#[bench]
fn es2015_arrow(b: &mut Bencher) {
tr!(b, |_| compat::es2015::arrow());
tr!(b, || compat::es2015::arrow());
}
#[bench]
fn es2015_block_scoped_fn(b: &mut Bencher) {
tr!(b, |_| compat::es2015::BlockScopedFns);
tr!(b, || compat::es2015::BlockScopedFns);
}
#[bench]
fn es2015_block_scoping(b: &mut Bencher) {
tr!(b, |_| compat::es2015::block_scoping());
tr!(b, || compat::es2015::block_scoping());
}
#[bench]
fn es2015_classes(b: &mut Bencher) {
tr!(b, |helpers| compat::es2015::Classes { helpers });
tr!(b, || compat::es2015::Classes);
}
#[bench]
@ -198,50 +198,50 @@ fn es2015_destructuring(b: &mut Bencher) {
#[bench]
fn es2015_duplicate_keys(b: &mut Bencher) {
tr!(b, |_| compat::es2015::duplicate_keys());
tr!(b, || compat::es2015::duplicate_keys());
}
#[bench]
fn es2015_parameters(b: &mut Bencher) {
tr!(b, |_| compat::es2015::parameters());
tr!(b, || compat::es2015::parameters());
}
#[bench]
fn es2015_fn_name(b: &mut Bencher) {
tr!(b, |_| compat::es2015::function_name());
tr!(b, || compat::es2015::function_name());
}
#[bench]
fn es2015_for_of(b: &mut Bencher) {
tr!(b, |_| compat::es2015::for_of());
tr!(b, || compat::es2015::for_of());
}
#[bench]
fn es2015_instanceof(b: &mut Bencher) {
tr!(b, |helpers| compat::es2015::InstanceOf { helpers });
tr!(b, || compat::es2015::InstanceOf);
}
#[bench]
fn es2015_shorthand_property(b: &mut Bencher) {
tr!(b, |_| compat::es2015::Shorthand);
tr!(b, || compat::es2015::Shorthand);
}
#[bench]
fn es2015_spread(b: &mut Bencher) {
tr!(b, |helpers| compat::es2015::Spread { helpers });
tr!(b, || compat::es2015::Spread);
}
#[bench]
fn es2015_sticky_regex(b: &mut Bencher) {
tr!(b, |_| compat::es2015::StickyRegex);
tr!(b, || compat::es2015::StickyRegex);
}
#[bench]
fn es2015_typeof_symbol(b: &mut Bencher) {
tr!(b, |helpers| compat::es2015::TypeOfSymbol { helpers });
tr!(b, || compat::es2015::TypeOfSymbol);
}
#[bench]
fn es3(b: &mut Bencher) {
tr!(b, |_| compat::es3());
tr!(b, || compat::es3());
}

View File

@ -2,7 +2,7 @@ use super::*;
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Arrow,
|_| Arrow,
destructuring,
r#"let foo = ({bar}) => undefined;"#,
r#"let foo = function ({bar}) {
@ -12,7 +12,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Arrow,
|_| Arrow,
basic,
r#"let echo = (bar) => bar"#,
r#"let echo = function(bar) {
@ -22,7 +22,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Arrow,
|_| Arrow,
empty_arguments,
r#"var t = () => 5 + 5;"#,
r#"var t = function () {
@ -32,7 +32,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Arrow,
|_| Arrow,
expression,
r#"arr.map(x => x * x);"#,
r#"arr.map(function (x) {
@ -42,7 +42,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Arrow,
|_| Arrow,
inside_call,
r#"arr.map(i => i + 1);"#,
r#"arr.map(function (i) {
@ -52,7 +52,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Arrow,
|_| Arrow,
multiple_arguments,
r#"var t = (i, x) => i * x;"#,
r#"var t = function (i, x) {
@ -61,7 +61,7 @@ test!(
);
// test!(::swc_ecma_parser::Syntax::default(),
// |_, _| Arrow,
// |_| Arrow,
// nested,
// r#"module.exports = {
// init: function () {
@ -96,7 +96,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Arrow,
|_| Arrow,
paren_insertion,
r#"var t = i => i * 5;"#,
r#"var t = function (i) {
@ -106,7 +106,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Arrow,
|_| Arrow,
single_argument,
r#"var t = (i) => i * 5;"#,
r#"var t = function (i) {
@ -116,7 +116,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Arrow,
|_| Arrow,
statement,
r#"nums.forEach(v => {
if (v % 5 === 0) {

View File

@ -34,7 +34,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| BlockScopedFns,
|_| BlockScopedFns,
basic,
r#"{
function name (n) {

View File

@ -1,5 +1,5 @@
use super::get_prototype_of;
use crate::{helpers::Helpers, util::ExprFactory};
use crate::util::ExprFactory;
use ast::*;
use std::iter;
use swc_atoms::JsWord;
@ -129,7 +129,6 @@ pub(super) fn constructor_fn(c: Constructor) -> Function {
/// _this = ...;
/// ```
pub(super) struct ConstructorFolder<'a> {
pub helpers: &'a Helpers,
pub class_name: &'a Ident,
pub mode: Option<SuperFoldingMode>,
/// Mark for `_this`
@ -158,14 +157,11 @@ impl<'a> Fold<Stmt> for ConstructorFolder<'a> {
args,
..
})) => {
let expr = make_possible_return_value(
self.helpers,
ReturningMode::Prototype {
is_constructor_default: self.is_constructor_default,
class_name: self.class_name.clone(),
args: Some(args),
},
);
let expr = make_possible_return_value(ReturningMode::Prototype {
is_constructor_default: self.is_constructor_default,
class_name: self.class_name.clone(),
args: Some(args),
});
match self.mode {
Some(SuperFoldingMode::Assign) => Stmt::Expr(box Expr::Assign(AssignExpr {
@ -207,13 +203,10 @@ impl<'a> Fold<ReturnStmt> for ConstructorFolder<'a> {
let arg = stmt.arg.fold_with(self);
let arg = Some(box make_possible_return_value(
self.helpers,
ReturningMode::Returning {
mark: self.mark,
arg,
},
));
let arg = Some(box make_possible_return_value(ReturningMode::Returning {
mark: self.mark,
arg,
}));
ReturnStmt { arg, ..stmt }
}
@ -256,14 +249,11 @@ impl<'a> Fold<Expr> for ConstructorFolder<'a> {
args,
..
}) => {
let right = box make_possible_return_value(
self.helpers,
ReturningMode::Prototype {
class_name: self.class_name.clone(),
args: Some(args),
is_constructor_default: self.is_constructor_default,
},
);
let right = box make_possible_return_value(ReturningMode::Prototype {
class_name: self.class_name.clone(),
args: Some(args),
is_constructor_default: self.is_constructor_default,
});
Expr::Assign(AssignExpr {
span: DUMMY_SP,
@ -300,8 +290,8 @@ pub(super) enum ReturningMode {
},
}
pub(super) fn make_possible_return_value(helpers: &Helpers, mode: ReturningMode) -> Expr {
helpers.possible_constructor_return();
pub(super) fn make_possible_return_value(mode: ReturningMode) -> Expr {
helper!(possible_constructor_return);
let callee = quote_ident!("_possibleConstructorReturn").as_callee();
Expr::Call(CallExpr {
@ -365,7 +355,7 @@ pub(super) fn make_possible_return_value(helpers: &Helpers, mode: ReturningMode)
vec![ThisExpr { span: DUMMY_SP }.as_arg(), {
let apply = box Expr::Call(CallExpr {
span: DUMMY_SP,
callee: get_prototype_of(helpers, &Expr::Ident(class_name))
callee: get_prototype_of(&Expr::Ident(class_name))
.member(fn_name)
.as_callee(),
@ -404,10 +394,9 @@ pub(super) fn replace_this_in_constructor(mark: Mark, c: Constructor) -> (Constr
self.found = true;
let this = quote_ident!(DUMMY_SP.apply_mark(self.mark), "_this");
// TODO:
// self.helpers.assert_this_initialized();
if self.wrap_with_assertiion {
helper!(assert_this_initialized);
Expr::Call(CallExpr {
span: DUMMY_SP,
callee: quote_ident!("_assertThisInitialized").as_callee(),

View File

@ -7,16 +7,13 @@ use self::{
prop_name::HashKey,
super_field::SuperFieldAccessFolder,
};
use crate::{
helpers::Helpers,
util::{
alias_ident_for, default_constructor, prepend, prop_name_to_expr, ExprFactory,
ModuleItemLike, StmtLike,
},
use crate::util::{
alias_ident_for, default_constructor, prepend, prop_name_to_expr, ExprFactory, ModuleItemLike,
StmtLike,
};
use ast::*;
use indexmap::IndexMap;
use std::{iter, sync::Arc};
use std::iter;
use swc_common::{Fold, FoldWith, Mark, Spanned, Visit, VisitWith, DUMMY_SP};
#[macro_use]
@ -59,10 +56,8 @@ mod tests;
/// return Test;
/// }();
/// ```
#[derive(Default, Clone)]
pub struct Classes {
pub helpers: Arc<Helpers>,
}
#[derive(Default, Clone, Copy)]
pub struct Classes;
struct Data {
key_prop: Box<Prop>,
@ -231,7 +226,7 @@ impl Classes {
_ => false,
};
if is_super_native {
self.helpers.wrap_native_super();
helper!(wrap_native_super);
(
params,
vec![CallExpr {
@ -348,8 +343,8 @@ impl Classes {
if let Some(ref super_class_ident) = super_class_ident {
// inject helper methods
self.helpers.inherits();
self.helpers.possible_constructor_return();
helper!(inherits);
helper!(possible_constructor_return);
stmts.push(Stmt::Expr(box Expr::Call(CallExpr {
span: DUMMY_SP,
@ -392,7 +387,7 @@ impl Classes {
}
// inject _classCallCheck(this, Bar);
self.helpers.class_call_check();
helper!(class_call_check);
inject_class_call_check(&mut constructor, class_name.clone());
let mut body = constructor.body.unwrap().stmts;
// should we insert `var _this`?
@ -418,7 +413,6 @@ impl Classes {
// Handle `super()`
body = body.fold_with(&mut ConstructorFolder {
is_constructor_default,
helpers: &self.helpers,
class_name: &class_name,
mode,
mark,
@ -456,10 +450,11 @@ impl Classes {
arg: Some(box Expr::Ident(this)),
}));
} else {
let possible_return_value = box make_possible_return_value(
&self.helpers,
ReturningMode::Returning { mark, arg: None },
);
let possible_return_value =
box make_possible_return_value(ReturningMode::Returning {
mark,
arg: None,
});
body.push(Stmt::Return(ReturnStmt {
span: DUMMY_SP,
arg: Some(possible_return_value),
@ -520,7 +515,6 @@ impl Classes {
let mut vars = vec![];
let mut folder = SuperFieldAccessFolder {
class_name,
helpers: &self.helpers,
vars: &mut vars,
constructor_this_mark: this_mark,
// constructor cannot be static
@ -665,7 +659,6 @@ impl Classes {
let mut vars = vec![];
let mut folder = SuperFieldAccessFolder {
class_name: &class_name,
helpers: &self.helpers,
vars: &mut vars,
constructor_this_mark: None,
is_static: m.is_static,
@ -736,7 +729,7 @@ impl Classes {
if props.is_empty() && static_props.is_empty() {
return vec![];
}
self.helpers.create_class();
helper!(create_class);
vec![mk_create_class_call(
class_name,
mk_arg_obj_for_create_class(props),
@ -754,8 +747,8 @@ impl Classes {
/// ```js
/// Child.__proto__ || Object.getPrototypeOf(Child)
/// ```
fn get_prototype_of(helpers: &Helpers, obj: &Expr) -> Expr {
helpers.get_prototype_of();
fn get_prototype_of(obj: &Expr) -> Expr {
helper!(get_prototype_of);
Expr::Call(CallExpr {
span: DUMMY_SP,

View File

@ -1,8 +1,5 @@
use super::get_prototype_of;
use crate::{
helpers::Helpers,
util::{alias_ident_for, is_rest_arguments, ExprFactory},
};
use crate::util::{alias_ident_for, is_rest_arguments, ExprFactory};
use ast::*;
use std::iter;
use swc_common::{Fold, FoldWith, Mark, Span, Spanned, DUMMY_SP};
@ -22,7 +19,7 @@ use swc_common::{Fold, FoldWith, Mark, Span, Spanned, DUMMY_SP};
/// 'foo', this).call(this, a);
pub(super) struct SuperFieldAccessFolder<'a> {
pub class_name: &'a Ident,
pub helpers: &'a Helpers,
pub vars: &'a mut Vec<VarDeclarator>,
/// Mark for the `_this`. Used only when folding constructor.
pub constructor_this_mark: Option<Mark>,
@ -41,7 +38,6 @@ pub(super) struct SuperFieldAccessFolder<'a> {
}
struct SuperCalleeFolder<'a> {
helpers: &'a Helpers,
vars: &'a mut Vec<VarDeclarator>,
class_name: &'a Ident,
/// True if we should inject get and
@ -179,16 +175,13 @@ impl<'a> SuperCalleeFolder<'a> {
fn super_to_get_call(&mut self, super_token: Span, prop: Box<Expr>, computed: bool) -> Expr {
self.inject_get = true;
let proto_arg = get_prototype_of(
self.helpers,
&if self.is_static {
// Foo
Expr::Ident(self.class_name.clone())
} else {
// Foo.prototype
self.class_name.clone().member(quote_ident!("prototype"))
},
)
let proto_arg = get_prototype_of(&if self.is_static {
// Foo
Expr::Ident(self.class_name.clone())
} else {
// Foo.prototype
self.class_name.clone().member(quote_ident!("prototype"))
})
.as_arg();
let prop_arg = match *prop {
@ -209,7 +202,7 @@ impl<'a> SuperCalleeFolder<'a> {
Some(mark) => {
let this = quote_ident!(super_token.apply_mark(mark), "_this");
self.helpers.assert_this_initialized();
helper!(assert_this_initialized);
CallExpr {
span: DUMMY_SP,
callee: quote_ident!("_assertThisInitialized").as_callee(),
@ -264,11 +257,8 @@ impl<'a> SuperCalleeFolder<'a> {
}
}
let proto_arg = get_prototype_of(
self.helpers,
&self.class_name.clone().member(quote_ident!("prototype")),
)
.as_arg();
let proto_arg =
get_prototype_of(&self.class_name.clone().member(quote_ident!("prototype"))).as_arg();
let prop_arg = match *prop {
Expr::Ident(Ident {
@ -401,7 +391,6 @@ impl<'a> Fold<Expr> for SuperFieldAccessFolder<'a> {
class_name: self.class_name,
inject_get: false,
inject_set: false,
helpers: self.helpers,
vars: self.vars,
constructor_this_mark: self.constructor_this_mark,
is_static: self.is_static,
@ -427,7 +416,7 @@ impl<'a> Fold<Expr> for SuperFieldAccessFolder<'a> {
}
if callee_folder.inject_get {
self.helpers.get();
helper!(get);
if should_invoke_call {
match n {
@ -482,7 +471,7 @@ impl<'a> Fold<Expr> for SuperFieldAccessFolder<'a> {
}
if callee_folder.inject_set {
self.helpers.set();
helper!(set);
}
n.fold_children(self)

View File

@ -9,26 +9,17 @@ fn syntax() -> Syntax {
Syntax::default()
}
fn tr(helpers: Arc<Helpers>) -> impl Fold<Module> {
Classes { helpers }
fn tr() -> impl Fold<Module> {
Classes
}
fn spec_tr(helpers: Arc<Helpers>) -> impl Fold<Module> {
chain!(
resolver(),
Classes {
helpers: helpers.clone()
},
Spread {
helpers: helpers.clone()
},
block_scoping(),
)
fn spec_tr() -> impl Fold<Module> {
chain!(resolver(), Classes, Spread, block_scoping(),)
}
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
custom_singleton,
r#"
let singleton;
@ -61,7 +52,7 @@ let Sub = function(_Foo) {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
custom_native,
r#"
class List extends Array {}
@ -80,7 +71,7 @@ let List = function(_Array) {
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
custom_nested,
r#"
class Hello{
@ -110,7 +101,7 @@ expect(new Outer().hello).toBe('hello');
// spec_constructor_only
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_constructor_only,
r#"
class Foo {
@ -138,7 +129,7 @@ var Foo = function Foo() {
// regression_5769_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
regression_5769_exec,
r#"
class Point {
@ -170,7 +161,7 @@ cp.m();
// spec_this_not_allowed_before_super_in_derived_classes_5_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_this_not_allowed_before_super_in_derived_classes_5_exec,
r#"
class Bar {}
@ -191,7 +182,7 @@ test!(
// We don't use function-name pass
ignore,
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_statement,
r#"
var BaseView = class BaseView {
@ -252,7 +243,7 @@ function () {
// get_set_set_semantics_getter_defined_on_parent
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_getter_defined_on_parent,
r#"
@ -348,7 +339,7 @@ expect(obj.test).toBe(2);
// spec_derived_constructor_must_call_super
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_derived_constructor_must_call_super,
r#"
class Foo extends Bar {
@ -382,7 +373,7 @@ function (_Bar) {
// get_set_get_semantics_data_defined_on_parent
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_get_semantics_data_defined_on_parent,
r#"
@ -443,7 +434,7 @@ expect(obj.get()).toBe(1);
// get_set_call_semantics_setter_defined_on_parent_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_call_semantics_setter_defined_on_parent_exec,
r#"
@ -477,7 +468,7 @@ expect(() => {
// get_set_set_semantics_data_defined_on_parent_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_data_defined_on_parent_exec,
r#"
@ -516,7 +507,7 @@ expect(obj.test).toBe(3);
// regression_8499
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
regression_8499,
r#"
// Pretend that `Reflect.construct` isn't supported.
@ -575,7 +566,7 @@ expect(constructor).toBe(CustomElement);
// regression_5817
test!(
syntax(),
|_, helpers| chain!(tr(helpers), arrow()),
|_| chain!(tr(), arrow()),
regression_5817,
r#"
class A extends B {
@ -624,7 +615,7 @@ function (_B) {
// get_set_set_semantics_not_defined_on_parent_setter_on_obj
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_not_defined_on_parent_setter_on_obj,
r#"
@ -700,7 +691,7 @@ expect(obj.test).toBe(3);
// get_set_get_semantics_setter_defined_on_parent
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_get_semantics_setter_defined_on_parent,
r#"
@ -782,7 +773,7 @@ expect(obj.get()).toBeUndefined();
// spec_this_not_allowed_before_super_in_derived_classes_4
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_this_not_allowed_before_super_in_derived_classes_4,
r#"
class Foo extends Bar {
@ -823,7 +814,7 @@ function (_Bar) {
// spec_calling_super_properties
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_calling_super_properties,
r#"
class Test extends Foo {
@ -874,7 +865,7 @@ function (_Foo) {
// spec_instance_getter_and_setter
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_instance_getter_and_setter,
r#"
class Test {
@ -917,7 +908,7 @@ test!(
// Module
ignore,
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
regression_2941,
r#"
export default class {}
@ -943,7 +934,7 @@ exports.default = _default;
// regression_T2494
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
regression_t2494,
r#"
var x = {
@ -975,7 +966,7 @@ var x = {
// spec_inferred_expression_name
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_inferred_expression_name,
r#"
var o = { foo: class foo {} };
@ -998,7 +989,7 @@ test_exec!(
// Wrong test (babel + jest fails)
ignore,
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
regression_8499_exec,
r#"
// Pretend that `Reflect.construct` isn't supported.
@ -1025,7 +1016,7 @@ expect(constructor).toBe(CustomElement);
// spec_default_super
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_default_super,
r#"
class Test {
@ -1080,7 +1071,7 @@ expect(Test.test()).toBe(Function);
// spec_default_super
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_default_super_constructor,
r#"
class Test {
@ -1100,7 +1091,7 @@ var Test = function Test() {
// get_set_get_semantics_getter_defined_on_parent
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_get_semantics_getter_defined_on_parent,
r#"
@ -1186,7 +1177,7 @@ expect(obj.get()).toBe(1);
// spec_this_not_allowed_before_super_in_derived_classes
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_this_not_allowed_before_super_in_derived_classes,
r#"
class Foo extends Bar {
@ -1223,7 +1214,7 @@ function (_Bar) {
// get_set_call_semantics_getter_defined_on_parent
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_call_semantics_getter_defined_on_parent,
r#"
@ -1321,7 +1312,7 @@ expect(obj.call(1, 2, 3)).toBe(1);
// spec_derived_constructor_must_call_super_4_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_derived_constructor_must_call_super_4_exec,
r#"
class Bar {}
@ -1342,7 +1333,7 @@ expect(() => new Foo()).toThrow("this hasn't been initialised");
// spec_export_default
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_export_default,
r#"
export default class Foo {}
@ -1361,7 +1352,7 @@ export { Foo as default };
// get_set_call_semantics_data_defined_on_parent_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_call_semantics_data_defined_on_parent_exec,
r#"
@ -1395,7 +1386,7 @@ expect(obj.call(1, 2, 3)).toBe(1);
// extend_builtins_spec_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
extend_builtins_spec_exec,
r#"
class List extends Array {}
@ -1409,7 +1400,7 @@ expect(new List).toBeInstanceOf(Array);
// get_set_call_semantics_data_defined_on_parent
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_call_semantics_data_defined_on_parent,
r#"
@ -1501,7 +1492,7 @@ expect(obj.call(1, 2, 3)).toBe(1);
// spec_static
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_static,
r#"
class A {
@ -1546,7 +1537,7 @@ function () {
// regression_T6755
// get_set_memoized_update
test!(syntax(),|_,helpers| tr(helpers), get_set_memoized_update, r#"
test!(syntax(),|_| tr(), get_set_memoized_update, r#"
class Base {}
Object.defineProperty(Base.prototype, 0, {
@ -1657,7 +1648,7 @@ expect(obj[1]).toBe(2);
"#);
// spec_nested_class_super_property_in_key
test!(syntax(),|_,helpers| spec_tr(helpers),
test!(syntax(),|_| spec_tr(),
spec_nested_class_super_property_in_key, r#"
class Hello {
@ -1736,7 +1727,7 @@ expect(new Outer().hello()).toBe('hello');
"#);
// spec_super_reference_in_prop_exression
test!(syntax(),|_,helpers| spec_tr(helpers), spec_super_reference_in_prop_exression, r#"
test!(syntax(),|_| spec_tr(), spec_super_reference_in_prop_exression, r#"
class Foo extends Bar {
constructor() {
super[super().method]();
@ -1767,7 +1758,7 @@ function (_Bar) {
// spec_super_reference_before_in_lambda_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_super_reference_before_in_lambda_exec,
r#"
class Bar {
@ -1788,7 +1779,7 @@ new Foo();
// spec_nested_class_super_call_in_key_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_nested_class_super_call_in_key_exec,
r#"
@ -1822,7 +1813,7 @@ expect(new Outer().hello()).toBe('hello');
// spec_this_not_allowed_before_super_in_derived_classes_4_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_this_not_allowed_before_super_in_derived_classes_4_exec,
r#"
class Bar {}
@ -1843,7 +1834,7 @@ new Foo();
// spec_this_not_allowed_before_super_in_derived_classes_2
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_this_not_allowed_before_super_in_derived_classes_2,
r#"
class Foo extends Bar {
@ -1878,7 +1869,7 @@ function (_Bar) {
// spec_accessing_super_properties
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_accessing_super_properties,
r#"
class Test extends Foo {
@ -1917,7 +1908,7 @@ function (_Foo) {
// spec_computed_methods
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_computed_methods,
r#"
class Foo {
@ -1962,7 +1953,7 @@ function () {
// get_set_set_semantics_not_defined_on_parent_data_on_obj_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_not_defined_on_parent_data_on_obj_exec,
r#"
@ -1992,7 +1983,7 @@ expect(obj.test).toBe(3);
// get_set_set_semantics_data_defined_on_parent
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_data_defined_on_parent,
r#"
@ -2073,7 +2064,7 @@ expect(obj.test).toBe(3);
// get_set_set_semantics_not_defined_on_parent_getter_on_obj
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_not_defined_on_parent_getter_on_obj,
r#"
@ -2140,7 +2131,7 @@ expect(obj.test).toBe(3);
// spec_returning_from_derived_constructor_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_returning_from_derived_constructor_exec,
r#"
@ -2208,7 +2199,7 @@ expect(() => {
// regression_2694
// regression_5769
test!(syntax(),|_,helpers| tr(helpers), regression_5769, r#"
test!(syntax(),|_| tr(), regression_5769, r#"
class Point {
getX() {
expect(this.x).toBe(3); // C
@ -2289,7 +2280,7 @@ cp.m();
// spec_super_function_fallback
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_super_function_fallback,
r#"
class Test {
@ -2318,10 +2309,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| chain!(
tr(helpers.clone()),
jsx(tester.cm.clone(), Default::default(), helpers)
),
|tester| chain!(tr(), jsx(tester.cm.clone(), Default::default())),
regression_2775,
r#"
import React, {Component} from 'react';
@ -2382,7 +2370,7 @@ exports.default = RandomComponent;
// extend_builtins_imported_babel_plugin_transform_builtin_classes_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
extend_builtins_imported_babel_plugin_transform_builtin_classes_exec,
r#"
@ -2432,7 +2420,7 @@ test!(
// Module
ignore,
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
regression_3028,
r#"
class b {
@ -2517,7 +2505,7 @@ exports.default = a2;
// spec_instance_setter
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_instance_setter,
r#"
class Test {
@ -2552,7 +2540,7 @@ function () {
// spec_nested_object_super_call_in_key
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_nested_object_super_call_in_key,
r#"
@ -2623,7 +2611,7 @@ expect(new Outer().hello()).toBe('hello');
// get_set_set_semantics_not_defined_on_parent_not_on_obj
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_not_defined_on_parent_not_on_obj,
r#"
@ -2683,7 +2671,7 @@ expect(obj.test).toBe(3);
// spec_derived_constructor_no_super_return_falsey
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_derived_constructor_no_super_return_falsey,
r#"
class Child extends Base {
@ -2717,7 +2705,7 @@ function (_Base) {
// spec_this_not_allowed_before_super_in_derived_classes_5
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_this_not_allowed_before_super_in_derived_classes_5,
r#"
class Foo extends Bar {
@ -2754,7 +2742,7 @@ function (_Bar) {
// spec_constructor
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_constructor,
r#"
class Test {
@ -2822,7 +2810,7 @@ var ConstructorScoping = function ConstructorScoping() {
// spec_preserves_directives
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_preserves_directives,
r#"
class MyCtrl {
@ -2884,7 +2872,7 @@ var MyCtrl3 = function MyCtrl3(a) {
// spec_derived_constructor_no_super_return_object
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_derived_constructor_no_super_return_object,
r#"
class Child extends Base {
@ -2918,7 +2906,7 @@ function (_Base) {
// get_set_get_semantics_getter_defined_on_parent_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_get_semantics_getter_defined_on_parent_exec,
r#"
@ -2952,7 +2940,7 @@ test!(
// Module
ignore,
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
regression_t6750,
r#"
export default function() {
@ -2994,7 +2982,7 @@ function _default() {
// spec_instance_method
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_instance_method,
r#"
class Test {
@ -3027,7 +3015,7 @@ function () {
);
// regression_T2997
test!(syntax(),|_,helpers| tr(helpers), regression_t2997, r#"
test!(syntax(),|_| tr(), regression_t2997, r#"
class A {}
class B extends A {
@ -3065,7 +3053,7 @@ function (_A) {
// spec_constructor_binding_collision
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_constructor_binding_collision,
r#"
class Example {
@ -3092,7 +3080,7 @@ var t = new Example();
);
// spec_super_class_anonymous
test!(syntax(),|_,helpers| spec_tr(helpers), spec_super_class_anonymous, r#"
test!(syntax(),|_| spec_tr(), spec_super_class_anonymous, r#"
class TestEmpty extends (class {}) {
}
@ -3260,7 +3248,7 @@ function () {
// spec_nested_class_super_call_in_key
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_nested_class_super_call_in_key,
r#"
@ -3344,7 +3332,7 @@ expect(new Outer().hello()).toBe('hello');
// get_set_get_semantics_data_defined_on_parent_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_get_semantics_data_defined_on_parent_exec,
r#"
@ -3381,7 +3369,7 @@ test!(
// Module
ignore,
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
regression_2663,
r#"
import net from 'net';
@ -3462,7 +3450,7 @@ exports.default = Connection;
// get_set_set_semantics_setter_defined_on_parent_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_setter_defined_on_parent_exec,
r#"
@ -3497,7 +3485,7 @@ expect(obj.test).toBe(2);
// regression_5817_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
regression_5817_exec,
r#"
// https://github.com/babel/babel/issues/5817
@ -3525,7 +3513,7 @@ expect(table.returnParam(false)).toBe(false);
// spec_instance_getter
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_instance_getter,
r#"
class Test {
@ -3560,7 +3548,7 @@ function () {
// spec_this_not_allowed_before_super_in_derived_classes_3
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_this_not_allowed_before_super_in_derived_classes_3,
r#"
class Foo extends Bar {
@ -3602,7 +3590,7 @@ function (_Bar) {
test!(
// TODO(kdy1): Unignore this.
ignore,
syntax(),|_,helpers| spec_tr(helpers), spec_accessing_super_class, r#"
syntax(),|_| spec_tr(), spec_accessing_super_class, r#"
class Test extends Foo {
constructor() {
woops.super.test();
@ -3684,7 +3672,7 @@ function (_Foo) {
// get_set_call_semantics_getter_defined_on_parent_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_call_semantics_getter_defined_on_parent_exec,
r#"
@ -3721,7 +3709,7 @@ expect(obj.call(1, 2, 3)).toBe(1);
// spec_this_not_allowed_before_super_in_derived_classes_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_this_not_allowed_before_super_in_derived_classes_exec,
r#"
class Bar {}
@ -3741,7 +3729,7 @@ expect(() => new Foo()).toThrow();
// spec_nested_object_super_property_in_key_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_nested_object_super_property_in_key_exec,
r#"
@ -3772,7 +3760,7 @@ expect(new Outer().hello()).toBe('hello');
// regression_T6712
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
regression_t6712,
r#"
class A {
@ -3807,7 +3795,7 @@ function () {
// get_set_set_semantics_setter_defined_on_parent
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_setter_defined_on_parent,
r#"
@ -3898,7 +3886,7 @@ expect(obj.test).toBe(2);
// get_set_call_semantics_not_defined_on_parent
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_call_semantics_not_defined_on_parent,
r#"
@ -3969,7 +3957,7 @@ expect(() => {
// spec_derived_constructor_must_call_super_2_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_derived_constructor_must_call_super_2_exec,
r#"
class Bar {}
@ -3990,7 +3978,7 @@ expect(() => new Foo()).toThrow("this hasn't been initialised");
// spec_nested_object_super_property_in_key
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_nested_object_super_property_in_key,
r#"
@ -4063,7 +4051,7 @@ expect(new Outer().hello()).toBe('hello');
// get_set_set_semantics_not_defined_on_parent_setter_on_obj_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_not_defined_on_parent_setter_on_obj_exec,
r#"
@ -4098,7 +4086,7 @@ expect(obj.test).toBe(3);
// spec_this_not_allowed_before_super_in_derived_classes_2_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_this_not_allowed_before_super_in_derived_classes_2_exec,
r#"
class Bar {}
@ -4117,7 +4105,7 @@ expect(() => new Foo()).toThrow("this hasn't been initialised");
// extend_builtins_overwritten_null_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
extend_builtins_overwritten_null_exec,
r#"
let Array = null;
@ -4130,7 +4118,7 @@ expect(List.prototype.__proto__).toBeUndefined();
// spec_super_reference_before_bare_super_inline_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_super_reference_before_bare_super_inline_exec,
r#"
class Bar {}
@ -4149,7 +4137,7 @@ expect(() => new Foo()).toThrow("this hasn't been initialised");
// get_set_call_semantics_setter_defined_on_parent
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_call_semantics_setter_defined_on_parent,
r#"
@ -4236,7 +4224,7 @@ expect(() => {
// spec_super_class
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_super_class,
r#"
class Test extends Foo { }
@ -4266,7 +4254,7 @@ function (_Foo) {
// spec_nested_object_super_call_in_key_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_nested_object_super_call_in_key_exec,
r#"
@ -4300,7 +4288,7 @@ expect(new Outer().hello()).toBe('hello');
// spec_derived_constructor_must_call_super_2
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_derived_constructor_must_call_super_2,
r#"
class Foo extends Bar {
@ -4335,7 +4323,7 @@ function (_Bar) {
// spec_derived_constructor_must_call_super_3
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_derived_constructor_must_call_super_3,
r#"
class Foo extends Bar {
@ -4374,7 +4362,7 @@ function (_Bar) {
// spec_this_not_allowed_before_super_in_derived_classes_3_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_this_not_allowed_before_super_in_derived_classes_3_exec,
r#"
class Bar {}
@ -4395,7 +4383,7 @@ expect(() => new Foo()).toThrow("this hasn't been initialised");
// get_set_get_semantics_not_defined_on_parent_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_get_semantics_not_defined_on_parent_exec,
r#"
@ -4423,7 +4411,7 @@ expect(obj.get()).toBeUndefined();
// get_set_set_semantics_not_defined_on_parent_data_on_obj
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_not_defined_on_parent_data_on_obj,
r#"
@ -4493,7 +4481,7 @@ expect(obj.test).toBe(3);
// get_set_set_semantics_not_defined_on_parent_getter_on_obj_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_not_defined_on_parent_getter_on_obj_exec,
r#"
@ -4524,7 +4512,7 @@ expect(obj.test).toBe(3);
// exec
// get_set_memoized_assign
test!(syntax(),|_,helpers| tr(helpers), get_set_memoized_assign, r#"
test!(syntax(),|_| tr(), get_set_memoized_assign, r#"
class Base {}
Object.defineProperty(Base.prototype, 0, {
@ -4637,7 +4625,7 @@ expect(obj[1]).toBe(2);
// extend_builtins_builtin_objects_throw_when_wrapped_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
extend_builtins_builtin_objects_throw_when_wrapped_exec,
r#"
// JSON is wrapped because it starts with an uppercase letter, but it
@ -4651,7 +4639,7 @@ expect(() => class BetterJSON extends JSON {}).toThrow();
// get_set_call_semantics_not_defined_on_parent_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_call_semantics_not_defined_on_parent_exec,
r#"
@ -4682,7 +4670,7 @@ expect(() => {
// spec_nested_class_super_property_in_key_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_nested_class_super_property_in_key_exec,
r#"
@ -4713,7 +4701,7 @@ expect(new Outer().hello()).toBe('hello');
// spec_relaxed_method_coercion
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_relaxed_method_coercion,
r#"
// #1649
@ -4751,7 +4739,7 @@ function () {
// spec_derived_constructor_must_call_super_3_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_derived_constructor_must_call_super_3_exec,
r#"
class Bar {}
@ -4771,7 +4759,7 @@ new Foo();
// get_set_memoized_update_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_memoized_update_exec,
r#"
@ -4822,7 +4810,7 @@ expect(obj[1]).toBe(2);
// spec_computed_methods_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_computed_methods_exec,
r#"
const sym = Symbol();
@ -4849,7 +4837,7 @@ expect(i[sym]()).toBe(3);
// get_set_memoized_assign_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_memoized_assign_exec,
r#"
@ -4900,7 +4888,7 @@ expect(obj[1]).toBe(2);
// spec_derived_constructor_must_call_super_4
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_derived_constructor_must_call_super_4,
r#"
class Foo extends Bar {
@ -4937,7 +4925,7 @@ function (_Bar) {
// spec_super_class_id_member_expression
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_super_class_id_member_expression,
r#"
class BaseController extends Chaplin.Controller {
@ -4986,7 +4974,7 @@ function (_Another) {
// spec_delay_arrow_function_for_bare_super_derived
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_delay_arrow_function_for_bare_super_derived,
r#"
class Foo extends Bar {
@ -5027,7 +5015,7 @@ function (_Bar) {
// spec_default_super_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_default_super_exec,
r#"
class Test {
@ -5054,7 +5042,7 @@ expect(Test.test()).toBe(Function);
// spec_super_reference_before_in_lambda_3_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_super_reference_before_in_lambda_3_exec,
r#"
class Bar {
@ -5079,7 +5067,7 @@ new Foo();
// get_set_get_semantics_not_defined_on_parent
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_get_semantics_not_defined_on_parent,
r#"
@ -5147,7 +5135,7 @@ expect(obj.get()).toBeUndefined();
// spec_plain_class
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_plain_class,
r#"
class Test { }
@ -5166,7 +5154,7 @@ var Test = function Test() {
// spec_super_reference_before_bare_super_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_super_reference_before_bare_super_exec,
r#"
class Bar {}
@ -5186,7 +5174,7 @@ expect(() => new Foo()).toThrow("this hasn't been initialised");
// spec_super_reference_before_in_lambda_2_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_super_reference_before_in_lambda_2_exec,
r#"
class Bar {
@ -5209,7 +5197,7 @@ expect(() => new Foo()).toThrow("this hasn't been initialised");
// get_set_set_semantics_not_defined_on_parent_not_on_obj_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_not_defined_on_parent_not_on_obj_exec,
r#"
@ -5234,7 +5222,7 @@ expect(obj.test).toBe(3);
// get_set_get_semantics_setter_defined_on_parent_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_get_semantics_setter_defined_on_parent_exec,
r#"
@ -5265,7 +5253,7 @@ expect(obj.get()).toBeUndefined();
// extend_builtins_super_called_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
extend_builtins_super_called_exec,
r#"
var called = false;
@ -5286,7 +5274,7 @@ expect(called).toBe(true);
// spec_derived_constructor_must_call_super_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_derived_constructor_must_call_super_exec,
r#"
class Bar {}
@ -5307,7 +5295,7 @@ expect(() => new Foo()).toThrow("this hasn't been initialised");
// spec_export_super_class
test!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_export_super_class,
r#"
export default class extends A {}
@ -5335,7 +5323,7 @@ export { _default as default };
// extend_builtins_wrap_native_super_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
extend_builtins_wrap_native_super_exec,
r#"
// basic sanity check to confirm the external wrapNativeSuper helper works
@ -5370,7 +5358,7 @@ expect(t2).toBeInstanceOf(Array);
// spec_super_reference_in_prop_exression_exec
test_exec!(
syntax(),
|_, helpers| spec_tr(helpers),
|_| spec_tr(),
spec_super_reference_in_prop_exression_exec,
r#"
let called = false;
@ -5402,7 +5390,7 @@ expect(called).toBe(true);
// get_set_set_semantics_getter_defined_on_parent_exec
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
get_set_set_semantics_getter_defined_on_parent_exec,
r#"

View File

@ -1,10 +1,8 @@
use crate::{
helpers::Helpers,
pass::Pass,
util::{ExprFactory, StmtLike},
};
use ast::*;
use std::sync::Arc;
use swc_common::{Fold, FoldWith, Mark, Spanned, Visit, VisitWith, DUMMY_SP};
#[cfg(test)]
@ -41,14 +39,12 @@ mod tests;
///
/// TODO(kdy1): cache reference like (_f = f, mutatorMap[_f].get = function(){})
/// instead of (mutatorMap[f].get = function(){}
pub fn computed_properties(helpers: Arc<Helpers>) -> impl Pass + Clone {
ComputedProps { helpers }
pub fn computed_properties() -> impl Pass + Clone + Copy {
ComputedProps
}
#[derive(Default, Clone)]
struct ComputedProps {
helpers: Arc<Helpers>,
}
#[derive(Default, Clone, Copy)]
struct ComputedProps;
#[derive(Default)]
struct ObjectLitFolder {
@ -300,7 +296,7 @@ where
// Add variable declaration
// e.g. var ref
if !folder.vars.is_empty() {
self.helpers.define_property();
helper!(define_property);
buf.push(T::from_stmt(Stmt::Decl(Decl::Var(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
@ -309,7 +305,7 @@ where
}))));
}
if folder.used_define_enum_props {
self.helpers.define_enumerable_properties();
helper!(define_enumerable_properties);
}
buf.push(T::from_stmt(stmt));

View File

@ -2,7 +2,7 @@ use super::*;
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| ComputedProps { helpers },
|_| ComputedProps,
accessors,
r#"var obj = {
get [foobar]() {
@ -39,7 +39,7 @@ var obj = ( _obj = {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| ComputedProps { helpers },
|_| ComputedProps,
argument,
r#"foo({
[bar]: "foobar"
@ -50,7 +50,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| ComputedProps { helpers },
|_| ComputedProps,
assignment,
r#"foo = {
[bar]: "foobar"
@ -60,7 +60,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| ComputedProps { helpers },
|_| ComputedProps,
method,
r#"var obj = {
[foobar]() {
@ -80,7 +80,7 @@ var obj = (_obj = {}, _defineProperty(_obj, foobar, function () {
);
test!(::swc_ecma_parser::Syntax::default(),
|_, helpers| ComputedProps{helpers},
|_| ComputedProps,
mixed,
r#"var obj = {
["x" + foo]: "heh",
@ -96,7 +96,7 @@ var obj = (_obj = {}, _defineProperty(_obj, "x" + foo, "heh"), _defineProperty(_
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| ComputedProps { helpers },
|_| ComputedProps,
multiple,
r#"var obj = {
["x" + foo]: "heh",
@ -110,7 +110,7 @@ _defineProperty(_obj, "y" + bar, "noo"), _obj);"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| ComputedProps { helpers },
|_| ComputedProps,
single,
r#"var obj = {
["x" + foo]: "heh"
@ -120,7 +120,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| ComputedProps { helpers },
|_| ComputedProps,
symbol,
r#"var k = Symbol();
var foo = {
@ -142,7 +142,7 @@ var foo = ( _obj = {
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| ComputedProps { helpers },
|_| ComputedProps,
symbol_exec,
r#"
var k = Symbol();
@ -159,7 +159,7 @@ expect(foo[k]).toBe(k)"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| ComputedProps { helpers },
|_| ComputedProps,
this,
r#"var obj = {
["x" + foo.bar]: "heh"
@ -169,7 +169,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| ComputedProps { helpers },
|_| ComputedProps,
two,
r#"var obj = {
first: "first",
@ -182,7 +182,7 @@ var obj = ( _obj = {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| ComputedProps { helpers },
|_| ComputedProps,
variable,
r#"var foo = {
[bar]: "foobar"

View File

@ -1,10 +1,9 @@
use crate::{
helpers::Helpers,
pass::Pass,
util::{prop_name_to_expr, ExprFactory, StmtLike},
};
use ast::*;
use std::{iter, sync::Arc};
use std::iter;
use swc_atoms::JsWord;
use swc_common::{Fold, FoldWith, Mark, Spanned, Visit, VisitWith, DUMMY_SP};
@ -32,14 +31,12 @@ mod tests;
/// b = _arr2[1],
/// rest = _arr2.slice(2);
/// ```
pub fn destructuring(helpers: Arc<Helpers>) -> impl Pass + Clone {
Destructuring { helpers }
pub fn destructuring() -> impl Pass + Clone + Copy {
Destructuring
}
#[derive(Clone)]
struct Destructuring {
helpers: Arc<Helpers>,
}
#[derive(Clone, Copy)]
struct Destructuring;
macro_rules! impl_for_for_stmt {
($T:tt) => {
@ -195,7 +192,7 @@ impl Fold<Vec<VarDeclarator>> for Destructuring {
let ref_ident = make_ref_ident(&mut decls, decl.init);
let ref_ident = if can_be_null {
self.helpers.throw();
helper!(throw);
make_ref_ident(
&mut decls,
Some(box Expr::Cond(CondExpr {
@ -377,7 +374,6 @@ impl Fold<Function> for Destructuring {
#[derive(Default)]
struct AssignFolder {
helpers: Arc<Helpers>,
vars: Vec<VarDeclarator>,
}
@ -385,10 +381,7 @@ impl Fold<Expr> for AssignFolder {
fn fold(&mut self, expr: Expr) -> Expr {
let expr = match expr {
// Handle iife
Expr::Fn(..) | Expr::Object(..) => Destructuring {
helpers: self.helpers.clone(),
}
.fold(expr),
Expr::Fn(..) | Expr::Object(..) => Destructuring.fold(expr),
_ => expr.fold_children(self),
};

View File

@ -1,12 +1,12 @@
use super::*;
fn tr(helpers: Arc<Helpers>) -> impl Fold<Module> {
Destructuring { helpers }
fn tr() -> impl Fold<Module> {
Destructuring
}
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
obj_assign_pat,
r#"let { a = 1 } = foo"#,
r#"let ref = foo ? foo : _throw(new TypeError("Cannot destructure 'undefined' or 'null'")),
@ -15,7 +15,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
obj_assign_expr,
r#"let a;
[{ a = 1 }] = foo"#,
@ -26,7 +26,7 @@ ref = foo, ref1 = ref[0], ref2 = ref1.a, a = ref2 === void 0 ? 1 : ref2, ref;"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
array1,
r#"var [a, [b], [c]] = ["hello", [", ", "junk"], ["world"]];"#,
r#"var ref = ['hello', [', ', 'junk'], ['world']], a = ref[0], ref1 = ref[1],
@ -35,7 +35,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
array2,
r#"[a, [b], [c]] = ["hello", [", ", "junk"], ["world"]];"#,
r#"var ref, ref1, ref2;
@ -46,7 +46,7 @@ ref = ['hello', [', ', 'junk'], ['world']], a = ref[0], ref1 = ref[1],
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
assign_expr_completion_record,
r#"var x, y;
[x, y] = [1, 2];"#,
@ -56,7 +56,7 @@ ref = [1, 2], x = ref[0], y = ref[1], ref;"#
);
test!(::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
assign_expr_pat,
r#"var z = {};
var { x: { y } = {} } = z;"#,
@ -69,7 +69,7 @@ var ref = z ? z : _throw(new TypeError("Cannot destructure 'undefined' or 'null'
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
assign_expr,
r#"console.log([x] = [123]);"#,
r#"var ref;
@ -78,7 +78,7 @@ console.log((ref = [123], x = ref[0], ref));"#
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| Destructuring { helpers },
|_| Destructuring,
chained,
r#"var a, b, c, d;
({ a, b } = ({ c, d } = { a: 1, b: 2, c: 3, d: 4}));
@ -90,7 +90,7 @@ expect(d).toBe(4);"#
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| Destructuring { helpers },
|_| Destructuring,
empty_obj_pat_1,
r#"expect(function () {
var {} = null;
@ -98,7 +98,7 @@ test_exec!(
);
// test!(::swc_ecma_parser::Syntax::default(),
// |_, helpers| tr(helpers),
// |_| tr(),
// empty_obj_pat_2,
// r#"var {} = null;"#,
// r#"var _ref = null;
@ -107,7 +107,7 @@ test_exec!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
empty,
r#"var [, a, [b], [c], d] = ["foo", "hello", [", ", "junk"], ["world"]];"#,
r#"var ref = ['foo', 'hello', [', ', 'junk'], ['world']], a = ref[1], ref1 = ref[2],
@ -118,7 +118,7 @@ test!(
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
es7_object_rest_builtins,
r#"var z = {};
var { ...x } = z;
@ -152,7 +152,7 @@ _o;"#
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
es7_object_rest,
r#"var z = {};
var { ...x } = z;
@ -186,7 +186,7 @@ _o;"#
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
export_variable,
r#"export let {a, b, c: {d, e: {f = 4}}} = {};"#,
r#"
@ -202,7 +202,7 @@ export { a, b, d, f };"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
for_in,
r#"for (var [name, value] in obj) {
print("Name: " + name + ", Value: " + value);
@ -216,7 +216,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
for_let,
r#"for (let [ i, n ] = range; ; ) {}"#,
r#"for(let i = range[0], n = range[1];;){}"#
@ -224,7 +224,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
for_of,
r#"for (var [ name, before, after ] of test.expectation.registers) {
@ -237,7 +237,7 @@ test!(
test_exec!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| Destructuring { helpers },
|_| Destructuring,
fn_key_with_obj_rest_spread,
r#"const { [(() => 1)()]: a, ...rest } = { 1: "a" };
@ -246,7 +246,7 @@ expect(rest).toEqual({});"#
);
test!(::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
babel_issue_3081,
r#"let list = [1, 2, 3, 4];
for (let i = 0, { length } = list; i < length; i++) {
@ -262,7 +262,7 @@ for(let i = 0, ref = list ? list : _throw(new TypeError("Cannot destructure 'und
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| Destructuring { helpers },
|_| Destructuring,
babel_issue_5090,
r#"const assign = function([...arr], index, value) {
arr[index] = value;
@ -277,7 +277,7 @@ expect(arr).toEqual([1, 2, 3]);"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
babel_issue_5628,
r#"
(function () {
@ -297,7 +297,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
babel_issue_5744,
r#"if (true) [a, b] = [b, a];"#,
r#"var ref;
@ -307,7 +307,7 @@ if (true) ref = [b, a], a = ref[0], b = ref[1], ref;"#
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
babel_issue_6373,
r#"import { NestedObjects } from "./some-module"
@ -322,7 +322,7 @@ const Foo = _someModule.NestedObjects.Foo,
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
known_array,
r#"var z = [];
var [x, ...y] = z;"#,
@ -333,7 +333,7 @@ var x = z[0],
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
member_expr,
r#"[foo.foo, foo.bar] = [1, 2];"#,
r#"var ref;
@ -342,7 +342,7 @@ ref = [1, 2], foo.foo = ref[0], foo.bar = ref[1], ref;"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
mixed,
r#"var rect = {};
var {topLeft: [x1, y1], bottomRight: [x2, y2] } = rect;"#,
@ -354,7 +354,7 @@ var ref = rect ? rect : _throw(new TypeError("Cannot destructure 'undefined' or
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
multiple,
r#"var coords = [1, 2];
var { x, y } = coords,
@ -367,7 +367,7 @@ var ref = coords ? coords : _throw(new TypeError("Cannot destructure 'undefined'
test_exec!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| Destructuring { helpers },
|_| Destructuring,
number_key_with_object_spread,
r#"const foo = {
1: "a",
@ -383,7 +383,7 @@ expect(rest).toEqual({ 2: "b", 3: "c" });"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
object_advanced,
r#"var rect = {};
var {topLeft: {x: x1, y: y1}, bottomRight: {x: x2, y: y2}} = rect;
@ -399,7 +399,7 @@ var ref3 = [0, 1, 2, 3, 4, 5, 6], foo = ref3[3], bar = ref3[5];"#
);
test!(::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
object_basic,
r#"var coords = [1, 2];
var { x, y } = coords;"#,
@ -410,7 +410,7 @@ var ref = coords ? coords : _throw(new TypeError("Cannot destructure 'undefined'
test_exec!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| Destructuring { helpers },
|_| Destructuring,
spread_generator,
r#"function* f() {
for (var i = 0; i < 3; i++) {
@ -423,7 +423,7 @@ expect(xs).toEqual([0, 1, 2]);"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
spread,
r#"function isSorted([x, y, ...wow]) {
if (!zs.length) return true

View File

@ -3,7 +3,7 @@ use super::*;
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, _| DuplicateKeys,
|_| DuplicateKeys,
combination_dupes,
r#"var x = { a: 5, a: 6 };"#,
r#"var x = _defineProperty({
@ -13,7 +13,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| DuplicateKeys,
|_| DuplicateKeys,
combination_no_dupes,
r#"var x = { a: 5, b: 6 };"#,
r#"var x = { a: 5, b: 6 };"#
@ -21,7 +21,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| DuplicateKeys,
|_| DuplicateKeys,
dup_keys_both_quoted,
r#"var x = { "a\n b": 5, "a\n b": 6 };"#,
r#"var x = {
@ -32,7 +32,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| DuplicateKeys,
|_| DuplicateKeys,
dup_keys_dupes,
r#"var x = { a: 5, a: 6 };"#,
r#"var x = {
@ -43,7 +43,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| DuplicateKeys,
|_| DuplicateKeys,
dup_keys_getter,
r#"var x = { a: 5, get a() {return 6;} };"#,
r#"var x = {
@ -58,7 +58,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| DuplicateKeys,
|_| DuplicateKeys,
dup_keys_getter_and_setter,
r#"var x = {
get a() {},
@ -109,7 +109,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| DuplicateKeys,
|_| DuplicateKeys,
dup_keys_one_quoted,
r#"var x = { a: 5, "a": 6 };"#,
r#"var x = {

View File

@ -2,7 +2,7 @@ use super::*;
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| ForOf,
|_| ForOf,
spec_identifier,
r#"for (i of arr) {
}"#,
@ -32,7 +32,7 @@ try {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| ForOf,
|_| ForOf,
spec_ignore_cases,
r#"for (var i of foo) {
switch (i) {
@ -71,7 +71,7 @@ try {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| ForOf,
|_| ForOf,
spec_let,
r#"for (let i of arr) {
@ -103,7 +103,7 @@ try {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| ForOf,
|_| ForOf,
spec_member_expr,
r#"for (obj.prop of arr) {
@ -134,7 +134,7 @@ try {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| ForOf,
|_| ForOf,
spec_multiple,
r#"for (var i of arr) {
@ -192,7 +192,7 @@ try {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| ForOf,
|_| ForOf,
spec_nested_label_for_of,
r#"b: for (let c of d()) {
for (let e of f()) {
@ -247,7 +247,7 @@ try {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| ForOf,
|_| ForOf,
spec_var,
r#"for (var i of arr) {

View File

@ -1,17 +1,13 @@
use super::*;
use crate::{
compat::es2015::{block_scoping, resolver},
helpers::Helpers,
};
use std::sync::Arc;
use crate::compat::es2015::{block_scoping, resolver};
fn tr(_helpers: Arc<Helpers>) -> impl Fold<Module> {
fn tr() -> impl Fold<Module> {
chain!(resolver(), function_name(), block_scoping())
}
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
basic,
r#"var number = function (x) {
return x;
@ -23,7 +19,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
assign,
r#"number = function (x) {
return x;
@ -35,7 +31,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
let_complex,
r#"
let TestClass = {
@ -65,7 +61,7 @@ var TestClass = {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
class_simple,
r#"
var Foo = function() {

View File

@ -1,6 +1,5 @@
use crate::{helpers::Helpers, util::ExprFactory};
use crate::util::ExprFactory;
use ast::*;
use std::sync::Arc;
use swc_common::{Fold, FoldWith, Visit, VisitWith};
/// `@babel/plugin-transform-instanceof`
@ -29,9 +28,7 @@ use swc_common::{Fold, FoldWith, Visit, VisitWith};
/// _instanceof(foo, Bar);
/// ```
#[derive(Clone)]
pub struct InstanceOf {
pub helpers: Arc<Helpers>,
}
pub struct InstanceOf;
impl Fold<Expr> for InstanceOf {
fn fold(&mut self, expr: Expr) -> Expr {
@ -64,7 +61,7 @@ impl Fold<Expr> for InstanceOf {
op: op!("instanceof"),
right,
}) => {
self.helpers.instanceof();
helper!(instanceof);
Expr::Call(CallExpr {
span,

View File

@ -6,9 +6,8 @@ pub use self::{
shorthand_property::Shorthand, spread::Spread, sticky_regex::StickyRegex,
template_literal::TemplateLiteral, typeof_symbol::TypeOfSymbol,
};
use crate::{helpers::Helpers, pass::Pass};
use crate::pass::Pass;
use ast::{Expr, Module, Stmt};
use std::sync::Arc;
mod arrow;
mod block_scoped_fn;
@ -29,49 +28,33 @@ mod template_literal;
mod typeof_symbol;
/// Compiles es2015 to es5.
pub fn es2015(helpers: &Arc<Helpers>) -> impl Pass + Clone {
fn exprs(helpers: &Arc<Helpers>) -> impl Pass + Clone {
pub fn es2015() -> impl Pass + Clone {
fn exprs() -> impl Pass + Clone {
chain_at!(
Expr,
arrow(),
duplicate_keys(),
Spread {
helpers: helpers.clone(),
},
Spread,
StickyRegex,
InstanceOf {
helpers: helpers.clone(),
},
TypeOfSymbol {
helpers: helpers.clone(),
},
TemplateLiteral {
helpers: helpers.clone(),
},
InstanceOf,
TypeOfSymbol,
TemplateLiteral,
Shorthand,
)
}
fn stmts(helpers: &Arc<Helpers>) -> impl Pass + Clone {
chain_at!(
Stmt,
function_name(),
exprs(helpers),
BlockScopedFns,
parameters(),
)
fn stmts() -> impl Pass + Clone {
chain_at!(Stmt, function_name(), exprs(), BlockScopedFns, parameters(),)
}
chain_at!(
Module,
resolver(),
Classes {
helpers: helpers.clone(),
},
stmts(helpers),
Classes,
stmts(),
for_of(),
computed_properties(helpers.clone()),
destructuring(helpers.clone()),
computed_properties(),
destructuring(),
block_scoping(),
)
}

View File

@ -1,19 +1,18 @@
use super::*;
use crate::{compat::es2015::Classes, helpers::Helpers};
use std::sync::Arc;
use crate::compat::es2015::Classes;
fn tr(helpers: Arc<Helpers>) -> impl Fold<Module> {
fn tr() -> impl Fold<Module> {
chain!(
crate::compat::es2015::resolver(),
Params,
crate::compat::es2015::destructuring(helpers.clone()),
crate::compat::es2015::destructuring(),
crate::compat::es2015::block_scoping(),
)
}
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
babel_6057_simple,
r#"const a = 'bar';
function foo(...a) {
@ -30,7 +29,7 @@ function foo() {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_before_last,
r#"function foo(a = "foo", b) {}"#,
r#"function foo(param, b) {
@ -40,7 +39,7 @@ test!(
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_destructuring,
r#"function required(msg) {
throw new Error(msg);
@ -65,7 +64,7 @@ expect(sum({arr:[1,2]})).toBe(3);"#
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_earlier_params,
r#"function f(a, b = a, c = b) { return c; }
@ -75,7 +74,7 @@ expect(3).toBe(f(3));"#
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_eval,
r#"let x = "outside";
function outer(a = () => eval("x")) {
@ -100,7 +99,7 @@ outer();"#
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_iife_1128,
r#"const bar = true;
@ -115,12 +114,7 @@ foo(1, 2, 3);"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| chain!(
Classes {
helpers: helpers.clone()
},
tr(helpers)
),
|_| chain!(Classes, tr()),
default_iife_4253,
r#"class Ref {
constructor(id = ++Ref.nextID) {
@ -140,7 +134,7 @@ test_exec!(
ignore,
::swc_ecma_parser::Syntax::default(),
// Stage0
|_, helpers| tr(helpers),
|_| tr(),
default_iife_4253_exec,
r#"class Ref {
static nextId = 0
@ -155,12 +149,7 @@ expect(new Ref().id).toBe(2);"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| chain!(
Classes {
helpers: helpers.clone()
},
tr(helpers)
),
|_| chain!(Classes, tr()),
default_iife_self,
r#"class Ref {
constructor(ref = Ref) {
@ -188,7 +177,7 @@ var X = function X(param) {
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_iife_self_exec,
r#"class Ref {
constructor(ref = Ref) {
@ -200,7 +189,7 @@ expect(new Ref().ref).toBe(Ref);"#
);
test!(::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_multiple,
r#"var t = function (e = "foo", f = 5) {
return e + " bar " + f;
@ -221,7 +210,7 @@ var a = function(e, param) {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_rest_mix,
r#"function fn(
a1,
@ -239,7 +228,7 @@ test!(
);
test!(::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_rest_1,
r#"const a = 1;
function rest(b = a, ...a) {
@ -258,7 +247,7 @@ rest(undefined, 2);"#
);
test!(::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_rest_2,
r#"const a = 1;
function rest2(b = a, ...a) {
@ -277,7 +266,7 @@ rest2(undefined, 2);"#
);
test!(::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_rest_3,
r#"const a = 1;
function rest3(b = a, ...a) {
@ -297,7 +286,7 @@ rest3(undefined, 2);"#
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_rest_exec,
r#"const a = 1;
function rest(b = a, ...a) {
@ -318,7 +307,7 @@ rest3(undefined, 2)"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_setter_noexec,
r#"const obj = {
set field(num = 1) {
@ -336,7 +325,7 @@ test!(
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_setter_exec,
r#"const defaultValue = 1;
const obj = {
@ -351,7 +340,7 @@ expect(obj.num).toBe(defaultValue);"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
default_single,
r#"var t = function (f = "foo") {
return f + " bar";
@ -364,7 +353,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
destructuring_rest,
r#"// #3861
function t(x = "default", { a, b }, ...args) {
@ -384,7 +373,7 @@ function t(param, param1) {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
regression_4333,
r#"const args = 'bar';
function foo(...args) {
@ -402,7 +391,7 @@ function foo() {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
regression_4348,
r#"function first(...values) {
var index = 0;
@ -421,7 +410,7 @@ test!(
ignore,
::swc_ecma_parser::Syntax::default(),
// type
|_, helpers| tr(helpers),
|_| tr(),
regression_4634,
r#"let oneOf = (...nodes) => {
if (nodes.length === 1) {
@ -444,7 +433,7 @@ let oneOf = function () {
);
test!(::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
regression_5787,
r#"function f(a, ...rest) {
let b = rest[rest.length - 3];
@ -475,7 +464,7 @@ function f(a) {
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
regression_5787_exec,
r#"function f1(a, ...rest) {
let b = rest[rest.length - 3];
@ -493,7 +482,7 @@ expect(f2(1, 2)).toBeUndefined();"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_args_deoptimiazation,
r#"function x (...rest) {
arguments;
@ -512,7 +501,7 @@ test!(
// Stage 0
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_arrow_fn,
r#"var concat = (...arrs) => {
var x = arrs[0];
@ -614,7 +603,7 @@ var innerclassproperties = function () {
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_async_arrow_fn,
r#"var concat = async (...arrs) => {
var x = arrs[0];
@ -659,7 +648,7 @@ function () {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| chain!(crate::compat::es2015::arrow(), tr(helpers)),
|_| chain!(crate::compat::es2015::arrow(), tr()),
rest_binding_deoptimisation,
r#"const deepAssign = (...args) => args = [];
"#,
@ -676,7 +665,7 @@ test!(
// optimiation is not implemented
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_deepest_common_ancestor_earliest_child,
r#"// single reference
function r(...rest){
@ -871,7 +860,7 @@ function r() {
);
test!(::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_length,
r#"var t = function (f, ...items) {
items[0];
@ -902,7 +891,7 @@ function t(f) {
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_length_exec,
r#"var length = function (a, b, ...items) {
return items.length;
@ -918,7 +907,7 @@ test!(
// optimisation is not implemented
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_member_expression_deoptimisation,
r#"var t = function (...items) {
var x = items[0];
@ -976,7 +965,7 @@ test!(
// optimisation is not implemented
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_member_expression_optimisation,
r#"var t = function (...items) {
var x = items[0];
@ -1031,7 +1020,7 @@ function t() {
);
test!(::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_multiple,
r#"var t = function (f, ...items) {
var x = f;
@ -1084,7 +1073,7 @@ test!(
// optimisation is not implemented
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_nested_5656,
r#"function a(...args) {
const foo = (...list) => bar(...list);
@ -1145,7 +1134,7 @@ function d(thing) {
);
test!(::swc_ecma_parser::Syntax::default(),
|_, helpers|chain!(Classes{helpers:helpers.clone()}, tr(helpers),
|_|chain!(Classes, tr(),
crate::compat::es2015::Spread::default()),
rest_nested_iife,
r#"function broken(x, ...foo) {
@ -1175,7 +1164,7 @@ test!(::swc_ecma_parser::Syntax::default(),
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_patterns,
r#"function foo(...[a]) {}"#,
r#"function foo() {
@ -1188,7 +1177,7 @@ test!(
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_patterns_exec,
r#"
function foo(...{ length }) {
@ -1202,7 +1191,7 @@ test!(
// optimisation is not implemented
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_spread_optimisation,
r#"// optimisation

View File

@ -9,7 +9,7 @@ macro_rules! identical {
($name:ident, $src:literal) => {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
$name,
$src,
$src
@ -57,7 +57,7 @@ fn test_mark_for() {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
basic_no_usage,
"
let foo;
@ -75,7 +75,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
class_nested_var,
"
var ConstructorScoping = function ConstructorScoping() {
@ -99,7 +99,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
basic,
r#"
{
@ -125,7 +125,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
general_assignment_patterns,
r#"const foo = "foo";
@ -147,7 +147,7 @@ function foobar() {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
general_function,
r#"function test() {
let foo = "bar";
@ -160,7 +160,7 @@ test!(
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
babel_issue_1051,
r#"foo.func1 = function() {
if (cond1) {
@ -202,7 +202,7 @@ test!(
ignore,
::swc_ecma_parser::Syntax::default(),
// TODO(kdy1): WTF is this (again)?
|_, _| tr(),
|_| tr(),
babel_issue_2174,
r#"if (true) {
function foo() {}
@ -226,7 +226,7 @@ if (true) {
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
babel_issue_4363,
r#"function WithoutCurlyBraces() {
if (true)
@ -295,7 +295,7 @@ test!(
// Cannot represent function expression without parens (in result code)
ignore,
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
babel_issue_4946,
r#"(function foo() {
let foo = true;
@ -309,7 +309,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
babel_issue_973,
r#"let arr = [];
for(let i = 0; i < 10; i++) {
@ -328,7 +328,7 @@ for(var i = 0; i < 10; i++){
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
pass_assignment,
r#"let a = 1;
a = 2;
@ -337,7 +337,7 @@ expect(a).toBe(2);"#
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
pass_call,
r#"let a = 1;
@ -350,7 +350,7 @@ expect(b()).toBe(2);"#
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
pass_update,
r#"let a = 1;
a++;
@ -359,7 +359,7 @@ expect(a).toBe(2);"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
fn_param,
r#"let a = 'foo';
function foo(a) {
@ -373,7 +373,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
fn_body,
r#"let a = 'foo';
function foo() {
@ -389,7 +389,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
shorthand,
r#"let a = 'foo';
function foo() {
@ -405,7 +405,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
same_level,
r#"
var a = 'foo';
@ -419,7 +419,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
class_block,
r#"
var Foo = function(_Bar) {
@ -441,7 +441,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
class_block_2,
r#"
var Foo = (function(_Bar) {
@ -463,7 +463,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
class_nested,
r#"
var Outer = function(_Hello) {

View File

@ -65,7 +65,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Shorthand,
|_| Shorthand,
babel_method_plain,
"var obj = {
method() {
@ -81,7 +81,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Shorthand,
|_| Shorthand,
babel_comments,
"var A = 'a';
var o = {
@ -96,7 +96,7 @@ var o = {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Shorthand,
|_| Shorthand,
babel_mixed,
"var coords = { x, y, foo: 'bar' };",
"var coords = {
@ -108,7 +108,7 @@ var o = {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Shorthand,
|_| Shorthand,
babel_multiple,
"var coords = { x, y };",
"var coords = {
@ -119,7 +119,7 @@ var o = {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Shorthand,
|_| Shorthand,
babel_single,
"var coords = { x };",
"var coords = {

View File

@ -1,13 +1,11 @@
use crate::{helpers::Helpers, util::ExprFactory};
use crate::util::ExprFactory;
use ast::*;
use std::{iter, mem, sync::Arc};
use std::{iter, mem};
use swc_common::{Fold, FoldWith, Span, DUMMY_SP};
/// es2015 - `SpreadElement`
#[derive(Default, Clone)]
pub struct Spread {
pub helpers: Arc<Helpers>,
}
pub struct Spread;
impl Fold<Expr> for Spread {
fn fold(&mut self, e: Expr) -> Expr {
@ -24,7 +22,7 @@ impl Fold<Expr> for Spread {
return Expr::Array(ArrayLit { span, elems });
}
let args_array = concat_args(&self.helpers, span, elems.into_iter());
let args_array = concat_args(span, elems.into_iter());
return args_array;
}
@ -45,7 +43,7 @@ impl Fold<Expr> for Spread {
type_args,
});
}
let args_array = concat_args(&self.helpers, span, args.into_iter().map(Some));
let args_array = concat_args(span, args.into_iter().map(Some));
//
// f.apply(undefined, args)
//
@ -74,7 +72,6 @@ impl Fold<Expr> for Spread {
}
let args = concat_args(
&self.helpers,
span,
iter::once(quote_expr!(span, null).as_arg())
.chain(args)
@ -101,11 +98,7 @@ impl Fold<Expr> for Spread {
}
}
fn concat_args(
helpers: &Helpers,
span: Span,
args: impl Iterator<Item = Option<ExprOrSpread>>,
) -> Expr {
fn concat_args(span: Span, args: impl Iterator<Item = Option<ExprOrSpread>>) -> Expr {
//
// []
//
@ -140,7 +133,7 @@ fn concat_args(
//
make_arr!();
helpers.to_consumable_array();
helper!(to_consumable_array);
buf.push(
Expr::Call(CallExpr {
@ -190,7 +183,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| Spread { helpers },
|_| Spread,
call,
"ca(a, b, c, ...d, e)",
"ca.apply(undefined, [a, b, c].concat(_toConsumableArray(d), [e]));"
@ -198,7 +191,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| Spread { helpers },
|_| Spread,
call_multi_spread,
"ca(a, b, ...d, e, f, ...h)",
"ca.apply(undefined, [a, b].concat(_toConsumableArray(d), [e, f], _toConsumableArray(h)));"
@ -206,7 +199,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| Spread { helpers },
|_| Spread,
call_noop,
"ca(a, b, c, d, e)",
"ca(a, b, c, d, e);"
@ -214,7 +207,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| Spread { helpers },
|_| Spread,
array,
"[a, b, c, ...d, e]",
"[a, b, c].concat(_toConsumableArray(d), [e])"
@ -222,7 +215,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| Spread { helpers },
|_| Spread,
array_empty,
"[a,, b, c, ...d,,, e]",
"[a,, b, c].concat(_toConsumableArray(d), [,, e])"
@ -230,7 +223,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| Spread { helpers },
|_| Spread,
new,
"new C(a, b, c, ...d, e)",
"new Function.prototype.bind.apply(C, [null, a, b, c].concat(_toConsumableArray(d), \
@ -240,7 +233,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| Spread { helpers },
|_| Spread,
new_noop,
"new C(a, b, c, c, d, e)",
"new C(a, b, c, c, d, e);"

View File

@ -59,7 +59,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| StickyRegex,
|_| StickyRegex,
babel_basic,
"var re = /o+/y;",
"var re = new RegExp('o+', 'y');"
@ -67,7 +67,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| StickyRegex,
|_| StickyRegex,
babel_ignore_non_sticky,
"var re = /o+/;",
"var re = /o+/;"

View File

@ -1,14 +1,12 @@
use crate::{helpers::Helpers, util::ExprFactory};
use crate::util::ExprFactory;
use ast::*;
use std::{iter, sync::Arc};
use std::iter;
use swc_common::{Fold, FoldWith, Spanned, DUMMY_SP};
#[cfg(test)]
mod tests;
#[derive(Default, Clone)]
pub struct TemplateLiteral {
pub helpers: Arc<Helpers>,
}
pub struct TemplateLiteral;
impl Fold<Expr> for TemplateLiteral {
fn fold(&mut self, e: Expr) -> Expr {
@ -68,7 +66,7 @@ impl Fold<Expr> for TemplateLiteral {
tag, exprs, quasis, ..
}) => {
assert!(quasis.len() == exprs.len() + 1);
self.helpers.tagged_template_literal();
helper!(tagged_template_literal);
let fn_ident = private_ident!("_templateObject");

View File

@ -2,7 +2,7 @@ use super::*;
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
escape_quotes,
r#"var t = `'${foo}' "${bar}"`;"#,
r#"var t = "'" + foo + '\' "' + bar + '"';"#,
@ -11,7 +11,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
expr_first,
r#"var foo = 5;
var bar = 10;
@ -35,7 +35,7 @@ var example5 = '' + '';"#,
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
functions,
r#"var foo = `test ${_.test(foo)} ${bar}`;"#,
r#"var foo = 'test ' + _.test(foo) + ' ' + bar;"#
@ -43,7 +43,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
literals,
r#"var foo = `${1}${f}oo${true}${b}ar${0}${baz}`;"#,
r#"var foo = '' + 1 + f + 'oo' + true + b + 'ar' + 0 + baz;"#
@ -51,7 +51,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
multiline,
r#"var o = `wow
this is
@ -62,7 +62,7 @@ actually multiline!`;"#,
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
multiple,
r#"var foo = `test ${foo} ${bar}`;"#,
r#"var foo = 'test ' + foo + ' ' + bar;"#
@ -70,7 +70,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
none,
r#"var foo = `test`;"#,
r#"var foo = "test";"#
@ -78,7 +78,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
only,
r#"var foo = `${test}`;"#,
r#"var foo = '' + test"#
@ -86,7 +86,7 @@ test!(
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
order,
r#"
const calls = [];
@ -115,7 +115,7 @@ expect(calls).toEqual([1, 2, 3, 4]);"#
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
order2,
r#"const calls = [];
@ -140,7 +140,7 @@ expect(calls).toEqual([1, 2]);"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
simple_tag,
r#"var foo = tag`wow`;
var bar = tag`first${1}second`;"#,
@ -163,7 +163,7 @@ var bar = tag(function _templateObject1() {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
single,
r#"var foo = `test ${foo}`;"#,
r#"var foo = 'test ' + foo;"#
@ -171,7 +171,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
statement,
r#"var foo = `test ${foo + bar}`;"#,
r#"var foo = 'test ' + foo + bar;"#,
@ -180,7 +180,7 @@ test!(
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
symbol,
r#"const fn = () => `${Symbol()}`;
@ -189,7 +189,7 @@ expect(fn).toThrow(TypeError);"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
tag,
r#"
var foo = bar`wow\na${ 42 }b ${_.foobar()}`;
@ -224,7 +224,7 @@ var bar = bar(function _templateObject2() {
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| TemplateLiteral { helpers },
|_| TemplateLiteral,
template_revision,
r#"tag`\unicode and \u{55}`;
tag`\01`;

View File

@ -1,6 +1,5 @@
use crate::{helpers::Helpers, util::ExprFactory};
use crate::util::ExprFactory;
use ast::*;
use std::sync::Arc;
use swc_common::{Fold, FoldWith, Visit, VisitWith};
/// `@babel/plugin-transform-typeof-symbol`
@ -21,9 +20,7 @@ use swc_common::{Fold, FoldWith, Visit, VisitWith};
/// _typeof(Symbol()) === "symbol";
/// ```
#[derive(Clone)]
pub struct TypeOfSymbol {
pub helpers: Arc<Helpers>,
}
pub struct TypeOfSymbol;
impl Fold<Expr> for TypeOfSymbol {
fn fold(&mut self, expr: Expr) -> Expr {
@ -55,7 +52,7 @@ impl Fold<Expr> for TypeOfSymbol {
op: op!("typeof"),
arg,
}) => {
self.helpers.type_of();
helper!(type_of);
return Expr::Call(CallExpr {
span,
callee: quote_ident!(span, "_typeof").as_callee(),

View File

@ -172,7 +172,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Exponentation,
|_| Exponentation,
babel_binary,
"2 ** 2",
"Math.pow(2, 2)"
@ -181,7 +181,7 @@ mod tests {
test_exec!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, _| Exponentation,
|_| Exponentation,
babel_comprehensive,
r#"expect(2 ** 3).toBe(8);
expect(3 * 2 ** 3).toBe(24);
@ -210,7 +210,7 @@ expect(2 ** 3 ** 2).toBe(512);"#
// FIXME
ignore,
::swc_ecma_parser::Syntax::default(),
|_, _| Exponentation,
|_| Exponentation,
babel_memoize_object,
r#"var counters = 0;
Object.defineProperty(global, "reader", {
@ -226,7 +226,7 @@ expect(counters).toBe(1);"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| Exponentation,
|_| Exponentation,
assign,
r#"x **= 3"#,
r#"x = Math.pow(x, 3)"#,
@ -234,7 +234,7 @@ expect(counters).toBe(1);"#
);
// test!(::swc_ecma_parser::Syntax::default(),
// |_, _| Exponentation,
// |_| Exponentation,
// babel_4403,
// "var a, b;
// a[`${b++}`] **= 1;",

View File

@ -1,10 +1,9 @@
use crate::{
helpers::Helpers,
pass::Pass,
util::{contains_this_expr, ExprFactory, StmtLike},
};
use ast::*;
use std::{iter, sync::Arc};
use std::iter;
use swc_common::{Fold, FoldWith, Mark, Spanned, Visit, VisitWith, DUMMY_SP};
#[cfg(test)]
@ -30,16 +29,13 @@ mod tests;
/// yield bar();
/// });
/// ```
pub fn async_to_generator(helpers: Arc<Helpers>) -> impl Pass + Clone {
AsyncToGenerator { helpers }
pub fn async_to_generator() -> impl Pass + Clone {
AsyncToGenerator
}
#[derive(Default, Clone)]
struct AsyncToGenerator {
helpers: Arc<Helpers>,
}
struct AsyncToGenerator;
struct Actual {
helpers: Arc<Helpers>,
extra_stmts: Vec<Stmt>,
}
@ -61,7 +57,6 @@ where
Err(module_item) => buf.push(module_item),
Ok(stmt) => {
let mut actual = Actual {
helpers: self.helpers.clone(),
extra_stmts: vec![],
};
let stmt = stmt.fold_with(&mut actual);
@ -84,16 +79,13 @@ impl Fold<MethodProp> for Actual {
}
let params = prop.function.params;
let fn_ref = make_fn_ref(
&self.helpers,
FnExpr {
ident: None,
function: Function {
params: vec![],
..prop.function
},
let fn_ref = make_fn_ref(FnExpr {
ident: None,
function: Function {
params: vec![],
..prop.function
},
);
});
let fn_ref = Expr::Call(CallExpr {
span: DUMMY_SP,
callee: fn_ref.as_callee(),
@ -348,13 +340,10 @@ impl Fold<Method> for Actual {
let mut folder = MethodFolder { vars: vec![] };
let function = m.function.fold_children(&mut folder);
let expr = make_fn_ref(
&self.helpers,
FnExpr {
ident: None,
function,
},
);
let expr = make_fn_ref(FnExpr {
ident: None,
function,
});
let hoisted_super = if folder.vars.is_empty() {
None
@ -416,7 +405,7 @@ impl Fold<Expr> for Actual {
});
}
return make_fn_ref(&self.helpers, fn_expr);
return make_fn_ref(fn_expr);
}
_ => {}
}
@ -488,13 +477,10 @@ impl Actual {
let ident = raw_ident.clone().unwrap_or_else(|| quote_ident!("ref"));
let real_fn_ident = private_ident!(ident.span, format!("_{}", ident.sym));
let right = make_fn_ref(
&self.helpers,
FnExpr {
ident: None,
function: f,
},
);
let right = make_fn_ref(FnExpr {
ident: None,
function: f,
});
if is_decl {
let real_fn = FnDecl {
@ -593,7 +579,7 @@ impl Actual {
/// Creates
///
/// `_asyncToGenerator(function*() {})` from `async function() {}`;
fn make_fn_ref(helpers: &Helpers, mut expr: FnExpr) -> Expr {
fn make_fn_ref(mut expr: FnExpr) -> Expr {
struct AwaitToYield;
impl Fold<Function> for AwaitToYield {
@ -625,7 +611,7 @@ fn make_fn_ref(helpers: &Helpers, mut expr: FnExpr) -> Expr {
expr.function.is_generator = true;
let span = expr.span();
helpers.async_to_generator();
helper!(async_to_generator);
let contains_this = contains_this_expr(&expr.function.body);
let expr = if contains_this {

View File

@ -15,23 +15,21 @@ impl Fold<Expr> for ParenRemover {
}
}
fn tr(helpers: Arc<Helpers>) -> impl Fold<Module> {
fn tr() -> impl Fold<Module> {
chain!(
ParenRemover,
arrow(),
parameters(),
destructuring(helpers.clone()),
destructuring(),
function_name(),
AsyncToGenerator {
helpers: helpers.clone()
},
AsyncToGenerator {},
fixer()
)
}
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
async_arrow_in_method,
r#"
let TestClass = {
@ -64,7 +62,7 @@ let TestClass = {
);
test!(::swc_ecma_parser::Syntax::default(),
|_,helpers|tr(helpers),
|_|tr(),
async_default_arguments,
r#"
function mandatory(paramName) {
@ -94,7 +92,7 @@ function foo(param) {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
async_iife,
r#"
(async function() { await 'ok' })();
@ -133,7 +131,7 @@ _asyncToGenerator(function*() {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
async,
r#"
class Foo {
@ -155,7 +153,7 @@ class Foo {
);
test!(::swc_ecma_parser::Syntax::default(),
|_,helpers|tr(helpers),
|_|tr(),
deeply_nested_asyncs,
r#"
async function s(x, ...args) {
@ -217,7 +215,7 @@ function s(x) {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
expression,
r#"
var foo = async function () {
@ -263,7 +261,7 @@ var foo2 = function () {
);
test!(::swc_ecma_parser::Syntax::default(),
|_,helpers|tr(helpers),
|_|tr(),
function_arity,
r#"
async function one(a, b = 1) {}
@ -339,7 +337,7 @@ function six(a, param) {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
named_expression,
r#"
var foo = async function bar() {
@ -360,7 +358,7 @@ var foo = function() {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
no_parameters_and_no_id,
r#"
foo(async function () {
@ -378,7 +376,7 @@ foo(function() {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
object_method_with_arrows,
r#"
class Class {
@ -453,7 +451,7 @@ class Class{
);
test!(::swc_ecma_parser::Syntax::default(),
|_,helpers|tr(helpers),
|_|tr(),
object_method_with_super,
r#"class Foo extends class {} {
async method() {
@ -480,7 +478,7 @@ class Foo extends class{
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
class_method_this,
r#"
class Foo {
@ -499,7 +497,7 @@ return foo.foo().then(cur => {
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
class_method_super,
r#"
class Foo {
@ -523,7 +521,7 @@ return bar.bar().then(cur => {
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
class_getter_super,
r#"
let called = false;
@ -549,7 +547,7 @@ return bar.bar().then(foo => {
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
class_setter_super,
r#"
let called = false;
@ -576,7 +574,7 @@ return bar.bar().then(bar => {
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
class_method_this_complex,
r#"
class Class {
@ -609,7 +607,7 @@ return c.method();
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
object_method,
r#"
let obj = {
@ -633,7 +631,7 @@ let obj = {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
babel_parameters,
r#"
async function foo(bar) {
@ -653,7 +651,7 @@ function foo(bar) {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
statement,
r#"
async function foo() {

View File

@ -1,9 +1,8 @@
pub use self::async_to_generator::async_to_generator;
use crate::{helpers::Helpers, pass::Pass};
use std::sync::Arc;
use crate::pass::Pass;
mod async_to_generator;
pub fn es2017(helpers: &Arc<Helpers>) -> impl Pass + Clone {
async_to_generator(helpers.clone())
pub fn es2017() -> impl Pass + Clone {
async_to_generator()
}

View File

@ -1,10 +1,9 @@
pub use self::object_rest_spread::object_rest_spread;
use crate::{helpers::Helpers, pass::Pass};
use std::sync::Arc;
use crate::pass::Pass;
mod object_rest_spread;
pub fn es2018(helpers: &Arc<Helpers>) -> impl Pass + Clone {
object_rest_spread(helpers.clone())
pub fn es2018() -> impl Pass + Clone {
object_rest_spread()
}

View File

@ -1,10 +1,9 @@
use crate::{
helpers::Helpers,
pass::Pass,
util::{alias_ident_for, ExprFactory, StmtLike},
};
use ast::*;
use std::{iter, mem, sync::Arc};
use std::{iter, mem};
use swc_common::{
util::move_map::MoveMap, Fold, FoldWith, Mark, Spanned, Visit, VisitWith, DUMMY_SP,
};
@ -13,22 +12,14 @@ use swc_common::{
mod tests;
/// `@babel/plugin-proposal-object-rest-spread`
pub fn object_rest_spread(helpers: Arc<Helpers>) -> impl Pass + Clone {
chain!(
ObjectRest {
helpers: helpers.clone(),
},
ObjectSpread { helpers }
)
pub fn object_rest_spread() -> impl Pass + Clone {
chain!(ObjectRest, ObjectSpread)
}
#[derive(Clone)]
struct ObjectRest {
helpers: Arc<Helpers>,
}
struct ObjectRest;
struct RestFolder {
helpers: Arc<Helpers>,
/// Injected before the original statement.
vars: Vec<VarDeclarator>,
/// Variables which should ceclaraed using `var`
@ -376,7 +367,6 @@ where
for stmt in stmts {
let mut folder = RestFolder {
helpers: self.helpers.clone(),
vars: vec![],
mutable_vars: vec![],
exprs: vec![],
@ -761,18 +751,14 @@ impl RestFolder {
span: DUMMY_SP,
left: PatOrExpr::Pat(last.arg),
op: op!("="),
right: box object_without_properties(&self.helpers, obj.clone(), excluded_props),
right: box object_without_properties(obj.clone(), excluded_props),
}));
} else {
// println!("Var: rest = objectWithoutProperties()",);
self.push_var_if_not_empty(VarDeclarator {
span: DUMMY_SP,
name: *last.arg,
init: Some(box object_without_properties(
&self.helpers,
obj.clone(),
excluded_props,
)),
init: Some(box object_without_properties(obj.clone(), excluded_props)),
definite: false,
});
}
@ -785,13 +771,9 @@ impl RestFolder {
}
}
fn object_without_properties(
helpers: &Helpers,
obj: Box<Expr>,
excluded_props: Vec<Option<ExprOrSpread>>,
) -> Expr {
fn object_without_properties(obj: Box<Expr>, excluded_props: Vec<Option<ExprOrSpread>>) -> Expr {
if excluded_props.is_empty() {
helpers.extends();
helper!(extends);
return Expr::Call(CallExpr {
span: DUMMY_SP,
@ -808,7 +790,7 @@ fn object_without_properties(
});
}
helpers.object_without_properties();
helper!(object_without_properties);
Expr::Call(CallExpr {
span: DUMMY_SP,
@ -888,9 +870,7 @@ fn simplify_pat(pat: Pat) -> Pat {
}
#[derive(Clone)]
struct ObjectSpread {
helpers: Arc<Helpers>,
}
struct ObjectSpread;
impl Fold<Expr> for ObjectSpread {
fn fold(&mut self, expr: Expr) -> Expr {
@ -952,8 +932,8 @@ impl Fold<Expr> for ObjectSpread {
buf
};
self.helpers.define_property();
self.helpers.object_spread();
helper!(define_property);
helper!(object_spread);
Expr::Call(CallExpr {
span,
callee: quote_ident!("_objectSpread").as_callee(),

View File

@ -1,16 +1,15 @@
use super::object_rest_spread;
use crate::{compat::es2015::Spread, helpers::Helpers};
use crate::compat::es2015::Spread;
use ast::Module;
use std::sync::Arc;
use swc_common::Fold;
fn tr(helpers: Arc<Helpers>) -> impl Fold<Module> {
object_rest_spread(helpers.clone())
fn tr() -> impl Fold<Module> {
object_rest_spread()
}
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_function_array,
r#"
function foo([{...bar}]) {
@ -26,7 +25,7 @@ function foo([_param]) {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_var_basic,
r#"
var { a , ...b } = _ref;
@ -38,7 +37,7 @@ var { a } = _ref, b = _objectWithoutProperties(_ref, ['a']);
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_assignment_exec,
r#"
let foo = {
@ -54,7 +53,7 @@ expect(c).toEqual({b: 2});
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_catch_exec,
r#"
try {
@ -76,7 +75,7 @@ try {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_assignment_expression,
r#"({ a1 } = c1);
({ a2, ...b2 } = c2);
@ -93,7 +92,7 @@ console.log(( _c3 = c3, b3 = _objectWithoutProperties(_c3, ['a3']), { a3 } = _c
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_catch_clause,
r#"
try {} catch({ ...a34 }) {}
@ -135,7 +134,7 @@ try{
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_export,
r#"
// ExportNamedDeclaration
@ -162,12 +161,7 @@ export var [dd, ee] = ads;
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| chain!(
tr(helpers.clone()),
Spread {
helpers: helpers.clone()
}
),
|_| chain!(tr(), Spread {}),
rest_for_x,
r#"
// ForXStatement
@ -243,7 +237,7 @@ async function a() {
test_exec!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_impure_computed_exec,
r#"
var key, x, y, z;
@ -273,7 +267,7 @@ expect(z).toBe("zee");
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_impure_computed,
r#"
var key, x, y, z;
@ -354,7 +348,7 @@ expect(z).toBe("zee");"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_nested_2,
r#"
const test = {
@ -403,7 +397,7 @@ const {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_nested_computed_key,
r#"
const {
@ -430,7 +424,7 @@ const _ref = {
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_nested_default_value_exec,
r#"
const {
@ -448,7 +442,7 @@ expect(d).toEqual({})
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_nested_default_value,
r#"
const {
@ -475,7 +469,7 @@ const _ref = {
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_nested_order_exec,
r#"
var result = "";
@ -502,20 +496,22 @@ expect(result).toBe("barbazfoo");
"#
);
test!(::swc_ecma_parser::Syntax::default(),
|_,helpers|tr(helpers),
rest_nested_order,
r#"
test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
rest_nested_order,
r#"
const { a: { ...bar }, b: { ...baz }, ...foo } = obj;
"#,
r#"
const bar = _extends({}, obj.a), baz = _extends({}, obj.b), foo = _objectWithoutProperties(obj, ['a', 'b']);
r#"
const bar = _extends({}, obj.a), baz = _extends({}, obj.b), foo =
_objectWithoutProperties(obj, ['a', 'b']);
"#
);
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_nested_1,
r#"
const defunct = {
@ -552,7 +548,7 @@ const {
test_exec!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_non_string_computed_exec,
r#"
const a = {
@ -615,7 +611,7 @@ expect(dy).toBe("sy");
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_non_string_computed,
r#"
const a = {
@ -729,7 +725,7 @@ expect(dy).toBe("sy");"#
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_parameters,
r#"
function a({ ...a34 }) {}
@ -824,7 +820,7 @@ function b3({
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_symbol_exec,
r#"
const sym = Symbol("test");
@ -852,7 +848,7 @@ expect(Object.getOwnPropertySymbols(noSym)).toEqual([]);"#
test!(
ignore,
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_symbol,
r#"
let {
@ -893,7 +889,7 @@ if (_ref3 = {}, _Symbol$for3 = Symbol.for("foo"), ({
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_variable_destructuring_1,
r#"
var z = {};
@ -948,7 +944,7 @@ const {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_variable_destructuring_2,
r#"
let {
@ -972,7 +968,7 @@ let {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_variable_destructuring_3,
r#"
let { x4: { ...y4 } } = z;
@ -984,7 +980,7 @@ let y4 = _extends({}, z.x4);
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_with_array_rest_exec,
r#"
let [{ a, ...foo}, ...bar] = [{ a: 1, b:2 }, 2, 3, 4];
@ -996,7 +992,7 @@ expect(bar).toEqual([2, 3, 4]);
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_with_array_rest_exec_2,
r#"
let {
@ -1016,7 +1012,7 @@ expect(objectRest).toEqual({d: 'oyez'})
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
rest_with_array_rest,
r#"
let {
@ -1038,7 +1034,7 @@ let _ref = {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
spread_assignment,
r#"
z = { x, ...y };
@ -1058,7 +1054,7 @@ z = {
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
spread_expression,
r#"
({ x, ...y, a, ...b, c });
@ -1093,8 +1089,9 @@ _objectSpread({}, {
"#
);
test_exec!(::swc_ecma_parser::Syntax::default(),
|_,helpers|tr(helpers),
test_exec!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
spread_no_object_assign_exec,
r#"
Object.defineProperty(Object.prototype, 'NOSET', {
@ -1115,7 +1112,8 @@ const objSpread = { ...obj };
const obj2 = { NOSET: 123, NOWRITE: 456 };
// this line would throw `TypeError: Cannot assign to read only property 'NOWRITE'`
// if transformed as Object.assign (or equivalent) because those use *assignment* for creating properties
// if transformed as Object.assign (or equivalent) because
// those use *assignment* for creating properties
// (spread defines them)
const obj2Spread = { ...obj2 };
@ -1142,7 +1140,7 @@ expect(Array.isArray(Object.getPrototypeOf(o2))).toBe(false);
test!(
::swc_ecma_parser::Syntax::default(),
|_, helpers| tr(helpers),
|_| tr(),
spread_variable_declaration,
r#"var z = { ...x };"#,
r#"var z = _objectSpread({}, x);"#

View File

@ -61,7 +61,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| MemberExprLit,
|_| MemberExprLit,
basic,
r#"obj["foo"] = "isValid";

View File

@ -72,7 +72,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| PropertyLiteral,
|_| PropertyLiteral,
babel_basic,
r#"var foo = {
// changed

View File

@ -91,7 +91,7 @@ mod tests {
($name:ident, $src:literal) => {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| ReservedWord,
|_| ReservedWord,
$name,
$src,
$src
@ -101,7 +101,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| ReservedWord,
|_| ReservedWord,
babel_issue_6477,
r#"
function utf8CheckByte(byte) {

View File

@ -371,7 +371,7 @@ mod tests {
macro_rules! test_fixer {
($name:ident, $from:literal, $to:literal) => {
// We use noop because fixer is invoked by tests::apply_transform.
test!(Default::default(), |_, _| Noop, $name, $from, $to);
test!(Default::default(), |_| Noop, $name, $from, $to);
};
}

View File

@ -1,8 +1,6 @@
use ast::*;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use scoped_tls::scoped_thread_local;
use std::sync::atomic::{AtomicBool, Ordering};
use swc_common::{
errors::{ColorConfig, Handler},
sync::Lrc,
@ -17,8 +15,15 @@ lazy_static! {
static ref SESSION: Session<'static> = { Session { handler: &*HANDLER } };
}
#[macro_export]
macro_rules! helper {
($i:ident) => {{
$crate::helpers::HELPERS.with(|helpers| helpers.$i());
}};
}
macro_rules! add_to {
($buf:expr, $name:tt, $b:expr) => {{
($buf:expr, $name:ident, $b:expr) => {{
lazy_static! {
static ref STMTS: Vec<Stmt> = {
let code = include_str!(concat!("_", stringify!($name), ".js"));
@ -44,22 +49,30 @@ macro_rules! add_to {
}};
}
scoped_thread_local!(pub static HELPERS: Helpers);
/// Tracks used helper methods. (e.g. __extends)
#[derive(Default)]
pub struct Helpers {
inner: Inner,
}
macro_rules! define_helpers {
(
Helpers {
$( $name:ident : ( $( $dep:ident ),* ), )*
}
) => {
/// Tracks used helper methods. (e.g. __extends)
#[derive(Default)]
pub struct Helpers {
struct Inner {
$( $name: AtomicBool, )*
}
impl Helpers {
$(
pub fn $name(&self) {
self.$name.store(true, Ordering::Relaxed);
self.inner.$name.store(true, Ordering::Relaxed);
$(
self.$dep();
)*
@ -71,34 +84,18 @@ macro_rules! define_helpers {
fn mk_helpers(&self) -> Vec<Stmt>{
let mut buf = vec![];
$(
add_to!(buf, $name, self.helpers.$name);
)*
HELPERS.with(|helpers|{
$(
add_to!(buf, $name, helpers.inner.$name);
)*
});
buf
}
}
impl Fold<Module> for HelperResetter {
fn fold(&mut self, module:Module)->Module{
$(
self.helpers.$name.store(false, Ordering::Relaxed);
)*
module
}
}
};
}
/// Disables all helpers.
/// Used to reset list of injected helpers.
#[derive(Clone)]
pub struct HelperResetter {
pub helpers: Arc<Helpers>,
}
define_helpers!(Helpers {
apply_decorated_descriptor: (),
array_with_holes: (),
@ -181,7 +178,6 @@ define_helpers!(Helpers {
#[derive(Clone)]
pub struct InjectHelpers {
pub cm: Lrc<SourceMap>,
pub helpers: Arc<Helpers>,
}
impl Fold<Module> for InjectHelpers {

View File

@ -114,7 +114,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|tester, _| InlineGlobals {
|tester| InlineGlobals {
envs: envs(tester, &[("NODE_ENV", "development")]),
globals: globals(tester, &[]),
},
@ -125,7 +125,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|tester, _| InlineGlobals {
|tester| InlineGlobals {
envs: envs(tester, &[]),
globals: globals(tester, &[("__DEBUG__", "true")]),
},
@ -136,7 +136,7 @@ mod tests {
test!(
::swc_ecma_parser::Syntax::default(),
|tester, _| InlineGlobals {
|tester| InlineGlobals {
envs: envs(tester, &[]),
globals: globals(tester, &[("debug", "true")]),
},

View File

@ -18,6 +18,7 @@ extern crate fxhash;
extern crate indexmap;
extern crate inflector;
extern crate ordered_float;
extern crate scoped_tls;
extern crate swc_ecma_ast as ast;
#[cfg(test)]
extern crate swc_ecma_codegen;
@ -46,6 +47,8 @@ pub use self::{
#[macro_use]
mod tests;
#[macro_use]
pub mod helpers;
#[macro_use]
mod quote;
#[macro_use]
mod macros;
@ -53,7 +56,6 @@ mod macros;
mod hygiene;
pub mod compat;
mod fixer;
pub mod helpers;
mod inline_globals;
pub mod modules;
pub mod pass;

View File

@ -3,14 +3,13 @@ use super::util::{
use_strict, Scope, VarCollector,
};
use crate::{
helpers::Helpers,
pass::Pass,
util::{undefined, DestructuringFinder, ExprFactory, State},
};
use ast::*;
use fxhash::FxHashSet;
use serde::{Deserialize, Serialize};
use std::{collections::hash_map::Entry, iter, sync::Arc};
use std::{collections::hash_map::Entry, iter};
use swc_atoms::JsWord;
use swc_common::{Fold, FoldWith, VisitWith, DUMMY_SP};
@ -67,9 +66,8 @@ impl Default for Lazy {
}
}
pub fn common_js(helpers: Arc<Helpers>, config: Config) -> impl Pass + Clone {
pub fn common_js(config: Config) -> impl Pass + Clone {
CommonJs {
helpers,
config,
scope: Default::default(),
in_top_level: Default::default(),
@ -78,7 +76,6 @@ pub fn common_js(helpers: Arc<Helpers>, config: Config) -> impl Pass + Clone {
#[derive(Clone)]
struct CommonJs {
helpers: Arc<Helpers>,
config: Config,
scope: State<Scope>,
in_top_level: State<bool>,
@ -522,7 +519,7 @@ impl Fold<Vec<ModuleItem>> for CommonJs {
let rhs = match ty {
Some(true) if !self.config.no_interop => {
self.helpers.interop_require_wildcard();
helper!(interop_require_wildcard);
box Expr::Call(CallExpr {
span: DUMMY_SP,
callee: quote_ident!("_interopRequireWildcard").as_callee(),
@ -531,7 +528,7 @@ impl Fold<Vec<ModuleItem>> for CommonJs {
})
}
Some(false) if !self.config.no_interop => {
self.helpers.interop_require_default();
helper!(interop_require_default);
box Expr::Call(CallExpr {
span: DUMMY_SP,
callee: quote_ident!("_interopRequireDefault").as_callee(),

File diff suppressed because it is too large Load Diff

View File

@ -5,24 +5,23 @@ use super::util::{
make_descriptor, make_require_call, use_strict, Scope, VarCollector,
};
use crate::{
helpers::Helpers,
pass::Pass,
util::{prepend_stmts, DestructuringFinder, ExprFactory, State},
};
use ast::*;
use fxhash::FxHashSet;
use std::{collections::hash_map::Entry, iter, sync::Arc};
use std::{collections::hash_map::Entry, iter};
use swc_common::{sync::Lrc, Fold, FoldWith, Mark, SourceMap, VisitWith, DUMMY_SP};
mod config;
#[cfg(test)]
mod tests;
pub fn umd(cm: Lrc<SourceMap>, helpers: Arc<Helpers>, config: Config) -> impl Pass + Clone {
pub fn umd(cm: Lrc<SourceMap>, config: Config) -> impl Pass + Clone {
Umd {
config: config.build(cm.clone()),
cm,
helpers,
scope: Default::default(),
exports: Default::default(),
}
@ -32,7 +31,6 @@ pub fn umd(cm: Lrc<SourceMap>, helpers: Arc<Helpers>, config: Config) -> impl Pa
struct Umd {
cm: Lrc<SourceMap>,
config: BuiltConfig,
helpers: Arc<Helpers>,
scope: State<Scope>,
exports: State<Exports>,
}
@ -443,9 +441,9 @@ impl Fold<Module> for Umd {
match ty {
Some(&wildcard) => {
if wildcard {
self.helpers.interop_require_wildcard();
helper!(interop_require_wildcard);
} else {
self.helpers.interop_require_default();
helper!(interop_require_default);
}
let right = box Expr::Call(CallExpr {
span: DUMMY_SP,

View File

@ -5,20 +5,15 @@ fn syntax() -> ::swc_ecma_parser::Syntax {
Default::default()
}
fn tr(
tester: &mut crate::tests::Tester,
helpers: Arc<Helpers>,
config: Config,
) -> impl Fold<Module> {
chain!(resolver(), umd(tester.cm.clone(), helpers, config))
fn tr(tester: &mut crate::tests::Tester, config: Config) -> impl Fold<Module> {
chain!(resolver(), umd(tester.cm.clone(), config))
}
// exports_variable
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -90,9 +85,8 @@ export class foo9 {}
// export_named
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -133,9 +127,8 @@ export {foo};
// export_default_11
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -184,9 +177,8 @@ export function Cachier(databaseName) {}
// export_from_4
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -231,9 +223,8 @@ export {foo as bar} from "foo";
// export_default_3
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -277,9 +268,8 @@ export default [];
// imports_default
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -320,9 +310,8 @@ foo2;
// export_named_3
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -364,9 +353,8 @@ export {foo as default, bar};
// imports_glob
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -404,9 +392,8 @@ foo;
// export_default_6
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -448,9 +435,8 @@ export default class {}
// export_default_5
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -489,9 +475,8 @@ export default function () {}
// hoist_function_exports
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -552,9 +537,8 @@ export var isOdd = (function (isEven) {
// export_from_2
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -599,9 +583,8 @@ export {foo as default} from "foo";
// export_default_8
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -647,9 +630,8 @@ export default class Foo {}
// export_named_5
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -691,9 +673,8 @@ export {foo, bar};
// imports_exact_globals_false
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -736,9 +717,8 @@ import fizzBuzz from "fizzbuzz";
// export_default_10
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -782,9 +762,8 @@ export default (function(){return "foo"})();
// export_from
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -827,9 +806,8 @@ export {foo} from "foo";
// export_from_5
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -880,9 +858,8 @@ export {foo, bar} from "foo";
// export_default_2
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -922,9 +899,8 @@ export default {};
// imports_named
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -974,9 +950,8 @@ xyz;
// imports_mixing
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -1013,9 +988,8 @@ xyz;
// remap
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -1096,9 +1070,8 @@ d = 4;
// export_named_2
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -1139,9 +1112,8 @@ export {foo as default};
// export_default_7
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -1183,9 +1155,8 @@ export default function foo () {}
// non_default_imports
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -1218,9 +1189,8 @@ import { render } from "./lib/render";
// imports
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -1255,9 +1225,8 @@ import "./directory/foo-bar";
// export_default
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -1297,9 +1266,8 @@ export default 42;
// export_default_4
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -1339,9 +1307,8 @@ export default foo;
// export_from_3
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -1390,9 +1357,8 @@ export {foo as default, bar} from "foo";
// export_default_9
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -1434,9 +1400,8 @@ export { foo as default };
// overview
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -1499,9 +1464,8 @@ bar2;
// export_named_4
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}
@ -1542,9 +1506,8 @@ export {foo as bar};
// export_from_6
test!(
syntax(),
|tester, helpers| tr(
|tester| tr(
tester,
helpers,
Config {
..Default::default()
}

View File

@ -1,9 +1,8 @@
use crate::{helpers::Helpers, util::ExprFactory};
use crate::util::ExprFactory;
use ast::*;
use swc_common::{Fold, FoldWith, DUMMY_SP};
pub(super) struct ClassNameTdzFolder<'a> {
pub helpers: &'a Helpers,
pub class_name: &'a Ident,
}
@ -14,7 +13,7 @@ impl<'a> Fold<Expr> for ClassNameTdzFolder<'a> {
//
if i.sym == self.class_name.sym {
self.helpers.class_name_tdz_error();
helper!(class_name_tdz_error);
return Expr::Seq(SeqExpr {
span: DUMMY_SP,

View File

@ -4,7 +4,6 @@ use self::{
used_name::{UsedNameCollector, UsedNameRenamer},
};
use crate::{
helpers::Helpers,
pass::Pass,
util::{
alias_ident_for, constructor::inject_after_super, default_constructor, undefined,
@ -13,7 +12,6 @@ use crate::{
};
use ast::*;
use fxhash::FxHashSet;
use std::sync::Arc;
use swc_atoms::JsWord;
use swc_common::{Fold, FoldWith, Mark, Spanned, VisitWith, DUMMY_SP};
@ -30,16 +28,12 @@ mod used_name;
/// # Impl note
///
/// We use custom helper to handle export defaul class
pub fn class_properties(helpers: Arc<Helpers>) -> impl Pass + Clone {
ClassProperties {
helpers,
mark: Mark::root(),
}
pub fn class_properties() -> impl Pass + Clone {
ClassProperties { mark: Mark::root() }
}
#[derive(Clone)]
struct ClassProperties {
helpers: Arc<Helpers>,
mark: Mark,
}
@ -270,10 +264,8 @@ impl ClassProperties {
// we handle computed key here to preserve the execution order
let key = match method.key {
PropName::Computed(expr) => {
let expr = expr.fold_with(&mut ClassNameTdzFolder {
helpers: &self.helpers,
class_name: &ident,
});
let expr =
expr.fold_with(&mut ClassNameTdzFolder { class_name: &ident });
let ident = private_ident!("tmp");
// Handle computed property
vars.push(VarDeclarator {
@ -293,10 +285,9 @@ impl ClassProperties {
ClassMember::ClassProp(mut prop) => {
let prop_span = prop.span();
prop.key = prop.key.fold_with(&mut ClassNameTdzFolder {
helpers: &self.helpers,
class_name: &ident,
});
prop.key = prop
.key
.fold_with(&mut ClassNameTdzFolder { class_name: &ident });
let key = match *prop.key {
Expr::Ident(ref i) if !prop.computed => Lit::Str(Str {
@ -325,7 +316,7 @@ impl ClassProperties {
});
let value = prop.value.unwrap_or_else(|| undefined(prop_span)).as_arg();
self.helpers.define_property();
helper!(define_property);
let callee = quote_ident!(DUMMY_SP, "_defineProperty").as_callee();
if prop.is_static {
@ -440,7 +431,6 @@ impl ClassProperties {
statics: &statics,
vars: vec![],
class_name: &ident,
helpers: &self.helpers,
});
(

View File

@ -1,7 +1,4 @@
use crate::{
helpers::Helpers,
util::{alias_ident_for, prepend, ExprFactory},
};
use crate::util::{alias_ident_for, prepend, ExprFactory};
use ast::*;
use fxhash::FxHashSet;
use std::{iter, mem};
@ -13,7 +10,6 @@ pub(super) struct FieldAccessFolder<'a> {
pub class_name: &'a Ident,
pub vars: Vec<VarDeclarator>,
pub statics: &'a FxHashSet<JsWord>,
pub helpers: &'a Helpers,
}
impl<'a> Fold<Expr> for FieldAccessFolder<'a> {
@ -123,7 +119,7 @@ impl<'a> Fold<Expr> for FieldAccessFolder<'a> {
.as_arg()
};
self.helpers.class_private_field_set();
helper!(class_private_field_set);
let set = quote_ident!("_classPrivateFieldSet").as_callee();
let expr = Expr::Call(CallExpr {
@ -248,7 +244,7 @@ impl<'a> Fold<Expr> for FieldAccessFolder<'a> {
};
if is_static {
self.helpers.class_static_private_field_spec_set();
helper!(class_static_private_field_spec_set);
let set = quote_ident!("_classStaticPrivateFieldSpecSet").as_callee();
Expr::Call(CallExpr {
@ -264,7 +260,7 @@ impl<'a> Fold<Expr> for FieldAccessFolder<'a> {
type_args: Default::default(),
})
} else {
self.helpers.class_private_field_set();
helper!(class_private_field_set);
let set = quote_ident!("_classPrivateFieldSet").as_callee();
Expr::Call(CallExpr {
@ -345,7 +341,7 @@ impl<'a> FieldAccessFolder<'a> {
);
if is_static {
self.helpers.class_static_private_field_spec_get();
helper!(class_static_private_field_spec_get);
let get = quote_ident!("_classStaticPrivateFieldSpecGet").as_callee();
(
@ -362,7 +358,7 @@ impl<'a> FieldAccessFolder<'a> {
Some(Expr::Ident(self.class_name.clone())),
)
} else {
self.helpers.class_private_field_get();
helper!(class_private_field_get);
let get = quote_ident!("_classPrivateFieldGet").as_callee();
match *obj {

View File

@ -1,12 +1,8 @@
use super::*;
use crate::{
compat::{
es2015::{block_scoping, function_name, resolver, Classes},
es3::ReservedWord,
},
helpers::Helpers,
use crate::compat::{
es2015::{block_scoping, function_name, resolver, Classes},
es3::ReservedWord,
};
use std::sync::Arc;
use swc_ecma_parser::{EsConfig, Syntax};
fn syntax() -> Syntax {
@ -17,12 +13,12 @@ fn syntax() -> Syntax {
})
}
fn tr(helpers: Arc<Helpers>) -> impl Fold<Module> {
fn tr() -> impl Fold<Module> {
chain!(
resolver(),
function_name(),
class_properties(helpers.clone()),
Classes { helpers },
class_properties(),
Classes,
block_scoping(),
ReservedWord,
)
@ -30,7 +26,7 @@ fn tr(helpers: Arc<Helpers>) -> impl Fold<Module> {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_static_infer_name,
r#"
var Foo = class {
@ -51,7 +47,7 @@ var Foo = function() {
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_call_exec,
r#"
class Foo {
@ -75,7 +71,7 @@ expect(test[1]).toBe(o);
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_instance_computed,
r#"
function test(x) {
@ -117,7 +113,7 @@ test('foo');
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_super_statement,
r#"
class Foo extends Bar {
@ -154,7 +150,7 @@ function (_Bar) {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_foobar,
r#"
class Child extends Parent {
@ -202,7 +198,7 @@ var _scopedFunctionWithThis = new WeakMap();
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_call_exec,
r#"
class Foo {
@ -226,7 +222,7 @@ expect(test[1]).toBe(o);
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_derived_multiple_supers,
r#"
class Foo extends Bar {
@ -274,7 +270,7 @@ function (_Bar) {
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_static_call_exec,
r#"
class Foo {
@ -296,7 +292,7 @@ expect(f.test("bar")).toBe("bar");
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_instance_undefined_exec,
r#"
class Foo {
@ -314,7 +310,7 @@ expect(new Foo().test()).toBe(undefined);
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_instance_exec,
r#"
class Foo {
@ -358,7 +354,7 @@ expect(f.test()).toBe(4);
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_regression_t6719,
r#"
function withContext(ComposedComponent) {
@ -404,7 +400,7 @@ function withContext(ComposedComponent) {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_super_with_collision,
r#"
class A {
@ -429,7 +425,7 @@ var A = function A(force1) {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_call,
r#"
class Foo {
@ -472,7 +468,7 @@ function () {
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_instance_computed_exec,
r#"
function test(x) {
@ -494,7 +490,7 @@ test('foo');
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_declaration_order,
r#"
class C {
@ -531,7 +527,7 @@ expect(() => {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
nested_class_super_call_in_key,
r#"
@ -599,7 +595,7 @@ expect(new Outer().hello).toBe('hello');
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_instance_undefined,
r#"
class Foo {
@ -620,7 +616,7 @@ var Foo = function Foo() {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_derived_multiple_supers,
r#"
class Foo extends Bar {
@ -678,7 +674,7 @@ var _bar = new WeakMap();
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_native_classes_exec,
r#"
class Foo {
@ -705,7 +701,7 @@ expect(f.test()).toBe("bar")
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_regression_t2983,
r#"
call(class {
@ -735,7 +731,7 @@ export { _class as default }
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_static,
r#"
class Foo {
@ -757,7 +753,7 @@ _defineProperty(Foo, "bar", "foo");
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_instance_undefined,
r#"
class Foo {
@ -784,7 +780,7 @@ var _bar = new WeakMap();
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_declaration_order_exec,
r#"
class C {
@ -801,7 +797,7 @@ expect(() => {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_update,
r#"
class Foo {
@ -842,7 +838,7 @@ function () {
"#
);
test!(syntax(), |_, helpers| tr(helpers), public_super_call, r#"
test!(syntax(), |_| tr(), public_super_call, r#"
class A {
foo() {
return "bar";
@ -895,7 +891,7 @@ function (_A) {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_constructor_collision,
r#"
var foo = "bar";
@ -932,7 +928,7 @@ var _bar = new WeakMap();
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_constructor_collision,
r#"
var foo = "bar";
@ -967,7 +963,7 @@ _defineProperty(Foo, "bar", baz);
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_computed,
r#"
const foo = "foo";
@ -1051,7 +1047,7 @@ _defineProperty(MyClass, _ref3, '247');
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_assignment,
r#"
class Foo {
@ -1098,7 +1094,7 @@ function () {
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_static_exec,
r#"
class Foo {
@ -1119,7 +1115,7 @@ test_exec!(
// TODO(kdy1): unignore
ignore,
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
regression_7371_exec,
r#"
@ -1227,7 +1223,7 @@ new ComputedField();
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_static_inherited_exec,
r#"
class Base {
@ -1305,7 +1301,7 @@ expect(Sub2.getClass()).toBe(7);
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
nested_class_super_property_in_key_exec,
r#"
@ -1333,7 +1329,7 @@ expect(new Outer().hello).toBe('hello');
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_super_statement,
r#"
class Foo extends Bar {
@ -1377,7 +1373,7 @@ var _bar = new WeakMap();
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_private_in_derived,
r#"
class Outer {
@ -1421,7 +1417,7 @@ var _outer = new WeakMap();
"#
);
test!(syntax(), |_, helpers| tr(helpers), private_update, r#"
test!(syntax(), |_| tr(), private_update, r#"
class Foo {
#foo = 0;
@ -1466,7 +1462,7 @@ var _foo = new WeakMap();
"#);
test!(syntax(), |_, helpers| tr(helpers), public_super_expression, r#"
test!(syntax(), |_| tr(), public_super_expression, r#"
class Foo extends Bar {
bar = "foo";
@ -1497,7 +1493,7 @@ var Foo = function (_Bar) {
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_computed_initialization_order_exec,
r#"
const actualOrder = [];
@ -1544,7 +1540,7 @@ expect(inst[9]).toBe(15);
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
nested_class_super_call_in_key_exec,
r#"
@ -1575,7 +1571,7 @@ expect(new Outer().hello).toBe('hello');
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_update_exec,
r#"
class Foo {
@ -1611,7 +1607,7 @@ expect(results[7]).toBe(4);
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_extracted_this,
r#"
var foo = "bar";
@ -1641,7 +1637,7 @@ var Foo = function Foo(foo1) {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_derived,
r#"
class Foo {
@ -1698,7 +1694,7 @@ var _prop2 = new WeakMap();
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_super_call,
r#"
class A {
@ -1760,7 +1756,7 @@ var _foo = new WeakMap();
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_reference_in_other_property,
r#"
class Foo {
@ -1806,7 +1802,7 @@ var _four = new WeakMap();
"#
);
test!(syntax(), |_, helpers| tr(helpers), nested_class_super_property_in_key, r#"
test!(syntax(), |_| tr(), nested_class_super_property_in_key, r#"
class Hello {
toString() {
@ -1870,7 +1866,7 @@ expect(new Outer().hello).toBe('hello');
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_reevaluated_exec,
r#"
function classFactory() {
@ -1930,7 +1926,7 @@ expect(() => {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_numeric,
r#"
class Foo {
@ -1953,7 +1949,7 @@ var Foo = function Foo() {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_assignment,
r#"
class Foo {
@ -2004,7 +2000,7 @@ var _foo = new WeakMap();
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_constructor_collision_exec,
r#"
var foo = "bar";
@ -2030,7 +2026,7 @@ expect("bar" in f).toBe(false);
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_static_export,
r#"
export class MyClass {
@ -2060,7 +2056,7 @@ export { MyClass2 as default };
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_multiple,
r#"
class Foo {
@ -2095,7 +2091,7 @@ var _y = new WeakMap();
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_derived,
r#"
class Foo extends Bar {
@ -2128,7 +2124,7 @@ function (_Bar) {
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_static_undefined_exec,
r#"
class Foo {
@ -2143,7 +2139,7 @@ expect(Foo.num).toBeUndefined();
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_instance,
r#"
class Foo {
@ -2164,7 +2160,7 @@ var Foo = function Foo() {
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
static_property_tdz_edgest_case_exec,
r#"
expect(() => {
@ -2178,7 +2174,7 @@ expect(() => {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_non_block_arrow_func,
r#"
export default param =>
@ -2218,7 +2214,7 @@ export default (param)=>{
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_static_undefined,
r#"
class Foo {
@ -2240,7 +2236,7 @@ _defineProperty(Foo, "bar", void 0);
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_static_infer_name_exec,
r#"
var Foo = class {
@ -2256,7 +2252,7 @@ expect(Foo.name).toBe("Foo");
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
static_property_tdz_general_exec,
r#"
expect(() => {
@ -2270,7 +2266,7 @@ expect(() => {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_call,
r#"
class Foo {
@ -2320,7 +2316,7 @@ var _foo = new WeakMap();
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_derived_exec,
r#"
class Foo {
@ -2351,7 +2347,7 @@ expect(b.bar()).toBe("bar");
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_extracted_this,
r#"
var foo = "bar";
@ -2393,7 +2389,7 @@ var _baz = new WeakMap();
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_canonical_exec,
r#"
class Point {
@ -2475,7 +2471,7 @@ expect(p3.toString()).toBe("Point<0,0>")
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_static_undefined_exec,
r#"
class Foo {
@ -2499,7 +2495,7 @@ expect(Foo.test()).toBe(undefined);
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
public_update_exec,
r#"
class Foo {
@ -2535,7 +2531,7 @@ expect(results[7]).toBe(4);
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_static_call,
r#"
class Foo {
@ -2581,7 +2577,7 @@ var _foo = {
test!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_super_expression,
r#"
class Foo extends Bar {
@ -2624,7 +2620,7 @@ var _bar = new WeakMap();
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_native_classes_exec,
r#"
class Foo {
@ -2651,7 +2647,7 @@ expect(f.test()).toBe("bar")
test_exec!(
syntax(),
|_, helpers| tr(helpers),
|_| tr(),
private_multiple_exec,
r#"
class Foo {

View File

@ -1,5 +1,4 @@
use crate::{
helpers::Helpers,
pass::Pass,
util::{
alias_ident_for, constructor::inject_after_super, prop_name_to_expr_value, undefined,
@ -7,7 +6,7 @@ use crate::{
},
};
use ast::*;
use std::{iter, sync::Arc};
use std::iter;
use swc_common::{util::move_map::MoveMap, Fold, FoldWith, Spanned, Visit, VisitWith, DUMMY_SP};
#[cfg(test)]
@ -53,16 +52,14 @@ mod tests;
/// }
/// }
/// ```
pub fn decorators(helpers: Arc<Helpers>) -> impl Pass + Clone {
pub fn decorators() -> impl Pass + Clone {
Decorators {
helpers,
is_in_strict: false,
}
}
#[derive(Clone)]
struct Decorators {
helpers: Arc<Helpers>,
is_in_strict: bool,
}
@ -465,7 +462,7 @@ impl Decorators {
.map(Some)
.collect();
self.helpers.decorate();
helper!(decorate);
let decorate_call = Expr::Call(make_decorate_call(
class.decorators,
iter::once({

View File

@ -1,6 +1,5 @@
use super::*;
use crate::{helpers::Helpers, proposals::class_properties};
use std::sync::Arc;
use crate::proposals::class_properties;
use swc_ecma_parser::{EsConfig, Syntax};
fn syntax(decorators_before_export: bool) -> Syntax {
@ -12,25 +11,19 @@ fn syntax(decorators_before_export: bool) -> Syntax {
})
}
fn tr(helpers: Arc<Helpers>) -> impl Fold<Module> {
chain!(
decorators(helpers.clone()),
class_properties(helpers.clone()),
)
fn tr() -> impl Fold<Module> {
chain!(decorators(), class_properties(),)
}
/// Folder for `transformation_*` tests
fn transformation(helpers: Arc<Helpers>) -> impl Fold<Module> {
chain!(
decorators(helpers.clone()),
class_properties(helpers.clone()),
)
fn transformation() -> impl Fold<Module> {
chain!(decorators(), class_properties(),)
}
// transformation_declaration
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_declaration,
r#"
@dec()
@ -58,7 +51,7 @@ let A = _decorate([dec()], function (_initialize) {
// transformation_initialize_after_super_multiple
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_initialize_after_super_multiple,
r#"
@dec
@ -121,7 +114,7 @@ let B = _decorate([dec], function (_initialize, _A) {
// transformation_export_default_anonymous
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_export_default_anonymous,
r#"
export default @dec() class {}
@ -147,7 +140,7 @@ export default _decorate([dec()], function (_initialize) {
// transformation_initialize_after_super_statement
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_initialize_after_super_statement,
r#"
@dec
@ -182,7 +175,7 @@ let B = _decorate([dec], function (_initialize, _A) {
// element_descriptors_created_own_method_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_created_own_method_exec,
r#"
function pushElement(e) {
@ -219,7 +212,7 @@ expect(Object.getOwnPropertyDescriptor(new A(), "foo")).toEqual({
// finishers_return_class_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
finishers_return_class_exec,
r#"
class C {}
@ -244,7 +237,7 @@ expect(A).toBe(C);
// misc_method_name_not_shadow
test!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
misc_method_name_not_shadow,
r#"
var method = 1;
@ -286,7 +279,7 @@ let Foo = _decorate([decorator], function (_initialize) {
// element_descriptors_original_class_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_original_class_exec,
r#"
var el = null;
@ -314,7 +307,7 @@ expect(el.elements).toHaveLength(3);
// duplicated_keys_create_existing_element_with_extras_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_create_existing_element_with_extras_exec,
r#"
function decorate(el) {
@ -348,7 +341,7 @@ expect(() => {
// finishers_no_in_extras_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
finishers_no_in_extras_exec,
r#"
class C {}
@ -378,7 +371,7 @@ expect(() => {
// duplicated_keys_computed_keys_same_value
test!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_computed_keys_same_value,
r#"
@(_ => desc = _)
@ -427,7 +420,7 @@ let Foo = _decorate([_ => desc = _], function (_initialize) {
// transformation_only_decorated
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_only_decorated,
r#"
class B {
@ -450,7 +443,7 @@ class B {
// ordering_finishers_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
ordering_finishers_exec,
r#"
var log = [];
@ -493,7 +486,7 @@ expect(log).toEqual(numsFrom0to9);
// transformation_initiailzer_after_super_bug_8808
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_initiailzer_after_super_bug_8808,
r#"
@decorator(parameter)
@ -527,7 +520,7 @@ let Sub = _decorate([decorator(parameter)], function (_initialize, _Super) {
// duplicated_keys_original_method_overwritten_no_decorators_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_original_method_overwritten_no_decorators_exec,
r#"
var el;
@ -552,7 +545,7 @@ expect(A.prototype.method()).toBe(2);
// transformation_arguments
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_arguments,
r#"
@dec(a, b, ...c)
@ -587,7 +580,7 @@ let A = _decorate([dec(a, b, ...c)], function (_initialize) {
// duplicated_keys_original_method_overwritten_both_decorated_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_original_method_overwritten_both_decorated_exec,
r#"
expect(() => {
@ -611,7 +604,7 @@ test_exec!(
// Babel 7.3.0 fails
ignore,
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
ordering_field_initializers_after_methods_exec,
r#"
var counter = 0;
@ -647,7 +640,7 @@ expect(counter).toBe(2);
// misc_to_primitive_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
misc_to_primitive_exec,
r#"
let calls = 0;
@ -673,7 +666,7 @@ expect(calls).toBe(1);
// transformation_initialize_after_super_expression
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_initialize_after_super_expression,
r#"
@dec
@ -708,7 +701,7 @@ let B = _decorate([dec], function (_initialize, _A) {
// element_descriptors_not_reused_field_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_not_reused_field_exec,
r#"
var dec1, dec2;
@ -730,7 +723,7 @@ expect(dec1.initializer).toBe(dec2.initializer);
// transformation_export_default_named
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_export_default_named,
r#"
export default @dec() class Foo {}
@ -757,7 +750,7 @@ export { Foo as default };
// element_descriptors_original_own_field_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_original_own_field_exec,
r#"
var el = null;
@ -788,7 +781,7 @@ expect(el.initializer()).toBe(val);
// ordering_decorators_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
ordering_decorators_exec,
r#"
var log = [];
@ -829,7 +822,7 @@ expect(log).toEqual(numsFrom0to23);
// element_descriptors_default_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_default_exec,
r#"
function decorate(el) {
@ -852,7 +845,7 @@ expect(Foo.prototype.bar).toBe(2);
// element_descriptors_original_prototype_method_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_original_prototype_method_exec,
r#"
var el = null;
@ -879,7 +872,7 @@ expect(el).toEqual(Object.defineProperty({
// misc_method_name_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
misc_method_name_exec,
r#"
function decorator() {}
@ -897,7 +890,7 @@ expect(Foo.prototype.method.name).toBe("method");
test!(
ignore,
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_strict_directive,
r#"
(() => {
@ -967,7 +960,7 @@ test!(
// element_descriptors_created_static_method_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_created_static_method_exec,
r#"
function pushElement(e) {
@ -1004,7 +997,7 @@ expect(Object.getOwnPropertyDescriptor(A, "foo")).toEqual({
// misc_method_name_not_shadow_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
misc_method_name_not_shadow_exec,
r#"
function decorator() {}
@ -1027,7 +1020,7 @@ expect(Foo.prototype.method.name).toBe("method");
// duplicated_keys_original_method_overwritten_second_decorated_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_original_method_overwritten_second_decorated_exec,
r#"
expect(() => {
@ -1048,7 +1041,7 @@ expect(() => {
// duplicated_keys_get_set_both_decorated_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_get_set_both_decorated_exec,
r#"
function dec(el) { return el }
@ -1068,7 +1061,7 @@ expect(() => {
// duplicated_keys_original_method_overwritten_first_decorated_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_original_method_overwritten_first_decorated_exec,
r#"
expect(() => {
@ -1089,7 +1082,7 @@ expect(() => {
// element_descriptors_created_prototype_field_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_created_prototype_field_exec,
r#"
function pushElement(e) {
@ -1127,7 +1120,7 @@ expect(Object.getOwnPropertyDescriptor(A.prototype, "foo")).toEqual({
// transformation_extends
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_extends,
r#"
@dec class A extends B {}
@ -1158,7 +1151,7 @@ let A = _decorate([dec], function (_initialize, _B) {
// transformation_extends_await
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_extends_await,
r#"
async function g() {
@ -1192,7 +1185,7 @@ async function g() {
// transformation_extends_yield
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_extends_yield,
r#"
function* g() {
@ -1226,7 +1219,7 @@ function* g() {
// element_descriptors_created_static_field_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_created_static_field_exec,
r#"
function pushElement(e) {
@ -1265,7 +1258,7 @@ expect(Object.getOwnPropertyDescriptor(A, "foo")).toEqual({
// element_descriptors_created_own_field_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_created_own_field_exec,
r#"
function pushElement(e) {
@ -1304,7 +1297,7 @@ expect(Object.getOwnPropertyDescriptor(new A(), "foo")).toEqual({
// element_descriptors_not_reused_method_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_not_reused_method_exec,
r#"
var dec1, dec2;
@ -1326,7 +1319,7 @@ expect(dec1.descriptor.value).toBe(dec2.descriptor.value);
// element_descriptors_not_reused_class_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_not_reused_class_exec,
r#"
var dec1, dec2;
@ -1343,7 +1336,7 @@ expect(dec1).not.toBe(dec2);
// duplicated_keys_computed_keys_same_ast_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_computed_keys_same_ast_exec,
r#"
var i = 0;
@ -1380,7 +1373,7 @@ expect(i).toBe(2);
// transformation_initialize_after_super_bug_8931
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_initialize_after_super_bug_8931,
r#"
@dec
@ -1421,7 +1414,7 @@ test_exec!(
// Babel 7.3.0 fails
ignore,
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
ordering_static_field_initializers_after_methods_exec,
r#"
var counter = 0;
@ -1453,7 +1446,7 @@ expect(counter).toBe(2);
// element_descriptors_original_static_method_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_original_static_method_exec,
r#"
var el = null;
@ -1480,7 +1473,7 @@ expect(el).toEqual(Object.defineProperty({
// duplicated_keys_extras_duplicated_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_extras_duplicated_exec,
r#"
function decorate(el) {
@ -1521,7 +1514,7 @@ expect(() => {
// duplicated_keys_extras_same_as_return_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_extras_same_as_return_exec,
r#"
function decorate(el) {
@ -1561,7 +1554,7 @@ expect(() => {
// finishers_class_as_parameter_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
finishers_class_as_parameter_exec,
r#"
var C;
@ -1586,7 +1579,7 @@ expect(C).toBe(A);
// duplicated_keys_moved_and_created_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_moved_and_created_exec,
r#"
var value1, value2 = {};
@ -1628,7 +1621,7 @@ expect(Foo.prototype.bar).toBe(value2);
// transformation_expression
test!(
syntax(false),
|_, helpers| transformation(helpers),
|_| transformation(),
transformation_expression,
r#"
(@dec() class {});
@ -1655,7 +1648,7 @@ _decorate([dec()], function (_initialize) {
// duplicated_keys_original_method_prototype_and_static_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_original_method_prototype_and_static_exec,
r#"
var el;
@ -1682,7 +1675,7 @@ expect(A.method()).toBe(2);
// duplicated_keys_computed_keys_same_ast
test!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_computed_keys_same_ast,
r#"
@(_ => desc = _)
@ -1731,7 +1724,7 @@ let Foo = _decorate([_ => desc = _], function (_initialize) {
// element_descriptors_created_prototype_method_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_created_prototype_method_exec,
r#"
function pushElement(e) {
@ -1767,7 +1760,7 @@ expect(Object.getOwnPropertyDescriptor(A.prototype, "foo")).toEqual({
// duplicated_keys_create_existing_element_from_method_decorator_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_create_existing_element_from_method_decorator_exec,
r#"
function decorate() {
@ -1799,7 +1792,7 @@ expect(() => {
// element_descriptors_original_static_field_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_original_static_field_exec,
r#"
var el = null;
@ -1829,7 +1822,7 @@ expect(el.initializer()).toBe(val);
// duplicated_keys_coalesce_get_set_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_coalesce_get_set_exec,
r#"
var el, el1;
@ -1861,7 +1854,7 @@ expect(desc.set()).toBe(2);
// transformation_extends_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
transformation_extends_exec,
r#"
class B {}
@ -1877,7 +1870,7 @@ expect(new A).toBeInstanceOf(B);
// duplicated_keys_create_existing_element_from_class_decorator_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_create_existing_element_from_class_decorator_exec,
r#"
function pushElement(e) {
@ -1907,7 +1900,7 @@ expect(() => {
// duplicated_keys_computed_keys_same_value_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
duplicated_keys_computed_keys_same_value_exec,
r#"
var i = 0;
@ -1946,7 +1939,7 @@ expect(j).toBe(1);
// element_descriptors_original_own_field_without_initiailzer_exec
test_exec!(
syntax(false),
|_, helpers| tr(helpers),
|_| tr(),
element_descriptors_original_own_field_without_initiailzer_exec,
r#"
var el = null;

View File

@ -6,7 +6,7 @@ fn tr() -> impl Fold<Module> {
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
assignment_expression,
r#"
foo = createReactClass({});
@ -24,7 +24,7 @@ bar = React.createClass({
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
nested,
r#"
var foo = qux(createReactClass({}));
@ -42,7 +42,7 @@ var bar = qux(React.createClass({
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
object_property,
r#"
({
@ -67,7 +67,7 @@ test!(
test!(
::swc_ecma_parser::Syntax::default(),
|_, _| tr(),
|_| tr(),
variable_declarator,
r#"
var foo = createReactClass({});

View File

@ -1,7 +1,7 @@
use crate::{helpers::Helpers, pass::Pass, util::ExprFactory};
use crate::{pass::Pass, util::ExprFactory};
use ast::*;
use serde::{Deserialize, Serialize};
use std::{iter, mem, sync::Arc};
use std::{iter, mem};
use swc_atoms::JsWord;
use swc_common::{
errors::{ColorConfig, Handler},
@ -58,7 +58,7 @@ fn default_throw_if_namespace() -> bool {
/// `@babel/plugin-transform-react-jsx`
///
/// Turn JSX into React function calls
pub fn jsx(cm: Lrc<SourceMap>, options: Options, helpers: Arc<Helpers>) -> impl Pass + Clone {
pub fn jsx(cm: Lrc<SourceMap>, options: Options) -> impl Pass + Clone {
let handler = Handler::with_tty_emitter(ColorConfig::Always, false, true, Some(cm.clone()));
let session = Session { handler: &handler };
@ -81,7 +81,6 @@ pub fn jsx(cm: Lrc<SourceMap>, options: Options, helpers: Arc<Helpers>) -> impl
expr: parse("pragma_frag", options.pragma_frag),
},
use_builtins: options.use_builtins,
helpers,
}
}
@ -90,7 +89,6 @@ struct Jsx {
pragma: ExprOrSuper,
pragma_frag: ExprOrSpread,
use_builtins: bool,
helpers: Arc<Helpers>,
}
impl Jsx {
@ -212,7 +210,7 @@ impl Jsx {
if self.use_builtins {
member_expr!(DUMMY_SP, Object.assign).as_callee()
} else {
self.helpers.extends();
helper!(extends);
quote_ident!("_extends").as_callee()
}
},

View File

@ -4,20 +4,11 @@ use crate::{
es2015::{arrow, Classes},
es3::PropertyLiteral,
},
helpers::Helpers,
react::display_name,
};
use std::sync::Arc;
fn tr(cm: Lrc<SourceMap>, options: Options, helpers: Arc<Helpers>) -> impl Fold<Module> {
chain!(
jsx(cm.clone(), options, helpers.clone()),
display_name(),
Classes {
helpers: helpers.clone()
},
arrow(),
)
fn tr(cm: Lrc<SourceMap>, options: Options) -> impl Fold<Module> {
chain!(jsx(cm.clone(), options), display_name(), Classes, arrow(),)
}
test!(
@ -25,7 +16,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_add_appropriate_newlines,
r#"
<Component
@ -44,7 +35,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_arrow_functions,
r#"
var foo = function () {
@ -74,7 +65,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_concatenates_adjacent_string_literals,
r#"
var x =
@ -102,7 +93,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_display_name_assignment_expression,
r#"var Component;
Component = React.createClass({
@ -125,7 +116,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_display_name_export_default,
r#"
export default React.createClass({
@ -149,7 +140,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_display_name_if_missing,
r#"
var Whateva = React.createClass({
@ -187,7 +178,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_display_name_object_declaration,
r#"
exports = {
@ -213,7 +204,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_display_name_property_assignment,
r#"
exports.Component = React.createClass({
@ -237,7 +228,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_display_name_variable_declaration,
r#"
var Component = React.createClass({
@ -258,7 +249,7 @@ var Component = React.createClass({
test!(
::swc_ecma_parser::Syntax::Es(::swc_ecma_parser::EsConfig{jsx:true,..Default::default()}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_dont_coerce_expression_containers,
r#"
<Text>
@ -275,7 +266,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_honor_custom_jsx_comment_if_jsx_pragma_option_set,
r#"/** @jsx dom */
@ -300,7 +291,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_honor_custom_jsx_comment,
r#"
/** @jsx dom */
@ -327,13 +318,12 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(
|tester| tr(
tester.cm.clone(),
Options {
pragma: "dom".into(),
..Default::default()
},
helpers
}
),
react_honor_custom_jsx_pragma_option,
r#"
@ -357,7 +347,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_jsx_with_retainlines_option,
r#"var div = <div>test</div>;"#,
r#"var div = React.createElement("div", null, "test");"#
@ -368,7 +358,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_jsx_without_retainlines_option,
r#"var div = <div>test</div>;"#,
r#"var div = React.createElement("div", null, "test");"#
@ -381,7 +371,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_optimisation_react_constant_elements,
r#"
class App extends React.Component {
@ -445,10 +435,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| chain!(
tr(tester.cm.clone(), Default::default(), helpers),
PropertyLiteral
),
|tester| chain!(tr(tester.cm.clone(), Default::default()), PropertyLiteral),
react_should_add_quotes_es3,
r#"var es3 = <F aaa new const var default foo-bar/>;"#,
r#"
@ -468,7 +455,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_allow_constructor_as_prop,
r#"<Component constructor="foo" />;"#,
r#"
@ -483,7 +470,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_allow_deeper_js_namespacing,
r#"<Namespace.DeepNamespace.Component />;"#,
r#"React.createElement(Namespace.DeepNamespace.Component, null);"#
@ -494,7 +481,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_allow_elements_as_attributes,
r#"<div attr=<div /> />"#,
r#"
@ -508,7 +495,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_allow_js_namespacing,
r#"<Namespace.Component />;"#,
r#"React.createElement(Namespace.Component, null);"#
@ -519,7 +506,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_allow_nested_fragments,
r#"
<div>
@ -555,7 +542,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_allow_no_pragmafrag_if_frag_unused,
r#"
/** @jsx dom */
@ -574,7 +561,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_allow_pragmafrag_and_frag,
r#"
/** @jsx dom */
@ -595,7 +582,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_avoid_wrapping_in_extra_parens_if_not_needed,
r#"
var x = <div>
@ -627,7 +614,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_convert_simple_tags,
r#"var x = <div></div>;"#,
r#"var x = React.createElement("div", null);"#
@ -638,7 +625,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_convert_simple_text,
r#"var x = <div>text</div>;"#,
r#"var x = React.createElement("div", null, "text");"#
@ -649,7 +636,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_escape_xhtml_jsxattribute,
r#"
<div id="wôw" />;
@ -675,7 +662,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_escape_xhtml_jsxtext_1,
r#"
<div>wow</div>;
@ -706,7 +693,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_escape_xhtml_jsxtext_2,
r#"
<div>this should not parse as unicode: \u00a0</div>;
@ -724,7 +711,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_escape_xhtml_jsxtext_3,
r#"
<div>this should parse as nbsp: </div>;
@ -739,7 +726,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_handle_attributed_elements,
r#"
var HelloMessage = React.createClass({
@ -772,7 +759,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_handle_has_own_property_correctly,
r#"<hasOwnProperty>testing</hasOwnProperty>;"#,
r#"React.createElement("hasOwnProperty", null, "testing");"#
@ -783,7 +770,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_have_correct_comma_in_nested_children,
r#"
var x = <div>
@ -807,7 +794,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_insert_commas_after_expressions_before_whitespace,
r#"
var x =
@ -842,7 +829,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_not_add_quotes_to_identifier_names,
r#"var e = <F aaa new const var default foo-bar/>;"#,
r#"
@ -862,7 +849,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_not_mangle_expressioncontainer_attribute_values,
r#"<button data-value={"a value\n with\nnewlines\n and spaces"}>Button</button>;"#,
r#"
@ -877,7 +864,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_not_strip_nbsp_even_coupled_with_other_whitespace,
r#"<div>&nbsp; </div>;"#,
r#"React.createElement("div", null, "\xA0 ");"#,
@ -889,7 +876,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_not_strip_tags_with_a_single_child_of_nbsp,
r#"<div>&nbsp;</div>;"#,
r#"React.createElement("div", null, "\xA0");"#,
@ -903,7 +890,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_properly_handle_comments_between_props,
r#"
var x = (
@ -934,7 +921,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_quote_jsx_attributes,
r#"<button data-value='a value'>Button</button>;"#,
r#"
@ -949,14 +936,13 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(
|tester| tr(
tester.cm.clone(),
Options {
pragma: "h".into(),
throw_if_namespace: false,
..Default::default()
},
helpers
),
react_should_support_xml_namespaces_if_flag,
r#"<f:image n:attr />;"#,
@ -970,7 +956,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_should_transform_known_hyphenated_tags,
r#"<font-face />;"#,
r#"React.createElement("font-face", null);"#
@ -981,7 +967,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_wraps_props_in_react_spread_for_first_spread_attributes,
r#"
<Component { ... x } y
@ -1000,7 +986,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_wraps_props_in_react_spread_for_last_spread_attributes,
r#"<Component y={2} z { ... x } />"#,
r#"
@ -1016,7 +1002,7 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(tester.cm.clone(), Default::default(), helpers),
|tester| tr(tester.cm.clone(), Default::default()),
react_wraps_props_in_react_spread_for_middle_spread_attributes,
r#"<Component y={2} { ... x } z />"#,
r#"
@ -1032,13 +1018,12 @@ test!(
jsx: true,
..Default::default()
}),
|tester, helpers| tr(
|tester| tr(
tester.cm.clone(),
Options {
use_builtins: true,
..Default::default()
},
helpers
),
use_builtins_assignment,
r#"var div = <Component {...props} foo="bar" />"#,

View File

@ -9,7 +9,7 @@ test!(
jsx: true,
..Default::default()
}),
|_, _| tr(),
|_| tr(),
basic_sample,
r#"var x = <sometag />"#,
r#"var x = <sometag __self={this} />;"#

View File

@ -12,7 +12,7 @@ test_exec!(
jsx: true,
..Default::default()
}),
|_, _| tr(),
|_| tr(),
basic_sample,
r#"
var actual = transform(
@ -37,7 +37,7 @@ test!(
jsx: true,
..Default::default()
}),
|_, _| tr(),
|_| tr(),
no_jsx,
r#"var x = 42;"#,
r#"var x = 42;"#
@ -49,7 +49,7 @@ test_exec!(
jsx: true,
..Default::default()
}),
|_, _| tr(),
|_| tr(),
with_source,
r#"
var actual = transform(

View File

@ -4,8 +4,7 @@ pub use self::{
jsx_self::jsx_self,
jsx_src::jsx_src,
};
use crate::{helpers::Helpers, pass::Pass};
use std::sync::Arc;
use crate::pass::Pass;
use swc_common::{sync::Lrc, SourceMap};
mod display_name;
@ -16,11 +15,11 @@ mod jsx_src;
/// `@babel/preset-react`
///
/// Preset for all React plugins.
pub fn react(cm: Lrc<SourceMap>, options: Options, helpers: Arc<Helpers>) -> impl Pass + Clone {
pub fn react(cm: Lrc<SourceMap>, options: Options) -> impl Pass + Clone {
let Options { development, .. } = options;
chain!(
jsx(cm.clone(), options, helpers),
jsx(cm.clone(), options),
display_name(),
jsx_src(development, cm),
jsx_self(development)

View File

@ -3,7 +3,7 @@ use super::SimplifyExpr;
fn test_expr(src: &str, expected: &str) {
test_transform!(
::swc_ecma_parser::Syntax::default(),
|_, _| SimplifyExpr,
|_| SimplifyExpr,
src,
expected,
true

View File

@ -5,7 +5,7 @@ macro_rules! test_stmt {
($l:expr, $r:expr) => {
test_transform!(
::swc_ecma_parser::Syntax::default(),
|_, _| Simplifier { enable: true },
|_| Simplifier { enable: true },
$l,
$r
)

View File

@ -1,4 +1,4 @@
use crate::helpers::{Helpers, InjectHelpers};
use crate::helpers::{InjectHelpers, HELPERS};
use ast::*;
use sourcemap::SourceMapBuilder;
use std::{
@ -28,7 +28,9 @@ impl<'a> Tester<'a> {
where
F: FnOnce(&mut Tester) -> Result<(), ()>,
{
let out = ::testing::run_test(false, |cm, handler| op(&mut Tester { cm, handler }));
let out = ::testing::run_test(false, |cm, handler| {
HELPERS.set(&Default::default(), || op(&mut Tester { cm, handler }))
});
match out {
Ok(()) => {}
@ -150,12 +152,12 @@ impl<'a> Tester<'a> {
}
}
fn make_tr<F, P>(op: F, tester: &mut Tester, helpers: Arc<Helpers>) -> P
fn make_tr<F, P>(op: F, tester: &mut Tester) -> P
where
F: FnOnce(&mut Tester, Arc<Helpers>) -> P,
F: FnOnce(&mut Tester) -> P,
P: Fold<Module>,
{
op(tester, helpers)
op(tester)
}
#[cfg(test)]
@ -176,15 +178,15 @@ pub(crate) fn test_transform<F, P>(
expected: &str,
ok_if_code_eq: bool,
) where
F: FnOnce(&mut Tester, Arc<Helpers>) -> P,
F: FnOnce(&mut Tester) -> P,
{
crate::tests::Tester::run(|tester| {
let expected =
tester.apply_transform(::testing::DropSpan, "output.js", syntax, expected)?;
eprintln!("----- Actual -----");
let helpers = Arc::new(Helpers::default());
let tr = crate::tests::make_tr(tr, tester, helpers.clone());
let tr = crate::tests::make_tr(tr, tester);
let actual = tester
.apply_transform(tr, "input.js", syntax, input)?
.fold_with(&mut crate::hygiene::hygiene())
@ -258,11 +260,10 @@ macro_rules! exec_tr {
pub(crate) fn exec_tr<F, P>(test_name: &str, syntax: Syntax, tr: F, input: &str)
where
F: FnOnce(&mut Tester, Arc<Helpers>) -> P,
F: FnOnce(&mut Tester) -> P,
{
Tester::run(|tester| {
let helpers = Arc::new(Helpers::default());
let tr = make_tr(tr, tester, helpers.clone());
let tr = make_tr(tr, tester);
let module = tester.apply_transform(
tr,
@ -282,7 +283,6 @@ where
let src_without_helpers = tester.print(&module);
let module = module.fold_with(&mut InjectHelpers {
cm: tester.cm.clone(),
helpers: helpers.clone(),
});
let src = tester.print(&module);