oxc-project/oxc · error · OxcDiagnostic

Prefer default parameters over reassignment for '{name}'.

Error message

Prefer default parameters over reassignment for '{name}'.

What it means

Diagnostic from the unicorn/prefer-default-parameters lint rule. It fires when a function body immediately reassigns a formal parameter with an expression like `name = name || 'default'` or `if (name === undefined) name = ...`. The parameter name in the message is the variable being reassigned. ES2015 default parameters (`function f(name = 'default')`) express the same intent in the signature and avoid a body-level mutation. It is a lint warning, not a runtime error.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_default_parameters.rs:16

use oxc_ast::{
    AstKind,
    ast::{
        AssignmentOperator, AssignmentTarget, BindingPattern, Expression, FormalParameter,
        LogicalOperator, MethodDefinitionKind, PropertyKind, Statement,
    },
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::node::NodeId;

use crate::{AstNode, context::LintContext, rule::Rule};

fn prefer_default_parameters_diagnostic(span: Span, name: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Prefer default parameters over reassignment for '{name}'."))
        .with_help("Replace the reassignment with a default parameter.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct PreferDefaultParameters;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Instead of reassigning a function parameter, default parameters should be used. The `foo = foo || 123` statement evaluates to `123` when `foo` is falsy, possibly leading to confusing behavior, whereas default parameters only apply when passed an `undefined` value.
    /// This rule only reports reassignments to literal values.
    ///
    /// You should disable this rule if you want your functions to deal with `null` and other falsy values the same way as `undefined`.
    /// Default parameters are exclusively applied [when `undefined` is received.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters#passing_undefined_vs._other_falsy_values).
    /// However, we recommend [moving away from `null`](https://github.com/sindresorhus/meta/discussions/7).
    ///
    /// ### Why is this bad?

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Move the fallback value into the parameter list: `function f(name = 'default')`
  2. Remove the reassignment statement from the function body
  3. Use a nullish-coalescing default in the signature (`name = fallback`) when `null` should also trigger the default
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_default_parameters.rs:16 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/23ffd42f61e8a52a. Report an issue: GitHub.