Skip to content

Commit

Permalink
Revert "Improve output when wrapping functions" (#15979)
Browse files Browse the repository at this point in the history
* Add a test

* Revert "Improve output when wrapping functions (e.g. `async` functions) (#15922)"

This reverts commit b9a2244.

* Update snapshot
  • Loading branch information
jjonescz committed Sep 16, 2023
1 parent 95dcdae commit ad26f90
Show file tree
Hide file tree
Showing 76 changed files with 753 additions and 383 deletions.
4 changes: 3 additions & 1 deletion packages/babel-helper-remap-async-to-generator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* @noflow */

import type { NodePath } from "@babel/traverse";
import wrapFunction from "@babel/helper-wrap-function";
import annotateAsPure from "@babel/helper-annotate-as-pure";
Expand Down Expand Up @@ -63,7 +65,7 @@ export default function (
path.parentPath.isObjectProperty() ||
path.parentPath.isClassProperty();

if (!isProperty && !isIIFE && path.isCallExpression()) {
if (!isProperty && !isIIFE && path.isExpression()) {
annotateAsPure(path);
}

Expand Down
106 changes: 76 additions & 30 deletions packages/babel-helper-wrap-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,56 @@ import {
isRestElement,
returnStatement,
isCallExpression,
cloneNode,
toExpression,
} from "@babel/types";
import type * as t from "@babel/types";

const buildWrapper = template.statement(`
function NAME(PARAMS) {
return (REF = REF || FUNCTION).apply(this, arguments);
type ExpressionWrapperBuilder<ExtraBody extends t.Node[]> = (
replacements?: Parameters<ReturnType<typeof template.expression>>[0],
) => t.CallExpression & {
callee: t.FunctionExpression & {
body: {
body: [
t.VariableDeclaration & {
declarations: [
{ init: t.FunctionExpression | t.ArrowFunctionExpression },
];
},
...ExtraBody,
];
};
};
};

const buildAnonymousExpressionWrapper = template.expression(`
(function () {
var REF = FUNCTION;
return function NAME(PARAMS) {
return REF.apply(this, arguments);
};
})()
`) as ExpressionWrapperBuilder<
[t.ReturnStatement & { argument: t.FunctionExpression }]
>;

const buildNamedExpressionWrapper = template.expression(`
(function () {
var REF = FUNCTION;
function NAME(PARAMS) {
return REF.apply(this, arguments);
}
return NAME;
})()
`) as ExpressionWrapperBuilder<
[t.FunctionDeclaration, t.ReturnStatement & { argument: t.Identifier }]
>;

const buildDeclarationWrapper = template.statements(`
function NAME(PARAMS) { return REF.apply(this, arguments); }
function REF() {
REF = FUNCTION;
return REF.apply(this, arguments);
}
`) as (
replacements: Parameters<ReturnType<typeof template.expression>>[0],
) => t.FunctionDeclaration;
`);

function classOrObjectMethod(
path: NodePath<t.ClassMethod | t.ClassPrivateMethod | t.ObjectMethod>,
Expand Down Expand Up @@ -102,32 +140,40 @@ function plainFunction(
params.push(path.scope.generateUidIdentifier("x"));
}

const ref = path.scope.generateUidIdentifier(
functionId ? functionId.name : "ref",
);

let wrapper: t.Function = buildWrapper({
NAME: functionId,
REF: ref,
const wrapperArgs = {
NAME: functionId || null,
REF: path.scope.generateUidIdentifier(functionId ? functionId.name : "ref"),
FUNCTION: built,
PARAMS: params,
});

if (!isDeclaration) {
wrapper = toExpression(wrapper);
nameFunction({
node: wrapper,
parent: (path as NodePath<t.FunctionExpression>).parent,
scope: path.scope,
});
}
};

if (isDeclaration || wrapper.id || (!ignoreFunctionLength && params.length)) {
path.replaceWith(wrapper);
path.parentPath.scope.push({ id: cloneNode(ref) });
if (isDeclaration) {
const container = buildDeclarationWrapper(wrapperArgs);
path.replaceWith(container[0]);
path.insertAfter(container[1]);
} else {
// we can omit this wrapper as the conditions it protects for do not apply
path.replaceWith(built);
let container;

if (functionId) {
container = buildNamedExpressionWrapper(wrapperArgs);
} else {
container = buildAnonymousExpressionWrapper(wrapperArgs);

const returnFn = container.callee.body.body[1].argument;
nameFunction({
node: returnFn,
parent: (path as NodePath<t.FunctionExpression>).parent,
scope: path.scope,
});
functionId = returnFn.id;
}

if (functionId || (!ignoreFunctionLength && params.length)) {
path.replaceWith(container);
} else {
// we can omit this wrapper as the conditions it protects for do not apply
path.replaceWith(built);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var _fn;
function fn() {
return (_fn = _fn || babelHelpers.asyncToGenerator(function* () {
return _fn.apply(this, arguments);
}
function _fn() {
_fn = babelHelpers.asyncToGenerator(function* () {
yield 0;
try {
var _stack = [];
Expand All @@ -12,5 +14,6 @@ function fn() {
} finally {
yield babelHelpers.dispose(_stack, _error, _hasError);
}
})).apply(this, arguments);
});
return _fn.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var _gen;
function gen() {
return (_gen = _gen || babelHelpers.skipFirstGeneratorNext(function* () {
return _gen.apply(this, arguments);
}
function _gen() {
_gen = babelHelpers.skipFirstGeneratorNext(function* () {
let _functionSent = yield;
let sent = _functionSent;
})).apply(this, arguments);
});
return _gen.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var _foo;
function foo() {
return (_foo = _foo || babelHelpers.wrapAsyncGenerator(babelHelpers.skipFirstGeneratorNext(function* () {
return _foo.apply(this, arguments);
}
function _foo() {
_foo = babelHelpers.wrapAsyncGenerator(babelHelpers.skipFirstGeneratorNext(function* () {
let _functionSent = yield;
_functionSent = yield babelHelpers.awaitAsyncGenerator(_functionSent);
}))).apply(this, arguments);
}));
return _foo.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var _ref;
export default function () {
return (_ref = _ref || babelHelpers.skipFirstGeneratorNext(function* () {
return _ref.apply(this, arguments);
}
function _ref() {
_ref = babelHelpers.skipFirstGeneratorNext(function* () {
let _functionSent = yield;
return _functionSent;
})).apply(this, arguments);
});
return _ref.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var _gen;
export default function gen() {
return (_gen = _gen || babelHelpers.skipFirstGeneratorNext(function* () {
return _gen.apply(this, arguments);
}
function _gen() {
_gen = babelHelpers.skipFirstGeneratorNext(function* () {
let _functionSent = yield;
return _functionSent;
})).apply(this, arguments);
});
return _gen.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var _gen;
export function gen() {
return (_gen = _gen || babelHelpers.skipFirstGeneratorNext(function* () {
return _gen.apply(this, arguments);
}
function _gen() {
_gen = babelHelpers.skipFirstGeneratorNext(function* () {
let _functionSent = yield;
return _functionSent;
})).apply(this, arguments);
});
return _gen.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var _gen;
const foo = function gen() {
return (_gen = _gen || babelHelpers.skipFirstGeneratorNext(function* () {
const foo = function () {
var _gen = babelHelpers.skipFirstGeneratorNext(function* () {
let _functionSent = yield;
return _functionSent;
})).apply(this, arguments);
};
});
function gen() {
return _gen.apply(this, arguments);
}
return gen;
}();
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var _gen;
function gen() {
return (_gen = _gen || babelHelpers.skipFirstGeneratorNext(function* () {
return _gen.apply(this, arguments);
}
function _gen() {
_gen = babelHelpers.skipFirstGeneratorNext(function* () {
let _functionSent = yield;
return _functionSent;
})).apply(this, arguments);
});
return _gen.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
var _agf;
function agf() {
return (_agf = _agf || babelHelpers.wrapAsyncGenerator(function* () {
return _agf.apply(this, arguments);
}
function _agf() {
_agf = babelHelpers.wrapAsyncGenerator(function* () {
this;
yield babelHelpers.awaitAsyncGenerator(1);
yield 2;
return 3;
})).apply(this, arguments);
});
return _agf.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
var _agf;
(function agf() {
return (_agf = _agf || babelHelpers.wrapAsyncGenerator(function* () {
/*#__PURE__*/(function () {
var _agf = babelHelpers.wrapAsyncGenerator(function* () {
this;
yield babelHelpers.awaitAsyncGenerator(1);
yield 2;
return 3;
})).apply(this, arguments);
});
});
function agf() {
return _agf.apply(this, arguments);
}
return agf;
})();
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
var _fn;
function fn() {
return (_fn = _fn || babelHelpers.wrapAsyncGenerator(function* () {
return _fn.apply(this, arguments);
}
function _fn() {
_fn = babelHelpers.wrapAsyncGenerator(function* () {
class A {
[yield 1]() {}
}
class B extends A {
[yield babelHelpers.awaitAsyncGenerator(1)]() {}
}
})).apply(this, arguments);
});
return _fn.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var _g;
function g() {
return (_g = _g || babelHelpers.wrapAsyncGenerator(function* () {
return _g.apply(this, arguments);
}
function _g() {
_g = babelHelpers.wrapAsyncGenerator(function* () {
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator([1, 2, 3]), babelHelpers.awaitAsyncGenerator);
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator(iterable), babelHelpers.awaitAsyncGenerator);
})).apply(this, arguments);
});
return _g.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var _g;
function g() {
return (_g = _g || babelHelpers.wrapAsyncGenerator(function* () {
return _g.apply(this, arguments);
}
function _g() {
_g = babelHelpers.wrapAsyncGenerator(function* () {
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator([1, 2, 3]));
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator(iterable));
})).apply(this, arguments);
});
return _g.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var _f;
function f() {
return (_f = _f || babelHelpers.asyncToGenerator(function* () {
return _f.apply(this, arguments);
}
function _f() {
_f = babelHelpers.asyncToGenerator(function* () {
var _iteratorAbruptCompletion = false;
var _didIteratorError = false;
var _iteratorError;
Expand All @@ -25,5 +27,6 @@ function f() {
}
}
}
})).apply(this, arguments);
});
return _f.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var _g;
function g() {
return (_g = _g || babelHelpers.wrapAsyncGenerator(function* () {
return _g.apply(this, arguments);
}
function _g() {
_g = babelHelpers.wrapAsyncGenerator(function* () {
var _iteratorAbruptCompletion = false;
var _didIteratorError = false;
var _iteratorError;
Expand All @@ -25,5 +27,6 @@ function g() {
}
}
}
})).apply(this, arguments);
});
return _g.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var _fn;
function fn() {
return (_fn = _fn || babelHelpers.wrapAsyncGenerator(function* () {
return _fn.apply(this, arguments);
}
function _fn() {
_fn = babelHelpers.wrapAsyncGenerator(function* () {
var _iteratorAbruptCompletion = false;
var _didIteratorError = false;
var _iteratorError;
Expand All @@ -27,5 +29,6 @@ function fn() {
}
}
}
})).apply(this, arguments);
});
return _fn.apply(this, arguments);
}

0 comments on commit ad26f90

Please sign in to comment.