Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ad26f90

Browse filesBrowse files
authored
Revert "Improve output when wrapping functions" (#15979)
* Add a test * Revert "Improve output when wrapping functions (e.g. `async` functions) (#15922)" This reverts commit b9a2244. * Update snapshot
1 parent 95dcdae commit ad26f90
Copy full SHA for ad26f90

File tree

76 files changed

+753
-383
lines changed
Filter options

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner

76 files changed

+753
-383
lines changed

‎packages/babel-helper-remap-async-to-generator/src/index.ts

Copy file name to clipboardExpand all lines: packages/babel-helper-remap-async-to-generator/src/index.ts
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* @noflow */
2+
13
import type { NodePath } from "@babel/traverse";
24
import wrapFunction from "@babel/helper-wrap-function";
35
import annotateAsPure from "@babel/helper-annotate-as-pure";
@@ -63,7 +65,7 @@ export default function (
6365
path.parentPath.isObjectProperty() ||
6466
path.parentPath.isClassProperty();
6567

66-
if (!isProperty && !isIIFE && path.isCallExpression()) {
68+
if (!isProperty && !isIIFE && path.isExpression()) {
6769
annotateAsPure(path);
6870
}
6971

‎packages/babel-helper-wrap-function/src/index.ts

Copy file name to clipboardExpand all lines: packages/babel-helper-wrap-function/src/index.ts
+76-30Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,56 @@ import {
1010
isRestElement,
1111
returnStatement,
1212
isCallExpression,
13-
cloneNode,
14-
toExpression,
1513
} from "@babel/types";
1614
import type * as t from "@babel/types";
1715

18-
const buildWrapper = template.statement(`
19-
function NAME(PARAMS) {
20-
return (REF = REF || FUNCTION).apply(this, arguments);
16+
type ExpressionWrapperBuilder<ExtraBody extends t.Node[]> = (
17+
replacements?: Parameters<ReturnType<typeof template.expression>>[0],
18+
) => t.CallExpression & {
19+
callee: t.FunctionExpression & {
20+
body: {
21+
body: [
22+
t.VariableDeclaration & {
23+
declarations: [
24+
{ init: t.FunctionExpression | t.ArrowFunctionExpression },
25+
];
26+
},
27+
...ExtraBody,
28+
];
29+
};
30+
};
31+
};
32+
33+
const buildAnonymousExpressionWrapper = template.expression(`
34+
(function () {
35+
var REF = FUNCTION;
36+
return function NAME(PARAMS) {
37+
return REF.apply(this, arguments);
38+
};
39+
})()
40+
`) as ExpressionWrapperBuilder<
41+
[t.ReturnStatement & { argument: t.FunctionExpression }]
42+
>;
43+
44+
const buildNamedExpressionWrapper = template.expression(`
45+
(function () {
46+
var REF = FUNCTION;
47+
function NAME(PARAMS) {
48+
return REF.apply(this, arguments);
49+
}
50+
return NAME;
51+
})()
52+
`) as ExpressionWrapperBuilder<
53+
[t.FunctionDeclaration, t.ReturnStatement & { argument: t.Identifier }]
54+
>;
55+
56+
const buildDeclarationWrapper = template.statements(`
57+
function NAME(PARAMS) { return REF.apply(this, arguments); }
58+
function REF() {
59+
REF = FUNCTION;
60+
return REF.apply(this, arguments);
2161
}
22-
`) as (
23-
replacements: Parameters<ReturnType<typeof template.expression>>[0],
24-
) => t.FunctionDeclaration;
62+
`);
2563

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

105-
const ref = path.scope.generateUidIdentifier(
106-
functionId ? functionId.name : "ref",
107-
);
108-
109-
let wrapper: t.Function = buildWrapper({
110-
NAME: functionId,
111-
REF: ref,
143+
const wrapperArgs = {
144+
NAME: functionId || null,
145+
REF: path.scope.generateUidIdentifier(functionId ? functionId.name : "ref"),
112146
FUNCTION: built,
113147
PARAMS: params,
114-
});
115-
116-
if (!isDeclaration) {
117-
wrapper = toExpression(wrapper);
118-
nameFunction({
119-
node: wrapper,
120-
parent: (path as NodePath<t.FunctionExpression>).parent,
121-
scope: path.scope,
122-
});
123-
}
148+
};
124149

125-
if (isDeclaration || wrapper.id || (!ignoreFunctionLength && params.length)) {
126-
path.replaceWith(wrapper);
127-
path.parentPath.scope.push({ id: cloneNode(ref) });
150+
if (isDeclaration) {
151+
const container = buildDeclarationWrapper(wrapperArgs);
152+
path.replaceWith(container[0]);
153+
path.insertAfter(container[1]);
128154
} else {
129-
// we can omit this wrapper as the conditions it protects for do not apply
130-
path.replaceWith(built);
155+
let container;
156+
157+
if (functionId) {
158+
container = buildNamedExpressionWrapper(wrapperArgs);
159+
} else {
160+
container = buildAnonymousExpressionWrapper(wrapperArgs);
161+
162+
const returnFn = container.callee.body.body[1].argument;
163+
nameFunction({
164+
node: returnFn,
165+
parent: (path as NodePath<t.FunctionExpression>).parent,
166+
scope: path.scope,
167+
});
168+
functionId = returnFn.id;
169+
}
170+
171+
if (functionId || (!ignoreFunctionLength && params.length)) {
172+
path.replaceWith(container);
173+
} else {
174+
// we can omit this wrapper as the conditions it protects for do not apply
175+
path.replaceWith(built);
176+
}
131177
}
132178
}
133179

‎packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/integration/async-to-generator/output.js

Copy file name to clipboardExpand all lines: packages/babel-plugin-proposal-explicit-resource-management/test/fixtures/integration/async-to-generator/output.js
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
var _fn;
21
function fn() {
3-
return (_fn = _fn || babelHelpers.asyncToGenerator(function* () {
2+
return _fn.apply(this, arguments);
3+
}
4+
function _fn() {
5+
_fn = babelHelpers.asyncToGenerator(function* () {
46
yield 0;
57
try {
68
var _stack = [];
@@ -12,5 +14,6 @@ function fn() {
1214
} finally {
1315
yield babelHelpers.dispose(_stack, _error, _hasError);
1416
}
15-
})).apply(this, arguments);
17+
});
18+
return _fn.apply(this, arguments);
1619
}
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
var _gen;
21
function gen() {
3-
return (_gen = _gen || babelHelpers.skipFirstGeneratorNext(function* () {
2+
return _gen.apply(this, arguments);
3+
}
4+
function _gen() {
5+
_gen = babelHelpers.skipFirstGeneratorNext(function* () {
46
let _functionSent = yield;
57
let sent = _functionSent;
6-
})).apply(this, arguments);
8+
});
9+
return _gen.apply(this, arguments);
710
}
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
var _foo;
21
function foo() {
3-
return (_foo = _foo || babelHelpers.wrapAsyncGenerator(babelHelpers.skipFirstGeneratorNext(function* () {
2+
return _foo.apply(this, arguments);
3+
}
4+
function _foo() {
5+
_foo = babelHelpers.wrapAsyncGenerator(babelHelpers.skipFirstGeneratorNext(function* () {
46
let _functionSent = yield;
57
_functionSent = yield babelHelpers.awaitAsyncGenerator(_functionSent);
6-
}))).apply(this, arguments);
8+
}));
9+
return _foo.apply(this, arguments);
710
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
var _ref;
21
export default function () {
3-
return (_ref = _ref || babelHelpers.skipFirstGeneratorNext(function* () {
2+
return _ref.apply(this, arguments);
3+
}
4+
function _ref() {
5+
_ref = babelHelpers.skipFirstGeneratorNext(function* () {
46
let _functionSent = yield;
57
return _functionSent;
6-
})).apply(this, arguments);
8+
});
9+
return _ref.apply(this, arguments);
710
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
var _gen;
21
export default function gen() {
3-
return (_gen = _gen || babelHelpers.skipFirstGeneratorNext(function* () {
2+
return _gen.apply(this, arguments);
3+
}
4+
function _gen() {
5+
_gen = babelHelpers.skipFirstGeneratorNext(function* () {
46
let _functionSent = yield;
57
return _functionSent;
6-
})).apply(this, arguments);
8+
});
9+
return _gen.apply(this, arguments);
710
}
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
var _gen;
21
export function gen() {
3-
return (_gen = _gen || babelHelpers.skipFirstGeneratorNext(function* () {
2+
return _gen.apply(this, arguments);
3+
}
4+
function _gen() {
5+
_gen = babelHelpers.skipFirstGeneratorNext(function* () {
46
let _functionSent = yield;
57
return _functionSent;
6-
})).apply(this, arguments);
8+
});
9+
return _gen.apply(this, arguments);
710
}
+8-5Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
var _gen;
2-
const foo = function gen() {
3-
return (_gen = _gen || babelHelpers.skipFirstGeneratorNext(function* () {
1+
const foo = function () {
2+
var _gen = babelHelpers.skipFirstGeneratorNext(function* () {
43
let _functionSent = yield;
54
return _functionSent;
6-
})).apply(this, arguments);
7-
};
5+
});
6+
function gen() {
7+
return _gen.apply(this, arguments);
8+
}
9+
return gen;
10+
}();
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
var _gen;
21
function gen() {
3-
return (_gen = _gen || babelHelpers.skipFirstGeneratorNext(function* () {
2+
return _gen.apply(this, arguments);
3+
}
4+
function _gen() {
5+
_gen = babelHelpers.skipFirstGeneratorNext(function* () {
46
let _functionSent = yield;
57
return _functionSent;
6-
})).apply(this, arguments);
8+
});
9+
return _gen.apply(this, arguments);
710
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
var _agf;
21
function agf() {
3-
return (_agf = _agf || babelHelpers.wrapAsyncGenerator(function* () {
2+
return _agf.apply(this, arguments);
3+
}
4+
function _agf() {
5+
_agf = babelHelpers.wrapAsyncGenerator(function* () {
46
this;
57
yield babelHelpers.awaitAsyncGenerator(1);
68
yield 2;
79
return 3;
8-
})).apply(this, arguments);
10+
});
11+
return _agf.apply(this, arguments);
912
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
var _agf;
2-
(function agf() {
3-
return (_agf = _agf || babelHelpers.wrapAsyncGenerator(function* () {
1+
/*#__PURE__*/(function () {
2+
var _agf = babelHelpers.wrapAsyncGenerator(function* () {
43
this;
54
yield babelHelpers.awaitAsyncGenerator(1);
65
yield 2;
76
return 3;
8-
})).apply(this, arguments);
9-
});
7+
});
8+
function agf() {
9+
return _agf.apply(this, arguments);
10+
}
11+
return agf;
12+
})();
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
var _fn;
21
function fn() {
3-
return (_fn = _fn || babelHelpers.wrapAsyncGenerator(function* () {
2+
return _fn.apply(this, arguments);
3+
}
4+
function _fn() {
5+
_fn = babelHelpers.wrapAsyncGenerator(function* () {
46
class A {
57
[yield 1]() {}
68
}
79
class B extends A {
810
[yield babelHelpers.awaitAsyncGenerator(1)]() {}
911
}
10-
})).apply(this, arguments);
12+
});
13+
return _fn.apply(this, arguments);
1114
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
var _g;
21
function g() {
3-
return (_g = _g || babelHelpers.wrapAsyncGenerator(function* () {
2+
return _g.apply(this, arguments);
3+
}
4+
function _g() {
5+
_g = babelHelpers.wrapAsyncGenerator(function* () {
46
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator([1, 2, 3]), babelHelpers.awaitAsyncGenerator);
57
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator(iterable), babelHelpers.awaitAsyncGenerator);
6-
})).apply(this, arguments);
8+
});
9+
return _g.apply(this, arguments);
710
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
var _g;
21
function g() {
3-
return (_g = _g || babelHelpers.wrapAsyncGenerator(function* () {
2+
return _g.apply(this, arguments);
3+
}
4+
function _g() {
5+
_g = babelHelpers.wrapAsyncGenerator(function* () {
46
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator([1, 2, 3]));
57
yield* babelHelpers.asyncGeneratorDelegate(babelHelpers.asyncIterator(iterable));
6-
})).apply(this, arguments);
8+
});
9+
return _g.apply(this, arguments);
710
}

‎packages/babel-plugin-transform-async-generator-functions/test/fixtures/for-await/async-function/output.js

Copy file name to clipboardExpand all lines: packages/babel-plugin-transform-async-generator-functions/test/fixtures/for-await/async-function/output.js
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
var _f;
21
function f() {
3-
return (_f = _f || babelHelpers.asyncToGenerator(function* () {
2+
return _f.apply(this, arguments);
3+
}
4+
function _f() {
5+
_f = babelHelpers.asyncToGenerator(function* () {
46
var _iteratorAbruptCompletion = false;
57
var _didIteratorError = false;
68
var _iteratorError;
@@ -25,5 +27,6 @@ function f() {
2527
}
2628
}
2729
}
28-
})).apply(this, arguments);
30+
});
31+
return _f.apply(this, arguments);
2932
}

‎packages/babel-plugin-transform-async-generator-functions/test/fixtures/for-await/async-generator/output.js

Copy file name to clipboardExpand all lines: packages/babel-plugin-transform-async-generator-functions/test/fixtures/for-await/async-generator/output.js
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
var _g;
21
function g() {
3-
return (_g = _g || babelHelpers.wrapAsyncGenerator(function* () {
2+
return _g.apply(this, arguments);
3+
}
4+
function _g() {
5+
_g = babelHelpers.wrapAsyncGenerator(function* () {
46
var _iteratorAbruptCompletion = false;
57
var _didIteratorError = false;
68
var _iteratorError;
@@ -25,5 +27,6 @@ function g() {
2527
}
2628
}
2729
}
28-
})).apply(this, arguments);
30+
});
31+
return _g.apply(this, arguments);
2932
}

‎packages/babel-plugin-transform-async-generator-functions/test/fixtures/for-await/create-async-from-sync-iterator/output.js

Copy file name to clipboardExpand all lines: packages/babel-plugin-transform-async-generator-functions/test/fixtures/for-await/create-async-from-sync-iterator/output.js
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
var _fn;
21
function fn() {
3-
return (_fn = _fn || babelHelpers.wrapAsyncGenerator(function* () {
2+
return _fn.apply(this, arguments);
3+
}
4+
function _fn() {
5+
_fn = babelHelpers.wrapAsyncGenerator(function* () {
46
var _iteratorAbruptCompletion = false;
57
var _didIteratorError = false;
68
var _iteratorError;
@@ -27,5 +29,6 @@ function fn() {
2729
}
2830
}
2931
}
30-
})).apply(this, arguments);
32+
});
33+
return _fn.apply(this, arguments);
3134
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.