From 6708bb32651162e1e35fd1e87c5a0bc7dd03273f Mon Sep 17 00:00:00 2001 From: shuxiachai Date: Sun, 15 Nov 2026 21:42:00 +1100 Subject: [PATCH 1/5] fix(linter/unicorn/prefer-single-call): skip comments when checking for a trailing comma The fix looked for a trailing comma with `trim_end().ends_with(',')` on the text before `)`, so a comment after the comma hid it, and merging the calls added a second comma: items.push( "a", "b", // keep this one last ); items.push("c"); became `"b", // keep this one last\n, "c");`, which doesn't parse. Skip comments when looking for the trailing comma, as eslint-plugin-unicorn does. Co-Authored-By: Claude Opus 5.5 --- .../src/rules/unicorn/prefer_single_call.rs | 24 ++++++++++++++++--- .../snapshots/unicorn_prefer_single_call.snap | 15 ++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs b/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs index 03a568e98cd33..e913a1b1f8772 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs @@ -188,11 +188,9 @@ impl Rule for PreferSingleCall { // Determine separator. Check whether the target call ends with a // trailing comma (like `push(a,)`) to avoid generating `push(a,, b)`. - let target_src = target_call.span.source_text(src); - let before_paren = target_src[..target_src.len().saturating_sub(1)].trim_end(); let separator = if target_call.arguments.is_empty() { "" - } else if before_paren.ends_with(',') { + } else if has_trailing_comma(target_call, ctx) { " " } else { ", " @@ -222,6 +220,21 @@ impl Rule for PreferSingleCall { } } +/// Whether the last token before the closing `)` of `call` is a `,`, skipping comments, +/// e.g. `push(a, /* b */)`. +fn has_trailing_comma(call: &CallExpression, ctx: &LintContext) -> bool { + let start = call.span.start; + let mut end = call.span.end - 1; + loop { + let text = ctx.source_range(Span::new(start, end)).trim_end(); + let text_end = start + u32::try_from(text.len()).unwrap(); + match ctx.comments_range(start..text_end).last() { + Some(comment) if comment.span.end == text_end => end = comment.span.start, + _ => return text.ends_with(','), + } + } +} + /// Callee source-text patterns to ignore for `Array#{push,unshift}`. const DEFAULT_ARRAY_MUTATION_IGNORE: &[&str] = &[ "stream.push", @@ -454,6 +467,9 @@ mod tests { "foo.push(); foo.push(1);", // Trailing comma in first call. "foo.push(1,); foo.push(2);", + // Trailing comma followed by a comment. + "foo.push(1, /* comment */); foo.push(2);", + "foo.push(1, // comment\n); foo.push(2);", // Multi-line, both with semicolons. "foo.push(1);\nfoo.push(2);", // ASI: first statement lacks a semicolon. @@ -496,6 +512,8 @@ mod tests { ("foo.push(1); foo.push();", "foo.push(1);"), ("foo.push(); foo.push(1);", "foo.push(1);"), ("foo.push(1,); foo.push(2);", "foo.push(1, 2);"), + ("foo.push(1, /* comment */); foo.push(2);", "foo.push(1, /* comment */ 2);"), + ("foo.push(1, // comment\n); foo.push(2);", "foo.push(1, // comment\n 2);"), ("foo.push(1);\nfoo.push(2);", "foo.push(1, 2);"), // ASI: semicolon from second statement is preserved. ("foo.push(1)\nfoo.push(2);", "foo.push(1, 2);"), diff --git a/crates/oxc_linter/src/snapshots/unicorn_prefer_single_call.snap b/crates/oxc_linter/src/snapshots/unicorn_prefer_single_call.snap index b837274baf1fb..fea49bcbe4129 100644 --- a/crates/oxc_linter/src/snapshots/unicorn_prefer_single_call.snap +++ b/crates/oxc_linter/src/snapshots/unicorn_prefer_single_call.snap @@ -37,6 +37,21 @@ source: crates/oxc_linter/src/tester.rs ╰──── help: Merge with the previous call. + ⚠ unicorn(prefer-single-call): Do not call `Array#push()` multiple times. + ╭─[prefer_single_call.tsx:1:33] + 1 │ foo.push(1, /* comment */); foo.push(2); + · ──── + ╰──── + help: Merge with the previous call. + + ⚠ unicorn(prefer-single-call): Do not call `Array#push()` multiple times. + ╭─[prefer_single_call.tsx:2:8] + 1 │ foo.push(1, // comment + 2 │ ); foo.push(2); + · ──── + ╰──── + help: Merge with the previous call. + ⚠ unicorn(prefer-single-call): Do not call `Array#push()` multiple times. ╭─[prefer_single_call.tsx:2:5] 1 │ foo.push(1); From 8944088660a32af85183483480318616cbf9b6ac Mon Sep 17 00:00:00 2001 From: Cameron Date: Sun, 15 Nov 2026 12:45:04 +0000 Subject: [PATCH 2/5] fix(linter/unicorn/prefer-single-call): handle complete trailing trivia --- .../src/rules/unicorn/prefer_single_call.rs | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs b/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs index e913a1b1f8772..36d56db1bf730 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs @@ -7,7 +7,10 @@ use oxc_ast::{ use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; use oxc_span::{GetSpan, Span}; -use oxc_syntax::identifier::is_identifier_part; +use oxc_syntax::{ + identifier::{is_identifier_part, is_white_space}, + line_terminator::is_line_terminator, +}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -226,10 +229,12 @@ fn has_trailing_comma(call: &CallExpression, ctx: &LintContext) -> bool { let start = call.span.start; let mut end = call.span.end - 1; loop { - let text = ctx.source_range(Span::new(start, end)).trim_end(); + let text = ctx + .source_range(Span::new(start, end)) + .trim_end_matches(|c| is_white_space(c) || is_line_terminator(c)); let text_end = start + u32::try_from(text.len()).unwrap(); match ctx.comments_range(start..text_end).last() { - Some(comment) if comment.span.end == text_end => end = comment.span.start, + Some(comment) if comment.span.end >= text_end => end = comment.span.start, _ => return text.ends_with(','), } } @@ -514,6 +519,17 @@ mod tests { ("foo.push(1,); foo.push(2);", "foo.push(1, 2);"), ("foo.push(1, /* comment */); foo.push(2);", "foo.push(1, /* comment */ 2);"), ("foo.push(1, // comment\n); foo.push(2);", "foo.push(1, // comment\n 2);"), + ("foo.push(1, // comment \n); foo.push(2);", "foo.push(1, // comment \n 2);"), + ("foo.push(1, //\t\n); foo.push(2);", "foo.push(1, //\t\n 2);"), + ("foo.push(1 // comma, \n); foo.push(2);", "foo.push(1 // comma, \n, 2);"), + ( + "foo.push(1, /* comment */\u{feff}); foo.push(2);", + "foo.push(1, /* comment */\u{feff} 2);", + ), + ( + "foo.unshift(1); foo.unshift(2, // comment \n);", + "foo.unshift(2, // comment \n 1);", + ), ("foo.push(1);\nfoo.push(2);", "foo.push(1, 2);"), // ASI: semicolon from second statement is preserved. ("foo.push(1)\nfoo.push(2);", "foo.push(1, 2);"), From b577b77f315671bc53061e2be99ed152ad0a8108 Mon Sep 17 00:00:00 2001 From: Cameron Date: Sun, 15 Nov 2026 12:45:15 +0000 Subject: [PATCH 3/5] perf(linter/unicorn/prefer-single-call): scan trailing comments once --- .../src/rules/unicorn/prefer_single_call.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs b/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs index 36d56db1bf730..460570abd521d 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs @@ -228,12 +228,13 @@ impl Rule for PreferSingleCall { fn has_trailing_comma(call: &CallExpression, ctx: &LintContext) -> bool { let start = call.span.start; let mut end = call.span.end - 1; + let mut comments = ctx.comments_range(start..end).rev(); loop { let text = ctx .source_range(Span::new(start, end)) .trim_end_matches(|c| is_white_space(c) || is_line_terminator(c)); let text_end = start + u32::try_from(text.len()).unwrap(); - match ctx.comments_range(start..text_end).last() { + match comments.next() { Some(comment) if comment.span.end >= text_end => end = comment.span.start, _ => return text.ends_with(','), } @@ -530,6 +531,14 @@ mod tests { "foo.unshift(1); foo.unshift(2, // comment \n);", "foo.unshift(2, // comment \n 1);", ), + ( + "foo.push(/* argument */ 1, /* first */ // second \n /* third */); foo.push(2);", + "foo.push(/* argument */ 1, /* first */ // second \n /* third */ 2);", + ), + ( + "foo.push(/* argument */ 1 /* first */ /* second */); foo.push(2);", + "foo.push(/* argument */ 1 /* first */ /* second */, 2);", + ), ("foo.push(1);\nfoo.push(2);", "foo.push(1, 2);"), // ASI: semicolon from second statement is preserved. ("foo.push(1)\nfoo.push(2);", "foo.push(1, 2);"), From 26d79aba6aee903556be6c1988017eea97b3f58f Mon Sep 17 00:00:00 2001 From: Cameron Date: Sun, 15 Nov 2026 12:45:21 +0000 Subject: [PATCH 4/5] test(linter/unicorn/prefer-single-call): remove redundant case narration --- crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs b/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs index 460570abd521d..9243b29cb641d 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs @@ -473,7 +473,6 @@ mod tests { "foo.push(); foo.push(1);", // Trailing comma in first call. "foo.push(1,); foo.push(2);", - // Trailing comma followed by a comment. "foo.push(1, /* comment */); foo.push(2);", "foo.push(1, // comment\n); foo.push(2);", // Multi-line, both with semicolons. From 0b838e31dfe0446c6fd9e04aa2ec6384efd04f0d Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sun, 15 Nov 2026 12:49:21 +0000 Subject: [PATCH 5/5] [autofix.ci] apply automated fixes --- crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs b/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs index 9243b29cb641d..321e2bbc4796f 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_single_call.rs @@ -526,10 +526,7 @@ mod tests { "foo.push(1, /* comment */\u{feff}); foo.push(2);", "foo.push(1, /* comment */\u{feff} 2);", ), - ( - "foo.unshift(1); foo.unshift(2, // comment \n);", - "foo.unshift(2, // comment \n 1);", - ), + ("foo.unshift(1); foo.unshift(2, // comment \n);", "foo.unshift(2, // comment \n 1);"), ( "foo.push(/* argument */ 1, /* first */ // second \n /* third */); foo.push(2);", "foo.push(/* argument */ 1, /* first */ // second \n /* third */ 2);",