Repository navigation
feat(prettier-plugin-pkg): Sort pre/post scripts around the base script - #500
Conversation
🦋 Changeset detectedLatest commit: 4a6da73 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
📝 WalkthroughWalkthroughAdds npm lifecycle-aware script sorting: new utilities classify and order Changes
Sequence Diagram(s)(omitted — changes are internal sorting utilities and rule integration without multi-component sequential interactions) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
There was a problem hiding this comment.
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 `@packages/pkg/src/utils.ts`:
- Around line 52-53: The code uses ES2023 array methods ranks.toReversed() and
scriptNames.toSorted(...) which break Node 14/16 compatibility; replace those
calls with compatibility-safe equivalents by cloning then using legacy methods:
change ranks.toReversed() to an immutable reverse via spreading and .reverse()
(e.g., create [...ranks].reverse()) and change scriptNames.toSorted(comparator)
to a stable sort via [...scriptNames].sort(comparator), ensuring you preserve
the original arrays and comparator logic in the functions that reference ranks
and scriptNames.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: eb97d786-ac7e-4098-bf45-3ee86e849ae7
⛔ Files ignored due to path filters (3)
packages/pkg/test/__snapshots__/sort-order-preset.spec.ts.snapis excluded by!**/*.snappackages/pkg/test/__snapshots__/sort-order.spec.ts.snapis excluded by!**/*.snappackages/pkg/test/__snapshots__/test.spec.ts.snapis excluded by!**/*.snap
📒 Files selected for processing (4)
packages/pkg/src/rules/object.tspackages/pkg/src/utils.tspackages/pkg/test/fixtures/fixture2.jsonpackages/pkg/test/utils.spec.ts
|
Note: I tried fixing the SonarQube issues by configuring it not to analyze test files, but that didn’t work. |
prettier-plugin-autocorrect
prettier-plugin-pkg
prettier-plugin-sh
prettier-plugin-sql
prettier-plugin-toml
commit: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #500 +/- ##
==========================================
+ Coverage 86.69% 88.13% +1.43%
==========================================
Files 10 10
Lines 203 236 +33
Branches 57 68 +11
==========================================
+ Hits 176 208 +32
- Misses 26 27 +1
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR updates prettier-plugin-pkg’s package.json sorting behavior so that npm hook scripts (pre* / post*) are ordered around their base script (including nested hook prefixes), rather than purely alphabetically.
Changes:
- Added a script-aware sorting algorithm that groups
pre*/post*scripts around their resolved base script name (with support for nested prefixes). - Applied this specialized ordering when sorting the
scriptsobject. - Added unit tests and updated fixtures/snapshots to validate the new ordering.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/pkg/src/utils.ts | Implements sortScriptNames + sortScripts to group pre/post hooks around base scripts (including nested prefixes). |
| packages/pkg/src/rules/object.ts | Uses sortScripts when processing the scripts key instead of generic object key sorting. |
| packages/pkg/test/utils.spec.ts | Adds focused unit tests for the new script sorting behavior. |
| packages/pkg/test/fixtures/fixture2.json | Updates fixture scripts to include prebuild for integration coverage. |
| packages/pkg/test/snapshots/test.spec.ts.snap | Updates snapshot to reflect new scripts ordering. |
| packages/pkg/test/snapshots/sort-order.spec.ts.snap | Updates snapshot to reflect new scripts ordering under custom sort order. |
| packages/pkg/test/snapshots/sort-order-preset.spec.ts.snap | Updates snapshot to reflect new scripts ordering under presets. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
There was a problem hiding this comment.
Pull request overview
Adds npm-aware sorting for scripts so that matching pre* hooks appear immediately before their base script and matching post* hooks immediately after, improving readability and aligning with npm semantics.
Changes:
- Introduced
sortScriptNames/sortScriptsutilities to grouppre/base/postscripts together per base name. - Updated object-sorting rule to apply the specialized script sorter for the
scriptsfield. - Added unit tests and updated fixtures/snapshots to cover the new ordering behavior.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/pkg/src/utils.ts | Implements pre/base/post-aware script name sorting and applies it to script object properties. |
| packages/pkg/src/rules/object.ts | Switches scripts object sorting to the new script-aware sorter. |
| packages/pkg/test/utils.spec.ts | Adds focused unit coverage for script name sorting behavior (matched and unmatched hooks). |
| packages/pkg/test/fixtures/fixture2.json | Extends fixture scripts to exercise prebuild ordering. |
| packages/pkg/test/snapshots/test.spec.ts.snap | Updates expected formatted output to include the new script ordering. |
| packages/pkg/test/snapshots/sort-order.spec.ts.snap | Updates snapshot to reflect prebuild placement. |
| packages/pkg/test/snapshots/sort-order-preset.spec.ts.snap | Updates preset snapshots to reflect prebuild placement. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
|
@JounQin — Let me know if this needs any other tweaks. |
Update the version of prettier-plugin-pkg to minor and clarify sorting behavior for scripts.
|
|
@coderabbitai resume |
✅ Actions performedReviews resumed. |
There was a problem hiding this comment.
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 @.changeset/cold-moles-vanish.md:
- Line 7: The fenced code block that contains the lines "pare postpare
preprepare prepare postprepare" is missing a language tag; update the opening
fence (the triple backticks for that fenced block) to include a language
identifier (e.g., ```text) so the block becomes ```text ... ``` to satisfy the
markdown/remark linter.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 679fcc51-5466-4bf3-882d-d114b02fad81
📒 Files selected for processing (3)
.changeset/cold-moles-vanish.mdpackages/pkg/src/utils.tspackages/pkg/test/utils.spec.ts
🚧 Files skipped from review as they are similar to previous changes (2)
- packages/pkg/src/utils.ts
- packages/pkg/test/utils.spec.ts



Resolves #403.
Before:
{ "scripts": { "build": "tsc -p tsconfig.build.json", "posttest": "rm -rf coverage", "prebuild": "rm -rf dist", "test": "vitest run --coverage" } }After:
{ "scripts": { "prebuild": "rm -rf dist", "build": "tsc -p tsconfig.build.json", "test": "vitest run --coverage", "posttest": "rm -rf coverage" } }Summary by CodeRabbit