Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
byara authored Dec 3, 2024
2 parents 2750365 + ea30e7a commit f107d27
Show file tree
Hide file tree
Showing 20 changed files with 944 additions and 796 deletions.
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: [12.x, 14.x, 16.x]
node: [12.x, 14.x, 16.x, 18.x, 20.x]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@


---
### v4.3.0
#### New features
- added support for sort-imports-ignore [#237](https://github.com/trivago/prettier-plugin-sort-imports/pull/237) by [stephdotnet](https://github.com/stephdotnet)
- Upgrade node in CI to current and lts versions [#264](https://github.com/trivago/prettier-plugin-sort-imports/pull/264) by [harryzcy](https://github.com/harryzcy)

### v4.2.1
#### Chore
- Resolves Issue 262 - CVE-2023-45133 - upgrade to latest babel traverse [#266](https://github.com/trivago/prettier-plugin-sort-imports/pull/266) by [c-h-russell-walker](https://github.com/c-h-russell-walker)

### v4.2.0
#### Chore
- update prettier peer dependency semVer to include 3.x [#239](https://github.com/trivago/prettier-plugin-sort-imports/pull/239) by [basselworkforce](https://github.com/basselworkforce)

### v4.1.1
#### Revert
- Type imports[#153](https://github.com/trivago/prettier-plugin-sort-imports/pull/153) by [Xenfo](https://github.com/broofa)
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ module.exports = {
}
```

**Note: There may be an issue with some package managers, such as `pnpm` or when using `prettier` v3.x. You can solve it by providing additional configuration option in prettier config file.

```js
module.exports = {
...
"plugins": ["@trivago/prettier-plugin-sort-imports"]
}
```

### APIs

#### **`importOrder`**
Expand Down Expand Up @@ -189,6 +198,17 @@ with options as a JSON string of the plugin array:
importOrderParserPlugins: []
```

### 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.

```javascript
// sort-imports-ignore
import './polyfills';

import foo from 'foo'
```

### How does import sort work ?

The plugin extracts the imports which are defined in `importOrder`. These imports are considered as _local imports_.
Expand Down Expand Up @@ -222,6 +242,7 @@ Feel free to make a Pull Request to add your project / company name.

- [trivago](https://company.trivago.com)
- [AuresKonnect](https://aures.com)
- [FactorialHR](https://factorialhr.com)

### Contribution

Expand Down
4 changes: 4 additions & 0 deletions docs/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ via prettier config.
plugins: [require('@trivago/prettier-plugin-sort-imports')],
}
```

#### Q. How can I prevent the plugin from reordering specific import statements?

Due to the complexity of maintaining the position of certain imports while reordering others, you can prevent the plugin from rearranging your file by adding the following comment at the top of your file: `// sort-imports-ignore`.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trivago/prettier-plugin-sort-imports",
"version": "4.1.1",
"version": "4.3.0",
"description": "A prettier plugins to sort imports in provided RegEx order",
"main": "lib/src/index.js",
"types": "types/index.d.ts",
Expand Down Expand Up @@ -35,7 +35,7 @@
"dependencies": {
"@babel/generator": "7.17.7",
"@babel/parser": "^7.20.5",
"@babel/traverse": "7.17.3",
"@babel/traverse": "7.23.2",
"@babel/types": "7.17.0",
"javascript-natural-sort": "0.7.1",
"lodash": "^4.17.21"
Expand All @@ -45,7 +45,7 @@
"@types/chai": "4.2.15",
"@types/jest": "26.0.20",
"@types/lodash": "4.14.168",
"@types/node": "14.14.34",
"@types/node": "20.8.6",
"@vue/compiler-sfc": "^3.2.41",
"jest": "26.6.3",
"prettier": "2.8",
Expand All @@ -54,7 +54,7 @@
},
"peerDependencies": {
"@vue/compiler-sfc": "3.x",
"prettier": "2.x"
"prettier": "2.x - 3.x"
},
"peerDependenciesMeta": {
"@vue/compiler-sfc": {
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const jsx: ParserPlugin = 'jsx';

export const newLineCharacters = '\n\n';

export const sortImportsIgnoredComment = 'sort-imports-ignore';

/*
* Used to mark the position between RegExps,
* where the not matched imports should be placed
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parsers as babelParsers } from 'prettier/parser-babel';
import { parsers as flowParsers } from 'prettier/parser-flow';
import { parsers as typescriptParsers } from 'prettier/parser-typescript';
import { parsers as htmlParsers } from 'prettier/parser-html';
import { parsers as typescriptParsers } from 'prettier/parser-typescript';

import { defaultPreprocessor } from './preprocessors/default-processor';
import { vuePreprocessor } from './preprocessors/vue-preprocessor';
Expand Down
2 changes: 2 additions & 0 deletions src/preprocessors/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { extractASTNodes } from '../utils/extract-ast-nodes';
import { getCodeFromAst } from '../utils/get-code-from-ast';
import { getExperimentalParserPlugins } from '../utils/get-experimental-parser-plugins';
import { getSortedNodes } from '../utils/get-sorted-nodes';
import { isSortImportsIgnored } from '../utils/is-sort-imports-ignored';

export function preprocessor(code: string, options: PrettierOptions) {
const {
Expand Down Expand Up @@ -33,6 +34,7 @@ export function preprocessor(code: string, options: PrettierOptions) {

// short-circuit if there are no import declaration
if (importNodes.length === 0) return code;
if (isSortImportsIgnored(importNodes)) return code;

const allImports = getSortedNodes(importNodes, {
importOrder,
Expand Down
23 changes: 23 additions & 0 deletions src/utils/__tests__/is-sort-imports-ignored.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getImportNodes } from '../get-import-nodes';
import { isSortImportsIgnored } from '../is-sort-imports-ignored';

const codeIgnored = `// sort-imports-ignore
// second comment
import z from 'z';
`;

const codeNotIgnored = `// second comment
import z from 'z';
`;

test('it should return true if specific leading comment detected', () => {
const importNodes = getImportNodes(codeIgnored);

expect(isSortImportsIgnored(importNodes)).toBeTruthy();
});

test('it should return false if no specific leading comment detected', () => {
const importNodes = getImportNodes(codeNotIgnored);

expect(isSortImportsIgnored(importNodes)).toBeFalsy();
});
1 change: 1 addition & 0 deletions src/utils/get-sorted-nodes-group.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Import, ImportDeclaration } from '@babel/types';

import { naturalSort } from '../natural-sort';
import { PrettierOptions } from '../types';

Expand Down
11 changes: 11 additions & 0 deletions src/utils/is-sort-imports-ignored.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Statement } from '@babel/types';

import { sortImportsIgnoredComment } from '../constants';
import { getAllCommentsFromNodes } from './get-all-comments-from-nodes';

export const isSortImportsIgnored = (nodes: Statement[]) =>
getAllCommentsFromNodes(nodes).some(
(comment) =>
comment.loc.start.line === 1 &&
comment.value.includes(sortImportsIgnoredComment),
);
27 changes: 27 additions & 0 deletions tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,30 @@ import "./commands";
// require('./commands')
`;

exports[`sort-imports-ignored.ts - typescript-verify: sort-imports-ignored.ts 1`] = `
// sort-imports-ignore
import './commands';
import b from 'b';
import a from 'a';
// Comment
function add(a:number,b:number) {
return a + b;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// sort-imports-ignore
import "./commands";
import b from "b";
import a from "a";
// Comment
function add(a: number, b: number) {
return a + b;
}
`;
11 changes: 11 additions & 0 deletions tests/ImportsNotSeparated/sort-imports-ignored.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// sort-imports-ignore

import './commands';
import b from 'b';
import a from 'a';

// Comment

function add(a:number,b:number) {
return a + b;
}
27 changes: 27 additions & 0 deletions tests/ImportsNotSeparatedRest/__snapshots__/ppsi.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,30 @@ import "./commands";
// require('./commands')
`;

exports[`sort-imports-ignored.ts - typescript-verify: sort-imports-ignored.ts 1`] = `
// sort-imports-ignore
import './commands';
import b from 'b';
import a from 'a';
// Comment
function add(a:number,b:number) {
return a + b;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// sort-imports-ignore
import "./commands";
import b from "b";
import a from "a";
// Comment
function add(a: number, b: number) {
return a + b;
}
`;
11 changes: 11 additions & 0 deletions tests/ImportsNotSeparatedRest/sort-imports-ignored.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// sort-imports-ignore

import './commands';
import b from 'b';
import a from 'a';

// Comment

function add(a:number,b:number) {
return a + b;
}
27 changes: 27 additions & 0 deletions tests/ImportsSeparated/__snapshots__/ppsi.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,30 @@ import "./commands";
// require('./commands')
`;

exports[`sort-imports-ignored.ts - typescript-verify: sort-imports-ignored.ts 1`] = `
// sort-imports-ignore
import './commands';
import b from 'b';
import a from 'a';
// Comment
function add(a:number,b:number) {
return a + b;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// sort-imports-ignore
import "./commands";
import b from "b";
import a from "a";
// Comment
function add(a: number, b: number) {
return a + b;
}
`;
11 changes: 11 additions & 0 deletions tests/ImportsSeparated/sort-imports-ignored.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// sort-imports-ignore

import './commands';
import b from 'b';
import a from 'a';

// Comment

function add(a:number,b:number) {
return a + b;
}
27 changes: 27 additions & 0 deletions tests/ImportsSeparatedRest/__snapshots__/ppsi.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,30 @@ import "./commands";
// require('./commands')
`;

exports[`sort-imports-ignored.ts - typescript-verify: sort-imports-ignored.ts 1`] = `
// sort-imports-ignore
import './commands';
import b from 'b';
import a from 'a';
// Comment
function add(a:number,b:number) {
return a + b;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// sort-imports-ignore
import "./commands";
import b from "b";
import a from "a";
// Comment
function add(a: number, b: number) {
return a + b;
}
`;
11 changes: 11 additions & 0 deletions tests/ImportsSeparatedRest/sort-imports-ignored.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// sort-imports-ignore

import './commands';
import b from 'b';
import a from 'a';

// Comment

function add(a:number,b:number) {
return a + b;
}
Loading

0 comments on commit f107d27

Please sign in to comment.