Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Edge cases for async functions and noNewArrow assumption #15181

Merged
merged 2 commits into from Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 28 additions & 14 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;
if (path.isArrowFunctionExpression()) {
let functionId = null;
const nodeParams = inPath.node.params;

if (inPath.isArrowFunctionExpression()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we always use path instead of inPath (i.e. only use inPath for the original let path = inPath declaration)? I was initially confused about what's the difference between the two variables, before realizing that it's just because path can later become a CallExpression but we only want to allow functions as parameters.

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<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 @@ -158,7 +158,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