From c344d37ea1daac7a667447b2cc35feb9d0acd622 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 7 Apr 2024 09:08:51 -0400 Subject: [PATCH] feat(eslint-plugin): [prefer-nullish-coalescing] change ignoreConditionalTests default to true --- .../eslint-plugin/docs/rules/prefer-nullish-coalescing.mdx | 2 +- .../eslint-plugin/src/rules/prefer-nullish-coalescing.ts | 2 +- .../tests/rules/prefer-nullish-coalescing.test.ts | 7 ------- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.mdx b/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.mdx index 3215b83dc832..6748b58873ba 100644 --- a/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.mdx +++ b/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.mdx @@ -65,7 +65,7 @@ foo ?? 'a string'; ### `ignoreConditionalTests` -Setting this option to `true` will cause the rule to ignore any cases that are located within a conditional test. This is set to `false` by default. +Setting this option to `false` will cause the rule to also check cases that are located within a conditional test. This is set to `true` by default. Generally expressions within conditional tests intentionally use the falsy fallthrough behavior of the logical or operator, meaning that fixing the operator to the nullish coalesce operator could cause bugs. diff --git a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts index 8a9bfae1928e..725bd115bbe0 100644 --- a/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts +++ b/packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts @@ -100,7 +100,7 @@ export default createRule({ defaultOptions: [ { allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false, - ignoreConditionalTests: false, + ignoreConditionalTests: true, ignoreTernaryTests: false, ignoreMixedLogicalExpressions: false, ignorePrimitives: { diff --git a/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts b/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts index c37a9431dae7..3259a8b6b2ea 100644 --- a/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts @@ -147,35 +147,30 @@ x === null ? x : y; declare const x: ${type} | ${nullish}; x || 'foo' ? null : null; `, - options: [{ ignoreConditionalTests: true }], })), ...nullishTypeValidTest((nullish, type) => ({ code: ` declare const x: ${type} | ${nullish}; if (x || 'foo') {} `, - options: [{ ignoreConditionalTests: true }], })), ...nullishTypeValidTest((nullish, type) => ({ code: ` declare const x: ${type} | ${nullish}; do {} while (x || 'foo') `, - options: [{ ignoreConditionalTests: true }], })), ...nullishTypeValidTest((nullish, type) => ({ code: ` declare const x: ${type} | ${nullish}; for (;x || 'foo';) {} `, - options: [{ ignoreConditionalTests: true }], })), ...nullishTypeValidTest((nullish, type) => ({ code: ` declare const x: ${type} | ${nullish}; while (x || 'foo') {} `, - options: [{ ignoreConditionalTests: true }], })), // ignoreMixedLogicalExpressions @@ -706,7 +701,6 @@ declare const x: ${type} | ${nullish}; if (() => x || 'foo') {} `, output: null, - options: [{ ignoreConditionalTests: true }], errors: [ { messageId: 'preferNullishOverOr', @@ -732,7 +726,6 @@ declare const x: ${type} | ${nullish}; if (function werid() { return x || 'foo' }) {} `, output: null, - options: [{ ignoreConditionalTests: true }], errors: [ { messageId: 'preferNullishOverOr',