Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- `noUnnecessaryCondition`: Catch useless `?? null` and `?? undefined` coalescing and add "Remove unnecessary nullish coalescing" suggestion.

## 1.0.27

- `preferOptionalChain`: fix chain analysis when the chain ends with `=== null`
Expand Down
201 changes: 186 additions & 15 deletions src/rules/noUnnecessaryCondition/noUnnecessaryCondition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ function test<T>(arg: T | { value: string }) {
}
}
`,
`declare const foo: string | undefined; const bar = foo ?? null;`,
],
invalid: [
// Ensure that it's checking in all the right places
Expand Down Expand Up @@ -1556,38 +1557,104 @@ function test<T extends object>(a: T) {
column: 15,
},
],
}, // Nullish coalescing operator
},
// Nullish coalescing operator
{
code: `
function test(a: string) {
return a ?? 'default';
}
`,
errors: [{ message: messages.neverNullish, line: 3, column: 10 }],
errors: [
{
message: messages.neverNullish,
line: 3,
column: 12,
suggestions: [
{
message: messages.removeNullishCoalescing,
output: `
function test(a: string) {
return a;
}
`,
},
],
},
],
},
{
code: `
function test(a: string | false) {
return a ?? 'default';
}
`,
errors: [{ message: messages.neverNullish, line: 3, column: 10 }],
errors: [
{
message: messages.neverNullish,
line: 3,
column: 12,
suggestions: [
{
message: messages.removeNullishCoalescing,
output: `
function test(a: string | false) {
return a;
}
`,
},
],
},
],
},
{
code: `
function test<T extends string>(a: T) {
return a ?? 'default';
}
`,
errors: [{ message: messages.neverNullish, line: 3, column: 10 }],
}, // nullish + array index without optional chaining
errors: [
{
message: messages.neverNullish,
line: 3,
column: 12,
suggestions: [
{
message: messages.removeNullishCoalescing,
output: `
function test<T extends string>(a: T) {
return a;
}
`,
},
],
},
],
},
// nullish + array index without optional chaining
{
code: `
function test(a: { foo: string }[]) {
return a[0].foo ?? 'default';
}
`,
errors: [{ message: messages.neverNullish, line: 3, column: 10 }],
errors: [
{
message: messages.neverNullish,
line: 3,
column: 19,
suggestions: [
{
message: messages.removeNullishCoalescing,
output: `
function test(a: { foo: string }[]) {
return a[0].foo;
}
`,
},
],
},
],
},
{
code: `
Expand Down Expand Up @@ -1619,16 +1686,49 @@ function test(a: never) {
return a ?? 'default';
}
`,
errors: [{ message: messages.never, line: 3, column: 10 }],
errors: [
{
message: messages.never,
line: 3,
column: 12,
suggestions: [
{
message: messages.removeNullishCoalescing,
output: `
function test(a: never) {
return a;
}
`,
},
],
},
],
},
{
code: `
function test<T extends { foo: number }, K extends 'foo'>(num: T[K]) {
num ?? 'default';
}
`,
errors: [{ message: messages.neverNullish, line: 3, column: 3 }],
}, // Predicate functions
errors: [
{
message: messages.neverNullish,
line: 3,
column: 7,
suggestions: [
{
message: messages.removeNullishCoalescing,
output: `
function test<T extends { foo: number }, K extends 'foo'>(num: T[K]) {
num;
}
`,
},
],
},
],
},
// Predicate functions
{
code: `
[1, 3, 5].filter(() => true);
Expand Down Expand Up @@ -2757,9 +2857,18 @@ foo ??= 1;
{
message: messages.neverNullish,
line: 3,
column: 1,
endColumn: 4,
column: 5,
endLine: 3,
endColumn: 10,
suggestions: [
{
message: messages.removeNullishCoalescing,
output: `
declare let foo: {};
foo;
`,
},
],
},
],
},
Expand All @@ -2772,9 +2881,18 @@ foo ??= 1;
{
message: messages.neverNullish,
line: 3,
column: 1,
endColumn: 4,
column: 5,
endLine: 3,
endColumn: 10,
suggestions: [
{
message: messages.removeNullishCoalescing,
output: `
declare let foo: number;
foo;
`,
},
],
},
],
},
Expand Down Expand Up @@ -2863,9 +2981,18 @@ foo.bar ??= 1;
{
message: messages.neverNullish,
line: 3,
column: 1,
endColumn: 8,
column: 9,
endLine: 3,
endColumn: 14,
suggestions: [
{
message: messages.removeNullishCoalescing,
output: `
declare const foo: { bar: number };
foo.bar;
`,
},
],
},
],
},
Expand Down Expand Up @@ -3185,5 +3312,49 @@ if (arr[42] && arr[42]) {}
`,
errors: [{ message: messages.alwaysTruthy, line: 3, column: 16 }],
},
{
code: `
declare const foo: string | null;
const bar = foo ?? null;
`,
errors: [
{
message: messages.uselessNullCoalescing,
line: 3,
column: 17,
suggestions: [
{
message: messages.removeNullishCoalescing,
output: `
declare const foo: string | null;
const bar = foo;
`,
},
],
},
],
},
{
code: `
declare const foo: string | undefined;
const bar = foo ?? undefined;
`,
errors: [
{
message: messages.uselessUndefinedCoalescing,
line: 3,
column: 17,
suggestions: [
{
message: messages.removeNullishCoalescing,
output: `
declare const foo: string | undefined;
const bar = foo;
`,
},
],
},
],
},
],
});
Loading
Loading