Skip to content

Zsort corner cases - #179

Merged
mstdokumaci merged 5 commits into
mainfrom
zsort-corner-cases
Aug 3, 2026
Merged

mstdokumaci merged 5 commits into
mainfrom
zsort-corner-cases

Conversation

@mstdokumaci

@mstdokumaci mstdokumaci commented Aug 3, 2026 •

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • Bug Fixes
    • Improved source processing for module aliases resembling struct, enum, union, or opaque declarations.
    • Preserved import blocks correctly when they contain such aliases.
    • Improved handling of division-dereference syntax and leading module documentation comments during import sorting.
    • Prevented longer identifiers containing declaration keywords from being misclassified.

@coderabbitai

coderabbitai Bot commented Aug 3, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 80de6777-4748-4e59-a423-6d9c6deeda43

📥 Commits

Reviewing files that changed from the base of the PR and between d48286d and 3fa5e17.

📒 Files selected for processing (2)
  • tools/zsort.zig
  • tools/zsort_test.zig
🚧 Files skipped from review as they are similar to previous changes (2)
  • tools/zsort_test.zig
  • tools/zsort.zig

📝 Walkthrough

Walkthrough

The import sorter now strips only line comments during semicolon detection and uses boundary-aware type keyword matching. Regression tests cover aliases, import preservation, expressions, and file-leading module documentation.

Changes

Import sorter scanning

Layer / File(s) Summary
Scanner and import classification behavior
tools/zsort.zig
skipStringOrComment no longer skips block comments. endsWithSemicolon strips trailing whitespace after line comments. hasTypeKeyword prevents declaration matches inside longer identifiers or trailing text.
Scanner and import classification regression coverage
tools/zsort_test.zig
Tests cover struct-like aliases, preserved imports, trailing type-keyword comments, division-dereference syntax, and file-leading module documentation.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 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 identifies corner-case fixes and tests for the zsort tool.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tools/zsort.zig (1)

143-161: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Do not match type keywords in trailing comments.

hasTypeKeyword scans the full line. A valid declaration such as const dep = @import("dep"); // = struct matches = struct and terminates the import block.

processSource then marks this import and later imports as stray. A correctly sorted file can fail zsort check because changed is true when stray imports exist.

Match the first token after the declaration assignment instead of searching the complete line. Add a regression case with a trailing // = struct comment.

Proposed fix
-fn hasTypeKeyword(trimmed: []const u8, kw: []const u8) bool {
-    const i = std.mem.indexOf(u8, trimmed, kw) orelse return false;
-    const after = i + kw.len;
-    return after >= trimmed.len or
-        (!std.ascii.isAlphanumeric(trimmed[after]) and trimmed[after] != '_');
+fn hasTypeKeyword(trimmed: []const u8, keyword: []const u8) bool {
+    const eq = std.mem.indexOfScalar(u8, trimmed, '=') orelse return false;
+    const rhs = std.mem.trimLeft(u8, trimmed[eq + 1 ..], " \t");
+    if (!std.mem.startsWith(u8, rhs, keyword)) return false;
+    const after = keyword.len;
+    return after >= rhs.len or
+        (!std.ascii.isAlphanumeric(rhs[after]) and rhs[after] != '_');
 }
 
-if (hasTypeKeyword(trimmed, "= struct")) return false;
+if (hasTypeKeyword(trimmed, "struct")) return false;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tools/zsort.zig` around lines 143 - 161, Update hasTypeKeyword and its
callers in isTopLevelImportLine to inspect only the first token following the
declaration assignment, excluding trailing comments from type detection.
Preserve recognition of actual struct, enum, union, and opaque declarations, and
add a regression case covering an import with a trailing “// = struct” comment
so zsort check remains unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@tools/zsort.zig`:
- Around line 143-161: Update hasTypeKeyword and its callers in
isTopLevelImportLine to inspect only the first token following the declaration
assignment, excluding trailing comments from type detection. Preserve
recognition of actual struct, enum, union, and opaque declarations, and add a
regression case covering an import with a trailing “// = struct” comment so
zsort check remains unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f3762517-0858-4ee4-8ae8-85077731fca4

📥 Commits

Reviewing files that changed from the base of the PR and between 003644e and d48286d.

📒 Files selected for processing (2)
  • tools/zsort.zig
  • tools/zsort_test.zig

@mstdokumaci
mstdokumaci merged commit b085221 into main Aug 3, 2026
8 checks passed
@mstdokumaci
mstdokumaci deleted the zsort-corner-cases branch August 3, 2026 05:11
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.

1 participant