stoeffel / babel-plugin-array-includes Goto Github PK
View Code? Open in Web Editor NEWReplaces `arr.includes(val)' with `arr.indexOf(val) >= 0`.
License: MIT License
Replaces `arr.includes(val)' with `arr.indexOf(val) >= 0`.
License: MIT License
I just tried using this plugin with Babel 6 and I got the following error:
Module build failed: TypeError: Plugin is not a function
at exports.default (/path/to/project/node_modules/babel-plugin-array-includes/lib/index.js:11:10)
at Function.memoisePluginContainer (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:117:13)
at Function.normalisePlugin (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:149:32)
at /path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:183:30
at Array.map (native)
at Function.normalisePlugins (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:161:20)
at OptionManager.mergeOptions (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:253:36)
at OptionManager.addConfig (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:205:10)
at OptionManager.findConfigs (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:346:16)
at OptionManager.init (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:391:12)
I haven't tried using this plugin before, so I'm not sure if it is related to the new version or not.
I wonder if it's a good practice to use peerDependency
here. Now you get the following when including the plugin to a Babel preset:
npm WARN EPEERINVALID [email protected] requires a peer of babel-core@>=6.3.0 but none was installed.
For example babel-plugin-transform-decorators-legacy skips the peerDependencies
declaration.
Maybe this would be a good place to use optionalDependencies
. I'm not sure what's the best practice for Babel plugins. This is worth researching, though.
Kind of an edge case, but if you reference the includes
property using []
instead of .
, the plugin will not transform the code. Exampe:
x.includes(123);
x['includes'](123);
which should be identical in behaviour result in:
'use strict';
x.indexOf(123) !== -1;
x['includes'](123);
//# sourceMappingURL=test.js.map
Which I don't beleive is correct. Using babel-node confirms this:
> var x = [1,2,3]
undefined
> x.includes(1)
true
> x['includes'](1)
true
This plugin would be great to use in our configs, except for the issue that it blindly translates any invocation of an includes method into an indexOf and -1 comparison
Here's a test case for where this might fail:
function foo(arr) {
return arr.includes('foo');
}
let x = {
includes: function(x) {
console.log('hi');
}
};
x.includes(123);
will get translated into the following js:
'use strict';
function foo(arr) {
return arr.indexOf('foo') !== -1;
}
var x = {
includes: function includes(x) {
console.log('hi');
}
};
x.indexOf(123) !== -1;
//# sourceMappingURL=test.js.map
which is not valid since x is not an array, and in this case does not have an indexOf()
method.
There should probably be something that does type checking at runtime to ensure that this transformation happens in a safe way. I'm not an expert JS spec reader or implementer, so this might have its own issues.
I was thinking that if instead of this transformation of all let z = x.includes(y[, w])
to let z = x.indexOf(y[, w]) !== -1
, that it could instead transform the code into:
let z;
if (Array.isArray(x)) {
z = x.indexOf(y, w) !== -1;
} else if (x.includes) {
console.error(new Error('not applying Array.prototype.includes behaviour, object already has includes prop').stack);
z = x.includes(y, w);
}
This might still have issues and is itself not valid JS. I don't really know how to implement this as a babel rule, but I might give it a go.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.