Skip to content

fix(jsonfmt): non-empty arrays should always expand to one element per line #630

Description

@kehoecj

Problem

cfv collapses short JSON arrays to a single line when they fit within printWidth:

"brackets": [["{", "}"], ["[", "]"], ["(", ")"]],

The correct behavior is to always expand non-empty arrays (one element per line):

"brackets": [
  ["{", "}"],
  ["[", "]"],
  ["(", ")"]
],

Empty arrays [] and empty objects {} stay on one line. Everything else expands.

Reproduction

Create a file test.json:

{"brackets": [["{", "}"], ["[", "]"], ["(", ")"]],"other": [1, 2, 3]}

Run:

cfv format --fix --no-config test.json
cat test.json

Actual: inner arrays collapse to [["{", "}"], ...] on one line.
Expected: each inner array on its own line.

Root cause

pkg/formatter/jsoncfmt/jsonc.goisInlineArray returns true when prefixLen + valLen <= maxLineWidth. This logic should be removed for JSON arrays. Non-empty arrays always expand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is neededv3.0Work specifically targeted version 3

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions