Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 519cc46

Browse files
committed
Add rule @typescript-eslint/prefer-nullish-coalescing
1 parent 9ba6b73 commit 519cc46

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ module.exports = defineConfig({
104104
'multiline',
105105
'nashorn',
106106
'nocheck',
107+
'nullish',
107108
'phantomjs',
108109
'pragma',
109110
'prototypejs',

src/rules/typescript-eslint/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { NoInferrableTypesRule } from './no-inferrable-types';
77
import type { NoParameterPropertiesRule } from './no-parameter-properties';
88
import type { NoUnsafeAssignmentRule } from './no-unsafe-assignment';
99
import type { NoUnusedVarsRule } from './no-unused-vars';
10+
import type { PreferNullishCoalescingRule } from './prefer-nullish-coalescing';
1011

1112
/**
1213
* All @typescript-eslint rules.
@@ -19,4 +20,5 @@ export type TypeScriptEslintRules = BanTsCommentRule &
1920
NoInferrableTypesRule &
2021
NoParameterPropertiesRule &
2122
NoUnsafeAssignmentRule &
22-
NoUnusedVarsRule;
23+
NoUnusedVarsRule &
24+
PreferNullishCoalescingRule;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { RuleConfig } from '../rule-config';
2+
3+
/**
4+
* Option.
5+
*/
6+
export type PreferNullishCoalescingOption = {
7+
/**
8+
* Setting this option to `true` (the default) will cause the rule to ignore any cases that are located within a conditional test.
9+
*
10+
* @default true
11+
*
12+
* @see [ignoreConditionalTests](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md#ignoreconditionaltests)
13+
*/
14+
ignoreConditionalTests?: boolean;
15+
/**
16+
* Setting this option to `true` (the default) will cause the rule to ignore any logical or expressions that are part of a mixed logical expression (with `&&`).
17+
*
18+
* @default true
19+
*
20+
* @see [ignoreMixedLogicalExpressions](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md#ignoremixedlogicalexpressions)
21+
*/
22+
ignoreMixedLogicalExpressions?: boolean;
23+
};
24+
25+
/**
26+
* Options.
27+
*/
28+
export type PreferNullishCoalescingOptions = [PreferNullishCoalescingOption?];
29+
30+
/**
31+
* Enforce the usage of the nullish coalescing operator instead of logical chaining.
32+
*
33+
* @see [prefer-nullish-coalescing](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md)
34+
*/
35+
export type PreferNullishCoalescingRuleConfig = RuleConfig<PreferNullishCoalescingOptions>;
36+
37+
/**
38+
* Enforce the usage of the nullish coalescing operator instead of logical chaining.
39+
*
40+
* @see [prefer-nullish-coalescing](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md)
41+
*/
42+
export interface PreferNullishCoalescingRule {
43+
/**
44+
* Enforce the usage of the nullish coalescing operator instead of logical chaining.
45+
*
46+
* @see [prefer-nullish-coalescing](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md)
47+
*/
48+
'@typescript-eslint/prefer-nullish-coalescing': PreferNullishCoalescingRuleConfig;
49+
}

0 commit comments

Comments
 (0)