Skip to content

fix(sort-modules): add missing partition cases#728

Merged
azat-io merged 5 commits into
azat-io:mainfrom
hugop95:fix/sort-modules/expressions
Mar 30, 2026
Merged

fix(sort-modules): add missing partition cases#728
azat-io merged 5 commits into
azat-io:mainfrom
hugop95:fix/sort-modules/expressions

Conversation

@hugop95
Copy link
Copy Markdown
Contributor

@hugop95 hugop95 commented Mar 26, 2026

Description

sort-modules doesn't handle several AST node types that can appear at module or namespace level and ignore them. In theory, this can create issues (see the tests).

This PR adds the following list of new node types that now trigger partitioning:

  • TSImportEqualsDeclaration
  • TSExportAssignment
  • DoWhileStatement
  • LabeledStatement
  • SwitchStatement
  • WhileStatement
  • ForInStatement
  • ForOfStatement
  • ThrowStatement
  • BlockStatement
  • ForStatement
  • TryStatement
  • IfStatement

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 681a4059-fafd-4a32-b69e-e19a661ed5d2

📥 Commits

Reviewing files that changed from the base of the PR and between 7cec264 and 20a9a99.

📒 Files selected for processing (2)
  • rules/sort-modules.ts
  • test/rules/sort-modules.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/rules/sort-modules.test.ts

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved module sorting rule to properly recognize diverse statement types and establish partition boundaries, ensuring accurate sorting enforcement across complex module structures.
  • Tests

    • Added comprehensive test coverage validating module sorting behavior with various statement separators, control flow constructs, and edge case scenarios.

Walkthrough

Added exhaustive type-guarding in module AST handling and expanded which AST node types are treated as non-sortable or as sorting-group boundaries; tests extended to cover many statement forms and TypeScript-specific declarations affecting partitioning.

Changes

Cohort / File(s) Summary
Rule Logic
rules/sort-modules.ts
Added assertIsNever in the switch default for exhaustiveness; added early-continue cases for additional top-level node types (TSNamespaceExportDeclaration, ExportAllDeclaration, ImportDeclaration, DebuggerStatement, ContinueStatement, ReturnStatement, EmptyStatement, BreakStatement, WithStatement); added explicit push/continue boundaries for many statement and TS declaration forms (TSImportEqualsDeclaration, TSExportAssignment, DoWhileStatement, LabeledStatement, SwitchStatement, WhileStatement, ForInStatement, ForOfStatement, ThrowStatement, BlockStatement, ForStatement, TryStatement, IfStatement).
Tests
test/rules/sort-modules.test.ts
Added alphabetical suite tests asserting rule creates partition boundaries before module declarations when separated by numerous statement forms and TS declarations (e.g., if, while, for...in, try/catch, for, for...of, do...while, switch, throw, block, labeled statements, import =, export =); converted several previously valid scenarios (empty statement, debugger, import, export * from, export as namespace) to invalid with unexpectedModulesOrder expectations.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • azat-io
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: adding missing partition cases to the sort-modules rule, which is directly reflected in the code changes and test additions.
Description check ✅ Passed The description explains the problem (unhandled AST node types at module level) and documents the solution (adding 13 new node types for partitioning), but lacks a Reviewer notes section and does not reference an issue number.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 and usage tips.

Comment thread rules/sort-modules.ts
break
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
default:
assertIsNever(node)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewers

I'm not doing throw new UnreachableCaseError to preserve current behavior in case custom parsers return other types.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (0b8fbcd) to head (20a9a99).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #728   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          223       223           
  Lines         4364      4385   +21     
  Branches      1353      1374   +21     
=========================================
+ Hits          4364      4385   +21     

☔ View full report in Codecov by Sentry.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hugop95 hugop95 marked this pull request as ready for review March 26, 2026 11:21
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@test/rules/sort-modules.test.ts`:
- Around line 409-418: Update the tests so they actually assert the partition
behavior: in the "creates partitions at TypeScript import-equals declarations"
case (and the related cases between lines 431-483) replace the neutral ordering
with reversed names so sorting must move declarations across the import-equals
boundary (e.g., use a B … A ordering where A should sort before B), and convert
the "sorts across …" tests to use invalid(...) assertions that verify the
rule-fixer reorders the sortable declarations but leaves the import-equals (the
skipped node) in place; adjust the test inputs and expectations for the
valid/invalid helpers (functions valid and invalid) accordingly so they fail
when TSImportEqualsDeclaration is not treated as a partition.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 58724648-7a0e-43da-9db9-4e560c93c6d6

📥 Commits

Reviewing files that changed from the base of the PR and between 4949b1d and d82e539.

📒 Files selected for processing (2)
  • rules/sort-modules.ts
  • test/rules/sort-modules.test.ts

Comment thread test/rules/sort-modules.test.ts
@hugop95 hugop95 force-pushed the fix/sort-modules/expressions branch from d82e539 to 7cec264 Compare March 26, 2026 11:33
Comment thread rules/sort-modules.ts Outdated
case AST_NODE_TYPES.ImportDeclaration:
case AST_NODE_TYPES.DebuggerStatement:
case AST_NODE_TYPES.ContinueStatement:
/* v8 ignore next 4 -- @preserve These statements cannot appear at module/namespace level in valid code. */
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this v8 ignore still correct? EmptyStatement is valid at top level (;), so the comment seems too broad.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azat-io

It was from an early implementation, we are testing everything now, it's useless 👍

20a9a99 (this PR)

`,
options: [options],
})
})
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these sorts across ... cases be turned into invalid(...) tests with B ... skipped node ... A?

Right now they use already sorted input, so they still pass even if those skipped nodes accidentally become partition boundaries. Coverage is green, but I don't think those tests actually pin the behavior down yet.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azat-io

Good point, there are indeed useless as of now!

4379a9c (this PR) 🤦

Copy link
Copy Markdown
Owner

@azat-io azat-io left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@azat-io azat-io merged commit 7244ff1 into azat-io:main Mar 30, 2026
13 checks passed
@hugop95 hugop95 deleted the fix/sort-modules/expressions branch March 30, 2026 13:19
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.

2 participants