-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [prefer-find] support ternary branches in prefer-find #8421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f372aba
0db4183
2200f83
7fd8285
9d1b79a
f397a3a
9e599c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,12 @@ ruleTester.run('prefer-find', rule, { | |
`, | ||
"[1, 2, 3].filter(x => x)[Symbol('0')];", | ||
"[1, 2, 3].filter(x => x)[Symbol.for('0')];", | ||
'(Math.random() < 0.5 ? [1, 2, 3].filter(x => true) : [1, 2, 3])[0];', | ||
` | ||
(Math.random() < 0.5 | ||
? [1, 2, 3].find(x => true) | ||
: [1, 2, 3].filter(x => true))[0]; | ||
`, | ||
], | ||
|
||
invalid: [ | ||
|
@@ -584,5 +590,142 @@ arr.find(f, thisArg); | |
}, | ||
], | ||
}, | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Testing] Just to be through, could you also add a test for a desired report with and within a ternary? Like the (Math.random() < 0.5
? [1, 2, 3].filter(x => false)
: [1, 2, 3].filter(x => true))[0]; (Math.random() < 0.5
? [1, 2, 3].find(x => true)
: [1, 2, 3].filter(x => true)[0]); The cases lower down are a bit more complex. It's nice to have more straightforward ones to use as a reference too. |
||
code: ` | ||
(Math.random() < 0.5 | ||
? [1, 2, 3].filter(x => false) | ||
: [1, 2, 3].filter(x => true))[0]; | ||
`, | ||
errors: [ | ||
{ | ||
line: 2, | ||
messageId: 'preferFind', | ||
suggestions: [ | ||
{ | ||
messageId: 'preferFindSuggestion', | ||
output: ` | ||
(Math.random() < 0.5 | ||
? [1, 2, 3].find(x => false) | ||
: [1, 2, 3].find(x => true)); | ||
`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
Math.random() < 0.5 | ||
? [1, 2, 3].find(x => true) | ||
: [1, 2, 3].filter(x => true)[0]; | ||
`, | ||
errors: [ | ||
{ | ||
line: 4, | ||
messageId: 'preferFind', | ||
suggestions: [ | ||
{ | ||
messageId: 'preferFindSuggestion', | ||
output: ` | ||
Math.random() < 0.5 | ||
? [1, 2, 3].find(x => true) | ||
: [1, 2, 3].find(x => true); | ||
`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
declare const f: (arg0: unknown, arg1: number, arg2: Array<unknown>) => boolean, | ||
g: (arg0: unknown) => boolean; | ||
const nestedTernaries = ( | ||
Math.random() < 0.5 | ||
? Math.random() < 0.5 | ||
? [1, 2, 3].filter(f) | ||
: []?.filter(x => 'shrug') | ||
: [2, 3, 4]['filter'](g) | ||
).at(0.2); | ||
`, | ||
errors: [ | ||
{ | ||
line: 4, | ||
messageId: 'preferFind', | ||
suggestions: [ | ||
{ | ||
messageId: 'preferFindSuggestion', | ||
output: ` | ||
declare const f: (arg0: unknown, arg1: number, arg2: Array<unknown>) => boolean, | ||
g: (arg0: unknown) => boolean; | ||
const nestedTernaries = ( | ||
Math.random() < 0.5 | ||
? Math.random() < 0.5 | ||
? [1, 2, 3].find(f) | ||
: []?.find(x => 'shrug') | ||
: [2, 3, 4]["find"](g) | ||
); | ||
`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
|
||
{ | ||
code: ` | ||
declare const f: (arg0: unknown) => boolean, g: (arg0: unknown) => boolean; | ||
const nestedTernariesWithSequenceExpression = ( | ||
Math.random() < 0.5 | ||
? ('sequence', | ||
'expression', | ||
Math.random() < 0.5 ? [1, 2, 3].filter(f) : []?.filter(x => 'shrug')) | ||
: [2, 3, 4]['filter'](g) | ||
).at(0.2); | ||
`, | ||
errors: [ | ||
{ | ||
line: 3, | ||
messageId: 'preferFind', | ||
suggestions: [ | ||
{ | ||
messageId: 'preferFindSuggestion', | ||
output: ` | ||
declare const f: (arg0: unknown) => boolean, g: (arg0: unknown) => boolean; | ||
const nestedTernariesWithSequenceExpression = ( | ||
Math.random() < 0.5 | ||
? ('sequence', | ||
'expression', | ||
Math.random() < 0.5 ? [1, 2, 3].find(f) : []?.find(x => 'shrug')) | ||
: [2, 3, 4]["find"](g) | ||
); | ||
`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
|
||
{ | ||
code: ` | ||
declare const spreadArgs: [(x: unknown) => boolean]; | ||
[1, 2, 3].filter(...spreadArgs).at(0); | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`, | ||
errors: [ | ||
{ | ||
line: 3, | ||
messageId: 'preferFind', | ||
suggestions: [ | ||
{ | ||
messageId: 'preferFindSuggestion', | ||
output: ` | ||
declare const spreadArgs: [(x: unknown) => boolean]; | ||
[1, 2, 3].find(...spreadArgs); | ||
`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.