Skip to content

#8745 - Refactor: Use nullish coalescing operator#9497

Open
bekaChaduneli wants to merge 1 commit intomasterfrom
8745-use-nullish-coalescing-operator
Open

#8745 - Refactor: Use nullish coalescing operator#9497
bekaChaduneli wants to merge 1 commit intomasterfrom
8745-use-nullish-coalescing-operator

Conversation

@bekaChaduneli
Copy link
Collaborator

How the feature works? / How did you fix the issue?

Replaced logical OR (||) operators with nullish coalescing (??) operators across all packages where the left-hand operand can only be null or undefined, following the @typescript-eslint/prefer-nullish-coalescing rule.

Changes made:

  1. ketcher-macromoleculeseditorSlice.ts, useIndigoVersionToRedux.ts - Replaced process.env.X || '' with ??

    • Before: process.env.BUILD_DATE || ''
    • After: process.env.BUILD_DATE ?? ''
  2. ketcher-reactKetcherBuilder.ts - Replaced optional config params fallbacks

    • Before: buttons || {}, customButtons || [], process.env.VERSION || ''
    • After: buttons ?? {}, customButtons ?? [], process.env.VERSION ?? ''
  3. ketcher-reactsgroup.ts, select.ts, rotate-controller.ts - Replaced editor.selection() || {} and ?.atoms || [] patterns

    • Before: this.editor.selection() || {}
    • After: this.editor.selection() ?? {}
  4. ketcher-react – ContextMenu hooks (useAtomEdit, useBondEdit, useBondTypeChange, useChangeBondDirection, useRGroupAttachmentPointEdit, useRGroupAttachmentPointRemove) - Replaced optional prop fallbacks

    • Before: props?.bondIds || []
    • After: props?.bondIds ?? []
  5. ketcher-react – Toolbar containers (TopToolbar, BottomToolbar, LeftToolbar, RightToolbar, FloatingTools) - Replaced Redux state fallbacks

    • Before: state.actionState || {}
    • After: state.actionState ?? {}
  6. ketcher-coreZoom.ts, BaseRenderer.ts, TransientDrawingView.ts - Replaced optional chained dimension fallbacks

    • Before: canvasWrapperBbox?.width || 0, ZoomTool.instance?.canvas || select(...)
    • After: canvasWrapperBbox?.width ?? 0, ZoomTool.instance?.canvas ?? select(...)
  7. ketcher-coreremoteStructService.ts, SettingsManager.ts, rotate.ts, sgroup.ts, atom.ts, AttachmentPoint.ts - Replaced remaining optional-chain fallback patterns

  8. ketcher-standalonestandaloneStructService.ts, indigoWorker.ts - Replaced selected || [] and options || {}

  9. ketcher-macromoleculesrnaBuilderSlice.ts, librarySlice.ts, rnaBuilderSlice.helper.ts, manipulateCachedRnaPresets.ts, getMonomerName.ts, supportedFormatProperties.ts - Replaced label/preset/option fallbacks

Note: structConverter.ts was intentionally left unchanged — its || operators are boolean OR conditions in predicate logic (checking numeric 0-based flags), where replacing with ?? would change semantics and introduce bugs.

Approach: Only replaced || with ?? when:

  • ✅ The left operand is typed as T | null | undefined
  • ✅ The default value should only apply for null/undefined, not 0, false, or ""
  • ✅ The replacement does not change runtime behavior

Check list

  • unit-tests written
  • e2e-tests written
  • documentation updated
  • PR name follows the pattern #1234 – issue name
  • branch name doesn't contain '#'
  • PR is linked with the issue
  • base branch (master or release/xx) is correct
  • task status changed to "Code review"
  • reviewers are notified about the pull request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: Use nullish coalescing operator

2 participants