Content-Length: 710819 | pFad | http://github.com/babel/babel/pull/15922/commits/1cab1d8b9eacbab891dab700950f2352ec38c62b

63 Improve output when wrapping functions (e.g. `async` functions) by liuxingbaoyu · Pull Request #15922 · babel/babel · GitHub
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

Improve output when wrapping functions (e.g. async functions) #15922

Merged
merged 6 commits into from
Sep 7, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix
  • Loading branch information
liuxingbaoyu authored and nicolo-ribaudo committed Sep 7, 2023
commit 1cab1d8b9eacbab891dab700950f2352ec38c62b
9 changes: 6 additions & 3 deletions packages/babel-helper-wrap-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ import type * as t from "@babel/types";
// };
Copy link
Contributor

Choose a reason for hiding this comment

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

Unused typings should be removed.

Copy link
Member

Choose a reason for hiding this comment

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

I just pushed a commit that does it


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

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

const buildDeclarationWrapper = template.statement(
`function NAME() { return (NAME = FUNCTION).apply(this, arguments); }`,
`function NAME(PARAMS) { return (NAME = FUNCTION).apply(this, arguments); }`,
);

function classOrObjectMethod(
Expand Down Expand Up @@ -135,6 +135,7 @@ function plainFunction(
buildDeclarationWrapper({
NAME: functionId || path.scope.generateUidIdentifier("ref"),
FUNCTION: built,
PARAMS: params,
}),
);
} else {
Expand All @@ -143,6 +144,7 @@ function plainFunction(
container = buildNamedExpressionWrapper({
NAME: functionId || null,
FUNCTION: built,
PARAMS: params,
});
} else {
ref = path.scope.generateUidIdentifier(
Expand All @@ -151,6 +153,7 @@ function plainFunction(
container = buildAnonymousExpressionWrapper({
REF: ref,
FUNCTION: built,
PARAMS: params,
});
nameFunction({
node: container,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function _ref() {
export default function _ref(_x) {
return (_ref = babelHelpers.asyncToGenerator(function* (x) {})).apply(this, arguments);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let TestClass = {
testMethodFailure() {
var _this = this,
_ref;
return new Promise( /*#__PURE__*/function () {
return new Promise( /*#__PURE__*/function (_x) {
return (_ref = _ref || babelHelpers.asyncToGenerator(function* (resolve) {
console.log(_this);
setTimeout(resolve, 1000);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function mandatory(paramName) {
throw new Error(`Missing parameter: ${paramName}`);
}
function foo() {
function foo(_x) {
return (foo = babelHelpers.asyncToGenerator(function* (_ref) {
let {
a,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
function s() {
function s(_x) {
return (s = babelHelpers.asyncToGenerator(function* (x) {
var _arguments = arguments,
_this = this,
_ref;
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
let t = /*#__PURE__*/function t() {
let t = /*#__PURE__*/function t(_x2, _x3) {
return (_ref = _ref || babelHelpers.asyncToGenerator(function* (y, a) {
var _ref2;
let r = /*#__PURE__*/function r() {
let r = /*#__PURE__*/function r(_x4, _x5) {
return (_ref2 = _ref2 || babelHelpers.asyncToGenerator(function* (z, b) {
yield z;
for (var _len2 = arguments.length, innerArgs = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
function one() {
function one(_x) {
return (one = babelHelpers.asyncToGenerator(function* (a, b = 1) {})).apply(this, arguments);
}
function two() {
function two(_x2, _x3) {
return (two = babelHelpers.asyncToGenerator(function* (a, b, ...c) {})).apply(this, arguments);
}
function three() {
function three(_x4) {
return (three = babelHelpers.asyncToGenerator(function* (a, b = 1, c, d = 3) {})).apply(this, arguments);
}
function four() {
function four(_x5) {
return (four = babelHelpers.asyncToGenerator(function* (a, b = 1, c, ...d) {})).apply(this, arguments);
}
function five() {
function five(_x6, _x7) {
return (five = babelHelpers.asyncToGenerator(function* (a, {
b
}) {})).apply(this, arguments);
}
function six() {
function six(_x8) {
return (six = babelHelpers.asyncToGenerator(function* (a, {
b
} = {}) {})).apply(this, arguments);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function foo() {
function foo(_x) {
return (foo = babelHelpers.asyncToGenerator(function* (bar) {})).apply(this, arguments);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function mandatory(paramName) {
throw new Error(`Missing parameter: ${paramName}`);
}
function foo() {
function foo(_x) {
return (foo = babelHelpers.asyncToGenerator(function (_ref) {
let a = _ref.a,
_ref$b = _ref.b,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Test {
return babelHelpers.asyncToGenerator(function* () {
var _ref2;
console.log(_this2);
setTimeout( /*#__PURE__*/function () {
setTimeout( /*#__PURE__*/function (_x) {
return (_ref2 = _ref2 || babelHelpers.asyncToGenerator(function* (arg) {
console.log(_this2);
})).apply(this, arguments);
Expand All @@ -34,7 +34,7 @@ class Test {
return babelHelpers.asyncToGenerator(function* () {
var _ref4;
console.log(_this4);
setTimeout( /*#__PURE__*/function () {
setTimeout( /*#__PURE__*/function (_x2) {
return (_ref4 = _ref4 || babelHelpers.asyncToGenerator(function* (arg) {
console.log(_this4);
})).apply(this, arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function foo() {
return babelHelpers.regeneratorRuntime().wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
( /*#__PURE__*/function () {
( /*#__PURE__*/function (_x) {
return (_ref = _ref || babelHelpers.asyncToGenerator( /*#__PURE__*/babelHelpers.regeneratorRuntime().mark(function _callee(number) {
var tmp;
return babelHelpers.regeneratorRuntime().wrap(function _callee$(_context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class A {
})();
b = (_ref2 => {
var _this2 = this;
return function () {
return function (_x, _x2, _x3) {
return (_ref2 = _ref2 || babelHelpers.asyncToGenerator(function* (x, y, z) {
return _this2;
})).apply(this, arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Class {
var _this2 = this;
return babelHelpers.asyncToGenerator(function* () {
var _ref2;
var c = /*#__PURE__*/function c() {
var c = /*#__PURE__*/function c(_x) {
return (_ref2 = _ref2 || babelHelpers.asyncToGenerator(function* (b) {
_this2;
})).apply(this, arguments);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var a = async (b) => {}
expect(a.length).toBe(1);
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm very happy that test262 helped me find a regression.🎉

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function test() {
babelHelpers.defineProperty(this, "b", /*#__PURE__*/babelHelpers.asyncToGenerator(function* () {
return _superprop_get('foo').bind(_this)``;
}));
babelHelpers.defineProperty(this, "c", /*#__PURE__*/function () {
babelHelpers.defineProperty(this, "c", /*#__PURE__*/function (_x) {
return (_ref3 = _ref3 || babelHelpers.asyncToGenerator(function* (foo) {
return _superprop_get(foo).bind(_this)``;
})).apply(this, arguments);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var _this = this,
_ref;
x = /*#__PURE__*/function () {
x = /*#__PURE__*/function (_x3) {
return (_ref = _ref || function (_x) {
var _marked = /*#__PURE__*/babelHelpers.regeneratorRuntime().mark(x);
function x(_x2) {
Expand Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/babel/babel/pull/15922/commits/1cab1d8b9eacbab891dab700950f2352ec38c62b

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy