fix: mark Pattern in CatchClause as scope (#12119)
This commit is contained in:
parent
afc03581cc
commit
e0b2bfb78f
@ -996,15 +996,14 @@ export default class Scope {
|
||||
const binding = scope.getOwnBinding(name);
|
||||
if (binding) {
|
||||
// Check if a pattern is a part of parameter expressions.
|
||||
// 9.2.10.28: The closure created by this expression should not have visibility of
|
||||
// Note: for performance reason we skip checking previousPath.parentPath.isFunction()
|
||||
// because `scope.path` is validated as scope in packages/babel-types/src/validators/isScope.js
|
||||
// That is, if a scope path is pattern, its parent must be Function/CatchClause
|
||||
|
||||
// Spec 9.2.10.28: The closure created by this expression should not have visibility of
|
||||
// declarations in the function body. If the binding is not a `param`-kind,
|
||||
// then it must be defined inside the function body, thus it should be skipped
|
||||
if (
|
||||
previousPath &&
|
||||
previousPath.isPattern() &&
|
||||
previousPath.parentPath.isFunction() &&
|
||||
binding.kind !== "param"
|
||||
) {
|
||||
if (previousPath?.isPattern() && binding.kind !== "param") {
|
||||
// do nothing
|
||||
} else {
|
||||
return binding;
|
||||
|
||||
1
packages/babel-traverse/test/fixtures/rename/catch-clause-2/input.js
vendored
Normal file
1
packages/babel-traverse/test/fixtures/rename/catch-clause-2/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
try {} catch ({ a }) {}
|
||||
3
packages/babel-traverse/test/fixtures/rename/catch-clause-2/options.json
vendored
Normal file
3
packages/babel-traverse/test/fixtures/rename/catch-clause-2/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["./plugin"]
|
||||
}
|
||||
3
packages/babel-traverse/test/fixtures/rename/catch-clause-2/output.js
vendored
Normal file
3
packages/babel-traverse/test/fixtures/rename/catch-clause-2/output.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
try {} catch ({
|
||||
a: z
|
||||
}) {}
|
||||
9
packages/babel-traverse/test/fixtures/rename/catch-clause-2/plugin.js
vendored
Normal file
9
packages/babel-traverse/test/fixtures/rename/catch-clause-2/plugin.js
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
module.exports = function () {
|
||||
return {
|
||||
visitor: {
|
||||
CatchClause(path) {
|
||||
path.scope.rename("a", "z");
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
5
packages/babel-traverse/test/fixtures/rename/catch-clause/input.js
vendored
Normal file
5
packages/babel-traverse/test/fixtures/rename/catch-clause/input.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
let [a] = ["outside"];
|
||||
|
||||
try {} catch ({ g = () => a }) {
|
||||
let [a] = ["inside"];
|
||||
}
|
||||
3
packages/babel-traverse/test/fixtures/rename/catch-clause/options.json
vendored
Normal file
3
packages/babel-traverse/test/fixtures/rename/catch-clause/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["./plugin"]
|
||||
}
|
||||
7
packages/babel-traverse/test/fixtures/rename/catch-clause/output.js
vendored
Normal file
7
packages/babel-traverse/test/fixtures/rename/catch-clause/output.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
let [a] = ["outside"];
|
||||
|
||||
try {} catch ({
|
||||
g = () => a
|
||||
}) {
|
||||
let [z] = ["inside"];
|
||||
}
|
||||
9
packages/babel-traverse/test/fixtures/rename/catch-clause/plugin.js
vendored
Normal file
9
packages/babel-traverse/test/fixtures/rename/catch-clause/plugin.js
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
module.exports = function () {
|
||||
return {
|
||||
visitor: {
|
||||
CatchClause(path) {
|
||||
path.scope.rename("a", "z");
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
@ -11,17 +11,15 @@ import {
|
||||
* Check if the input `node` is a scope.
|
||||
*/
|
||||
export default function isScope(node: Object, parent: Object): boolean {
|
||||
if (isBlockStatement(node) && isFunction(parent, { body: node })) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isBlockStatement(node) && isCatchClause(parent, { body: node })) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If a Pattern is an immediate descendent of a Function, it must be in the params.
|
||||
// If a BlockStatement is an immediate descendent of a Function/CatchClause, it must be in the body.
|
||||
// Hence we skipped the parentKey === "params" check
|
||||
if (isPattern(node) && isFunction(parent)) {
|
||||
if (isBlockStatement(node) && (isFunction(parent) || isCatchClause(parent))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If a Pattern is an immediate descendent of a Function/CatchClause, it must be in the params.
|
||||
// Hence we skipped the parentKey === "params" check
|
||||
if (isPattern(node) && (isFunction(parent) || isCatchClause(parent))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user