Skip to content

Refactor: Use nullish coalescing operator #8745

@NikitaKuzminn

Description

@NikitaKuzminn

Problem:
Prefer using nullish coalescing operator (??) instead of a logical or (||), as it is a safer operator.

Why is this an issue?
The nullish coalescing operator (??) allows providing a default value when dealing with null or undefined. It only coalesces when the original value is null or undefined. Therefore, it is safer and shorter than relying upon chaining logical (||) expressions or testing against null or undefined explicitly.

This rule reports when disjunctions (||) and conditionals (?) can be safely replaced with coalescing (??).

The TSConfig needs to set strictNullChecks to true for the rule to work properly.

How can i fix that?
Rewrite the logical expression || using ?? on the unchecked operands.

Noncompliant code example

function either(x: number | undefined, y: number) {
  return x || y;
}

Compliant solution

function either(x: number | undefined, y: number) {
  return x ?? y;
}

Noncompliant code example

function either(x: number | undefined, y: number) {
  return x !== undefined ? x : y;
}

Compliant solution

function either(x: number | undefined, y: number) {
  return x ?? y;
}

Problem locations:
packages/ketcher-core/src/application/editor/actions/rotate.ts:50
packages/ketcher-react/src/script/ui/component/view/Tabs/Tabs.tsx:45
packages/ketcher-react/src/script/ui/data/convert/structConverter.ts:50
packages/ketcher-react/src/script/ui/state/modal/sdata.ts:118
packages/ketcher-react/src/script/ui/state/modal/sdata.ts:119
packages/ketcher-react/src/script/ui/state/modal/sdata.ts:121
packages/ketcher-react/src/script/ui/state/modal/sdata.ts:147
packages/ketcher-react/src/script/ui/state/modal/sdata.ts:152
packages/ketcher-react/src/script/ui/state/modal/sdata.ts:157
packages/ketcher-react/src/script/ui/state/modal/sdata.ts:203

Metadata

Metadata

Assignees

Labels

Priority: LowSeverity: LowLow level visibility to the userSonarBucket: Issues found by Sonar scanner

Type

No type

Projects

Status

In review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions