oxc-project/oxc · warning · OxcDiagnostic
Expected a function {style}.
Error message
Expected a function {style}. What it means
Oxlint's port of the ESLint rule func-style: it forces one definition style per codebase. The Style enum (camelCase serde over kebab-case values) is `expression` (the default) or `declaration` (crates/oxc_linter/src/rules/eslint/func_style.rs:20). Under `expression`, top-level `function foo() {}` declarations are reported with 'Expected a function expression.'; under `declaration`, `const foo = function () {}` assignments are reported with 'Expected a function declaration.'.
Source
Thrown at crates/oxc_linter/src/rules/eslint/func_style.rs:22
use oxc_ast::{
AstKind,
ast::{ArrowFunctionExpression, Function, FunctionType, Super, ThisExpression},
};
use oxc_ast_visit::VisitJs;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::{AstNode, ScopeFlags};
use oxc_span::Span;
use crate::{
ast_util::nth_outermost_paren_parent,
context::LintContext,
rule::{Rule, TupleRuleConfig},
};
fn func_style_diagnostic(span: Span, style: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Expected a function {style}."))
.with_help("Enforce the consistent use of either `function` declarations or expressions assigned to variables")
.with_label(span)
}
#[derive(Debug, Default, PartialEq, Clone, Copy, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
enum Style {
#[default]
Expression,
Declaration,
}
impl Style {
pub fn as_str(&self) -> &str {
match self {
Style::Expression => "expression",
Style::Declaration => "declaration",
}View on GitHub (pinned to e1e7af627c)
Solutions
- Convert the flagged function to the enforced style (expression: const + function/arrow; declaration: `function name()`).
- Choose the option matching the majority of the codebase to minimize churn.
- Disable the rule if the team accepts both styles.
- Watch for hoisting: if the function is used before its definition point, a declaration is required.
Example fix
// before (style: expression)
function helper(x) {
return x * 2;
}
// after
const helper = (x) => x * 2; Defensive patterns
Strategy: validation
Validate before calling
// .oxlintrc.json — pick the dominant style of the codebase
{
"rules": {
"func-style": ["warn", "expression"]
}
} Prevention
- Settle one style in the shared config before scaling the team.
- Use declarations when hoisting is required (calls before definition in module scope).
- Do not fight the rule with per-file disables; change the config instead.
When it happens
Trigger: Config ["warn", "expression"] plus a `function helper() {}` declaration; config ["warn", "declaration"] plus a `const helper = function () {}` assignment.
Common situations: Standardizing a codebase that grew with mixed styles; hoisting-dependent code that actually requires declarations; formatter rewrites that do not change the underlying style.
Related errors
- Expected method{method_name_str} to have this.
- {prefix} {{ after '{keyword}'{condition_if_needed}.
- Enforce default clauses in switch statements to be last
- Function name `{func_name}` should match property name `{nam
- Function name `{func_name}` should match variable name `{nam
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/846adc849b3924c5.
Report an issue: GitHub.