Skip to content

linter: false positive in oxc/branches-sharing-code with ; starting at the line #21964

Description

@Mister-Hope

What version of Oxlint are you using?

latest

What command did you run?

No response

What does your .oxlintrc.json (or oxlint.config.ts) config file look like?

.

What happened?

Check the following code:

const { entries } = Object
const { isArray } = Array

const isPlainObject = (val: unknown): val is Record<string, unknown> =>
  Object.prototype.toString.call(val) === '[object Object]'

// oxlint-disable-next-line typescript/no-explicit-any
type IAnyObject = Record<string, any>

/**
 * Deep merge objects to the first one
 *
 * 深度合并对象到第一个对象
 *
 * @example
 *   const obj1 = { a: 1, b: { c: 2 } }
 *   const obj2 = { b: { d: 3 }, e: 4 }
 *   deepAssign(obj1, obj2) // { a: 1, b: { c: 2, d: 3 }, e: 4 }
 *
 * @param originObject - The target object to merge into / 要合并到的目标对象
 * @param overrideObjects - Objects to merge from / 要合并的对象
 * @returns Merged object / 合并后的对象
 */
export const deepAssign = <
  OriginType extends IAnyObject,
  MergeType extends IAnyObject = OriginType,
  // oxlint-disable-next-line typescript/no-unnecessary-type-parameters
  FinalType extends Partial<OriginType> & Partial<MergeType> = OriginType &
    MergeType,
>(
  originObject: OriginType,
  ...overrideObjects: (MergeType | null | undefined)[]
): FinalType => {
  if (overrideObjects.length === 0) return originObject as unknown as FinalType

  /** Object being merged */
  const assignObject = overrideObjects.shift()

  if (assignObject) {
    entries(assignObject).forEach(([property, value]) => {
      if (property === '__proto__' || property === 'constructor') return
      if (isPlainObject(originObject[property]) && isPlainObject(value)) {
        deepAssign(originObject[property], value)
      } else if (isArray(value)) {
        ;(originObject as IAnyObject)[property] = [...value]
      } else if (isPlainObject(value)) {
        ;(originObject as IAnyObject)[property] = {
          ...value,
        }
      } else {
        ;(originObject as IAnyObject)[property] = assignObject[
          property
        ] as unknown
      }
    })
  }

  return deepAssign(originObject, ...overrideObjects)
}

Oxlint gives:

 ⚠ oxc(branches-sharing-code): All `if` blocks contain the same code at the start
    ╭─[tools/helper/src/shared/deepAssign.ts:40:14]
 39 │         deepAssign(originObject[property], value)
 40 │       } else if (isArray(value)) {
    ·              ─┬
    ·               ╰── `if` statement declared here
 41 │         ;(originObject as IAnyObject)[property] = [...value]
    ·         ─
 42 │       } else if (isPlainObject(value)) {
 43 │         ;(originObject as IAnyObject)[property] = {
    ·         ─
 44 │           ...value,
 45 │         }
 46 │       } else {
 47 │         ;(originObject as IAnyObject)[property] = assignObject[
    ·         ─
 48 │           property
    ╰────
  help: Move the shared code outside the `if` statement to reduce code duplication

  ⚠ oxc(branches-sharing-code): All `if` blocks contain the same code at the start
    ╭─[tools/helper/src/shared/deepAssign.ts:42:14]
 41 │         ;(originObject as IAnyObject)[property] = [...value]
 42 │       } else if (isPlainObject(value)) {
    ·              ─┬
    ·               ╰── `if` statement declared here
 43 │         ;(originObject as IAnyObject)[property] = {
    ·         ─
 44 │           ...value,
 45 │         }
 46 │       } else {
 47 │         ;(originObject as IAnyObject)[property] = assignObject[
    ·         ─
 48 │           property
    ╰────
  help: Move the shared code outside the `if` statement to reduce code duplication

But the ; is needed with semi: false.

Metadata

Metadata

Assignees

Labels

Type

Fields

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions