Comments (11)
Sounds good. Anyone's free to submit a PR, or we'll get to it eventually.
from eslint-plugin-lodash.
How about usages of String.prototype.indexOf()
? Could we check them too?
from eslint-plugin-lodash.
Sorry, the issue is nonsensical:
It should have been detect _.indexOf in this situation, not indexOf string prototype. Simply because lodash prefer rule will ask to use the latter form anyway.
On Dec 18, 2015, at 07:01, Mikołaj D. [email protected] wrote:
How about usages of String.prototype.indexOf()?
—
Reply to this email directly or view it on GitHub.
from eslint-plugin-lodash.
Ah, ok, thanks for the clarification! Somehow I missed this.
from eslint-plugin-lodash.
So, I've done some tests and read the docs, and learned some interesting things about this rule:
_.indexOf()
only works for arrays.
And upon a test of my own, _.indexOf('abcd', 'bc')
returns -1
.
(This also means there's a probable bug in prefer-lodash-method
, since the two methods aren't interchangeable.)
I had worries about not being able to tell if the first argument is a string or an array, but it looks like, counterintuitively, _.startsWith()
also works for arrays!
Try it yourself, and you'll see that _.startsWith([1,2,3], 1)
returns true
, and _.startsWith([1,2,3], 2)
returns false.
This means this rule works, but not for the reason that we thought.
As for [String|Array].prototype.indexOf()
, our belief is that every rule in the plugin (and in ESLint) should be atomic, and not rely on other rules being activated.
We also want to separate the users of the rules to two categories: rules that suggest the use of Lodash instead of native (e.g prefer-lodash-typecheck
), and rules that enforce proper use of Lodash (e.g. no-single-chain
).
For these reasons, I think the rule should have an option whether or not to report these cases, with the values "lodash"
or "all"
, with "all" by default.
from eslint-plugin-lodash.
So, I've done some tests and read the docs, and learned some interesting things about this rule:
_.indexOf() only works for arrays.
I don't see where it says in the docs that the rule only works with arrays. Are you referring to the signature of the function?
Arguments
array (Array): The array to search.
In this sense, Array and String are interchangeable. It is an error in the docs. It should say Iterable
object, not specifically an instance of Array
.
To prove that string is simply an object that implements iterable interaface:
for (i of 'test') {
console.log('i', i);
}
> i t
> i e
> i s
> i t
from eslint-plugin-lodash.
I have opened an issue with LoDash regarding the signature of the _.indexOf
function.
from eslint-plugin-lodash.
I agree that _.indexOf()
should conceivably work with arrays. However, Try running:
_.indexOf('abcd', 'bc')
It would return -1.
In this case, the string should not be treated as an iterable, since we're looking for a substring, not an element (which is the native implementation).
So my statement wasn't exactly true. It works for Iterables, not just arrays (I think Lodash calls them array-like), such as arguments
or NodeList
s, but it doesn't have the same implementation as the native indexOf
for strings.
from eslint-plugin-lodash.
Using _.indexOf
with strings as the array
param is invalid and unsupported. The _.indexOf
method is an "Array" category method and not intended for use with strings.
from eslint-plugin-lodash.
Since usage of _.indexOf() with strings as the array
param is unsupported, I'm implementing the rule to suggest replacement of any a.indexOf(b) === 0
with _.startsWith(a, b)
.
from eslint-plugin-lodash.
Implemented the rule in v0.4.1
from eslint-plugin-lodash.
Related Issues (20)
- `prop-shorthand` does not report on `_.orderBy` HOT 1
- Dependabot couldn't reach repo.dev.wixpress.com/artifactory/api/npm/npm-repos as it timed out
- `collection-ordering`: Require same number of iteratees and orders
- Error when typing `_()`
- chaining rule doesn’t work when chain is imported from lodash/chain HOT 1
- Can I autofix by `preferred-alias` rule? HOT 1
- `path-style` rule has bug
- False positive for prefer-lodash-chain with _.range? HOT 1
- Version 7.1.0 is failing on the project with Lodash v3 HOT 1
- prefer-noop shouldn't trigger if a function has comment inside
- path-style is supposed to be "as-needed" by default
- bug: lodash/prefer-lodash-method false positive with nextjs (react) router replace
- path-style rule auto-fix incorrectly formats object accessor causing regressions! HOT 1
- Whitesource scan vulnerability on lodash < 4.19.19
- Support ESLint 8.x HOT 1
- Recommending `_.split` for v3 despite it not existing
- type import produces false positive under lodash/import-scope HOT 1
- `prefer-is-undefined` and `prefer-is-null`
- Combine rules for `import-scope`, accept both 'member' and 'method' HOT 2
- lodash/prefer-* rules cause problems with Jest mocks HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eslint-plugin-lodash.