Skip to content

Commit ada32d1

Browse files
authored
feat(eslint-plugin): [prefer-nullish-coalescing] create ignoreIfStatements option (#11000)
* feat(eslint-plugin): [prefer-nullish-coalescing] create `ignoreIfStatements` option * sorting * update snapshots
1 parent 5b187f4 commit ada32d1

File tree

5 files changed

+129
-8
lines changed

5 files changed

+129
-8
lines changed

packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.mdx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,49 @@ c ?? 'a string';
118118
</TabItem>
119119
</Tabs>
120120

121+
### `ignoreIfStatements`
122+
123+
{/* insert option description */}
124+
125+
Examples of code for this rule with `{ ignoreIfStatements: false }`:
126+
127+
<Tabs>
128+
<TabItem value="❌ Incorrect">
129+
130+
```ts option='{ "ignoreIfStatements": false }'
131+
declare let foo: { a: string } | null;
132+
declare function makeFoo(): { a: string };
133+
134+
function lazyInitializeFoo1() {
135+
if (!foo) {
136+
foo = makeFoo();
137+
}
138+
}
139+
140+
function lazyInitializeFoo2() {
141+
if (!foo) foo = makeFoo();
142+
}
143+
```
144+
145+
</TabItem>
146+
<TabItem value="✅ Correct">
147+
148+
```ts option='{ "ignoreIfStatements": false }'
149+
declare let foo: { a: string } | null;
150+
declare function makeFoo(): { a: string };
151+
152+
function lazyInitializeFoo1() {
153+
foo ??= makeFoo();
154+
}
155+
156+
function lazyInitializeFoo2() {
157+
foo ??= makeFoo();
158+
}
159+
```
160+
161+
</TabItem>
162+
</Tabs>
163+
121164
### `ignoreConditionalTests`
122165

123166
{/* insert option description */}

packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export type Options = [
4040
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean;
4141
ignoreBooleanCoercion?: boolean;
4242
ignoreConditionalTests?: boolean;
43+
ignoreIfStatements?: boolean;
4344
ignoreMixedLogicalExpressions?: boolean;
4445
ignorePrimitives?:
4546
| {
@@ -102,6 +103,11 @@ export default createRule<Options, MessageIds>({
102103
description:
103104
'Whether to ignore cases that are located within a conditional test.',
104105
},
106+
ignoreIfStatements: {
107+
type: 'boolean',
108+
description:
109+
'Whether to ignore any if statements that could be simplified by using the nullish coalescing operator.',
110+
},
105111
ignoreMixedLogicalExpressions: {
106112
type: 'boolean',
107113
description:
@@ -154,6 +160,7 @@ export default createRule<Options, MessageIds>({
154160
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false,
155161
ignoreBooleanCoercion: false,
156162
ignoreConditionalTests: true,
163+
ignoreIfStatements: false,
157164
ignoreMixedLogicalExpressions: false,
158165
ignorePrimitives: {
159166
bigint: false,
@@ -171,6 +178,7 @@ export default createRule<Options, MessageIds>({
171178
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing,
172179
ignoreBooleanCoercion,
173180
ignoreConditionalTests,
181+
ignoreIfStatements,
174182
ignoreMixedLogicalExpressions,
175183
ignorePrimitives,
176184
ignoreTernaryTests,
@@ -516,7 +524,7 @@ export default createRule<Options, MessageIds>({
516524
}
517525
},
518526
IfStatement(node: TSESTree.IfStatement): void {
519-
if (node.alternate != null) {
527+
if (ignoreIfStatements || node.alternate != null) {
520528
return;
521529
}
522530

packages/eslint-plugin/tests/docs-eslint-output-snapshots/prefer-nullish-coalescing.shot

Lines changed: 47 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,30 @@ x?.a ? y?.a : 'foo'
518518
code,
519519
options: [{ ignoreTernaryTests: false }] as const,
520520
})),
521+
{
522+
code: `
523+
declare let foo: { a: string } | null;
524+
declare function makeFoo(): { a: string };
525+
526+
function lazyInitialize() {
527+
if (!foo) {
528+
foo = makeFoo();
529+
}
530+
}
531+
`,
532+
options: [{ ignoreIfStatements: true }],
533+
},
534+
{
535+
code: `
536+
declare let foo: { a: string } | null;
537+
declare function makeFoo(): { a: string };
538+
539+
function lazyInitialize() {
540+
if (!foo) foo = makeFoo();
541+
}
542+
`,
543+
options: [{ ignoreIfStatements: true }],
544+
},
521545

522546
// ignoreConditionalTests
523547
...nullishTypeTest((nullish, type, equals) => ({

packages/eslint-plugin/tests/schema-snapshots/prefer-nullish-coalescing.shot

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)