Skip to content

Commit

Permalink
Fix scope of computed method keys (#12812)
Browse files Browse the repository at this point in the history
* Fix scope of computed method keys

* Test for nested computed keys

* Fix scope.rename with computed method keys

* Optional chaining tests
  • Loading branch information
overlookmotel committed Feb 19, 2021
1 parent a940c09 commit 792672e
Show file tree
Hide file tree
Showing 41 changed files with 391 additions and 2 deletions.
@@ -0,0 +1,4 @@
let x;
const a = {
[x.y?.z]() {}
};
@@ -0,0 +1,3 @@
{
"plugins": [["proposal-optional-chaining", { "loose": true }]]
}
@@ -0,0 +1,7 @@
var _x$y;

let x;
const a = {
[(_x$y = x.y) == null ? void 0 : _x$y.z]() {}

};
@@ -0,0 +1,4 @@
let x;
const a = {
[x.y?.z]() {}
};
@@ -0,0 +1,7 @@
var _x$y;

let x;
const a = {
[(_x$y = x.y) === null || _x$y === void 0 ? void 0 : _x$y.z]() {}

};
4 changes: 4 additions & 0 deletions packages/babel-traverse/src/path/context.ts
Expand Up @@ -119,6 +119,10 @@ export function setScope(this: NodePath) {
if (this.opts && this.opts.noScope) return;

let path = this.parentPath;

// Skip method scope if is computed method key
if (this.key === "key" && path.isMethod()) path = path.parentPath;

let target;
while (path && !target) {
if (path.opts && path.opts.noScope) return;
Expand Down
11 changes: 10 additions & 1 deletion packages/babel-traverse/src/scope/index.ts
Expand Up @@ -359,7 +359,16 @@ export default class Scope {
static contextVariables = ["arguments", "undefined", "Infinity", "NaN"];

get parent() {
const parent = this.path.findParent(p => p.isScope());
let parent,
path = this.path;
do {
// Skip method scope if coming from inside computed key
const isKey = path.key === "key";
path = path.parentPath;
if (isKey && path.isMethod()) path = path.parentPath;
if (path && path.isScope()) parent = path;
} while (path && !parent);

return parent?.scope;
}

Expand Down
17 changes: 16 additions & 1 deletion packages/babel-traverse/src/scope/lib/renamer.ts
Expand Up @@ -17,7 +17,7 @@ const renameVisitor: Visitor<Renamer> = {
state.binding.identifier,
)
) {
path.skip();
skipAllButComputedMethodKey(path);
}
},

Expand Down Expand Up @@ -142,3 +142,18 @@ export default class Renamer {
}
}
}

function skipAllButComputedMethodKey(path) {
// If the path isn't method with computed key, just skip everything.
if (!path.isMethod() || !path.node.computed) {
path.skip();
return;
}

// So it's a method with a computed key. Make sure to skip every other key the
// traversal would visit.
const keys = t.VISITOR_KEYS[path.type];
for (const key of keys) {
if (key !== "key") path.skipKey(key);
}
}
@@ -0,0 +1,8 @@
let a = "outside";

const obj = {
[a]() {
let a = "inside";
return a;
}
};
@@ -0,0 +1,3 @@
{
"plugins": ["./plugin"]
}
@@ -0,0 +1,8 @@
let z = "outside";
const obj = {
[z]() {
let a = "inside";
return a;
}

};
@@ -0,0 +1,9 @@
module.exports = function() {
return {
visitor: {
Program(path) {
path.scope.rename("a", "z");
}
}
};
};
@@ -0,0 +1,8 @@
let a = "outside";

class C {
[a]() {
let a = "inside";
return a;
}
}
@@ -0,0 +1,3 @@
{
"plugins": ["./plugin"]
}
@@ -0,0 +1,9 @@
let z = "outside";

class C {
[z]() {
let a = "inside";
return a;
}

}
@@ -0,0 +1,9 @@
module.exports = function() {
return {
visitor: {
Program(path) {
path.scope.rename("a", "z");
}
}
};
};
@@ -0,0 +1,7 @@
let a = "outside";

const obj = {
[a](a = "inside") {
return a;
}
};
@@ -0,0 +1,3 @@
{
"plugins": ["./plugin"]
}
@@ -0,0 +1,7 @@
let z = "outside";
const obj = {
[z](a = "inside") {
return a;
}

};
@@ -0,0 +1,9 @@
module.exports = function() {
return {
visitor: {
Program(path) {
path.scope.rename("a", "z");
}
}
};
};
@@ -0,0 +1,7 @@
let a = "outside";

class C {
[a](a = "inside") {
return a;
}
}
@@ -0,0 +1,3 @@
{
"plugins": ["./plugin"]
}
@@ -0,0 +1,8 @@
let z = "outside";

class C {
[z](a = "inside") {
return a;
}

}
@@ -0,0 +1,9 @@
module.exports = function() {
return {
visitor: {
Program(path) {
path.scope.rename("a", "z");
}
}
};
};
@@ -0,0 +1,8 @@
let a = "outside";

const obj = {
[(() => a)()]() {
let a = "inside";
return a;
}
};
@@ -0,0 +1,3 @@
{
"plugins": ["./plugin"]
}
@@ -0,0 +1,8 @@
let z = "outside";
const obj = {
[(() => z)()]() {
let a = "inside";
return a;
}

};
@@ -0,0 +1,9 @@
module.exports = function() {
return {
visitor: {
Program(path) {
path.scope.rename("a", "z");
}
}
};
};
@@ -0,0 +1,8 @@
let a = "outside";

class C {
[(() => a)()]() {
let a = "inside";
return a;
}
}
@@ -0,0 +1,3 @@
{
"plugins": ["./plugin"]
}
@@ -0,0 +1,9 @@
let z = "outside";

class C {
[(() => z)()]() {
let a = "inside";
return a;
}

}
@@ -0,0 +1,9 @@
module.exports = function() {
return {
visitor: {
Program(path) {
path.scope.rename("a", "z");
}
}
};
};
@@ -0,0 +1,15 @@
let a = "outside";

const obj = {
get [
{
get [a]() {
let a = "inside";
return a;
}
}.outside
]() {
let a = "middle";
return a;
}
};
@@ -0,0 +1,3 @@
{
"plugins": ["./plugin"]
}
@@ -0,0 +1,14 @@
let z = "outside";
const obj = {
get [{
get [z]() {
let a = "inside";
return a;
}

}.outside]() {
let a = "middle";
return a;
}

};
@@ -0,0 +1,9 @@
module.exports = function() {
return {
visitor: {
Program(path) {
path.scope.rename("a", "z");
}
}
};
};
@@ -0,0 +1,15 @@
let a = "outside";

class C {
static get [
class D {
static get [a]() {
let a = "inside";
return a;
}
}.outside
]() {
let a = "middle";
return a;
}
}
@@ -0,0 +1,3 @@
{
"plugins": ["./plugin"]
}
@@ -0,0 +1,15 @@
let z = "outside";

class C {
static get [class D {
static get [z]() {
let a = "inside";
return a;
}

}.outside]() {
let a = "middle";
return a;
}

}
@@ -0,0 +1,9 @@
module.exports = function() {
return {
visitor: {
Program(path) {
path.scope.rename("a", "z");
}
}
};
};

0 comments on commit 792672e

Please sign in to comment.