Skip to content

Commit

Permalink
Fix cases where the brackets contain a variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
JBYoshi authored and nicolo-ribaudo committed Jan 2, 2023
1 parent 79a729a commit f84faaa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/babel-traverse/src/path/evaluation.ts
Expand Up @@ -172,11 +172,11 @@ function _evaluate(path: NodePath, state: State): any {
const type = typeof value;

let key = null;
if (property.isIdentifier()) {
key = property.node.name;
} else if (path.node.computed) {
if (path.node.computed) {
key = evaluateCached(property, state);
if (!state.confident) return;
} else if (property.isIdentifier()) {
key = property.node.name;
}
if (
(type === "number" || type === "string") &&
Expand Down
5 changes: 5 additions & 0 deletions packages/babel-traverse/test/evaluation.js
Expand Up @@ -119,6 +119,11 @@ describe("evaluation", function () {
.get("body.0.declarations.0.init")
.evaluate().value,
).toBe("w");
expect(
getPath("var length = 1; var x = 'abc'[length];")
.get("body.1.declarations.0.init")
.evaluate().value,
).toBe("b");
const member_expr = getPath(
"var x = Math.min(2,Math.max(3,4));var y = Math.random();",
);
Expand Down

0 comments on commit f84faaa

Please sign in to comment.