Skip to content

Commit

Permalink
fix: Edge cases for async functions and noNewArrow assumption (#15181)
Browse files Browse the repository at this point in the history
Fixes #15170
  • Loading branch information
liuxingbaoyu committed Nov 25, 2022
1 parent 4c59d9f commit c78fc49
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 12 deletions.
36 changes: 25 additions & 11 deletions packages/babel-helper-wrap-function/src/index.ts
Expand Up @@ -9,6 +9,7 @@ import {
isFunctionDeclaration,
isRestElement,
returnStatement,
isCallExpression,
} from "@babel/types";
import type * as t from "@babel/types";

Expand Down Expand Up @@ -89,37 +90,50 @@ function classOrObjectMethod(
}

function plainFunction(
path: NodePath<Exclude<t.Function, t.Method>>,
inPath: NodePath<Exclude<t.Function, t.Method>>,
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 });
} else {
// 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<typeof node, t.FunctionDeclaration>,
]);
let built = node;
if (!isCallExpression(node)) {
functionId = node.id;
node.id = null;
node.type = "FunctionExpression";
built = callExpression(callId, [
node as Exclude<typeof node, t.FunctionDeclaration>,
]);
}

const params: t.Identifier[] = [];
for (const param of node.params) {
for (const param of nodeParams) {
if (isAssignmentPattern(param) || isRestElement(param)) {
break;
}
Expand Down
@@ -0,0 +1,3 @@
expect(() => {
x = async x => 0 ;
}).not.toThrow();
@@ -0,0 +1 @@
x = async x => 0 ;
@@ -0,0 +1,4 @@
{
"presets": ["env"],
"assumptions": { "noNewArrows": false }
}
@@ -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);
4 changes: 3 additions & 1 deletion packages/babel-traverse/src/path/conversion.ts
Expand Up @@ -160,7 +160,9 @@ export function arrowFunctionToExpression(
specCompliant?: boolean | void;
noNewArrows?: boolean;
} = {},
): NodePath<Exclude<t.Function, t.Method | t.ArrowFunctionExpression>> {
): NodePath<
Exclude<t.Function, t.Method | t.ArrowFunctionExpression> | t.CallExpression
> {
if (!this.isArrowFunctionExpression()) {
throw (this as NodePath).buildCodeFrameError(
"Cannot convert non-arrow function to a function expression.",
Expand Down

0 comments on commit c78fc49

Please sign in to comment.