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-condition] better message when comparing between literal types#10454

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 4 commits intotypescript-eslint:mainfromkirkwaiblinger:nuc
Dec 14, 2024
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
11 changes: 7 additions & 4 deletionspackages/eslint-plugin/src/rules/no-unnecessary-condition.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -183,7 +183,7 @@ export type MessageId =
| 'alwaysNullish'
| 'alwaysTruthy'
| 'alwaysTruthyFunc'
| 'literalBooleanExpression'
| 'comparisonBetweenLiteralTypes'
| 'never'
| 'neverNullish'
| 'neverOptionalChain'
Expand DownExpand Up@@ -211,8 +211,8 @@ export default createRule<Options, MessageId>({
alwaysTruthy: 'Unnecessary conditional, value is always truthy.',
alwaysTruthyFunc:
'This callback should return a conditional, but return is always truthy.',
literalBooleanExpression:
'Unnecessary conditional, comparison is always {{trueOrFalse}}. Both sides of the comparison always have a literal type.',
comparisonBetweenLiteralTypes:
'Unnecessary conditional, comparison is always {{trueOrFalse}}, since `{{left}} {{operator}} {{right}}` is {{trueOrFalse}}.',
never: 'Unnecessary conditional, value is `never`.',
neverNullish:
'Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined.',
Expand DownExpand Up@@ -489,8 +489,11 @@ export default createRule<Options, MessageId>({

context.report({
node,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
data: {
left: checker.typeToString(leftType),
operator,
right: checker.typeToString(rightType),
trueOrFalse: conditionIsTrue ? 'true' : 'false',
},
});
Expand Down
193 changes: 156 additions & 37 deletionspackages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1048,9 +1048,14 @@ switch (b1) {
{ column: 18, line: 16, messageId: 'alwaysTruthy' },
{
column: 8,
data: { trueOrFalse: 'true' },
data: {
left: 'true',
operator: '===',
right: 'true',
trueOrFalse: 'true',
},
line: 18,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand DownExpand Up@@ -1144,10 +1149,13 @@ function test(a: 'a') {
{
column: 10,
data: {
left: '"a"',
operator: '===',
right: '"a"',
trueOrFalse: 'true',
},
line: 3,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1160,10 +1168,13 @@ a > b;
errors: [
{
data: {
left: '"34"',
operator: '>',
right: '"56"',
trueOrFalse: 'false',
},
line: 4,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1175,9 +1186,14 @@ if (y === 0) {
`,
errors: [
{
data: { trueOrFalse: 'false' },
data: {
left: '1',
operator: '===',
right: '0',
trueOrFalse: 'false',
},
line: 3,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1189,9 +1205,14 @@ if (1 == '1') {
`,
errors: [
{
data: { trueOrFalse: 'true' },
data: {
left: '1',
operator: '==',
right: '"1"',
trueOrFalse: 'true',
},
line: 3,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1201,9 +1222,14 @@ if (1 == '1') {
`,
errors: [
{
data: { trueOrFalse: 'false' },
data: {
left: '2.3',
operator: '>',
right: '2.3',
trueOrFalse: 'false',
},
line: 2,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1213,9 +1239,14 @@ if (1 == '1') {
`,
errors: [
{
data: { trueOrFalse: 'true' },
data: {
left: '2.3',
operator: '>=',
right: '2.3',
trueOrFalse: 'true',
},
line: 2,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1225,9 +1256,14 @@ if (1 == '1') {
`,
errors: [
{
data: { trueOrFalse: 'false' },
data: {
left: '2n',
operator: '<',
right: '2n',
trueOrFalse: 'false',
},
line: 2,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1237,9 +1273,14 @@ if (1 == '1') {
`,
errors: [
{
data: { trueOrFalse: 'true' },
data: {
left: '2n',
operator: '<=',
right: '2n',
trueOrFalse: 'true',
},
line: 2,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1249,9 +1290,14 @@ if (1 == '1') {
`,
errors: [
{
data: { trueOrFalse: 'true' },
data: {
left: '-2n',
operator: '!==',
right: '2n',
trueOrFalse: 'true',
},
line: 2,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1263,9 +1309,14 @@ if (1 == '2') {
`,
errors: [
{
data: { trueOrFalse: 'false' },
data: {
left: '1',
operator: '==',
right: '"2"',
trueOrFalse: 'false',
},
line: 3,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1277,9 +1328,14 @@ if (1 != '2') {
`,
errors: [
{
data: { trueOrFalse: 'true' },
data: {
left: '1',
operator: '!=',
right: '"2"',
trueOrFalse: 'true',
},
line: 3,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1298,10 +1354,38 @@ if (x === Foo.a) {
{
column: 5,
data: {
left: 'Foo.a',
operator: '===',
right: 'Foo.a',
trueOrFalse: 'true',
},
line: 8,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
{
code: `
enum Foo {
a = 1,
b = 2,
}

const x = Foo.a;
if (x === 1) {
}
`,
errors: [
{
column: 5,
data: {
left: 'Foo.a',
operator: '===',
right: 1,
trueOrFalse: 'true',
},
line: 8,
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1317,11 +1401,16 @@ function takesMaybeValue(a: null | object) {
errors: [
{
column: 14,
data: { trueOrFalse: 'true' },
data: {
left: 'null',
operator: '==',
right: 'undefined',
trueOrFalse: 'true',
},
endColumn: 28,
endLine: 4,
line: 4,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1337,11 +1426,16 @@ function takesMaybeValue(a: null | object) {
errors: [
{
column: 14,
data: { trueOrFalse: 'false' },
data: {
left: 'null',
operator: '===',
right: 'undefined',
trueOrFalse: 'false',
},
endColumn: 29,
endLine: 4,
line: 4,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1357,11 +1451,16 @@ function takesMaybeValue(a: null | object) {
errors: [
{
column: 14,
data: { trueOrFalse: 'false' },
data: {
left: 'null',
operator: '!=',
right: 'undefined',
trueOrFalse: 'false',
},
endColumn: 28,
endLine: 4,
line: 4,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1377,11 +1476,16 @@ function takesMaybeValue(a: null | object) {
errors: [
{
column: 14,
data: { trueOrFalse: 'true' },
data: {
left: 'null',
operator: '!==',
right: 'undefined',
trueOrFalse: 'true',
},
endColumn: 29,
endLine: 4,
line: 4,
messageId: 'literalBooleanExpression',
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1391,8 +1495,13 @@ true === false;
`,
errors: [
{
data: { trueOrFalse: 'false' },
messageId: 'literalBooleanExpression',
data: {
left: 'true',
operator: '===',
right: 'false',
trueOrFalse: 'false',
},
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1402,8 +1511,13 @@ true === true;
`,
errors: [
{
data: { trueOrFalse: 'true' },
messageId: 'literalBooleanExpression',
data: {
left: 'true',
operator: '===',
right: 'true',
trueOrFalse: 'true',
},
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand All@@ -1413,8 +1527,13 @@ true === undefined;
`,
errors: [
{
data: { trueOrFalse: 'false' },
messageId: 'literalBooleanExpression',
data: {
left: 'true',
operator: '===',
right: 'undefined',
trueOrFalse: 'false',
},
messageId: 'comparisonBetweenLiteralTypes',
},
],
},
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp