diff --git a/packages/babel-helper-wrap-function/src/index.ts b/packages/babel-helper-wrap-function/src/index.ts index 503848cc23fb..a7a008f5b754 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,13 +90,21 @@ 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; + let functionId = null; + const nodeParams = inPath.node.params; + if (path.isArrowFunctionExpression()) { if (process.env.BABEL_8_BREAKING) { path = path.arrowFunctionToExpression({ noNewArrows }); @@ -103,23 +112,28 @@ function plainFunction( // arrowFunctionToExpression returns undefined in @babel/traverse < 7.18.10 path = path.arrowFunctionToExpression({ noNewArrows }) ?? path; } - 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 041abea0d8c1..554d3e6ae8ab 100644 --- a/packages/babel-traverse/src/path/conversion.ts +++ b/packages/babel-traverse/src/path/conversion.ts @@ -160,7 +160,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.",