oxc-project/oxc · warning · OxcDiagnostic

Unexpected named {function_name}.

Error message

Unexpected named {function_name}.

What it means

One of the two diagnostics in oxlint's func-names rule (crates/oxc_linter/src/rules/eslint/func_names.rs:28). named_diagnostic fires when a function expression carries a name the rule considers unnecessary: with the `as-needed` option, names are only permitted when actually needed (e.g. the body references itself), so an unneeded name is reported as 'Unexpected named function expression.' The name/kind in the message comes from get_function_name_with_kind. The complementary unnamed_diagnostic fires under the `always` option (the default) for nameless expressions.

Source

Thrown at crates/oxc_linter/src/rules/eslint/func_names.rs:29

use oxc_macros::declare_oxc_lint;
use oxc_semantic::NodeId;
use oxc_span::{GetSpan, Span};
use oxc_str::Ident;
use oxc_syntax::{identifier::is_identifier_name, keyword::is_reserved_keyword_or_global_object};

use schemars::JsonSchema;
use serde::Deserialize;

use crate::{
    AstNode,
    ast_util::get_function_name_with_kind,
    context::LintContext,
    fixer::{RuleFix, RuleFixer},
    rule::{Rule, TupleRuleConfig},
};

fn named_diagnostic(function_name: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Unexpected named {function_name}."))
        .with_label(span)
        .with_help("Remove the name on this function expression.")
}

fn unnamed_diagnostic(inferred_name_or_description: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Unexpected unnamed {inferred_name_or_description}."))
        .with_label(span)
        .with_help("Consider giving this function expression a name.")
}

#[derive(Debug, Default, Clone, Copy, PartialEq, JsonSchema, Deserialize)]
#[serde(rename_all = "kebab-case")]
enum FuncNamesConfigType {
    /// Requires all function expressions to have a name.
    #[default]
    Always,
    /// Requires a name only if one is not automatically inferred.
    AsNeeded,

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Remove the unneeded name: `const parse = function (t)` or an arrow function — the binding still names it.
  2. Keep names only where the body references them (recursion) or an API requires named handlers.
  3. If you want names everywhere for stack traces, set the option back to `always` or disable the rule.

Example fix

// before (option: as-needed)
const parse = function parseJson(text) {
  return JSON.parse(text);
};

// after
const parse = (text) => JSON.parse(text);
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json — choose "always" if names aid your stack traces
{
  "rules": {
    "func-names": ["warn", "as-needed"]
  }
}

Prevention

When it happens

Trigger: Rule configured with "as-needed" and code like `const parse = function parseJson(t) { return JSON.parse(t); }` where the body never references parseJson, so the name is inferable and not needed.

Common situations: Migrating configs that set as-needed; teams split between keep-names-for-stack-traces and anonymous styles; names added only for debugging that survive into commits.

Related errors


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