Skip to content

Conversation

@NullVoxPopuli
Copy link
Contributor

Alternate style of implementation from: #376

@NullVoxPopuli NullVoxPopuli changed the base branch from main to v6 July 28, 2025 18:51
import { preprocessor } from './preprocessor.js';

const sortImports = (code: string, options: PrettierOptions) => {
const importsExports = parseImportsExports(code, {
Copy link
Contributor Author

@NullVoxPopuli NullVoxPopuli Jul 28, 2025

Choose a reason for hiding this comment

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

@RobbieTheWagner since preprocess takes a string and returns a string and since ember-template-tag doesn't have a preprocess method, we can handle import sorting wholly separate from the ember-template-tag plugin

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! Seems good to me

NullVoxPopuli and others added 3 commits July 28, 2025 17:28
@NullVoxPopuli NullVoxPopuli marked this pull request as ready for review July 28, 2025 21:30
Copy link
Contributor Author

Choose a reason for hiding this comment

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

needs more tests (for like, type imports and * as imports).

@RobbieTheWagner
Copy link
Contributor

@byara @vladislavarsenev this PR is the one we'll want to have reviewed please. @NullVoxPopuli found an alternative method to simplify things, so I think this works better.

@NullVoxPopuli
Copy link
Contributor Author

image 🤔

@RobbieTheWagner
Copy link
Contributor

Okay, @vladislavarsenev @byara this should be ready now 😃

@vladislavarsenev vladislavarsenev self-requested a review July 29, 2025 16:56
Copy link
Collaborator

@vladislavarsenev vladislavarsenev left a comment

Choose a reason for hiding this comment

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

Looks good to me! Just left a couple minor comments

try {
let result = await output;
expect(
raw(source + '~'.repeat(80) + '\n' + (await output)),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Was it working incorrectly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it was working fine -- but during debugging, I wanted to see the value of the awaited value -- which isn't so easy without having to step in to raw

Copy link
Collaborator

@byara byara left a comment

Choose a reason for hiding this comment

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

I took a look and left some comments. In general, looks good to me 👍
It would be nice if you could address those comments.

"javascript-natural-sort": "^0.7.1",
"lodash-es": "^4.17.21"
"lodash-es": "^4.17.21",
"parse-imports-exports": "^0.2.4"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we absolutely need this?
If so, can we use this to avoid other plugins/packages?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we absolutely need this?

afaict, there is no other library that can parse imports without the need to understand custom syntaxes.

If so, can we use this to avoid other plugins/packages?

yea, any file format that is JS-based (jsx, tsx, gjs, gts) can use the same technique implemented here in this PR.
It's probably possible to to do for all syntaxes, just by keeping track of the where the line of the first import is.

but, I'm not sure if prettier allows for multiple parsers, so I don't think you can escape the preprocessor forwarding that happens in most of the other plugins.

gjs/gts gets away with it (being wholly contained) because the prettier plugin for those formats doesn't implement the preprocess method.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the clarification 👍

Copy link
Collaborator

@byara byara left a comment

Choose a reason for hiding this comment

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

LGTM 👍

"javascript-natural-sort": "^0.7.1",
"lodash-es": "^4.17.21"
"lodash-es": "^4.17.21",
"parse-imports-exports": "^0.2.4"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the clarification 👍

@byara byara merged commit bb68ecd into trivago:v6 Jul 30, 2025
3 checks passed
@RobbieTheWagner
Copy link
Contributor

@byara @vladislavarsenev since this is all on the v6 branch, is there a way to get a v6 alpha or beta released or what is the timeline on actually releasing v6?

@byara
Copy link
Collaborator

byara commented Jul 30, 2025

@byara @vladislavarsenev since this is all on the v6 branch, is there a way to get a v6 alpha or beta released or what is the timeline on actually releasing v6?

I think we can have pre-release/release latest this Friday.

@RobbieTheWagner
Copy link
Contributor

@byara @vladislavarsenev since this is all on the v6 branch, is there a way to get a v6 alpha or beta released or what is the timeline on actually releasing v6?

I think we can have pre-release/release latest this Friday.

Awesome, thanks @byara! Let me know when it's out and we can test it out.

@byara
Copy link
Collaborator

byara commented Aug 1, 2025

Thanks to @ayusharma now you should be able to test the new version: @trivago/[email protected]
https://www.npmjs.com/package/@trivago/prettier-plugin-sort-imports/v/6.0.0-0

FYI @RobbieTheWagner @NullVoxPopuli

byara added a commit that referenced this pull request Oct 28, 2025
* Add sort by length option

* fix types

* Correct default type

Co-authored-by: annes <[email protected]>

* Yet another <SEPARATOR> feature PR (#339)

* Added current-state snapshots

Capturing the current state of transformations so we can see how the
upcoming changes affect them.

* Add `<SEPARATOR>` support

When `importOrderSeparation` is enabled, users can further control
separation placement with the `<SEPARATOR>` keyword.

* fix tests, update docs

* feat: Import sort order skip files

* Downgraded minimatch for node 18

* Added new types

* (fix) Types error

* Switch to ESM

* Ensure all tests are running

* Correctly resolve plugin

* Remove some changes

* Fix svelte

* Require node >= 20.x (#367)

* Require node >= 20.x

* Add checking 24.x

* docs: add pnpm install command to README (#361)

* Switch to vitest for better ESM support

I was still hitting issues with jest and trying to get full ESM support, so I decided to switch to vitest. This seems to be working and snapshots are generated essentially the same.

* Explicitly import expect and test

* Fix types

* Fix accidentally checked file

* Add extensions to imports, fix some example issues

* Make tsc happy

* Convert all cjs to mjs

* Fix some things

* Get examples and tests working

* (fix) PR review comments analysed and reviewed

* State now matches upstream/main

* Revert "State now matches upstream/main"

This reverts commit a7db2b5.

* Fixed version mis matches

* [Format] Run prettier on most files

Summary:
```
$ npx prettier --write .
```

Manually inspected changes, and reverted changes to examples, package.json, and tsconfig.json

Test Plan: doitlive

* Delete ppsi.spec.js.snap

* Delete prettier.config.mjs

* Alt: Add support for gjs/gts, fix examples more (#377)

* Add support for gjs/gts, fix examples more

* WIP

* hm

* wheeeee

* Revert extraneous svelte change

* Revert extraneous vue change

* Update src/preprocessors/ember-preprocessor.ts

Co-authored-by: Robbie Wagner <[email protected]>

* Update examples/.prettierrc

Co-authored-by: Robbie Wagner <[email protected]>

* Update .prettierrc

Co-authored-by: Robbie Wagner <[email protected]>

* Update examples/example.gts

Co-authored-by: Robbie Wagner <[email protected]>

* Update examples/example.gjs

Co-authored-by: Robbie Wagner <[email protected]>

* Update tests/Ember/sfc.gts

Co-authored-by: Robbie Wagner <[email protected]>

* Support namespace imports

* Support named type imports

* Fix all type imports

* debugging improvement to run_spec

* Fix parser

* Reduce config

* Extract replaceAt to its own util and add tests for it

* Fix spelling: injest -> ingest

---------

Co-authored-by: Robbie Wagner <[email protected]>
Co-authored-by: Robbie Wagner <[email protected]>

* 6.0.0-0

* [Test] Stop ignoring exceptions thrown in snapshot tests

Summary:
This logic can hide breakages and cause tests to pass when the
underlying logic is actually broken. I encountered these hidden
breakages while working on some further feature changes, and figured
it was appropriate to fix this first.

As all existing tests pass without the try/catch, this change is
effectively a no-op.

Test Plan:
`yarn install && yarn run test ./tests/`

* [Perf] Improve performance of removeNodesFromOriginalCode()

Summary:
This change replaces the existing RegExp replace() logic with
concatenated string slices. This avoids reallocation the result
string for each node replacement, replacing it with string slice
operations (which are implemented as O(1) string views within v8) and
a single .join(''), which can be optimized by the runtime to a single
allocation.

This probably won't make a noticable difference, but the change also
simplifies some further feature work I am attempting to add.

Test Plan:
`yarn install && yarn run test --all`

* [Refactor] Stop rerendering directives, inject imports instead

Summary:
This commit changes how the sorted imports are combined with the
original source.

Prior to this commit, all ImportDeclaration nodes and their leading
comments, plus any InterpreterDirective and Directive nodes, were
extracted from the original code and re-rendered using babel. The
rendered nodes were then concatenated with the original source with
those nodes removed to produce the updated source.

This approach safely protected against functional changes, but
removed newlines around comments near the beginning of the file when
the first node of the original source was an ImportDeclaration, as
babel does not preserve whitespace when rendering content.

If a user has configured a plugin that attempts to manage comments
and/or whitespace near the top of the file, such as auto-inserting a
license header (as I am trying to do), this results in conflicts /
formatting churn.

This commit does not directly resolve this incompatibility, however
it better prepares the codebase for a plugin option to be added that
can resolve the issue.

Test Plan:
`yarn install && yarn run test --all`

Note that one snapshot was changed by this commit where a newline was
changed, acting as an effective example of how the original approach
could affect whitespace in the re-rendered portion of the file.

* [Feature] Expand detection for `sort-imports-ignore`

Summary:
Prior to this commit, isSortImportsIgnored() checked comments
associated with the extracted ImportDirectives, and only if the
comment started on line 1. This could result in a failure to suppress
sorting if the comment was not next to an ImportDirective (never
considered), the first import directive were embedded later in the
file (line mismatch), or directives/shebangs were used (Line 1 is
unavailable).

With this change, the line restriction is removed, and all comments
from the beginning of the file and the first statement are checked.
This ensures better coverage, especially with the
importOrderIgnoreHeaderComments feature stacked on this commit.

Test Plan:
`yarn install && yarn run test --all`

* Add sortNodeBuiltinModulesToTop

* Refactor: Use <BUILTIN_MODULES> placeholder instead of boolean option

* Fix test file extensions to .mjs for ES modules compatibility

* fix undefined error

* Update Ember snapshot and clean up debug code

* run yarn prettify

* prettify files

* remove changelog entry

* update CHANGELOG with features

* run yarn

* Revert "run yarn"

This reverts commit 5b1b267.

---------

Co-authored-by: KLewin23 <[email protected]>
Co-authored-by: annes <[email protected]>
Co-authored-by: Chason Choate <[email protected]>
Co-authored-by: RyderKishan <[email protected]>
Co-authored-by: Robbie Wagner <[email protected]>
Co-authored-by: Robert Wagner <[email protected]>
Co-authored-by: Vladislav Arsenev <[email protected]>
Co-authored-by: Nathan H. Leung <[email protected]>
Co-authored-by: Balkishan <[email protected]>
Co-authored-by: Cullen Walsh <[email protected]>
Co-authored-by: NullVoxPopuli <[email protected]>
Co-authored-by: Ayush Sharma <[email protected]>
Co-authored-by: Vladislav Arsenev <[email protected]>
Co-authored-by: Stuart Dotson <[email protected]>
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.

4 participants