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): [no-unnecessary-boolean-literal-compare] flag values of a type parameter with boolean type constraints#10474

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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,12 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';

import { createRule, getParserServices, isStrongPrecedenceNode } from '../util';
import {
createRule,
getConstrainedTypeAtLocation,
getParserServices,
isStrongPrecedenceNode,
} from '../util';

type MessageIds =
| 'comparingNullableToFalse'
Expand DownExpand Up@@ -89,7 +94,10 @@ export default createRule<Options, MessageIds>({
return undefined;
}

const expressionType = services.getTypeAtLocation(comparison.expression);
const expressionType = getConstrainedTypeAtLocation(
services,
comparison.expression,
);

if (isBooleanType(expressionType)) {
return {
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,6 +55,18 @@ ruleTester.run('no-unnecessary-boolean-literal-compare', rule, {
declare const varTrueOrStringOrUndefined: true | string | undefined;
varTrueOrStringOrUndefined == true;
`,
`
const test: <T>(someCondition: T) => void = someCondition => {
if (someCondition === true) {
}
};
`,
`
const test: <T>(someCondition: boolean | string) => void = someCondition => {
if (someCondition === true) {
}
};
`,
`
declare const varBooleanOrUndefined: boolean | undefined;
varBooleanOrUndefined === true;
Expand All@@ -73,6 +85,28 @@ ruleTester.run('no-unnecessary-boolean-literal-compare', rule, {
`,
options: [{ allowComparingNullableBooleansToTrue: false }],
},
{
code: `
const test: <T extends boolean | undefined>(
someCondition: T,
) => void = someCondition => {
if (someCondition === true) {
}
};
`,
options: [{ allowComparingNullableBooleansToFalse: false }],
},
{
code: `
const test: <T extends boolean | undefined>(
someCondition: T,
) => void = someCondition => {
if (someCondition === false) {
}
};
`,
options: [{ allowComparingNullableBooleansToTrue: false }],
},
"'false' === true;",
"'true' === false;",
],
Expand DownExpand Up@@ -481,5 +515,62 @@ ruleTester.run('no-unnecessary-boolean-literal-compare', rule, {
}
`,
},
{
code: `
const test: <T extends boolean>(someCondition: T) => void = someCondition => {
if (someCondition === true) {
}
};
`,
errors: [
{
messageId: 'direct',
},
],
output: `
const test: <T extends boolean>(someCondition: T) => void = someCondition => {
if (someCondition) {
}
};
`,
},
{
code: `
const test: <T extends boolean>(someCondition: T) => void = someCondition => {
if (!(someCondition !== false)) {
}
};
`,
errors: [
{
messageId: 'negated',
},
],
output: `
const test: <T extends boolean>(someCondition: T) => void = someCondition => {
if (!someCondition) {
}
};
`,
},
{
code: `
const test: <T extends boolean>(someCondition: T) => void = someCondition => {
if (!((someCondition ?? true) !== false)) {
}
};
`,
errors: [
{
messageId: 'negated',
},
],
output: `
const test: <T extends boolean>(someCondition: T) => void = someCondition => {
if (!(someCondition ?? true)) {
}
};
`,
},
],
});
Loading

[8]ページ先頭

©2009-2025 Movatter.jp