Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
f372aba0db41832200f837fd82859d1b79af397a3a9e599c0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -41,20 +41,41 @@ export default createRule({ | ||
| filterNode: TSESTree.Node; | ||
| } | ||
| functionparseArrayFilterExpressions( | ||
| expression: TSESTree.Expression, | ||
| ): FilterExpressionData[] { | ||
| if (expression.type === AST_NODE_TYPES.SequenceExpression) { | ||
| // Only the last expression in (a, b, [1, 2, 3].filter(condition))[0] matters | ||
| const lastExpression = nullThrows( | ||
| expression.expressions.at(-1), | ||
| 'Expected to have more than zero expressions in a sequence expression', | ||
| ); | ||
| returnparseArrayFilterExpressions(lastExpression); | ||
| } | ||
| if (expression.type === AST_NODE_TYPES.ChainExpression) { | ||
| return parseArrayFilterExpressions(expression.expression); | ||
| } | ||
| // This is the only reason we're returning a list rather than a single value. | ||
kirkwaiblinger marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if (expression.type === AST_NODE_TYPES.ConditionalExpression) { | ||
| // Both branches of the ternary _must_ return results. | ||
| const consequentResult = parseArrayFilterExpressions( | ||
| expression.consequent, | ||
| ); | ||
| if (consequentResult.length === 0) { | ||
| return []; | ||
| } | ||
| const alternateResult = parseArrayFilterExpressions( | ||
| expression.alternate, | ||
| ); | ||
| if (alternateResult.length === 0) { | ||
| return []; | ||
| } | ||
| // Accumulate the results from both sides and pass up the chain. | ||
| return [...consequentResult, ...alternateResult]; | ||
| } | ||
| // Check if it looks like <<stuff>>(...), but not <<stuff>>?.(...) | ||
| @@ -78,16 +99,19 @@ export default createRule({ | ||
| // As long as the object is a (possibly nullable) array, | ||
| // this is an Array.prototype.filter expression. | ||
| if (isArrayish(filteredObjectType)) { | ||
| return [ | ||
| { | ||
| isBracketSyntaxForFilter, | ||
| filterNode, | ||
| }, | ||
| ]; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // not a filter expression. | ||
| return []; | ||
| } | ||
| /** | ||
| @@ -223,8 +247,8 @@ export default createRule({ | ||
| CallExpression(node): void { | ||
| const object = getObjectIfArrayAtZeroExpression(node); | ||
| if (object) { | ||
| constfilterExpressions =parseArrayFilterExpressions(object); | ||
| if (filterExpressions.length !== 0) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'preferFind', | ||
| @@ -233,9 +257,11 @@ export default createRule({ | ||
| messageId: 'preferFindSuggestion', | ||
| fix: (fixer): TSESLint.RuleFix[] => { | ||
| return [ | ||
| ...filterExpressions.map(filterExpression => | ||
| generateFixToReplaceFilterWithFind( | ||
| fixer, | ||
| filterExpression, | ||
| ), | ||
| ), | ||
| // Get rid of the .at(0) or ['at'](0). | ||
| generateFixToRemoveArrayElementAccess( | ||
| @@ -261,8 +287,8 @@ export default createRule({ | ||
| ): void { | ||
| if (isMemberAccessOfZero(node)) { | ||
| const object = node.object; | ||
| constfilterExpressions =parseArrayFilterExpressions(object); | ||
| if (filterExpressions.length !== 0) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'preferFind', | ||
| @@ -271,9 +297,11 @@ export default createRule({ | ||
| messageId: 'preferFindSuggestion', | ||
| fix: (fixer): TSESLint.RuleFix[] => { | ||
| return [ | ||
| ...filterExpressions.map(filterExpression => | ||
| generateFixToReplaceFilterWithFind( | ||
| fixer, | ||
| filterExpression, | ||
| ), | ||
| ), | ||
| // Get rid of the [0]. | ||
| generateFixToRemoveArrayElementAccess( | ||
| 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); | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
Member
| ||
| 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 resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| `, | ||
| errors: [ | ||
| { | ||
| line: 3, | ||
| messageId: 'preferFind', | ||
| suggestions: [ | ||
| { | ||
| messageId: 'preferFindSuggestion', | ||
| output: ` | ||
| declare const spreadArgs: [(x: unknown) => boolean]; | ||
| [1, 2, 3].find(...spreadArgs); | ||
| `, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||