Skip to content
Merged

V6 #385

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
c03b7bf
Add sort by length option
KLewin23 Mar 25, 2023
5706700
fix types
KLewin23 Mar 25, 2023
fd26065
Merge branch 'main' into main
KLewin23 Jul 9, 2023
ed1cab6
Merge branch 'main' into main
KLewin23 Aug 29, 2023
8904092
Merge branch 'main' into Fix-conflicts-for-sort-by-length
KLewin23 Nov 2, 2023
da49f66
Correct default type
KLewin23 Nov 15, 2023
4884cdb
Merge branch 'main' into Fix-conflicts-for-sort-by-length
KLewin23 Nov 15, 2023
15bc4d8
Yet another <SEPARATOR> feature PR (#339)
chason-choate-trilliant Feb 6, 2025
ce68fd7
Merge branch 'main' into Fix-conflicts-for-sort-by-length
KLewin23 Mar 20, 2025
22e26b6
fix tests, update docs
KLewin23 Mar 20, 2025
7ef0bb2
feat: Import sort order skip files
RyderKishan May 15, 2025
b39a25f
Downgraded minimatch for node 18
RyderKishan May 15, 2025
716257b
Added new types
RyderKishan May 15, 2025
0ea7526
(fix) Types error
RyderKishan May 15, 2025
fc4c761
Switch to ESM
RobbieTheWagner Jun 26, 2025
c68dbfb
Ensure all tests are running
RobbieTheWagner Jun 26, 2025
34363f4
Correctly resolve plugin
RobbieTheWagner Jul 2, 2025
15781fd
Remove some changes
RobbieTheWagner Jul 3, 2025
0f6e913
Fix svelte
RobbieTheWagner Jul 4, 2025
5aa0a76
Merge pull request #366 from RobbieTheWagner/esm
vladislavarsenev Jul 9, 2025
916f7e2
Require node >= 20.x (#367)
RobbieTheWagner Jul 9, 2025
10414cf
docs: add pnpm install command to README (#361)
nathanhleung Jul 9, 2025
7e76785
Switch to vitest for better ESM support
RobbieTheWagner Jul 9, 2025
24b628b
Explicitly import expect and test
RobbieTheWagner Jul 9, 2025
6f90f67
Fix types
RobbieTheWagner Jul 9, 2025
233e1ce
Fix accidentally checked file
RobbieTheWagner Jul 10, 2025
9680197
Merge pull request #370 from RobbieTheWagner/vitest
vladislavarsenev Jul 10, 2025
130ef6f
Add extensions to imports, fix some example issues
RobbieTheWagner Jul 10, 2025
bcdd0cd
Make tsc happy
RobbieTheWagner Jul 10, 2025
c54f317
Convert all cjs to mjs
RobbieTheWagner Jul 10, 2025
a6d6932
Fix some things
RobbieTheWagner Jul 10, 2025
c04a193
Get examples and tests working
RobbieTheWagner Jul 10, 2025
bf80936
(fix) PR review comments analysed and reviewed
RyderKishan Jul 11, 2025
e7c311b
Reverse merge: resetting to upstream/main state
RyderKishan Jul 11, 2025
a7db2b5
State now matches upstream/main
RyderKishan Jul 11, 2025
b4dea42
Revert "State now matches upstream/main"
RyderKishan Jul 11, 2025
9d9c7de
Merge branch 'v6' into main
RyderKishan Jul 11, 2025
f5df5ae
Fixed version mis matches
RyderKishan Jul 11, 2025
8b1f2ba
[Format] Run prettier on most files
ckwalsh Jul 14, 2025
65487cb
Delete ppsi.spec.js.snap
RobbieTheWagner Jul 23, 2025
4ced0cd
Delete prettier.config.mjs
RobbieTheWagner Jul 23, 2025
1905324
Merge pull request #371 from RobbieTheWagner/fix-examples
vladislavarsenev Jul 23, 2025
bb68ecd
Alt: Add support for gjs/gts, fix examples more (#377)
NullVoxPopuli Jul 30, 2025
e663d1d
6.0.0-0
ayusharma Aug 1, 2025
0bb9035
Merge branch 'main' into Fix-conflicts-for-sort-by-length
KLewin23 Sep 9, 2025
e2487af
Merge branch 'v6' into pr/KLewin23/224
vladislavarsenev Oct 17, 2025
ed4f1c2
Merge branch 'v6' into pr/KLewin23/224
vladislavarsenev Oct 17, 2025
e4a1b27
Merge pull request #224 from KLewin23/Fix-conflicts-for-sort-by-length
vladislavarsenev Oct 17, 2025
d9cae1b
Merge branch 'v6' into pr354
vladislavarsenev Oct 17, 2025
e5326ee
Merge branch 'v6' into pr354
vladislavarsenev Oct 17, 2025
045e85e
Merge pull request #354 from ckwalsh/pr354
vladislavarsenev Oct 17, 2025
371e9f5
[Test] Stop ignoring exceptions thrown in snapshot tests
ckwalsh Jul 18, 2025
92e5374
Merge pull request #355 from ckwalsh/pr355
vladislavarsenev Oct 17, 2025
9266fdb
[Perf] Improve performance of removeNodesFromOriginalCode()
ckwalsh Jul 18, 2025
887b7eb
Merge pull request #356 from ckwalsh/pr356
vladislavarsenev Oct 17, 2025
4955de8
[Refactor] Stop rerendering directives, inject imports instead
ckwalsh Jul 18, 2025
a1d05d7
Merge pull request #357 from ckwalsh/pr357
vladislavarsenev Oct 17, 2025
e27889d
[Feature] Expand detection for `sort-imports-ignore`
ckwalsh Jul 18, 2025
e750c32
Merge pull request #358 from ckwalsh/pr358
vladislavarsenev Oct 17, 2025
6b7e905
Add sortNodeBuiltinModulesToTop
sdotson Oct 2, 2025
010ca09
Refactor: Use <BUILTIN_MODULES> placeholder instead of boolean option
sdotson Oct 17, 2025
2437b97
Fix test file extensions to .mjs for ES modules compatibility
sdotson Oct 17, 2025
a07683b
fix undefined error
sdotson Oct 17, 2025
6d767ff
Update Ember snapshot and clean up debug code
sdotson Oct 17, 2025
dfbb6cb
run yarn prettify
sdotson Oct 20, 2025
3347f3f
Merge pull request #381 from sdotson/add-sortNodeBuiltinModulesToTop
vladislavarsenev Oct 20, 2025
cc7c7eb
Merge branch 'v6' into RyderKishan/main
vladislavarsenev Oct 24, 2025
9acb229
prettify files
vladislavarsenev Oct 24, 2025
842fec9
Merge pull request #384 from vladislavarsenev/RyderKishan/main
vladislavarsenev Oct 24, 2025
85a768a
remove changelog entry
vladislavarsenev Oct 24, 2025
5f0f636
update CHANGELOG with features
vladislavarsenev Oct 24, 2025
5b1b267
run yarn
byara Oct 28, 2025
9ae1cc7
Revert "run yarn"
byara Oct 28, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [18.x, 20.x, 22.x]
node: [20.x, 22.x, 24.x]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand Down
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
tests/
lib/
examples/
.github/
docs/
*.md
16 changes: 16 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"printWidth": 80,
"tabWidth": 4,
"trailingComma": "all",
"singleQuote": true,
"bracketSameLine": true,
"semi": true,
"importOrder": ["^[./]"],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true,
"plugins": [
"prettier-plugin-ember-template-tag",
"prettier-plugin-svelte",
"./lib/src/index.js"
]
}
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@


---
### 6.0.0

#### Breaking Changes
- **Switch to ESM** [#366](https://github.com/trivago/prettier-plugin-sort-imports/pull/366) by [@RobbieTheWagner](https://github.com/RobbieTheWagner) - The plugin now uses ES modules instead of CommonJS
- **Require Node >= 20.x** [#367](https://github.com/trivago/prettier-plugin-sort-imports/pull/367) by [@RobbieTheWagner](https://github.com/RobbieTheWagner) - Drop support for Node.js versions below 20
- **Switch to vitest** [#370](https://github.com/trivago/prettier-plugin-sort-imports/pull/370) by [@RobbieTheWagner](https://github.com/RobbieTheWagner) - Replaced Jest with Vitest for better ESM support

#### New features
- **Ember.js gjs/gts support** [#377](https://github.com/trivago/prettier-plugin-sort-imports/pull/377) by [@NullVoxPopuli](https://github.com/NullVoxPopuli) and [@RobbieTheWagner](https://github.com/RobbieTheWagner) - Add support for Ember.js gjs/gts file formats with namespace and named type imports
- **`<BUILTIN_MODULES>` placeholder** [#381](https://github.com/trivago/prettier-plugin-sort-imports/pull/381) by [@sdotson](https://github.com/sdotson) - Add support for sorting Node.js builtin modules to a specific position using `<BUILTIN_MODULES>` placeholder in `importOrder`
- **`<SEPARATOR>` placeholder** [#339](https://github.com/trivago/prettier-plugin-sort-imports/pull/339) - Add `<SEPARATOR>` keyword for fine-grained control over import group separation when `importOrderSeparation` is enabled
- **Sort by length option** [#224](https://github.com/trivago/prettier-plugin-sort-imports/pull/224) by [@KLewin23](https://github.com/KLewin23) - Add option to sort imports by import statement length
- **`importOrderExclude` option** [#384](https://github.com/trivago/prettier-plugin-sort-imports/pull/384) by [@RyderKishan](https://github.com/RyderKishan) - Add support for excluding specific files from import sorting using glob patterns
- **Expand `sort-imports-ignore` detection** [#358](https://github.com/trivago/prettier-plugin-sort-imports/pull/358) by [@ckwalsh](https://github.com/ckwalsh) - Improved detection of `sort-imports-ignore` comments throughout the file, not just at line 1

#### Performance improvements
- **Improve `removeNodesFromOriginalCode()` performance** [#356](https://github.com/trivago/prettier-plugin-sort-imports/pull/356) by [@ckwalsh](https://github.com/ckwalsh) - Replace RegExp logic with string slices for better performance

#### Refactoring
- **Stop rerendering directives** [#357](https://github.com/trivago/prettier-plugin-sort-imports/pull/357) by [@ckwalsh](https://github.com/ckwalsh) - Inject imports instead of re-rendering directives to better preserve whitespace and reduce formatting conflicts
- **Stop ignoring exceptions in snapshot tests** [#355](https://github.com/trivago/prettier-plugin-sort-imports/pull/355) by [@ckwalsh](https://github.com/ckwalsh) - Improve test reliability by properly handling exceptions

#### Chores
- Add pnpm install command to README [#361](https://github.com/trivago/prettier-plugin-sort-imports/pull/361) - Document pnpm installation option

### 5.2.2
- Update packages and pin babel/types [#343](https://github.com/trivago/prettier-plugin-sort-imports/pull/343) by [@byara](https://github.com/byara)

Expand Down
24 changes: 12 additions & 12 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ orientation.
Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
- The use of sexualized language or imagery and unwelcome sexual attention or
advances
- Trolling, insulting/derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
- Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities
Expand Down Expand Up @@ -71,4 +71,4 @@ This Code of Conduct is adapted from the [Contributor Covenant][homepage], versi
available at [http://contributor-covenant.org/version/1/4][version]

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
[version]: http://contributor-covenant.org/version/1/4/
53 changes: 43 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import React, {
KeyboardEvent,
} from 'react';
import { logger } from '@core/logger';
import { reduce, debounce } from 'lodash';
import { reduce, debounce } from 'lodash-es';
import { Message } from '../Message';
import { createServer } from '@server/node';
import { Alert } from '@ui/Alert';
Expand All @@ -29,7 +29,7 @@ import { createConnection } from '@server/database';
### Output

```javascript
import { debounce, reduce } from 'lodash';
import { debounce, reduce } from 'lodash-es';
import React, {
ChangeEvent,
FC,
Expand All @@ -53,18 +53,24 @@ import { add, filter, repeat } from '../utils';

### Install

npm
using npm

```shell script
npm install --save-dev @trivago/prettier-plugin-sort-imports
```

or, using yarn
using yarn

```shell script
yarn add --dev @trivago/prettier-plugin-sort-imports
```

using pnpm

```shell script
pnpm add -D @trivago/prettier-plugin-sort-imports
```

**Note: If you are migrating from v2.x.x to v3.x.x, [Please Read Migration Guidelines](./docs/MIGRATION.md)**

**Note: If formatting `.vue` sfc files please install `@vue/compiler-sfc` if not in your dependency tree - this normally is within Vue projects.**
Expand Down Expand Up @@ -114,6 +120,14 @@ To move the third party imports at desired place, you can use `<THIRD_PARTY_MODU
"importOrder": ["^@core/(.*)$", "<THIRD_PARTY_MODULES>", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
```

You can also use `<BUILTIN_MODULES>` to control the position of Node.js builtin modules (like `fs`, `path`, `http`, and their `node:` prefixed variants):

```
"importOrder": ["<BUILTIN_MODULES>", "<THIRD_PARTY_MODULES>", "^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
```

When `<BUILTIN_MODULES>` is included in your `importOrder`, Node.js builtin modules will be sorted to that position. If not included, builtin modules are treated as regular third-party imports.

#### `importOrderSeparation`

**type**: `boolean`
Expand All @@ -127,6 +141,9 @@ between sorted import declarations group. The separation takes place according t
"importOrderSeparation": true,
```

If this option is enabled and `<SEPARATOR>` is used in the `importOrder` array, the plugin
will ONLY add newlines at those locations and at the end of the imports.

#### `importOrderSortSpecifiers`

**type**: `boolean`
Expand Down Expand Up @@ -198,6 +215,13 @@ with options as a JSON string of the plugin array:
importOrderParserPlugins: []
```

### `importOrderSortByLength`
**type**: `'asc' | 'desc' | null`
**default value**: `null`

A choice value to enable sorting imports within their groups based on their string lengths, the two options being ascending and descending.
Leaving the value blank or setting it to null will result in length being ignored

### `importOrderSideEffects`
**type**: `boolean`
**default value**: `true`
Expand Down Expand Up @@ -229,6 +253,7 @@ import b from 'b'
import c from 'c'
```


### Ignoring import ordering

In some cases it's desired to ignore import ordering, specifically if you require to instantiate a common service or polyfill in your application logic before all the other imports. The plugin supports the `// sort-imports-ignore` comment, which will exclude the file from ordering the imports.
Expand Down Expand Up @@ -267,6 +292,12 @@ In the end, the plugin returns final imports with _third party imports_ on top a

The _third party imports_ position (it's top by default) can be overridden using the `<THIRD_PARTY_MODULES>` special word in the `importOrder`.

### Pattern Matching Implementation

This plugin uses [minimatch](https://github.com/isaacs/minimatch) for pattern matching of import paths. The matching is performed using the exact version specified in the plugin's dependencies to ensure consistent behavior. This is important to note because different versions of minimatch or other glob matching libraries might have subtle differences in their pattern matching behavior.

If you're experiencing unexpected matching behavior, please ensure you're using patterns compatible with minimatch's syntax, which might differ slightly from other glob implementations.

### FAQ / Troubleshooting

Having some trouble or an issue ? You can check [FAQ / Troubleshooting section](./docs/TROUBLESHOOTING.md).
Expand All @@ -277,11 +308,13 @@ Having some trouble or an issue ? You can check [FAQ / Troubleshooting section](
| ---------------------- | ------------------------ | ------------------------------------------------ |
| JS with ES Modules | ✅ Everything | - |
| NodeJS with ES Modules | ✅ Everything | - |
| Angular | ✅ Everything | Supported through `importOrderParserPlugins` API |
| Ember | ✅ Everything | `prettier-plugin-ember-template-tag` is required |
| React | ✅ Everything | - |
| Solid | ✅ Everything | - |
| Angular | ✅ Everything | Supported through `importOrderParserPlugins` API |
| Vue | ✅ Everything | `@vue/compiler-sfc` is required |
| Svelte | ✅ Everything | `prettier-plugin-svelte` is required |
| Vue | ✅ Everything | `@vue/compiler-sfc` is required |


### Used by

Expand All @@ -299,10 +332,10 @@ debug some code in the plugin, check [Debugging Guidelines](./docs/DEBUG.md)

### Maintainers

| [Ayush Sharma](https://github.com/ayusharma) | [Behrang Yarahmadi](https://github.com/byara) | [Vladislav Arsenev](https://github.com/vladislavarsenev) |
| ------------------------------------------------------------------------ | --------------------------------------------------------------------- |--------------------------------------------------------------------------|
| ![ayusharma](https://avatars2.githubusercontent.com/u/6918450?s=120&v=4) | ![@byara](https://avatars2.githubusercontent.com/u/6979966?s=120&v=4) |![@vladislavarsenev](https://avatars.githubusercontent.com/u/51095682?s=120&v=4)|
| [@ayusharma](https://twitter.com/ayusharma_) | [@behrang_y](https://twitter.com/behrang_y) | |
| [Ayush Sharma](https://github.com/ayusharma) | [Behrang Yarahmadi](https://github.com/byara) |
| ------------------------------------------------------------------------ | --------------------------------------------------------------------- |
| ![ayusharma](https://avatars2.githubusercontent.com/u/6918450?s=120&v=4) | ![@byara](https://avatars2.githubusercontent.com/u/6979966?s=120&v=4) |
| [@ayusharma\_](https://twitter.com/ayusharma_) | [@behrang_y](https://twitter.com/behrang_y) |

### Disclaimer

Expand Down
2 changes: 1 addition & 1 deletion docs/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Due to the package handling of the pnpm, sometimes, the plugin is not automatica
via prettier config.
```js
module.exports = {
plugins: [require('@trivago/prettier-plugin-sort-imports')],
plugins: ['@trivago/prettier-plugin-sort-imports'],
}
```

Expand Down
6 changes: 5 additions & 1 deletion examples/.prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
"importOrder": ["^@server/(.*)$", "^@core/(.*)$", "^@ui/(.*)$", "^[./]"],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true,
"plugins": ["../lib/src/index.js"]
"plugins": [
"prettier-plugin-ember-template-tag",
"prettier-plugin-svelte",
"../lib/src/index.js"
]
}
29 changes: 29 additions & 0 deletions examples/example.gjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// I am top level comment in this file.
import z from 'z';
import threeLevelRelativePath from '../../../threeLevelRelativePath';
import sameLevelRelativePath from './sameLevelRelativePath';
import thirdParty from 'third-party';
import oneLevelRelativePath from '../oneLevelRelativePath';
import otherthing from '@core/otherthing';
import abc from '@core/abc';
import twoLevelRelativePath from '../../twoLevelRelativePath';
import component from '@ui/hello';
import fourLevelRelativePath from '../../../../fourLevelRelativePath';
import * as a from 'a';
import something from '@server/something';
import xyz from '@ui/xyz';
import Component from '@glimmer/component';
import { action } from '@ember/object';

const what = <template>Used as an expression</template>;

export const who = <template>Used as an expression</template>;

export default class Foo extends Component {
@action
myCoolFunction(){}

<template>
Top-level class template
</template>
}
41 changes: 41 additions & 0 deletions examples/example.gts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// I am top level comment in this file.
import z from 'z';
import threeLevelRelativePath from '../../../threeLevelRelativePath';
import sameLevelRelativePath from './sameLevelRelativePath';
import thirdParty from 'third-party';
import oneLevelRelativePath from '../oneLevelRelativePath';
import otherthing from '@core/otherthing';
import abc from '@core/abc';
import twoLevelRelativePath from '../../twoLevelRelativePath';
import component from '@ui/hello';
import fourLevelRelativePath from '../../../../fourLevelRelativePath';
import * as a from 'a';
import type RouterService from '@ember/routing/router-service';
import something from '@server/something';
import xyz from '@ui/xyz';
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { getOwner } from '@ember/owner';
import { setOwner } from '@ember/owner';
import type Owner from '@ember/owner';
import type DefaultOwner from '@ember/owner';
import type * as AllOwner from '@ember/owner';

interface FooSignature {
Args: {
bar: string
};
}

const what = <template>Used as an expression</template>;

export const who = <template>Used as an expression</template>;

export default class Foo extends Component<FooSignature> {
@action
myCoolFunction(){}

<template>
Top-level class template
</template>
}
2 changes: 1 addition & 1 deletion examples/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, {
KeyboardEvent,
} from 'react';
import { logger } from '@core/logger';
import { reduce, debounce } from 'lodash';
import { reduce, debounce } from 'lodash-es';
import { Message } from '../Message';
import { createServer } from '@server/node';
import { Alert } from '@ui/Alert';
Expand Down
14 changes: 0 additions & 14 deletions jest.config.js

This file was deleted.

Loading