Skip to content

Commit c8bb4fa

Browse files
authored
Merge pull request #237 from stephdotnet/sort-imports-ignored
2 parents 09de470 + f37d48f commit c8bb4fa

File tree

15 files changed

+196
-1
lines changed

15 files changed

+196
-1
lines changed

docs/TROUBLESHOOTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,7 @@ via prettier config.
9090
plugins: [require('@trivago/prettier-plugin-sort-imports')],
9191
}
9292
```
93+
94+
#### Q. How can I prevent the plugin from reordering specific import statements?
95+
96+
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`.

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export const jsx: ParserPlugin = 'jsx';
77

88
export const newLineCharacters = '\n\n';
99

10+
export const sortImportsIgnoredComment = 'sort-imports-ignore';
11+
1012
/*
1113
* Used to mark the position between RegExps,
1214
* where the not matched imports should be placed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { parsers as babelParsers } from 'prettier/parser-babel';
22
import { parsers as flowParsers } from 'prettier/parser-flow';
3-
import { parsers as typescriptParsers } from 'prettier/parser-typescript';
43
import { parsers as htmlParsers } from 'prettier/parser-html';
4+
import { parsers as typescriptParsers } from 'prettier/parser-typescript';
55

66
import { defaultPreprocessor } from './preprocessors/default-processor';
77
import { vuePreprocessor } from './preprocessors/vue-preprocessor';

src/preprocessors/preprocessor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { extractASTNodes } from '../utils/extract-ast-nodes';
66
import { getCodeFromAst } from '../utils/get-code-from-ast';
77
import { getExperimentalParserPlugins } from '../utils/get-experimental-parser-plugins';
88
import { getSortedNodes } from '../utils/get-sorted-nodes';
9+
import { isSortImportsIgnored } from '../utils/is-sort-imports-ignored';
910

1011
export function preprocessor(code: string, options: PrettierOptions) {
1112
const {
@@ -33,6 +34,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
3334

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

3739
const allImports = getSortedNodes(importNodes, {
3840
importOrder,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { getImportNodes } from '../get-import-nodes';
2+
import { isSortImportsIgnored } from '../is-sort-imports-ignored';
3+
4+
const codeIgnored = `// sort-imports-ignore
5+
// second comment
6+
import z from 'z';
7+
`;
8+
9+
const codeNotIgnored = `// second comment
10+
import z from 'z';
11+
`;
12+
13+
test('it should return true if specific leading comment detected', () => {
14+
const importNodes = getImportNodes(codeIgnored);
15+
16+
expect(isSortImportsIgnored(importNodes)).toBeTruthy();
17+
});
18+
19+
test('it should return false if no specific leading comment detected', () => {
20+
const importNodes = getImportNodes(codeNotIgnored);
21+
22+
expect(isSortImportsIgnored(importNodes)).toBeFalsy();
23+
});

src/utils/get-sorted-nodes-group.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Import, ImportDeclaration } from '@babel/types';
2+
23
import { naturalSort } from '../natural-sort';
34
import { PrettierOptions } from '../types';
45

src/utils/is-sort-imports-ignored.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Statement } from '@babel/types';
2+
3+
import { sortImportsIgnoredComment } from '../constants';
4+
import { getAllCommentsFromNodes } from './get-all-comments-from-nodes';
5+
6+
export const isSortImportsIgnored = (nodes: Statement[]) =>
7+
getAllCommentsFromNodes(nodes).some(
8+
(comment) =>
9+
comment.loc.start.line === 1 &&
10+
comment.value.includes(sortImportsIgnoredComment),
11+
);

tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,30 @@ import "./commands";
385385
// require('./commands')
386386
387387
`;
388+
389+
exports[`sort-imports-ignored.ts - typescript-verify: sort-imports-ignored.ts 1`] = `
390+
// sort-imports-ignore
391+
392+
import './commands';
393+
import b from 'b';
394+
import a from 'a';
395+
396+
// Comment
397+
398+
function add(a:number,b:number) {
399+
return a + b;
400+
}
401+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
402+
// sort-imports-ignore
403+
404+
import "./commands";
405+
import b from "b";
406+
import a from "a";
407+
408+
// Comment
409+
410+
function add(a: number, b: number) {
411+
return a + b;
412+
}
413+
414+
`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// sort-imports-ignore
2+
3+
import './commands';
4+
import b from 'b';
5+
import a from 'a';
6+
7+
// Comment
8+
9+
function add(a:number,b:number) {
10+
return a + b;
11+
}

tests/ImportsNotSeparatedRest/__snapshots__/ppsi.spec.js.snap

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,30 @@ import "./commands";
328328
// require('./commands')
329329
330330
`;
331+
332+
exports[`sort-imports-ignored.ts - typescript-verify: sort-imports-ignored.ts 1`] = `
333+
// sort-imports-ignore
334+
335+
import './commands';
336+
import b from 'b';
337+
import a from 'a';
338+
339+
// Comment
340+
341+
function add(a:number,b:number) {
342+
return a + b;
343+
}
344+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
345+
// sort-imports-ignore
346+
347+
import "./commands";
348+
import b from "b";
349+
import a from "a";
350+
351+
// Comment
352+
353+
function add(a: number, b: number) {
354+
return a + b;
355+
}
356+
357+
`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// sort-imports-ignore
2+
3+
import './commands';
4+
import b from 'b';
5+
import a from 'a';
6+
7+
// Comment
8+
9+
function add(a:number,b:number) {
10+
return a + b;
11+
}

tests/ImportsSeparated/__snapshots__/ppsi.spec.js.snap

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,30 @@ import "./commands";
412412
// require('./commands')
413413
414414
`;
415+
416+
exports[`sort-imports-ignored.ts - typescript-verify: sort-imports-ignored.ts 1`] = `
417+
// sort-imports-ignore
418+
419+
import './commands';
420+
import b from 'b';
421+
import a from 'a';
422+
423+
// Comment
424+
425+
function add(a:number,b:number) {
426+
return a + b;
427+
}
428+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
429+
// sort-imports-ignore
430+
431+
import "./commands";
432+
import b from "b";
433+
import a from "a";
434+
435+
// Comment
436+
437+
function add(a: number, b: number) {
438+
return a + b;
439+
}
440+
441+
`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// sort-imports-ignore
2+
3+
import './commands';
4+
import b from 'b';
5+
import a from 'a';
6+
7+
// Comment
8+
9+
function add(a:number,b:number) {
10+
return a + b;
11+
}

tests/ImportsSeparatedRest/__snapshots__/ppsi.spec.js.snap

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,30 @@ import "./commands";
347347
// require('./commands')
348348
349349
`;
350+
351+
exports[`sort-imports-ignored.ts - typescript-verify: sort-imports-ignored.ts 1`] = `
352+
// sort-imports-ignore
353+
354+
import './commands';
355+
import b from 'b';
356+
import a from 'a';
357+
358+
// Comment
359+
360+
function add(a:number,b:number) {
361+
return a + b;
362+
}
363+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
364+
// sort-imports-ignore
365+
366+
import "./commands";
367+
import b from "b";
368+
import a from "a";
369+
370+
// Comment
371+
372+
function add(a: number, b: number) {
373+
return a + b;
374+
}
375+
376+
`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// sort-imports-ignore
2+
3+
import './commands';
4+
import b from 'b';
5+
import a from 'a';
6+
7+
// Comment
8+
9+
function add(a:number,b:number) {
10+
return a + b;
11+
}

0 commit comments

Comments
 (0)