From 2b841c8c9c3dcdda5e94700de18e992fb773b6a0 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Thu, 10 Nov 2022 20:52:43 +0800 Subject: [PATCH 1/2] fix --- .../babel-helper-wrap-function/src/index.ts | 42 ++++++++++++------- .../plugins-integration/issue-15170/exec.js | 3 ++ .../plugins-integration/issue-15170/input.js | 1 + .../issue-15170/options.json | 4 ++ .../plugins-integration/issue-15170/output.js | 42 +++++++++++++++++++ .../babel-traverse/src/path/conversion.ts | 4 +- 6 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/exec.js create mode 100644 packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/input.js create mode 100644 packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/options.json create mode 100644 packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/output.js diff --git a/packages/babel-helper-wrap-function/src/index.ts b/packages/babel-helper-wrap-function/src/index.ts index 503848cc23fb..0e6b0eab1212 100644 --- a/packages/babel-helper-wrap-function/src/index.ts +++ b/packages/babel-helper-wrap-function/src/index.ts @@ -9,6 +9,7 @@ import { isFunctionDeclaration, isRestElement, returnStatement, + isCallExpression, } from "@babel/types"; import type * as t from "@babel/types"; @@ -89,37 +90,50 @@ function classOrObjectMethod( } function plainFunction( - path: NodePath>, + inPath: NodePath>, callId: t.Expression, noNewArrows: boolean, ignoreFunctionLength: boolean, ) { - let functionId = null; + let path: NodePath< + | t.FunctionDeclaration + | t.FunctionExpression + | t.CallExpression + | t.ArrowFunctionExpression + > = inPath; let node; - if (path.isArrowFunctionExpression()) { + let functionId = null; + const nodeParams = inPath.node.params; + + if (inPath.isArrowFunctionExpression()) { if (process.env.BABEL_8_BREAKING) { - path = path.arrowFunctionToExpression({ noNewArrows }); + path = inPath.arrowFunctionToExpression({ noNewArrows }); } else { // arrowFunctionToExpression returns undefined in @babel/traverse < 7.18.10 - path = path.arrowFunctionToExpression({ noNewArrows }) ?? path; + path = inPath.arrowFunctionToExpression({ noNewArrows }) ?? inPath; } - node = path.node as t.FunctionDeclaration | t.FunctionExpression; + node = path.node as + | t.FunctionDeclaration + | t.FunctionExpression + | t.CallExpression; } else { node = path.node as t.FunctionDeclaration | t.FunctionExpression; } const isDeclaration = isFunctionDeclaration(node); - functionId = node.id; - node.id = null; - node.type = "FunctionExpression"; - - const built = callExpression(callId, [ - node as Exclude, - ]); + let built = node; + if (!isCallExpression(node)) { + functionId = node.id; + node.id = null; + node.type = "FunctionExpression"; + built = callExpression(callId, [ + node as Exclude, + ]); + } const params: t.Identifier[] = []; - for (const param of node.params) { + for (const param of nodeParams) { if (isAssignmentPattern(param) || isRestElement(param)) { break; } diff --git a/packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/exec.js b/packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/exec.js new file mode 100644 index 000000000000..804017c9d44a --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/exec.js @@ -0,0 +1,3 @@ +expect(() => { + x = async x => 0 ; +}).not.toThrow(); diff --git a/packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/input.js b/packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/input.js new file mode 100644 index 000000000000..fbe739cb07b1 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/input.js @@ -0,0 +1 @@ +x = async x => 0 ; diff --git a/packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/options.json b/packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/options.json new file mode 100644 index 000000000000..672f52f811fb --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/options.json @@ -0,0 +1,4 @@ +{ + "presets": ["env"], + "assumptions": { "noNewArrows": false } +} diff --git a/packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/output.js b/packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/output.js new file mode 100644 index 000000000000..5097fb4f42f9 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/plugins-integration/issue-15170/output.js @@ -0,0 +1,42 @@ +var _this = this; +x = /*#__PURE__*/function () { + var _ref = function (_x) { + var _marked = /*#__PURE__*/babelHelpers.regeneratorRuntime().mark(x); + function x(_x2) { + var _args = arguments; + return babelHelpers.regeneratorRuntime().wrap(function x$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + return _context.delegateYield(_x.apply(this, _args), "t0", 1); + case 1: + return _context.abrupt("return", _context.t0); + case 2: + case "end": + return _context.stop(); + } + } + }, _marked, this); + } + x.toString = function () { + return _x.toString(); + }; + return x; + }( /*#__PURE__*/babelHelpers.regeneratorRuntime().mark(function _callee(x) { + return babelHelpers.regeneratorRuntime().wrap(function _callee$(_context2) { + while (1) { + switch (_context2.prev = _context2.next) { + case 0: + babelHelpers.newArrowCheck(this, _this); + return _context2.abrupt("return", 0); + case 2: + case "end": + return _context2.stop(); + } + } + }, _callee, this); + })); + return function (_x3) { + return _ref.apply(this, arguments); + }; +}().bind(this); diff --git a/packages/babel-traverse/src/path/conversion.ts b/packages/babel-traverse/src/path/conversion.ts index eecdf85af83a..2c36ddcb2034 100644 --- a/packages/babel-traverse/src/path/conversion.ts +++ b/packages/babel-traverse/src/path/conversion.ts @@ -158,7 +158,9 @@ export function arrowFunctionToExpression( specCompliant?: boolean | void; noNewArrows?: boolean; } = {}, -): NodePath> { +): NodePath< + Exclude | t.CallExpression +> { if (!this.isArrowFunctionExpression()) { throw (this as NodePath).buildCodeFrameError( "Cannot convert non-arrow function to a function expression.", From e6013dc98fe3fc7cd1073c049e340fb8621b3a8e Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Fri, 25 Nov 2022 20:27:15 +0800 Subject: [PATCH 2/2] review --- packages/babel-helper-wrap-function/src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/babel-helper-wrap-function/src/index.ts b/packages/babel-helper-wrap-function/src/index.ts index 0e6b0eab1212..a7a008f5b754 100644 --- a/packages/babel-helper-wrap-function/src/index.ts +++ b/packages/babel-helper-wrap-function/src/index.ts @@ -105,12 +105,12 @@ function plainFunction( let functionId = null; const nodeParams = inPath.node.params; - if (inPath.isArrowFunctionExpression()) { + if (path.isArrowFunctionExpression()) { if (process.env.BABEL_8_BREAKING) { - path = inPath.arrowFunctionToExpression({ noNewArrows }); + path = path.arrowFunctionToExpression({ noNewArrows }); } else { // arrowFunctionToExpression returns undefined in @babel/traverse < 7.18.10 - path = inPath.arrowFunctionToExpression({ noNewArrows }) ?? inPath; + path = path.arrowFunctionToExpression({ noNewArrows }) ?? path; } node = path.node as | t.FunctionDeclaration