Code Monkey home page Code Monkey logo

babel-plugin-array-includes's People

Contributors

karlhorky avatar motiz88 avatar stoeffel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

babel-plugin-array-includes's Issues

"TypeError: Plugin is not a function" with Babel 6

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.

Gives a `peerDependency` warning when used in a preset

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.

Does not translate Array.prototype.include calls when using x['includes'](y)

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

There should be a runtime check for Array-ness

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.