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
Support transforming params of arrow functions in class fields #13941
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...l-plugin-transform-parameters/test/fixtures/assumption-noNewArrows-false/default/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let f = (x = 0) => x + 1; |
7 changes: 7 additions & 0 deletions
7
...-plugin-transform-parameters/test/fixtures/assumption-noNewArrows-false/default/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
var _this = this; | ||
|
||
let f = function f() { | ||
let x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
babelHelpers.newArrowCheck(this, _this); | ||
return x + 1; | ||
}.bind(this); |
6 changes: 6 additions & 0 deletions
6
...babel-plugin-transform-parameters/test/fixtures/assumption-noNewArrows-false/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": ["transform-parameters"], | ||
"assumptions": { | ||
"noNewArrows": false | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/babel-plugin-transform-parameters/test/fixtures/regression/13939-complex/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class A extends B { | ||
handle = ((x = 0) => { | ||
console.log(x, this, new.target, super.y); | ||
})(() => { | ||
let y = 0; | ||
return (x = y) => x + this; | ||
})((x = 1) => {})(this); | ||
} |
5 changes: 5 additions & 0 deletions
5
...ges/babel-plugin-transform-parameters/test/fixtures/regression/13939-complex/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": [ | ||
"transform-parameters" | ||
] | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/babel-plugin-transform-parameters/test/fixtures/regression/13939-complex/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class A extends B { | ||
handle = (() => { | ||
var _newtarget = new.target, | ||
_superprop_getY = () => super.y, | ||
_this = this; | ||
|
||
return function () { | ||
let x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
console.log(x, _this, _newtarget, _superprop_getY()); | ||
}; | ||
})()(() => { | ||
var _this2 = this; | ||
|
||
let y = 0; | ||
return function () { | ||
let x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : y; | ||
return x + _this2; | ||
}; | ||
})((() => function () { | ||
let x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; | ||
})())(this); | ||
} |
8 changes: 8 additions & 0 deletions
8
...babel-plugin-transform-parameters/test/fixtures/regression/13939-private-complex/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class A extends B { | ||
#handle = ((x = 0) => { | ||
console.log(x, this, new.target, super.y); | ||
})(() => { | ||
let y = 0; | ||
return (x = y) => x + this; | ||
})((x = 1) => {})(this); | ||
} |
5 changes: 5 additions & 0 deletions
5
...l-plugin-transform-parameters/test/fixtures/regression/13939-private-complex/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": [ | ||
"transform-parameters" | ||
] | ||
} |
22 changes: 22 additions & 0 deletions
22
...abel-plugin-transform-parameters/test/fixtures/regression/13939-private-complex/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class A extends B { | ||
#handle = (() => { | ||
var _newtarget = new.target, | ||
_superprop_getY = () => super.y, | ||
_this = this; | ||
|
||
return function () { | ||
let x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
console.log(x, _this, _newtarget, _superprop_getY()); | ||
}; | ||
})()(() => { | ||
var _this2 = this; | ||
|
||
let y = 0; | ||
return function () { | ||
let x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : y; | ||
return x + _this2; | ||
}; | ||
})((() => function () { | ||
let x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; | ||
})())(this); | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin-transform-parameters/test/fixtures/regression/13939-private/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class A { | ||
#handle = (x = 0) => { | ||
console.log(x); | ||
}; | ||
} |
5 changes: 5 additions & 0 deletions
5
...ges/babel-plugin-transform-parameters/test/fixtures/regression/13939-private/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": [ | ||
"transform-parameters" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/babel-plugin-transform-parameters/test/fixtures/regression/13939-private/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class A { | ||
#handle = (() => function () { | ||
let x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
console.log(x); | ||
})(); | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin-transform-parameters/test/fixtures/regression/13939/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class A { | ||
handle = (x = 0) => { | ||
console.log(x); | ||
}; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin-transform-parameters/test/fixtures/regression/13939/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": [ | ||
"transform-parameters" | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/babel-plugin-transform-parameters/test/fixtures/regression/13939/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class A { | ||
handle = (() => function () { | ||
let x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; | ||
console.log(x); | ||
})(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import { | |
stringLiteral, | ||
super as _super, | ||
thisExpression, | ||
toExpression, | ||
unaryExpression, | ||
} from "@babel/types"; | ||
import type * as t from "@babel/types"; | ||
|
@@ -146,27 +147,26 @@ export function arrowFunctionToExpression( | |
); | ||
} | ||
|
||
const thisBinding = hoistFunctionEnvironment( | ||
const { thisBinding, fnPath: fn } = hoistFunctionEnvironment( | ||
this, | ||
noNewArrows, | ||
allowInsertArrow, | ||
); | ||
|
||
this.ensureBlock(); | ||
// @ts-expect-error todo(flow->ts): avoid mutating nodes | ||
this.node.type = "FunctionExpression"; | ||
fn.ensureBlock(); | ||
fn.node.type = "FunctionExpression"; | ||
if (!noNewArrows) { | ||
const checkBinding = thisBinding | ||
? null | ||
: this.parentPath.scope.generateUidIdentifier("arrowCheckId"); | ||
: fn.scope.generateUidIdentifier("arrowCheckId"); | ||
if (checkBinding) { | ||
this.parentPath.scope.push({ | ||
fn.parentPath.scope.push({ | ||
id: checkBinding, | ||
init: objectExpression([]), | ||
}); | ||
} | ||
|
||
this.get("body").unshiftContainer( | ||
fn.get("body").unshiftContainer( | ||
"body", | ||
expressionStatement( | ||
callExpression(this.hub.addHelper("newArrowCheck"), [ | ||
|
@@ -178,10 +178,10 @@ export function arrowFunctionToExpression( | |
), | ||
); | ||
|
||
this.replaceWith( | ||
fn.replaceWith( | ||
callExpression( | ||
memberExpression( | ||
nameFunction(this, true) || this.node, | ||
nameFunction(this, true) || fn.node, | ||
identifier("bind"), | ||
), | ||
[checkBinding ? identifier(checkBinding.name) : thisExpression()], | ||
|
@@ -193,26 +193,53 @@ export function arrowFunctionToExpression( | |
/** | ||
* Given a function, traverse its contents, and if there are references to "this", "arguments", "super", | ||
* or "new.target", ensure that these references reference the parent environment around this function. | ||
* | ||
* @returns `thisBinding`: the name of the injected reference to `this`; for example "_this" | ||
* @returns `fnPath`: the new path to the function node. This is different from the fnPath | ||
* parameter when the function node is wrapped in another node. | ||
*/ | ||
function hoistFunctionEnvironment( | ||
fnPath, | ||
fnPath: NodePath<t.Function>, | ||
// TODO(Babel 8): Consider defaulting to `false` for spec compliancy | ||
noNewArrows = true, | ||
allowInsertArrow = true, | ||
) { | ||
const thisEnvFn = fnPath.findParent(p => { | ||
): { thisBinding: string; fnPath: NodePath<t.Function> } { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add some comments about the return type? Especially we may have different |
||
let arrowParent; | ||
let thisEnvFn = fnPath.findParent(p => { | ||
if (p.isArrowFunctionExpression()) { | ||
arrowParent ??= p; | ||
return false; | ||
} | ||
return ( | ||
(p.isFunction() && !p.isArrowFunctionExpression()) || | ||
p.isFunction() || | ||
p.isProgram() || | ||
p.isClassProperty({ static: false }) | ||
p.isClassProperty({ static: false }) || | ||
p.isClassPrivateProperty({ static: false }) | ||
); | ||
}); | ||
const inConstructor = thisEnvFn?.node.kind === "constructor"; | ||
|
||
if (thisEnvFn.isClassProperty()) { | ||
throw fnPath.buildCodeFrameError( | ||
"Unable to transform arrow inside class property", | ||
); | ||
const inConstructor = thisEnvFn.isClassMethod({ kind: "constructor" }); | ||
|
||
if (thisEnvFn.isClassProperty() || thisEnvFn.isClassPrivateProperty()) { | ||
if (arrowParent) { | ||
thisEnvFn = arrowParent; | ||
} else if (allowInsertArrow) { | ||
// It's safe to wrap this function in another and not hoist to the | ||
// top level because the 'this' binding is constant in class | ||
// properties (since 'super()' has already been called), so we don't | ||
// need to capture/reassign it at the top level. | ||
fnPath.replaceWith( | ||
callExpression( | ||
arrowFunctionExpression([], toExpression(fnPath.node)), | ||
[], | ||
), | ||
); | ||
thisEnvFn = fnPath.get("callee"); | ||
fnPath = thisEnvFn.get("body"); | ||
} else { | ||
throw fnPath.buildCodeFrameError( | ||
"Unable to transform arrow inside class property", | ||
); | ||
} | ||
} | ||
|
||
const { thisPaths, argumentsPaths, newTargetPaths, superProps, superCalls } = | ||
|
@@ -365,7 +392,7 @@ function hoistFunctionEnvironment( | |
} | ||
} | ||
|
||
return thisBinding; | ||
return { thisBinding, fnPath }; | ||
} | ||
|
||
function standardizeSuperProperty(superProp) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is another bug fixed by the new
if (!path.isFunction()) return;
check in the plugin.