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

29 lines
653 B
JavaScript
Raw Normal View History

2018-06-21 10:29:42 +08:00
"use strict";
//
// BEFORE:
2019-11-17 22:01:17 +08:00
// foo.includes(bar)
2018-06-21 10:29:42 +08:00
//
// AFTER:
2019-11-17 22:01:17 +08:00
// foo.indexOf(bar) !== -1
2018-06-21 10:29:42 +08:00
//
2019-11-17 22:01:17 +08:00
// this plugin also replace `string.includes`,
// filename is not changed
// because it's temporary use for node@4 support
2018-06-21 10:29:42 +08:00
module.exports = ({ types: t }) => ({
visitor: {
CallExpression(path) {
const node = path.node;
const callee = node.callee;
if (
t.isMemberExpression(callee, { computed: false }) &&
t.isIdentifier(callee.property, { name: "includes" })
) {
callee.property.name = "indexOf";
path.replaceWith(t.binaryExpression("!==", node, t.numericLiteral(-1)));
}
}
}
});