Skip to content

feat(eslint-plugin): [no-unnecessary-cond] check logical assignments #6594

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
13 changes: 13 additions & 0 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,20 @@ export default createRule<Options, MessageId>({
checkOptionalChain(node, node.callee, '');
}

function checkAssignmentExpression(
node: TSESTree.AssignmentExpression,
): void {
// Similar to checkLogicalExpressionForUnnecessaryConditionals, since
// a ||= b is equivalent to a || (a = b)
if (['||=', '&&='].includes(node.operator)) {
checkNode(node.left);
} else if (node.operator === '??=') {
checkNodeForNullish(node.left);
}
}

return {
AssignmentExpression: checkAssignmentExpression,
BinaryExpression: checkIfBinaryExpressionIsNecessaryConditional,
CallExpression: checkCallExpression,
ConditionalExpression: (node): void => checkNode(node.test),
Expand Down
117 changes: 117 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,18 @@ interface Foo {
type OptionalFoo = Foo | undefined;
declare const foo: OptionalFoo;
foo?.[1]?.length;
`,
`
declare let foo: number | null;
foo ??= 1;
`,
`
declare let foo: number;
foo ||= 1;
`,
`
declare let foo: number;
foo &&= 1;
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/6264
`
Expand Down Expand Up @@ -1676,5 +1688,110 @@ function getElem(dict: Record<string, { foo: string }>, key: string) {
},
],
},
{
code: `
declare let foo: {};
foo ??= 1;
`,
errors: [
{
messageId: 'neverNullish',
line: 3,
endLine: 3,
column: 1,
endColumn: 4,
},
],
},
{
code: `
declare let foo: number;
foo ??= 1;
`,
errors: [
{
messageId: 'neverNullish',
line: 3,
endLine: 3,
column: 1,
endColumn: 4,
},
],
},
{
code: `
declare let foo: null;
foo ??= null;
`,
errors: [
{
messageId: 'alwaysNullish',
line: 3,
endLine: 3,
column: 1,
endColumn: 4,
},
],
},
{
code: `
declare let foo: {};
foo ||= 1;
`,
errors: [
{
messageId: 'alwaysTruthy',
line: 3,
endLine: 3,
column: 1,
endColumn: 4,
},
],
},
{
code: `
declare let foo: null;
foo ||= null;
`,
errors: [
{
messageId: 'alwaysFalsy',
line: 3,
endLine: 3,
column: 1,
endColumn: 4,
},
],
},
{
code: `
declare let foo: {};
foo &&= 1;
`,
errors: [
{
messageId: 'alwaysTruthy',
line: 3,
endLine: 3,
column: 1,
endColumn: 4,
},
],
},
{
code: `
declare let foo: null;
foo &&= null;
`,
errors: [
{
messageId: 'alwaysFalsy',
line: 3,
endLine: 3,
column: 1,
endColumn: 4,
},
],
},
],
});