Bug: [prefer-arrow-callback] autofix produces a syntax error when the function is the test of a conditional
Environment
Node version: v24.16.0 npm version: v11.13.0 Local ESLint version: v10.11.0 (also reproduced on main at efc4d6bd and in the playground, v10.11.0) Global ESLint version: Not found Operating System: darwin 24.6.0
What parser are you using?
Default (Espree)
What did you do?
Configurationexport default [
{
rules: {
"prefer-arrow-callback": "error",
},
},
];foo(function() {} ? a : b);Command:
printf '%s' 'foo(function() {} ? a : b);' | npx eslint --no-config-lookup --rule 'prefer-arrow-callback: error' --stdin --fix-dry-run --format jsonWhat did you expect to happen?
The fix should keep the code valid by wrapping the arrow function in parentheses:
foo((() => {}) ? a : b);The rule already does this when the function is an operand of a logical expression. foo(nativeCb || function() {}) becomes foo(nativeCb || (() => {})).
What actually happened?
The fixed code does not parse. Output of the command above, trimmed to output and messages:
{"output": "foo(() => {} ? a : b);", "messages": [{"ruleId": null, "fatal": true, "message": "Parsing error: Unexpected token ?", "line": 1, "column": 14}]}The same happens with these inputs (same command, same trimming):
{"output": "foo(() => { this; } ? a : b);", "messages": [{"ruleId": null, "fatal": true, "message": "Parsing error: Unexpected token ?", "line": 1, "column": 21}]}
{"output": "foo(bar ? () => {} ? a : b : c);", "messages": [{"ruleId": null, "fatal": true, "message": "Parsing error: Unexpected token ?", "line": 1, "column": 20}]}
{"output": "new Foo(() => {} ? a : b);", "messages": [{"ruleId": null, "fatal": true, "message": "Parsing error: Unexpected token ?", "line": 1, "column": 18}]}
{"output": "foo(async () => {} ? a : b);", "messages": [{"ruleId": null, "fatal": true, "message": "Parsing error: Unexpected token ?", "line": 1, "column": 20}]}Inputs, in the same order:
foo(function() { this; }.bind(this) ? a : b);foo(bar ? function() {} ? a : b : c);new Foo(function() {} ? a : b);foo(async function() {} ? a : b);
When the function is the consequent or the alternate, the output is valid: foo(bar ? function() {} : function() {}) becomes foo(bar ? () => {} : () => {}).
Link to Minimal Reproducible Example
Click Fix on the prefer-arrow-callback message: the result is foo(() => {} ? a : b); with Parsing error: Unexpected token ? at 2:14.
Participation
- I am willing to submit a pull request for this issue.
AI acknowledgment
- I did not use AI to generate this issue report.
- (If the above is not checked) I have reviewed the AI-generated content before submitting.
Additional comments
The fixer in lib/rules/prefer-arrow-callback.js skips the parentheses whenever the parent is a ConditionalExpression. That is right for the consequent and the alternate, but not for the test. An arrow function cannot be the test of ?: without parentheses. I have a fix with tests ready: add the parentheses only when the replaced node is the test of the conditional.
Source: eslint/eslint