From 0cb48b116ad3eadb9bc565229dd0ef032a2abbb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 3 Jul 2023 18:25:53 +0200 Subject: [PATCH 1/2] Inject tmp vars in the params list of IIFEs when possible --- .../completion-do-expression/output.js | 3 +- .../object-rest-and-keys/output.js | 3 +- .../object-rest-and-private-keys/output.js | 3 +- .../object-rest-under-private/output.js | 3 +- .../do-expressions/while-if/output.js | 3 +- .../fixtures/fsharp/indirect-eval/output.js | 3 +- .../fixtures/minimal/indirect-eval/output.js | 3 +- .../pipe-body-with-eval-tacit-call/output.js | 3 +- .../output.js | 20 +++--------- .../output.js | 10 ++---- .../output.js | 25 +++------------ .../output.js | 20 +++--------- .../transform-in-default-param/output.js | 5 +-- .../transform-in-default-param/output.js | 5 +-- .../in-function-params/output.js | 20 +++--------- .../in-function-params-loose/output.js | 20 +++--------- .../general/in-function-params/output.js | 20 +++--------- .../output.js | 15 ++++----- packages/babel-traverse/src/scope/index.ts | 31 ++++++++++++++++--- 19 files changed, 70 insertions(+), 145 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/output.js index 24e6f7513841..e4c4cd78424f 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/output.js @@ -4,8 +4,7 @@ var _x = { writable: true, value: void 0 }; -(() => { - var _m, _m2; +((_m, _m2) => { var x; result = (_m = C, _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), x = _m2 === void 0 ? 2 : _m2, _m); })(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/output.js index 27aea3348e9a..38ddc13db526 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/output.js @@ -12,8 +12,7 @@ var _y = { babelHelpers.defineProperty(C, "a", "a"); babelHelpers.defineProperty(C, "b", "b"); babelHelpers.defineProperty(C, "c", "c"); -(() => { - var _C; +(_C => { let x, y, z; x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), (_C = C, ({ y diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/output.js index 25e265e9f6d5..99d674b7fbfd 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/output.js @@ -11,8 +11,7 @@ var _y = { babelHelpers.defineProperty(C, "a", "a"); babelHelpers.defineProperty(C, "b", "b"); babelHelpers.defineProperty(C, "c", "c"); -(() => { - var _C; +(_C => { let x, y, z; x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y), (_C = C, ({} = _C), z = Object.assign({}, (babelHelpers.objectDestructuringEmpty(_C), _C)), _C); result = { diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/output.js index 3cfe61822ba8..eb91bdb6e2d3 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/output.js @@ -8,8 +8,7 @@ var _x = { writable: true, value: C }; -(() => { - var _babelHelpers$classSt; +(_babelHelpers$classSt => { var x, y, z; ({ x diff --git a/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/while-if/output.js b/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/while-if/output.js index ba034111a8f1..bd8fcb658702 100644 --- a/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/while-if/output.js +++ b/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/while-if/output.js @@ -1,6 +1,5 @@ let p; -let a = function () { - var _ret; +let a = function (_ret) { while (p = p.parentPath) { if (a) { _ret = 'a'; diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/indirect-eval/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/indirect-eval/output.js index 3cef0c0e5311..add7d16f6223 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/indirect-eval/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/indirect-eval/output.js @@ -1,7 +1,6 @@ -(function () { +(function (_functionReturn) { 'use strict'; - var _functionReturn; var result = (_functionReturn = '(function() { return this; })()', (0, eval)(_functionReturn)); expect(result).not.toBeUndefined(); })(); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/indirect-eval/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/indirect-eval/output.js index 3cef0c0e5311..add7d16f6223 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/indirect-eval/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/minimal/indirect-eval/output.js @@ -1,7 +1,6 @@ -(function () { +(function (_functionReturn) { 'use strict'; - var _functionReturn; var result = (_functionReturn = '(function() { return this; })()', (0, eval)(_functionReturn)); expect(result).not.toBeUndefined(); })(); diff --git a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/pipe-body-with-eval-tacit-call/output.js b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/pipe-body-with-eval-tacit-call/output.js index 3cef0c0e5311..add7d16f6223 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/pipe-body-with-eval-tacit-call/output.js +++ b/packages/babel-plugin-proposal-pipeline-operator/test/fixtures/smart/pipe-body-with-eval-tacit-call/output.js @@ -1,7 +1,6 @@ -(function () { +(function (_functionReturn) { 'use strict'; - var _functionReturn; var result = (_functionReturn = '(function() { return this; })()', (0, eval)(_functionReturn)); expect(result).not.toBeUndefined(); })(); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/output.js b/packages/babel-plugin-transform-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/output.js index ceb10b4df40a..ddc98ce0491c 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/output.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/output.js @@ -23,28 +23,16 @@ class Foo { function f(o, r = (() => o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(o.Foo, _m)[_m]())()) { return r; } - function g(o, r = (() => { - var _ref; - return (_ref = (() => o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(babelHelpers.classPrivateFieldLooseBase(o.Foo, _self)[_self].getSelf(), _m)[_m])()) == null ? void 0 : _ref(); - })()) { + function g(o, r = (_ref => (_ref = (() => o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(babelHelpers.classPrivateFieldLooseBase(o.Foo, _self)[_self].getSelf(), _m)[_m])()) == null ? void 0 : _ref())()) { return r; } - function h(fnDeep, r = (() => { - var _fnDeep$very$o$Foo, _fnDeep$very$o; - return (_fnDeep$very$o$Foo = fnDeep == null ? void 0 : (_fnDeep$very$o = fnDeep().very.o) == null ? void 0 : _fnDeep$very$o.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(_fnDeep$very$o$Foo, _m)[_m](); - })()) { + function h(fnDeep, r = ((_fnDeep$very$o$Foo, _fnDeep$very$o) => (_fnDeep$very$o$Foo = fnDeep == null ? void 0 : (_fnDeep$very$o = fnDeep().very.o) == null ? void 0 : _fnDeep$very$o.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(_fnDeep$very$o$Foo, _m)[_m]())()) { return r; } - function i(fn, r = (() => { - var _getSelf, _ref2; - return (_getSelf = (_ref2 = (() => fn === null || fn === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(fn().Foo, _self)[_self])()) == null ? void 0 : _ref2.getSelf()) === null || _getSelf === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(_getSelf.self, _m)[_m](); - })()) { + function i(fn, r = ((_getSelf, _ref2) => (_getSelf = (_ref2 = (() => fn === null || fn === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(fn().Foo, _self)[_self])()) == null ? void 0 : _ref2.getSelf()) === null || _getSelf === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(_getSelf.self, _m)[_m]())()) { return r; } - function j(fn, r = (() => { - var _babelHelpers$classPr, _babelHelpers$classPr2; - return (_babelHelpers$classPr = (_babelHelpers$classPr2 = babelHelpers.classPrivateFieldLooseBase(babelHelpers.classPrivateFieldLooseBase(fn().Foo, _self)[_self].getSelf().self, _m))[_m]) == null ? void 0 : _babelHelpers$classPr.call(_babelHelpers$classPr2); - })()) { + function j(fn, r = ((_babelHelpers$classPr, _babelHelpers$classPr2) => (_babelHelpers$classPr = (_babelHelpers$classPr2 = babelHelpers.classPrivateFieldLooseBase(babelHelpers.classPrivateFieldLooseBase(fn().Foo, _self)[_self].getSelf().self, _m))[_m]) == null ? void 0 : _babelHelpers$classPr.call(_babelHelpers$classPr2))()) { return r; } f(o); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/output.js b/packages/babel-plugin-transform-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/output.js index a1c948f4f5cf..9c0fa80b629c 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/output.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/output.js @@ -26,16 +26,10 @@ class Foo { function g(o, r = (() => o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(babelHelpers.classPrivateFieldLooseBase(o.Foo, _self)[_self].getSelf(), _m)[_m])()?.()) { return r; } - function h(fnDeep, r = (() => { - var _fnDeep$very$o$Foo; - return (_fnDeep$very$o$Foo = fnDeep?.().very.o?.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(_fnDeep$very$o$Foo, _m)[_m](); - })()) { + function h(fnDeep, r = (_fnDeep$very$o$Foo => (_fnDeep$very$o$Foo = fnDeep?.().very.o?.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(_fnDeep$very$o$Foo, _m)[_m]())()) { return r; } - function i(fn, r = (() => { - var _getSelf; - return (_getSelf = (() => fn === null || fn === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(fn().Foo, _self)[_self])()?.getSelf()) === null || _getSelf === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(_getSelf.self, _m)[_m](); - })()) { + function i(fn, r = (_getSelf => (_getSelf = (() => fn === null || fn === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(fn().Foo, _self)[_self])()?.getSelf()) === null || _getSelf === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(_getSelf.self, _m)[_m]())()) { return r; } function j(fn, r = (() => babelHelpers.classPrivateFieldLooseBase(babelHelpers.classPrivateFieldLooseBase(fn().Foo, _self)[_self].getSelf().self, _m)[_m]?.())()) { diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/output.js b/packages/babel-plugin-transform-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/output.js index 1cda889efab9..7d7f7611fbff 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/output.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/output.js @@ -17,34 +17,19 @@ class Foo { function fnDeep() { return deep; } - function f(o, r = (() => { - var _o$Foo; - return o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(_o$Foo = o.Foo, Foo, _m).call(_o$Foo); - })()) { + function f(o, r = (_o$Foo => o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(_o$Foo = o.Foo, Foo, _m).call(_o$Foo))()) { return r; } - function g(o, r = (() => { - var _ref; - return (_ref = (() => o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(babelHelpers.classStaticPrivateFieldSpecGet(o.Foo, Foo, _self).getSelf(), Foo, _m))()) === null || _ref === void 0 ? void 0 : _ref(); - })()) { + function g(o, r = (_ref => (_ref = (() => o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(babelHelpers.classStaticPrivateFieldSpecGet(o.Foo, Foo, _self).getSelf(), Foo, _m))()) === null || _ref === void 0 ? void 0 : _ref())()) { return r; } - function h(fnDeep, r = (() => { - var _fnDeep$very$o$Foo, _fnDeep$very$o; - return (_fnDeep$very$o$Foo = fnDeep === null || fnDeep === void 0 ? void 0 : (_fnDeep$very$o = fnDeep().very.o) === null || _fnDeep$very$o === void 0 ? void 0 : _fnDeep$very$o.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo, Foo, _m).call(_fnDeep$very$o$Foo); - })()) { + function h(fnDeep, r = ((_fnDeep$very$o$Foo, _fnDeep$very$o) => (_fnDeep$very$o$Foo = fnDeep === null || fnDeep === void 0 ? void 0 : (_fnDeep$very$o = fnDeep().very.o) === null || _fnDeep$very$o === void 0 ? void 0 : _fnDeep$very$o.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo, Foo, _m).call(_fnDeep$very$o$Foo))()) { return r; } - function i(fn, r = (() => { - var _getSelf, _getSelf$self, _ref2; - return (_getSelf = (_ref2 = (() => fn === null || fn === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(fn().Foo, Foo, _self))()) === null || _ref2 === void 0 ? void 0 : _ref2.getSelf()) === null || _getSelf === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(_getSelf$self = _getSelf.self, Foo, _m).call(_getSelf$self); - })()) { + function i(fn, r = ((_getSelf, _getSelf$self, _ref2) => (_getSelf = (_ref2 = (() => fn === null || fn === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(fn().Foo, Foo, _self))()) === null || _ref2 === void 0 ? void 0 : _ref2.getSelf()) === null || _getSelf === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(_getSelf$self = _getSelf.self, Foo, _m).call(_getSelf$self))()) { return r; } - function j(fn, r = (() => { - var _babelHelpers$classSt, _babelHelpers$classSt2; - return (_babelHelpers$classSt2 = babelHelpers.classStaticPrivateFieldSpecGet(_babelHelpers$classSt = babelHelpers.classStaticPrivateFieldSpecGet(fn().Foo, Foo, _self).getSelf().self, Foo, _m)) === null || _babelHelpers$classSt2 === void 0 ? void 0 : _babelHelpers$classSt2.call(_babelHelpers$classSt); - })()) { + function j(fn, r = ((_babelHelpers$classSt, _babelHelpers$classSt2) => (_babelHelpers$classSt2 = babelHelpers.classStaticPrivateFieldSpecGet(_babelHelpers$classSt = babelHelpers.classStaticPrivateFieldSpecGet(fn().Foo, Foo, _self).getSelf().self, Foo, _m)) === null || _babelHelpers$classSt2 === void 0 ? void 0 : _babelHelpers$classSt2.call(_babelHelpers$classSt))()) { return r; } f(o); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/private/optional-chain-in-function-param/output.js b/packages/babel-plugin-transform-class-properties/test/fixtures/private/optional-chain-in-function-param/output.js index ab916fd13b2a..5cacdc1c24f7 100644 --- a/packages/babel-plugin-transform-class-properties/test/fixtures/private/optional-chain-in-function-param/output.js +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/private/optional-chain-in-function-param/output.js @@ -17,31 +17,19 @@ class Foo { function fnDeep() { return deep; } - function f(o, r = (() => { - var _o$Foo; - return o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(_o$Foo = o.Foo, Foo, _m).call(_o$Foo); - })()) { + function f(o, r = (_o$Foo => o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(_o$Foo = o.Foo, Foo, _m).call(_o$Foo))()) { return r; } function g(o, r = (() => o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(babelHelpers.classStaticPrivateFieldSpecGet(o.Foo, Foo, _self).getSelf(), Foo, _m))()?.()) { return r; } - function h(fnDeep, r = (() => { - var _fnDeep$very$o$Foo; - return (_fnDeep$very$o$Foo = fnDeep?.().very.o?.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo, Foo, _m).call(_fnDeep$very$o$Foo); - })()) { + function h(fnDeep, r = (_fnDeep$very$o$Foo => (_fnDeep$very$o$Foo = fnDeep?.().very.o?.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo, Foo, _m).call(_fnDeep$very$o$Foo))()) { return r; } - function i(fn, r = (() => { - var _getSelf, _getSelf$self; - return (_getSelf = (() => fn === null || fn === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(fn().Foo, Foo, _self))()?.getSelf()) === null || _getSelf === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(_getSelf$self = _getSelf.self, Foo, _m).call(_getSelf$self); - })()) { + function i(fn, r = ((_getSelf, _getSelf$self) => (_getSelf = (() => fn === null || fn === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(fn().Foo, Foo, _self))()?.getSelf()) === null || _getSelf === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(_getSelf$self = _getSelf.self, Foo, _m).call(_getSelf$self))()) { return r; } - function j(fn, r = (() => { - var _babelHelpers$classSt; - return babelHelpers.classStaticPrivateFieldSpecGet(_babelHelpers$classSt = babelHelpers.classStaticPrivateFieldSpecGet(fn().Foo, Foo, _self).getSelf().self, Foo, _m)?.call(_babelHelpers$classSt); - })()) { + function j(fn, r = (_babelHelpers$classSt => babelHelpers.classStaticPrivateFieldSpecGet(_babelHelpers$classSt = babelHelpers.classStaticPrivateFieldSpecGet(fn().Foo, Foo, _self).getSelf().self, Foo, _m)?.call(_babelHelpers$classSt))()) { return r; } f(o); diff --git a/packages/babel-plugin-transform-nullish-coalescing-operator/test/fixtures/assumption-noDocumentAll/transform-in-default-param/output.js b/packages/babel-plugin-transform-nullish-coalescing-operator/test/fixtures/assumption-noDocumentAll/transform-in-default-param/output.js index e8abf378cda2..1c139b7172c2 100644 --- a/packages/babel-plugin-transform-nullish-coalescing-operator/test/fixtures/assumption-noDocumentAll/transform-in-default-param/output.js +++ b/packages/babel-plugin-transform-nullish-coalescing-operator/test/fixtures/assumption-noDocumentAll/transform-in-default-param/output.js @@ -1,5 +1,2 @@ -function foo(foo, qux = (() => { - var _foo$bar; - return (_foo$bar = foo.bar) != null ? _foo$bar : "qux"; -})()) {} +function foo(foo, qux = (_foo$bar => (_foo$bar = foo.bar) != null ? _foo$bar : "qux")()) {} function bar(bar, qux = bar != null ? bar : "qux") {} diff --git a/packages/babel-plugin-transform-nullish-coalescing-operator/test/fixtures/nullish-coalescing/transform-in-default-param/output.js b/packages/babel-plugin-transform-nullish-coalescing-operator/test/fixtures/nullish-coalescing/transform-in-default-param/output.js index f0cd7bfa5db0..d3a0a2f1523f 100644 --- a/packages/babel-plugin-transform-nullish-coalescing-operator/test/fixtures/nullish-coalescing/transform-in-default-param/output.js +++ b/packages/babel-plugin-transform-nullish-coalescing-operator/test/fixtures/nullish-coalescing/transform-in-default-param/output.js @@ -1,5 +1,2 @@ -function foo(foo, qux = (() => { - var _foo$bar; - return (_foo$bar = foo.bar) !== null && _foo$bar !== void 0 ? _foo$bar : "qux"; -})()) {} +function foo(foo, qux = (_foo$bar => (_foo$bar = foo.bar) !== null && _foo$bar !== void 0 ? _foo$bar : "qux")()) {} function bar(bar, qux = bar !== null && bar !== void 0 ? bar : "qux") {} diff --git a/packages/babel-plugin-transform-optional-chaining/test/fixtures/assumption-noDocumentAll/in-function-params/output.js b/packages/babel-plugin-transform-optional-chaining/test/fixtures/assumption-noDocumentAll/in-function-params/output.js index 0d24c266ca98..001a548c31b6 100644 --- a/packages/babel-plugin-transform-optional-chaining/test/fixtures/assumption-noDocumentAll/in-function-params/output.js +++ b/packages/babel-plugin-transform-optional-chaining/test/fixtures/assumption-noDocumentAll/in-function-params/output.js @@ -1,26 +1,14 @@ -function f(a = (() => { - var _x; - return (_x = x) == null ? void 0 : _x.y; -})()) {} +function f(a = (_x => (_x = x) == null ? void 0 : _x.y)()) {} function g({ a, b = a == null ? void 0 : a.c }) {} function h(a, { - b = (() => { - var _a$b, _a$b$c; - return (_a$b = a.b) == null ? void 0 : (_a$b$c = _a$b.c) == null ? void 0 : _a$b$c.d.e; - })() + b = ((_a$b, _a$b$c) => (_a$b = a.b) == null ? void 0 : (_a$b$c = _a$b.c) == null ? void 0 : _a$b$c.d.e)() }) {} function i(a, { - b = (() => { - var _a$b2, _a$b2$c; - return (_a$b2 = a.b) == null ? void 0 : (_a$b2$c = _a$b2.c) == null ? void 0 : _a$b2$c.d; - })().e + b = ((_a$b2, _a$b2$c) => (_a$b2 = a.b) == null ? void 0 : (_a$b2$c = _a$b2.c) == null ? void 0 : _a$b2$c.d)().e }) {} function j(a, { - b = (() => { - var _a$b3; - return a == null ? void 0 : (_a$b3 = a.b) == null ? void 0 : _a$b3.c().d.e; - })() + b = (_a$b3 => a == null ? void 0 : (_a$b3 = a.b) == null ? void 0 : _a$b3.c().d.e)() }) {} diff --git a/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/in-function-params-loose/output.js b/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/in-function-params-loose/output.js index 0d24c266ca98..001a548c31b6 100644 --- a/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/in-function-params-loose/output.js +++ b/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/in-function-params-loose/output.js @@ -1,26 +1,14 @@ -function f(a = (() => { - var _x; - return (_x = x) == null ? void 0 : _x.y; -})()) {} +function f(a = (_x => (_x = x) == null ? void 0 : _x.y)()) {} function g({ a, b = a == null ? void 0 : a.c }) {} function h(a, { - b = (() => { - var _a$b, _a$b$c; - return (_a$b = a.b) == null ? void 0 : (_a$b$c = _a$b.c) == null ? void 0 : _a$b$c.d.e; - })() + b = ((_a$b, _a$b$c) => (_a$b = a.b) == null ? void 0 : (_a$b$c = _a$b.c) == null ? void 0 : _a$b$c.d.e)() }) {} function i(a, { - b = (() => { - var _a$b2, _a$b2$c; - return (_a$b2 = a.b) == null ? void 0 : (_a$b2$c = _a$b2.c) == null ? void 0 : _a$b2$c.d; - })().e + b = ((_a$b2, _a$b2$c) => (_a$b2 = a.b) == null ? void 0 : (_a$b2$c = _a$b2.c) == null ? void 0 : _a$b2$c.d)().e }) {} function j(a, { - b = (() => { - var _a$b3; - return a == null ? void 0 : (_a$b3 = a.b) == null ? void 0 : _a$b3.c().d.e; - })() + b = (_a$b3 => a == null ? void 0 : (_a$b3 = a.b) == null ? void 0 : _a$b3.c().d.e)() }) {} diff --git a/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/in-function-params/output.js b/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/in-function-params/output.js index 8fe701b2e081..aa3e1ba282e0 100644 --- a/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/in-function-params/output.js +++ b/packages/babel-plugin-transform-optional-chaining/test/fixtures/general/in-function-params/output.js @@ -1,26 +1,14 @@ -function f(a = (() => { - var _x; - return (_x = x) === null || _x === void 0 ? void 0 : _x.y; -})()) {} +function f(a = (_x => (_x = x) === null || _x === void 0 ? void 0 : _x.y)()) {} function g({ a, b = a === null || a === void 0 ? void 0 : a.c }) {} function h(a, { - b = (() => { - var _a$b, _a$b$c; - return (_a$b = a.b) === null || _a$b === void 0 ? void 0 : (_a$b$c = _a$b.c) === null || _a$b$c === void 0 ? void 0 : _a$b$c.d.e; - })() + b = ((_a$b, _a$b$c) => (_a$b = a.b) === null || _a$b === void 0 ? void 0 : (_a$b$c = _a$b.c) === null || _a$b$c === void 0 ? void 0 : _a$b$c.d.e)() }) {} function i(a, { - b = (() => { - var _a$b2, _a$b2$c; - return (_a$b2 = a.b) === null || _a$b2 === void 0 ? void 0 : (_a$b2$c = _a$b2.c) === null || _a$b2$c === void 0 ? void 0 : _a$b2$c.d; - })().e + b = ((_a$b2, _a$b2$c) => (_a$b2 = a.b) === null || _a$b2 === void 0 ? void 0 : (_a$b2$c = _a$b2.c) === null || _a$b2$c === void 0 ? void 0 : _a$b2$c.d)().e }) {} function j(a, { - b = (() => { - var _a$b3; - return a === null || a === void 0 ? void 0 : (_a$b3 = a.b) === null || _a$b3 === void 0 ? void 0 : _a$b3.c().d.e; - })() + b = (_a$b3 => a === null || a === void 0 ? void 0 : (_a$b3 = a.b) === null || _a$b3 === void 0 ? void 0 : _a$b3.c().d.e)() }) {} diff --git a/packages/babel-plugin-transform-private-property-in-object/test/fixtures/to-native-fields/class-expression-in-default-param/output.js b/packages/babel-plugin-transform-private-property-in-object/test/fixtures/to-native-fields/class-expression-in-default-param/output.js index 4512aecc3a6c..e5c428401b5d 100644 --- a/packages/babel-plugin-transform-private-property-in-object/test/fixtures/to-native-fields/class-expression-in-default-param/output.js +++ b/packages/babel-plugin-transform-private-property-in-object/test/fixtures/to-native-fields/class-expression-in-default-param/output.js @@ -1,9 +1,6 @@ -(x = (() => { - var _fooBrandCheck; - return _fooBrandCheck = /*#__PURE__*/new WeakSet(), class { - #foo = void _fooBrandCheck.add(this); - test(other) { - return _fooBrandCheck.has(babelHelpers.checkInRHS(other)); - } - }; -})()) => {}; +(x = (_fooBrandCheck => (_fooBrandCheck = /*#__PURE__*/new WeakSet(), class { + #foo = void _fooBrandCheck.add(this); + test(other) { + return _fooBrandCheck.has(babelHelpers.checkInRHS(other)); + } +}))()) => {}; diff --git a/packages/babel-traverse/src/scope/index.ts b/packages/babel-traverse/src/scope/index.ts index 832b8b5b56aa..f32d752c9f3c 100644 --- a/packages/babel-traverse/src/scope/index.ts +++ b/packages/babel-traverse/src/scope/index.ts @@ -51,7 +51,7 @@ import { isPrivateName, isExportDeclaration, } from "@babel/types"; -import type * as t from "@babel/types"; +import * as t from "@babel/types"; import { scope as scopeCache } from "../cache"; import type { Visitor } from "../types"; import { isExplodedVisitor } from "../visitors"; @@ -1069,6 +1069,31 @@ export default class Scope { path = (this.getFunctionParent() || this.getProgramParent()).path; } + const { init, unique, kind = "var", id } = opts; + + // When injecting a non-const non-initialized binding inside + // an IIFE, if the number of call arguments is less than or + // equal to the number of function parameters, we can safely + // inject the binding into the parameter list. + if ( + !init && + !unique && + kind !== "const" && + path.isFunction() && + // @ts-expect-error ArrowFunctionExpression never has a name + !path.node.name && + t.isCallExpression(path.parent, { callee: path.node }) && + path.parent.arguments.length <= path.node.params.length && + t.isIdentifier(id) + ) { + path.pushContainer("params", id); + path.scope.registerBinding( + "param", + path.get("params")[path.node.params.length - 1], + ); + return; + } + if (path.isLoop() || path.isCatchClause() || path.isFunction()) { // @ts-expect-error TS can not infer NodePath | NodePath as NodePath path.ensureBlock(); @@ -1076,8 +1101,6 @@ export default class Scope { path = path.get("body"); } - const unique = opts.unique; - const kind = opts.kind || "var"; const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist; const dataKey = `declaration:${kind}:${blockHoist}`; @@ -1095,7 +1118,7 @@ export default class Scope { if (!unique) path.setData(dataKey, declarPath); } - const declarator = variableDeclarator(opts.id, opts.init); + const declarator = variableDeclarator(id, init); const len = declarPath.node.declarations.push(declarator); path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]); } From 1a25d862e575fe752001581e095899dcf576e1e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 3 Jul 2023 20:03:35 +0200 Subject: [PATCH 2/2] Update packages/babel-traverse/src/scope/index.ts --- packages/babel-traverse/src/scope/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-traverse/src/scope/index.ts b/packages/babel-traverse/src/scope/index.ts index f32d752c9f3c..66fd6167222d 100644 --- a/packages/babel-traverse/src/scope/index.ts +++ b/packages/babel-traverse/src/scope/index.ts @@ -1078,7 +1078,7 @@ export default class Scope { if ( !init && !unique && - kind !== "const" && + (kind === "var" || kind === "let") && path.isFunction() && // @ts-expect-error ArrowFunctionExpression never has a name !path.node.name &&