From 25647a08cab9c823f92a7d18f8e9e68cbc142075 Mon Sep 17 00:00:00 2001 From: Stephdotnet Date: Tue, 27 Jun 2023 16:56:48 +0000 Subject: [PATCH] refactor: code review --- docs/TROUBLESHOOTING.md | 4 ++++ src/constants.ts | 2 ++ src/utils/is-sort-imports-ignored.ts | 3 ++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md index 0004dff..3aadcd8 100644 --- a/docs/TROUBLESHOOTING.md +++ b/docs/TROUBLESHOOTING.md @@ -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`. diff --git a/src/constants.ts b/src/constants.ts index e04f402..7c2c3ac 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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 diff --git a/src/utils/is-sort-imports-ignored.ts b/src/utils/is-sort-imports-ignored.ts index 0500754..0606e86 100644 --- a/src/utils/is-sort-imports-ignored.ts +++ b/src/utils/is-sort-imports-ignored.ts @@ -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), );