├── .babelrc ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── package.json ├── src └── index.js └── test ├── mocha.opts └── test.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Dependency directory 6 | node_modules 7 | lib 8 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | src 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Christoph Hermann 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-plugin-array-includes 2 | 3 | > Replaces `arr.includes(val)` with `arr.indexOf(val) >= 0`. 4 | 5 | Thanks to [@kittens](https://github.com/kittens) for the help. 6 | 7 | ## Example 8 | 9 | **In** 10 | 11 | ```javascript 12 | [1, 2, 3, 5, 8, 13].includes(4); 13 | ``` 14 | 15 | **Out** 16 | 17 | ```javascript 18 | "use strict"; 19 | 20 | [1, 2, 3, 5, 8, 13].indexOf(4) >= 0; 21 | ``` 22 | 23 | ## Pitfalls 24 | 25 | This doesn't work: 26 | 27 | **In** 28 | 29 | ```js 30 | function foo(arr) { 31 | return arr.includes('foo'); 32 | } 33 | ``` 34 | 35 | **Out** 36 | 37 | ```js 38 | function foo(arr) { 39 | return arr.includes('foo'); // still includes 40 | } 41 | ``` 42 | 43 | ## Installation 44 | 45 | ```sh 46 | $ npm install babel-plugin-array-includes 47 | ``` 48 | 49 | ## Usage 50 | 51 | ### Via `.babelrc` (Recommended) 52 | 53 | **.babelrc** 54 | 55 | ```json 56 | { 57 | "plugins": ["array-includes"] 58 | } 59 | ``` 60 | 61 | ### Via CLI 62 | 63 | ```sh 64 | $ babel --plugins array-includes script.js 65 | ``` 66 | 67 | ### Via Node API 68 | 69 | ```javascript 70 | require("babel-core").transform("code", { 71 | plugins: ["array-includes"] 72 | }); 73 | ``` 74 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-array-includes", 3 | "version": "2.0.3", 4 | "description": "Replaces `arr.includes(val)' with `arr.indexOf(val) >= 0`.", 5 | "author": "Christoph Hermann", 6 | "license": "MIT", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/stoeffel/babel-plugin-array-includes.git" 10 | }, 11 | "main": "lib/index.js", 12 | "devDependencies": { 13 | "babel-cli": "^6.3.15", 14 | "babel-preset-es2015": "^6.3.13", 15 | "babel-register": "^6.3.13", 16 | "chai": "^3.4.1", 17 | "mocha": "^2.3.4" 18 | }, 19 | "scripts": { 20 | "build": "babel -D src -d lib", 21 | "test": "mocha", 22 | "prepublish": "npm run build", 23 | "patch-release": "npm version patch && npm publish && git push --follow-tags", 24 | "minor-release": "npm version minor && npm publish && git push --follow-tags", 25 | "major-release": "npm version major && npm publish && git push --follow-tags" 26 | }, 27 | "keywords": [ 28 | "babel", 29 | "babel-plugin", 30 | "array", 31 | "includes", 32 | "polyfill" 33 | ] 34 | } 35 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | export default function ({ types: t }) { 2 | return { 3 | visitor: { 4 | CallExpression(path) { 5 | 6 | const callee = path.node.callee; 7 | 8 | if (t.isMemberExpression(callee, { computed: false }) && 9 | t.isIdentifier(callee.property, { name: "includes" })) { 10 | 11 | callee.property.name = 'indexOf'; 12 | 13 | path.replaceWith( 14 | t.binaryExpression("!==", path.node, t.numericLiteral(-1)) 15 | ); 16 | 17 | } 18 | } 19 | } 20 | }; 21 | } 22 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --compilers js:babel-register -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var babel = require('babel-core'); 2 | import {expect} from 'chai'; 3 | import plugin from '../src'; 4 | 5 | describe('babel-plugin-transform-array-includes', function() { 6 | it('[1,2,3].includes(1)', function() { 7 | expect(babel.transform('[1,2,3].includes(1);', {plugins: [plugin], compact: true}).code) 8 | .to.equal('[1,2,3].indexOf(1)!==-1;'); 9 | }); 10 | it('var a=[]; a.includes(1)', function() { 11 | expect(babel.transform('var a=[]; a.includes(1);', {plugins: [plugin], compact: true}).code) 12 | .to.equal('var a=[];a.indexOf(1)!==-1;'); 13 | }); 14 | it('var a=[]; b=a.map(...); b.includes(1)', function() { 15 | expect(babel.transform('var a=[];b=a.map(x=>x+1);b.includes(1);', {plugins: [plugin], compact: true}).code) 16 | .to.equal('var a=[];b=a.map(x => x+1);b.indexOf(1)!==-1;'); 17 | }); 18 | }); --------------------------------------------------------------------------------