From 6cc51bed13ee5eaa489cdb1aed2b5a483944d14b Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Fri, 6 Dec 2024 20:14:41 +0000 Subject: [PATCH] refactor(transformer/class-properties): shorten output when `_super` function --- .../es2022/class_properties/constructor.rs | 61 ++++++++----------- .../private/derived-multiple-supers/output.js | 10 +-- .../public-loose/super-expression/output.js | 10 +-- .../public/derived-multiple-supers/output.js | 10 +-- .../public/super-expression/output.js | 10 +-- .../test/fixtures/regression/7371/output.js | 30 ++++----- .../multiple-super-in-termary/output.js | 10 +-- 7 files changed, 67 insertions(+), 74 deletions(-) diff --git a/crates/oxc_transformer/src/es2022/class_properties/constructor.rs b/crates/oxc_transformer/src/es2022/class_properties/constructor.rs index a2f96fbc2976c3..414cd89a986042 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/constructor.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/constructor.rs @@ -52,11 +52,11 @@ //! ```js //! class C extends S { //! constructor(yes) { -//! var _super = (..._args) => { -//! super(..._args); -//! this.prop = foo(); -//! return this; -//! }; +//! var _super = (..._args) => ( +//! super(..._args), +//! this.prop = foo(), +//! this +//! ); //! if (yes) { //! _super(2); //! } else { @@ -145,7 +145,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { ); params_rest = Some(ctx.ast.alloc_binding_rest_element(SPAN, binding.create_binding_pattern(ctx))); - stmts.push(create_super_call_stmt(&binding, ctx)); + stmts.push(ctx.ast.statement_expression(SPAN, create_super_call(&binding, ctx))); } // TODO: Should these have the span of the original `PropertyDefinition`s? stmts.extend(exprs_into_stmts(inits, ctx)); @@ -512,13 +512,8 @@ impl<'a, 'c> ConstructorBodyInitsInserter<'a, 'c> { } /// Insert `_super` function at top of constructor. - /// ```js - /// var _super = (..._args) => { - /// super(..._args); - /// - /// return this; - /// }; - /// ``` + /// + /// `var _super = (..._args) => (super(..._args), , this);` fn insert_super_func( &mut self, stmts: &mut ArenaVec<'a, Statement<'a>>, @@ -534,22 +529,24 @@ impl<'a, 'c> ConstructorBodyInitsInserter<'a, 'c> { let args_binding = ctx.generate_uid("args", super_func_scope_id, SymbolFlags::FunctionScopedVariable); - // `super(..._args); ; return this;` + // `(super(..._args), , this)` // // TODO(improve-on-babel): When not in loose mode, inits are `_defineProperty(this, propName, value)`. // `_defineProperty` returns `this`, so last statement could be `return _defineProperty(this, propName, value)`, // rather than an additional `return this` statement. - let super_call = create_super_call_stmt(&args_binding, ctx); - let return_stmt = ctx.ast.statement_return(SPAN, Some(ctx.ast.expression_this(SPAN))); - let body_stmts = ctx.ast.vec_from_iter( - [super_call].into_iter().chain(exprs_into_stmts(inits, ctx)).chain([return_stmt]), + let super_call = create_super_call(&args_binding, ctx); + let this_expr = ctx.ast.expression_this(SPAN); + let body_exprs = ctx.ast.expression_sequence( + SPAN, + ctx.ast.vec_from_iter([super_call].into_iter().chain(inits).chain([this_expr])), ); + let body = ctx.ast.vec1(ctx.ast.statement_expression(SPAN, body_exprs)); - // `(...args) => { super(..._args); ; return this; }` + // `(..._args) => (super(..._args), , this)` let super_func = Expression::ArrowFunctionExpression( ctx.ast.alloc_arrow_function_expression_with_scope_id( SPAN, - false, + true, false, NONE, ctx.ast.alloc_formal_parameters( @@ -562,12 +559,12 @@ impl<'a, 'c> ConstructorBodyInitsInserter<'a, 'c> { )), ), NONE, - ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), body_stmts), + ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), body), super_func_scope_id, ), ); - // `var _super = (...args) => { ... }` + // `var _super = (..._args) => ( ... );` // Note: `super_binding` can be `None` at this point if no `super()` found in constructor // (see comment above in `insert`). let super_binding = self @@ -664,20 +661,16 @@ impl<'a, 'c> ConstructorBodyInitsInserter<'a, 'c> { } /// `super(...args);` -fn create_super_call_stmt<'a>( +fn create_super_call<'a>( args_binding: &BoundIdentifier<'a>, ctx: &mut TraverseCtx<'a>, -) -> Statement<'a> { - ctx.ast.statement_expression( +) -> Expression<'a> { + ctx.ast.expression_call( SPAN, - ctx.ast.expression_call( - SPAN, - ctx.ast.expression_super(SPAN), - NONE, - ctx.ast.vec1( - ctx.ast.argument_spread_element(SPAN, args_binding.create_read_expression(ctx)), - ), - false, - ), + ctx.ast.expression_super(SPAN), + NONE, + ctx.ast + .vec1(ctx.ast.argument_spread_element(SPAN, args_binding.create_read_expression(ctx))), + false, ) } diff --git a/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/private/derived-multiple-supers/output.js b/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/private/derived-multiple-supers/output.js index affc219260cdc9..f86f77fef20337 100644 --- a/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/private/derived-multiple-supers/output.js +++ b/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/private/derived-multiple-supers/output.js @@ -1,11 +1,11 @@ var _bar = /*#__PURE__*/new WeakMap(); class Foo extends Bar { constructor() { - var _super = (..._args) => { - super(..._args); - babelHelpers.classPrivateFieldInitSpec(this, _bar, "foo"); - return this; - }; + var _super = (..._args) => ( + super(..._args), + babelHelpers.classPrivateFieldInitSpec(this, _bar, "foo"), + this + ); if (condition) { _super(); diff --git a/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/public-loose/super-expression/output.js b/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/public-loose/super-expression/output.js index c99d089e21f939..a110927909a642 100644 --- a/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/public-loose/super-expression/output.js +++ b/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/public-loose/super-expression/output.js @@ -1,10 +1,10 @@ class Foo extends Bar { constructor() { - var _super = (..._args) => { - super(..._args); - this.bar = "foo"; - return this; - }; + var _super = (..._args) => ( + super(..._args), + this.bar = "foo", + this + ); foo(_super()); } } diff --git a/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/public/derived-multiple-supers/output.js b/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/public/derived-multiple-supers/output.js index 78bb48487b6154..411b13e91885ec 100644 --- a/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/public/derived-multiple-supers/output.js +++ b/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/public/derived-multiple-supers/output.js @@ -1,10 +1,10 @@ class Foo extends Bar { constructor() { - var _super = (..._args) => { - super(..._args); - babelHelpers.defineProperty(this, "bar", "foo"); - return this; - }; + var _super = (..._args) => ( + super(..._args), + babelHelpers.defineProperty(this, "bar", "foo"), + this + ); if (condition) { _super(); diff --git a/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/public/super-expression/output.js b/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/public/super-expression/output.js index 0cc3f1ca0e0c78..958958fd353e36 100644 --- a/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/public/super-expression/output.js +++ b/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/public/super-expression/output.js @@ -1,10 +1,10 @@ class Foo extends Bar { constructor() { - var _super = (..._args) => { - super(..._args); - babelHelpers.defineProperty(this, "bar", "foo"); - return this; - }; + var _super = (..._args) => ( + super(..._args), + babelHelpers.defineProperty(this, "bar", "foo"), + this + ); foo(_super()); } } diff --git a/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/regression/7371/output.js b/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/regression/7371/output.js index af8cc7e0f3d294..7fbe93487a4ec6 100644 --- a/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/regression/7371/output.js +++ b/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/regression/7371/output.js @@ -27,11 +27,11 @@ class Obj { // ensure superClass is still transformed class SuperClass extends Obj { constructor() { - var _super = (..._args) => { - super(..._args); - babelHelpers.defineProperty(this, "field", 1); - return this; - }; + var _super = (..._args) => ( + super(..._args), + babelHelpers.defineProperty(this, "field", 1), + this + ); class B extends (_super(), Obj) { constructor() { super(); @@ -47,11 +47,11 @@ new SuperClass(); // ensure ComputedKey Method is still transformed class ComputedMethod extends Obj { constructor() { - var _super2 = (..._args2) => { - super(..._args2); - babelHelpers.defineProperty(this, "field", 1); - return this; - }; + var _super2 = (..._args2) => ( + super(..._args2), + babelHelpers.defineProperty(this, "field", 1), + this + ); class B extends Obj { constructor() { super(); @@ -69,11 +69,11 @@ new ComputedMethod(); class ComputedField extends Obj { constructor() { let _super4; - var _super3 = (..._args3) => { - super(..._args3); - babelHelpers.defineProperty(this, "field", 1); - return this; - }; + var _super3 = (..._args3) => ( + super(..._args3), + babelHelpers.defineProperty(this, "field", 1), + this + ); _super4 = _super3(); class B extends Obj { constructor() { diff --git a/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/regression/multiple-super-in-termary/output.js b/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/regression/multiple-super-in-termary/output.js index 2bda1d4d7495c7..bffead1cc55406 100644 --- a/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/regression/multiple-super-in-termary/output.js +++ b/tasks/transform_conformance/overrides/babel-plugin-transform-class-properties/test/fixtures/regression/multiple-super-in-termary/output.js @@ -1,10 +1,10 @@ class A extends B { constructor() { - var _super = (..._args) => { - super(..._args); - babelHelpers.defineProperty(this, "x", 2); - return this; - }; + var _super = (..._args) => ( + super(..._args), + babelHelpers.defineProperty(this, "x", 2), + this + ); x ? _super(a) : _super(b); } }