Skip to content

Commit

Permalink
refactor: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
stephdotnet committed Jun 27, 2023
1 parent 06c8f01 commit 25647a0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
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
3 changes: 2 additions & 1 deletion src/utils/is-sort-imports-ignored.ts
Original file line number Diff line number Diff line change
@@ -1,10 +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('sort-imports-ignore'),
comment.value.includes(sortImportsIgnoredComment),
);

0 comments on commit 25647a0

Please sign in to comment.