oxc-project/oxc · warning · OxcDiagnostic
Unexpected comma in middle of array
Error message
Unexpected comma in middle of array
What it means
`no-sparse-arrays` reports arrays containing elisions (holes), e.g. `[,,1]`. This variant fires when fewer than 10 holes exist: each elision gets its own zero-width label `"unexpected comma"` at the hole's start offset, and one diagnostic carries all the labels. Holes are `undefined` slots that most array methods (map, forEach, filter) skip, which makes behavior surprising.
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_sparse_arrays.rs:78
impl Rule for NoSparseArrays {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::ArrayExpression(array_expr) = node.kind() {
let violations = array_expr
.elements
.iter()
.filter_map(|el| match el {
ArrayExpressionElement::Elision(elision) => Some(elision),
_ => None,
})
.map(|elision| {
LabeledSpan::at(elision.span.start..elision.span.start, "unexpected comma")
})
.collect::<Vec<_>>();
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()
))View on GitHub (pinned to e1e7af627c)
Solutions
- Remove the extra comma so the array has no holes: `[1, 2, 3]`.
- If holes are intentional, use `Array(n)` or `Array.from({length: n})` to express intent explicitly.
- If the hole means 'no value', write `undefined` explicitly: `[1, undefined, 3]`.
Example fix
// before const scores = [90, , 75]; // after const scores = [90, undefined, 75];
Defensive patterns
Strategy: validation
Validate before calling
// runtime hole check for arrays crossing API boundaries
function hasHoles(arr) {
for (let i = 0; i < arr.length; i++) if (!(i in arr)) return true;
return false;
} Prevention
- Write explicit `undefined` (or Array(n)) instead of holes in literals.
- Remember map/forEach/filter skip holes — dense arrays behave predictably.
- Lint data exports and generated literals where stray commas creep in.
When it happens
Trigger: Any `ArrayExpressionElement::Elision` in an array literal: `[,,]`, `[1,,3]`, trailing `[,]`. Emitted from the `run` visitor when `violations` is non-empty and `violations.len() < 10`.
Common situations: Typo like a double comma when editing large literal arrays; code relying on `Array(n)`-style hole semantics via literals; JSON-ish config data pasted into JS where a value was deleted leaving `[a,,c]`.
Related errors
- {} unexpected commas in middle of array
- Avoid calls to the `Array` constructor
- Use Array destructuring.
- Empty array binding pattern
- Empty object binding pattern
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/3d6b607d63328d34.
Report an issue: GitHub.