Files
test-repo/scripts/build/babel-plugins/replace-array-includes-with-indexof.js
T
Ika db2bc3636d fix(yaml): update parsers (#5027)
- upgrade to `yaml@1.0.0-rc.8` and `yaml-unist-parser@1.0.0-rc.4`
- refactor some logic since the AST has slightly changed (ikatyang/yaml-unist-parser#82)
- unmatched aliases are now errors since it may introduce invalid AST from `yaml`
- rewrite the document separator (`...`/`---`) logic, this fixes some cases where it can use `---` but we printed `...`
- removed some unnecessary duplicate trailing newline
- trailing comments on `document` (`... #comment`) and `documentHead` (`--- #comment`) are preserved (i.e. they won't be moved somewhere)
2018-09-01 20:04:37 +08:00

33 lines
940 B
JavaScript

"use strict";
//
// BEFORE:
// [__something__].includes(__something__)
// CONSTANT_CASE.includes(__something__)
//
// AFTER:
// [__something__].indexOf(__something__) !== -1
// CONSTANT_CASE.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.object) &&
(/^[A-Z_]+$/.test(callee.object.name) ||
// https://github.com/eemeli/yaml/blob/1005d01/src/Anchors.js#L45
callee.object.name === "names"))) &&
t.isIdentifier(callee.property, { name: "includes" })
) {
callee.property.name = "indexOf";
path.replaceWith(t.binaryExpression("!==", node, t.numericLiteral(-1)));
}
}
}
});