Files
test-repo/scripts/build/babel-plugins/replace-array-includes-with-indexof.js
T

26 lines
619 B
JavaScript
Raw Normal View History

2018-06-21 10:29:42 +08:00
"use strict";
//
// BEFORE:
// [__something__].includes(__something__)
//
// AFTER:
// [__something__].indexOf(__something__) !== -1
//
module.exports = ({ types: t }) => ({
visitor: {
CallExpression(path) {
const node = path.node;
const callee = node.callee;
if (
t.isMemberExpression(callee, { computed: false }) &&
t.isArrayExpression(callee.object) &&
t.isIdentifier(callee.property, { name: "includes" })
) {
callee.property.name = "indexOf";
path.replaceWith(t.binaryExpression("!==", node, t.numericLiteral(-1)));
}
}
}
});