Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 47 additions & 19 deletionspackages/eslint-plugin/src/rules/prefer-find.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -41,20 +41,41 @@ export default createRule({
filterNode: TSESTree.Node;
}

functionparseIfArrayFilterExpression(
functionparseArrayFilterExpressions(
expression: TSESTree.Expression,
): FilterExpressionData | undefined {
): 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',
);
returnparseIfArrayFilterExpression(lastExpression);
returnparseArrayFilterExpressions(lastExpression);
}

if (expression.type === AST_NODE_TYPES.ChainExpression) {
return parseIfArrayFilterExpression(expression.expression);
return parseArrayFilterExpressions(expression.expression);
}

// This is the only reason we're returning a list rather than a single value.
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>>?.(...)
Expand All@@ -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,
};
return [
{
isBracketSyntaxForFilter,
filterNode,
},
];
}
}
}
}

return undefined;
// not a filter expression.
return [];
}

/**
Expand DownExpand Up@@ -223,8 +247,8 @@ export default createRule({
CallExpression(node): void {
const object = getObjectIfArrayAtZeroExpression(node);
if (object) {
constfilterExpression =parseIfArrayFilterExpression(object);
if (filterExpression) {
constfilterExpressions =parseArrayFilterExpressions(object);
if (filterExpressions.length !== 0) {
context.report({
node,
messageId: 'preferFind',
Expand All@@ -233,9 +257,11 @@ export default createRule({
messageId: 'preferFindSuggestion',
fix: (fixer): TSESLint.RuleFix[] => {
return [
generateFixToReplaceFilterWithFind(
fixer,
filterExpression,
...filterExpressions.map(filterExpression =>
generateFixToReplaceFilterWithFind(
fixer,
filterExpression,
),
),
// Get rid of the .at(0) or ['at'](0).
generateFixToRemoveArrayElementAccess(
Expand All@@ -261,8 +287,8 @@ export default createRule({
): void {
if (isMemberAccessOfZero(node)) {
const object = node.object;
constfilterExpression =parseIfArrayFilterExpression(object);
if (filterExpression) {
constfilterExpressions =parseArrayFilterExpressions(object);
if (filterExpressions.length !== 0) {
context.report({
node,
messageId: 'preferFind',
Expand All@@ -271,9 +297,11 @@ export default createRule({
messageId: 'preferFindSuggestion',
fix: (fixer): TSESLint.RuleFix[] => {
return [
generateFixToReplaceFilterWithFind(
fixer,
filterExpression,
...filterExpressions.map(filterExpression =>
generateFixToReplaceFilterWithFind(
fixer,
filterExpression,
),
),
// Get rid of the [0].
generateFixToRemoveArrayElementAccess(
Expand Down
143 changes: 143 additions & 0 deletionspackages/eslint-plugin/tests/rules/prefer-find.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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: [
Expand DownExpand Up@@ -584,5 +590,142 @@ arr.find(f, thisArg);
},
],
},
{
Copy link
Member

@JoshuaKGoldbergJoshuaKGoldbergFeb 23, 2024
edited
Loading

Choose a reason for hiding this comment

The 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 thevalid case above, but either with two.filters or with the[0] on the other side of the last)?

(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.

kirkwaiblinger reacted with thumbs up emoji
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);
`,
errors: [
{
line: 3,
messageId: 'preferFind',
suggestions: [
{
messageId: 'preferFindSuggestion',
output: `
declare const spreadArgs: [(x: unknown) => boolean];
[1, 2, 3].find(...spreadArgs);
`,
},
],
},
],
},
],
});

[8]ページ先頭

©2009-2025 Movatter.jp