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

feat(eslint-plugin): [no-unnecessary-boolean-literal-compare] enforce strictNullChecks#10712

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
JoshuaKGoldberg merged 2 commits intotypescript-eslint:mainfromyeonjuan:10706
Feb 3, 2025
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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -129,6 +129,22 @@ if (someNullCondition ?? true) {
</TabItem>
</Tabs>

### `allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing`

:::danger Deprecated

This option will be removed in the next major version of typescript-eslint.

:::

{/* insert option description */}

Without `strictNullChecks`, TypeScript essentially erases `undefined` and `null` from the types. This means when this rule inspects the types from a variable, **it will not be able to tell that the variable might be `null` or `undefined`**, which essentially makes this rule useless.

You should be using `strictNullChecks` to ensure complete type-safety in your codebase.

If for some reason you cannot turn on `strictNullChecks`, but still want to use this rule - you can use this option to allow it - but know that the behavior of this rule is _undefined_ with the compiler option turned off. We will not accept bug reports if you are using this option.

## Fixer

| Comparison | Fixer Output | Notes |
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,12 +16,14 @@ export type MessageIds =
| 'comparingNullableToTrueDirect'
| 'comparingNullableToTrueNegated'
| 'direct'
| 'negated';
| 'negated'
| 'noStrictNullCheck';

export type Options = [
{
allowComparingNullableBooleansToFalse?: boolean;
allowComparingNullableBooleansToTrue?: boolean;
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean;
},
];

Expand DownExpand Up@@ -57,6 +59,8 @@ export default createRule<Options, MessageIds>({
'This expression unnecessarily compares a boolean value to a boolean instead of using it directly.',
negated:
'This expression unnecessarily compares a boolean value to a boolean instead of negating it.',
noStrictNullCheck:
'This rule requires the `strictNullChecks` compiler option to be turned on to function correctly.',
},
schema: [
{
Expand All@@ -73,6 +77,11 @@ export default createRule<Options, MessageIds>({
description:
'Whether to allow comparisons between nullable boolean variables and `true`.',
},
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: {
type: 'boolean',
description:
'Unless this is set to `true`, the rule will error on every file whose `tsconfig.json` does _not_ have the `strictNullChecks` compiler option (or `strict`) set to `true`.',
},
},
},
],
Expand All@@ -81,11 +90,31 @@ export default createRule<Options, MessageIds>({
{
allowComparingNullableBooleansToFalse: true,
allowComparingNullableBooleansToTrue: true,
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false,
},
],
create(context, [options]) {
const services = getParserServices(context);
const checker = services.program.getTypeChecker();
const compilerOptions = services.program.getCompilerOptions();

const isStrictNullChecks = tsutils.isStrictCompilerOptionEnabled(
compilerOptions,
'strictNullChecks',
);

if (
!isStrictNullChecks &&
options.allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing !== true
) {
context.report({
loc: {
start: { column: 0, line: 0 },
end: { column: 0, line: 0 },
},
messageId: 'noStrictNullCheck',
});
}

function getBooleanComparison(
node: TSESTree.BinaryExpression,
Expand Down
1 change: 1 addition & 0 deletionspackages/eslint-plugin/tests/docs.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -201,6 +201,7 @@ describe('Validating rule docs', () => {
'switch-exhaustiveness-check',
'switch-exhaustiveness-check',
'unbound-method',
'no-unnecessary-boolean-literal-compare',
]);

it('All rules must have a corresponding rule doc', () => {
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
import { noFormat, RuleTester } from '@typescript-eslint/rule-tester';
import * as path from 'node:path';

import rule from '../../src/rules/no-unnecessary-boolean-literal-compare';
import { getFixturesRootDir } from '../RuleTester';
Expand DownExpand Up@@ -123,6 +124,24 @@ const extendsUnknown: <T extends unknown>(
}
};
`,
{
code: `
function test(a?: boolean): boolean {
// eslint-disable-next-line
return a !== false;
}
`,
languageOptions: {
parserOptions: {
tsconfigRootDir: path.join(rootDir, 'unstrict'),
},
},
options: [
{
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: true,
},
],
},
],

invalid: [
Expand DownExpand Up@@ -586,5 +605,25 @@ const extendsUnknown: <T extends unknown>(
};
`,
},
{
code: `
function foo(): boolean {}
`,
errors: [
{
messageId: 'noStrictNullCheck',
},
],
languageOptions: {
parserOptions: {
tsconfigRootDir: path.join(rootDir, 'unstrict'),
},
},
options: [
{
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false,
},
],
},
],
});

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp