Skip to content

Commit a28091f

Browse files
ckwalshvladislavarsenev
authored andcommitted
[Feature] Expand detection for sort-imports-ignore
Summary: Prior to this commit, isSortImportsIgnored() checked comments associated with the extracted ImportDirectives, and only if the comment started on line 1. This could result in a failure to suppress sorting if the comment was not next to an ImportDirective (never considered), the first import directive were embedded later in the file (line mismatch), or directives/shebangs were used (Line 1 is unavailable). With this change, the line restriction is removed, and all comments from the beginning of the file and the first statement are checked. This ensures better coverage, especially with the importOrderIgnoreHeaderComments feature stacked on this commit. Test Plan: `yarn install && yarn run test --all`
1 parent a1d05d7 commit a28091f

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/preprocessors/preprocessor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ImportDeclaration } from '@babel/types';
33

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

2930
const ast = babelParser(code, parserOptions);
3031

32+
if (isSortImportsIgnored(ast.program.body[0]?.leadingComments ?? []))
33+
return code;
34+
3135
const {
3236
importNodes,
3337
injectIdx,
@@ -36,7 +40,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
3640

3741
// short-circuit if there are no import declaration
3842
if (importNodes.length === 0) return code;
39-
if (isSortImportsIgnored(importNodes)) return code;
43+
if (isSortImportsIgnored(getAllCommentsFromNodes(importNodes))) return code;
4044

4145
const allImports = getSortedNodes(importNodes, {
4246
importOrder,

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect, test } from 'vitest';
22

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

@@ -15,11 +16,15 @@ import z from 'z';
1516
test('it should return true if specific leading comment detected', () => {
1617
const importNodes = getImportNodes(codeIgnored);
1718

18-
expect(isSortImportsIgnored(importNodes)).toBeTruthy();
19+
expect(
20+
isSortImportsIgnored(getAllCommentsFromNodes(importNodes)),
21+
).toBeTruthy();
1922
});
2023

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

24-
expect(isSortImportsIgnored(importNodes)).toBeFalsy();
27+
expect(
28+
isSortImportsIgnored(getAllCommentsFromNodes(importNodes)),
29+
).toBeFalsy();
2530
});
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import { Statement } from '@babel/types';
1+
import { Comment } from '@babel/types';
22

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

6-
export const isSortImportsIgnored = (nodes: Statement[]) =>
7-
getAllCommentsFromNodes(nodes).some(
8-
(comment) =>
9-
comment.loc &&
10-
comment.loc.start.line === 1 &&
11-
comment.value.includes(sortImportsIgnoredComment),
5+
export const isSortImportsIgnored = (comments: Comment[]) => {
6+
return comments.some((comment) =>
7+
comment.value.includes(sortImportsIgnoredComment),
128
);
9+
};

0 commit comments

Comments
 (0)