Long way round a builtin
This is how the comment reads inline, under the flagged line.
Why it matters
.filter(...).length > 0 builds a whole array to answer yes/no; range(len(x)) indexes by hand. The builtin is shorter and says what you mean.
What to do
.some(), .includes(), enumerate(), any().
Examples
if (users.filter((u) => u.admin).length > 0) {}if (users.some((u) => u.admin)) {}What it catches
It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript, Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
if (xs.filter((x) => x.ok).length > 0) {}if (s.indexOf(c) !== -1) {}src/app.py
for i in range(len(xs)):
pass
Left alone
if (xs.some((x) => x.ok)) {}Research
Hand-written versions of builtins (`.filter(...).length > 0`, `range(len(x))`). The long form is slower to read and sometimes slower to run.
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"reinvented-builtin"
],
"severity": {
"reinvented-builtin": "medium"
}
}
}
.filter((x) => x.ok).length > 0does by hand what a builtin says directly.