Skip to content

Commit

Permalink
fix defeating-array-mapping plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzoferre committed Sep 13, 2024
1 parent fa26f7d commit b488056
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/techniques/statics/defeating-array-mapping.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
import { setChanged } from "../../utils/util.js";

export default function (babel) {
const { types: t } = babel;

function replaceArrayMember(path, array, index) {
if (index >= array.length) return;
const member = array[index];
if (t.isLiteral(member)) {
path.replaceWith(member);
}
}

return {
name: "defeating-array-mapping",
visitor: {
MemberExpression: {
enter(path) {
const { node, scope } = path;
const { property } = node;
if (!property) return;
const { property, object } = node;
if (!t.isNumericLiteral(property)) return;
const index = property.value;
const binding = scope.getBinding(node.object.name);
if (!binding) return;
if (!t.isVariableDeclarator(binding.path.node)) return;
if (!t.isArrayExpression(binding.path.node.init)) return;
let array = binding.path.node.init;
if (index >= array.length) return;
let member = array.elements[index];
if (t.isLiteral(member)) {
path.replaceWith(member);
setChanged(true);
if (binding) {
if (
t.isVariableDeclarator(binding.path.node) &&
t.isArrayExpression(binding.path.node.init)
) {
const array = binding.path.node.init.elements;
replaceArrayMember(path, array, index);
}
} else if (t.isArrayExpression(object)) {
const array = object.elements;
replaceArrayMember(path, array, index);
}
},
},
Expand Down

0 comments on commit b488056

Please sign in to comment.