Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/preprocessors/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ImportDeclaration } from '@babel/types';

import { PrettierOptions } from '../types';
import { extractASTNodes } from '../utils/extract-ast-nodes.js';
import { getAllCommentsFromNodes } from '../utils/get-all-comments-from-nodes.js';
import { getCodeFromAst } from '../utils/get-code-from-ast.js';
import { getExperimentalParserPlugins } from '../utils/get-experimental-parser-plugins.js';
import { getSortedNodes } from '../utils/get-sorted-nodes.js';
Expand All @@ -28,6 +29,9 @@ export function preprocessor(code: string, options: PrettierOptions) {

const ast = babelParser(code, parserOptions);

if (isSortImportsIgnored(ast.program.body[0]?.leadingComments ?? []))
return code;

const {
importNodes,
injectIdx,
Expand All @@ -36,7 +40,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;
if (isSortImportsIgnored(getAllCommentsFromNodes(importNodes))) return code;

const allImports = getSortedNodes(importNodes, {
importOrder,
Expand Down
21 changes: 19 additions & 2 deletions src/utils/__tests__/is-sort-imports-ignored.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, test } from 'vitest';

import { getAllCommentsFromNodes } from '../get-all-comments-from-nodes';
import { getImportNodes } from '../get-import-nodes';
import { isSortImportsIgnored } from '../is-sort-imports-ignored';

Expand All @@ -12,14 +13,30 @@ const codeNotIgnored = `// second comment
import z from 'z';
`;

const notIgnoreTextWithIgnoreLine = `// you need to write sort-imports-ignore
import z from 'z';
`;

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

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

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

expect(isSortImportsIgnored(importNodes)).toBeFalsy();
expect(
isSortImportsIgnored(getAllCommentsFromNodes(importNodes)),
).toBeFalsy();
});

test('it should return false if ignore isnt on top of comment', () => {
const importNodes = getImportNodes(notIgnoreTextWithIgnoreLine);

expect(
isSortImportsIgnored(getAllCommentsFromNodes(importNodes)),
).toBeFalsy();
});
13 changes: 5 additions & 8 deletions src/utils/is-sort-imports-ignored.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Statement } from '@babel/types';
import { Comment } from '@babel/types';

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

export const isSortImportsIgnored = (nodes: Statement[]) =>
getAllCommentsFromNodes(nodes).some(
(comment) =>
comment.loc &&
comment.loc.start.line === 1 &&
comment.value.includes(sortImportsIgnoredComment),
export const isSortImportsIgnored = (comments: Comment[]) => {
return comments.some((comment) =>
comment.value.trimStart().startsWith(sortImportsIgnoredComment),
);
};