eslint-plugin-es5 icon indicating copy to clipboard operation
eslint-plugin-es5 copied to clipboard

no-es6-methods a bit too strict

Open refractalize opened this issue 6 years ago • 9 comments

an innocent file like this:

var x
x.values()

Will fail eslint with this error:

ES6 methods not allowed: values  es5/no-es6-methods

This is my .eslintrc file:

{
  "extends": [
    "plugin:es5/no-es2015"
  ]
}

In short, I think there's no easy way to tell if x (in the example above) is an array or something that really does have .values() - so I propose that we drop these rules... WDYT?

refractalize avatar Feb 28 '18 15:02 refractalize

I am having the same problem with .find

The following will fail even though we are using the jQuery API and not Array.find.

var jqueryObject = $('.example').find('.child-element');

https://api.jquery.com/find/

I ended up just setting "es5/no-es6-methods": "warn",

bzmw avatar Mar 06 '18 16:03 bzmw

I'm seeing the same thing for chained lodash methods

eg.

_.chain(
  .get(arr)
  .find('prop')
)

These are false positives.

BearAlliance avatar Apr 12 '18 16:04 BearAlliance

This seems like a very difficult thing to detect with ESLint and seems like a type checker like Flow or TypeScript, since it would be hard to analyze the variable to scope to know if you're calling .find() on a jQuery element or an array without the help of a type system.

I tried to introduce this rule into a large codebase using jQuery and had to disable it. I'm wondering if there should be an option to ignore certain function names (like .find()), or maybe that's overkill? Thoughts?

macklinu avatar May 15 '18 16:05 macklinu

I'm also experiencing the same issue, and ended up using the same solution that @bzmw recommended.

theprivileges avatar Jul 16 '18 16:07 theprivileges

Same problem here with jQuery's find method on forms:

var form = jQuery('#myForm');
var val = form.find('#myfield').val();

derwaldgeist avatar Nov 07 '18 09:11 derwaldgeist

Same as above.

One thing I started doing, which is far from perfect, is running something like grep '\.find(' *.js|grep -v '\$', which at least let's me find potential cases of non-jquery .find after using the plugin. Most are likely to be variables (i.e., x.find(y) where previously var x = $(y)) but it's a quick sanity check.

Amorymeltzer avatar Jun 03 '19 10:06 Amorymeltzer

While there's no simple and complete solution to this, in the case that brought me here, the ability to exempt specific methods would suffice, and would make a good addition to this plugin.

simonwiles avatar Dec 16 '19 19:12 simonwiles

#36 (and thus v1.5.0) made this much better, especially if naming jQuery variables with $.

Amorymeltzer avatar Jun 12 '20 18:06 Amorymeltzer

https://github.com/nkt/eslint-plugin-es5/pull/39 adds an ability to skip methods that still don't work correctly. For example, in my project, there are a lot of jQuery variables with names without a $ sign and it could take a long time to fix and check thousands of lines (the previous PR doesn't fix it). And, I've noticed that some es6 methods aren't banned.
I've added also the ability to add extra methods listed in MDN (Array methods, String methods ) to be highlighted by this plugin.

skyway777 avatar Jun 26 '20 23:06 skyway777