Skip to content

feat(max-statements-per-line): make the rule fixable - #1281

Merged
9romise merged 1 commit into
eslint-stylistic:mainfrom
anandghegde:feat/max-statements-per-line-fixable
Oct 28, 2026
Merged

9romise merged 1 commit into
eslint-stylistic:mainfrom
anandghegde:feat/max-statements-per-line-fixable

Conversation

@anandghegde

@anandghegde anandghegde commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds fixable: 'whitespace' to max-statements-per-line. The fix puts a line break before the reported statement (the first one over max on the line). The new line gets the same leading whitespace as the line being split. ESLint's fix passes then take care of any further statements on that line.

foo(); bar(); baz();     // -> foo();\nbar();\nbaz();
if (a) foo(); bar();     // -> if (a) foo();\nbar();
foo(); /* c */ bar();    // -> foo();\n/* c */ bar();   (comment stays with the next statement)
if (a) {} bar()          // -> if (a) {}\nbar()

It's deliberately conservative. It only fixes when:

  • the token before the statement is ; or }, so the break always falls between two sibling statements. That leaves out else foo();, case x: foo(); and the first statement after {.
  • the statement is at the top level, or its enclosing block/case starts on an earlier line. Single-line blocks like if (a) { b(); c(); }, () => { a(); b(); } or case 1: foo(); break; are still reported but not fixed, so we don't end up with half-split braces (that's really brace-style's territory).

In all other cases the rule reports the same way it does today. The fixer works from the statement the rule already reports, so ignoredNodes counts the same way for both.

Since ; or } already separates the two statements, adding a newline before the second one can't change how the code parses. That holds for semicolon-less code too.

Tests: I added output to the existing invalid cases that now get fixed, plus output: null on a few that must stay unfixed. I also added new cases for comments, semicolon-less code, do…while, export, labelled statements, multi-line vs single-line case, arrow bodies and ignoredNodes. rules.md and metadata.ts are regenerated with pnpm run update.

Verified locally: pnpm test (133 files / 18454 tests pass), pnpm run test:fixtures, pnpm run lint and pnpm run typecheck all pass. 16 of the rule's tests fail with the rule change reverted.

Linked Issues

resolves #1250

Additional context

Because all only includes fixable rules, this also adds max-statements-per-line (default max: 1) to that config.

Summary by CodeRabbit

  • New Features

    • The max-statements-per-line lint rule can now automatically fix supported formatting violations by moving statements onto separate lines.
    • The rule documentation and metadata now identify it as fixable.
  • Bug Fixes

    • Improved handling of comments, blocks, loops, conditionals, switch cases, exports, labels, arrow functions, and ignored nodes during autofix.

@coderabbitai

coderabbitai Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 7292f3c2-45f1-4967-994f-113654e86c37

📥 Commits

Reviewing files that changed from the base of the PR and between 7736997 and 964889f.

📒 Files selected for processing (4)
  • packages/eslint-plugin/rules.md
  • packages/eslint-plugin/rules/max-statements-per-line/max-statements-per-line.test.ts
  • packages/eslint-plugin/rules/max-statements-per-line/max-statements-per-line.ts
  • packages/metadata/src/metadata.ts

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

The max-statements-per-line rule now offers conditional whitespace autofixes, with tests for supported and unsupported syntax. Its rule metadata, generated registry metadata, and documentation now identify it as fixable.

Changes

max-statements-per-line autofix

Layer / File(s) Summary
Rule fixer and metadata
packages/eslint-plugin/rules/max-statements-per-line/max-statements-per-line.ts, packages/metadata/src/metadata.ts
The rule adds fixable: 'whitespace'. It splits eligible extra statements after semicolons or closing braces and preserves the line indentation.
Autofix behavior tests
packages/eslint-plugin/rules/max-statements-per-line/max-statements-per-line.test.ts
Tests cover fixed output and output: null for unsupported cases, including comments, control flow, blocks, exports, labels, arrow functions, and ignoredNodes.
Rules documentation
packages/eslint-plugin/rules.md
The rules table marks max-statements-per-line as fixable.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Severity of issue fixed: Medium

Suggested reviewers: 9romise

Merge Risk: ⚪ Minimal · up to 96488

The guarded whitespace fixer preserves the tested statement and comment layout behavior; no actionable merge risk remains.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: making the max-statements-per-line rule fixable.
Linked Issues check ✅ Passed For #1250, the rule now declares fixable: 'whitespace' and reports a fixer for safe statement boundaries. The fixer inserts a line break, copies the split line's leading whitespace, and preserves in…
Out of Scope Changes check ✅ Passed The changes remain within #1250. The rule implementation, focused automated tests, rules documentation, and generated metadata support the requested fixer. No unrelated source or behavior changes are …
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 3 files. (1 skipped: 1 …
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Sep 15, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@stylistic/eslint-plugin@1281
npm i https://pkg.pr.new/@eslint-stylistic/metadata@1281

commit: 964889f

@codecov

codecov Bot commented Sep 15, 2026

Copy link
Copy Markdown

Bundle Report

Changes will increase total bundle size by 643 bytes (0.11%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
@stylistic/eslint-plugin-esm 598.71kB 643 bytes (0.11%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: @stylistic/eslint-plugin-esm

Assets Changed:

Asset Name Size Change Total Size Change (%)
rules/max-statements-per-line.js 643 bytes 3.88kB 19.85% ⚠️

Files in rules/max-statements-per-line.js:

  • ./rules/max-statements-per-line/max-statements-per-line.ts → Total Size: 3.76kB

@codecov

codecov Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.80%. Comparing base (7736997) to head (964889f).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1281   +/-   ##
=======================================
  Coverage   98.80%   98.80%           
=======================================
  Files         102      102           
  Lines        8300     8310   +10     
  Branches     3075     3076    +1     
=======================================
+ Hits         8201     8211   +10     
  Misses         95       95           
  Partials        4        4           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@anandghegde

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @antfu. One thing left over: test-eslint9 is the only red check, and it looks like a hang rather than a real failure.

In run 34819716264 the job printed the vitest banner and then nothing at all for 12 minutes:

08:28:08  > vitest -c vitest.config.unit.ts
08:28:09  RUN  v4.1.11  /home/runner/work/eslint-stylistic/eslint-stylistic/packages
08:40:25  ##[error]The operation was canceled.

No test output, no failure — it just never got going. The three sibling test matrices in the same run finished green in ~2m30s, and main is green.

Could someone re-run that job? I'd rather not push a no-op commit and dismiss your approval to retrigger it.

@anandghegde

Copy link
Copy Markdown
Contributor Author

Follow-up on the test-eslint9 hang: I reproduced the job's environment locally against the current head (964889f6) — pnpm install eslint@9 -D (resolves to eslint 9.39.5), prebuilt dist, CI=true — and ran both suites the job runs:

  • vitest -c vitest.config.unit.ts — 133 files / 19,896 tests, green in ~90s
  • vitest -c vitest.config.fixtures.ts — green

test-eslint9 was also green on main and on other PRs immediately before and after this run, and the job again printed nothing between the vitest banner and the cancellation, so I couldn't find anything reproducible on my side — it still looks like a stuck runner rather than a test.

Could someone with access re-run the job once more? If a re-trigger via push is preferred instead, I'm happy to rebase — just say the word and I won't worry about the approval.

@9romise
9romise added this pull request to the merge queue Oct 28, 2026
Merged via the queue into eslint-stylistic:main with commit 4f61285 Oct 28, 2026
16 of 17 checks passed
This was referenced Nov 14, 2026
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.

max-statements-per-line: make the rule fixable

3 participants