Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support for sort-imports-ignore #237

Merged
merged 6 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`.
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;
}