{"record":{"id":"4d3b87d56ffb6531","repo":"oxc-project/oxc","slug":"unexpected-function-expression","errorCode":null,"errorMessage":"Unexpected function expression.","messagePattern":"Unexpected function expression\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/prefer_arrow_callback.rs","lineNumber":26,"sourceCode":"    },\n};\nuse oxc_ast_visit::Visit;\nuse oxc_codegen::{Context, Gen};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_ecmascript::{BoundNames, IsSimpleParameterList};\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::ScopeFlags;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    fixer::{RuleFix, RuleFixer},\n    rule::{DefaultRuleConfig, Rule},\n};\n\nfn prefer_arrow_callback_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected function expression.\")\n        .with_help(\"Use an arrow function instead.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\nstruct PreferArrowCallbackConfig {\n    /// If this option is set to `true`, named function expressions are allowed.\n    allow_named_functions: bool,\n    /// If this option is set to `false`, function expressions that reference `this` are reported even when they are not bound to a `this` value.\n    allow_unbound_this: bool,\n}\n\nimpl Default for PreferArrowCallbackConfig {\n    fn default() -> Self {\n        Self { allow_named_functions: false, allow_unbound_this: true }\n    }\n}","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/prefer_arrow_callback.rs#L8-L44","documentation":"Diagnostic from the oxlint `prefer-arrow-callback` rule. It fires when a function expression is passed as a callback (e.g. `arr.map(function (x) { ... })`) and an arrow function would work (prefer_arrow_callback.rs:26-30). By default named function expressions and callbacks that reference `this` (allowUnboundThis) are exempt, per the config in the same file.","triggerScenarios":"Enable `prefer-arrow-callback` and pass an anonymous function expression to any callback position: `setTimeout(function () { ... })`, `[1,2].map(function (n) { return n * 2; })`. Setting allowNamedFunctions=false also flags `map(function double(n) {...})`; allowUnboundThis=false flags callbacks using `this` even unbound.","commonSituations":"ES5-era code being modernized; callbacks written before the project adopted ES6; teams enabling the option set to catch Vue/jQuery-style `this` usage; interop code where arrow functions change `this` binding and the rule still suggests them (false-positive looking cases with thisArg arguments).","solutions":["Replace the function expression with an arrow function: `function (x) { return x; }` becomes `(x) => x` (auto-fixable where safe).","If the callback relies on its own `this` or `arguments`, keep `function` and add an inline disable, or pass an explicit thisArg and convert.","Tune options: allowNamedFunctions for named helpers, allowUnboundThis (default) to skip this-using callbacks.","Leave the rule on in CI and fix incrementally file by file."],"exampleFix":"// before\nconst doubled = nums.map(function (n) { return n * 2; });\n\n// after\nconst doubled = nums.map((n) => n * 2);","handlingStrategy":"validation","validationCode":"// .oxlintrc.json — tune exemptions before enabling broadly\n{ \"rules\": { \"prefer-arrow-callback\": [\"warn\", { \"allowNamedFunctions\": false, \"allowUnboundThis\": true }] } }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default to arrow functions for callbacks; reserve `function` for this/arguments-dependent logic.","Keep allowUnboundThis true (default) unless you specifically audit this-usage.","Let `oxlint --fix` convert callbacks where semantics are unchanged."],"tags":["oxlint","eslint","style","arrow-functions","callbacks","es6"],"backgroundTag":"eslint-prefer-arrow-callback","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}