Skip to content

Add --pretty-baseline flag for reviewable baseline diffs#6615

Open
swizzlr wants to merge 1 commit into
realm:mainfrom
swizzlr:pretty-baseline
Open

Add --pretty-baseline flag for reviewable baseline diffs#6615
swizzlr wants to merge 1 commit into
realm:mainfrom
swizzlr:pretty-baseline

Conversation

@swizzlr
Copy link
Copy Markdown

@swizzlr swizzlr commented Apr 21, 2026

Summary

Adds an opt-in --pretty-baseline flag (plus matching
pretty_baseline configuration key) that writes baseline files as
pretty-printed JSON with sorted keys and unescaped forward slashes.
Violations have always been sorted by file and location, so the
complete opt-in output is deterministic across runs and produces
stable, human-reviewable diffs whenever a baseline is regenerated.

Default behaviour is unchanged — existing baselines remain
byte-identical, so this change is fully backwards compatible.

Motivation: the default baseline is emitted as a single minified line
with escaped slashes and (in compact mode) non-deterministic key
order. That makes it nearly impossible to review updates in a PR, and
trivial changes can reorder unrelated entries. With --pretty-baseline,
regenerating a baseline after fixing a few violations produces a
minimal, readable diff.

Details

  • New --pretty-baseline flag on swiftlint lint / swiftlint analyze
    via LintOrAnalyzeArguments, threaded through LintOrAnalyzeOptions
    and LintOrAnalyzeCommand to Baseline.write(toPath:pretty:).
  • New pretty_baseline YAML key wired through Configuration,
    Configuration+Parsing, and Configuration+Merging.
  • Baseline.write(toPath:pretty:) defaults to pretty: false, so the
    existing default output is preserved exactly.
  • --pretty-baseline without --write-baseline is rejected at
    argument-parse time via a validate() method on
    LintOrAnalyzeArguments.
  • Snapshot tests on BaselineTests assert both the default (compact,
    one-line) and the opt-in (pretty, multiline, sorted) output using the
    existing withExampleFileCreated helper.
  • README and CHANGELOG updated.

Test plan

  • swift test --filter BaselineTests — all 8 cases pass, including
    a new snapshot test asserting the exact pretty-printed output and
    a companion test asserting the default remains compact.
  • swift test --filter LintOrAnalyzeOptionsTests.
  • Manual: recorded a baseline with --write-baseline alone
    (single-line output preserved) and with
    --write-baseline X.json --pretty-baseline (pretty, sorted,
    unescaped slashes).
  • Manual: swiftlint --pretty-baseline without --write-baseline
    prints a validation error.

@SwiftLintBot
Copy link
Copy Markdown

SwiftLintBot commented Apr 21, 2026

19 Messages
📖 Building this branch resulted in a binary size of 27623.05 KiB vs 27621.7 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.81 s vs 0.83 s on main (2% faster).
📖 Linting Alamofire with this PR took 1.06 s vs 1.08 s on main (1% faster).
📖 Linting Brave with this PR took 7.39 s vs 7.34 s on main (0% slower).
📖 Linting DuckDuckGo with this PR took 28.5 s vs 28.59 s on main (0% faster).
📖 Linting Firefox with this PR took 12.3 s vs 12.37 s on main (0% faster).
📖 Linting Kickstarter with this PR took 8.53 s vs 8.57 s on main (0% faster).
📖 Linting Moya with this PR took 0.43 s vs 0.43 s on main (0% slower).
📖 Linting NetNewsWire with this PR took 2.86 s vs 2.85 s on main (0% slower).
📖 Linting Nimble with this PR took 0.69 s vs 0.7 s on main (1% faster).
📖 Linting PocketCasts with this PR took 8.23 s vs 7.91 s on main (4% slower).
📖 Linting Quick with this PR took 0.47 s vs 0.47 s on main (0% slower).
📖 Linting Realm with this PR took 3.03 s vs 3.1 s on main (2% faster).
📖 Linting Sourcery with this PR took 1.89 s vs 1.92 s on main (1% faster).
📖 Linting Swift with this PR took 4.77 s vs 4.76 s on main (0% slower).
📖 Linting SwiftLintPerformanceTests with this PR took 0.35 s vs 0.36 s on main (2% faster).
📖 Linting VLC with this PR took 1.26 s vs 1.23 s on main (2% slower).
📖 Linting Wire with this PR took 19.16 s vs 19.16 s on main (0% slower).
📖 Linting WordPress with this PR took 12.92 s vs 12.95 s on main (0% faster).

Generated by 🚫 Danger

@swizzlr swizzlr changed the title Pretty-print baseline JSON output for reviewable diffs Add --pretty-baseline flag for reviewable baseline diffs Apr 21, 2026
Introduces a `--pretty-baseline` flag (plus `pretty_baseline`
configuration key) that writes baseline files as pretty-printed JSON
with sorted keys and unescaped slashes. Violations have always been
sorted by file and location, so the complete opt-in output is
deterministic across runs and produces stable, human-reviewable diffs
whenever the baseline is regenerated.

The default output is unchanged, so existing baselines remain
byte-identical. `--pretty-baseline` without `--write-baseline` is
rejected at argument-parse time.
@swizzlr swizzlr marked this pull request as ready for review April 21, 2026 20:30
@mildm8nnered
Copy link
Copy Markdown
Collaborator

This seems quite nice - the original format was optimized for size over readability - having the option to choose is good.

Is it possible to add a pretty subcommand to the baseline command as well, which could do this conversion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants