Skip to content

Rule proposal: Prefer .includes() #284

Closed
@sindresorhus

Description

@sindresorhus

This rule should prefer .includes() over .indexOf() when checking for existence. This would apply to the following types: String#includes, Array#includes, TypedArray#includes, Buffer#includes.

.includes() cases:

  • 'foobar'.indexOf('foo') !== -1
  • 'foobar'.indexOf('foo') != -1
  • 'foobar'.indexOf('foo') > -1
  • 'foobar'.indexOf('foo') >= 0

These would negate .includes():

  • 'foobar'.indexOf('foo') == -1
  • 'foobar'.indexOf('foo') === -1
  • 'foobar'.indexOf('foo') < 0

It would also be useful to catch cases where String#includes() would be better than a regex: /\r\n/.test(foo); => foo.includes('\r\n');


This was originally proposed for inclusion in ESLint, but declined for lack of type knowledge in JS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions