oxc-project/oxc · warning · OxcDiagnostic

{} unexpected commas in middle of array

Error message

{} unexpected commas in middle of array

What it means

Same `no-sparse-arrays` rule, but for arrays with 10 or more holes the per-hole labels are replaced by one aggregated message `"{n} unexpected commas in middle of array"`. If the array's source span is under 50 chars the whole array is labeled `"the array here"`; otherwise a zero-width label `"the array starting here"` points at the array's start. This keeps the diagnostic readable instead of spraying dozens of labels.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_sparse_arrays.rs:93

            if !violations.is_empty() {
                if violations.len() < 10 {
                    ctx.diagnostic(
                        OxcDiagnostic::warn("Unexpected comma in middle of array")
                            .with_help("remove the comma or insert `undefined`")
                            .with_labels(violations),
                    );
                } else {
                    let span = if (array_expr.span.end - array_expr.span.start) < 50 {
                        LabeledSpan::at(array_expr.span, "the array here")
                    } else {
                        LabeledSpan::at(
                            array_expr.span.start..array_expr.span.start,
                            "the array starting here",
                        )
                    };

                    ctx.diagnostic(
                        OxcDiagnostic::warn(format!(
                            "{} unexpected commas in middle of array",
                            violations.len()
                        ))
                        .with_help("remove the comma or insert `undefined`")
                        .with_label(span),
                    );
                }
            }
        }
    }
}

#[test]
fn test() {
    use crate::tester::Tester;

    let pass = vec!["var a = [ 1, 2, ]", "var a = [];"];

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace the hole-filled literal with `Array(n)` or `Array.from({ length: n }, ...)`.
  2. If the array is data, re-export it without empty cells rather than hand-editing commas.
  3. After fixing, re-run oxlint on the file to confirm the rule is quiet.

Example fix

// before
const slots = [,,,,,,,,,,];

// after
const slots = Array(10);
Defensive patterns

Strategy: validation

Validate before calling

# catch big hole-runs in literals before commit
rg -n '(\s*,){9,}' src/

Prevention

When it happens

Trigger: An array literal with `violations.len() >= 10` elisions, e.g. `const a = [,,,,,,,,,,];` — commonly the result of fat-fingered commas or generated data.

Common situations: Generated or data-table literals where many entries were removed; accidental long runs of commas from merge conflicts or spreadsheet exports; test fixtures for Array(n)-like behavior written as literals.

Related errors


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