Skip to content

fix: import sorting with interpreter command #348

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@ lib/
.idea

package-lock.json

# macOS utility files
.DS_Store
2 changes: 1 addition & 1 deletion src/preprocessors/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,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(importNodes, !!interpreter)) return code;

const allImports = getSortedNodes(importNodes, {
importOrder,
Expand Down
26 changes: 22 additions & 4 deletions src/utils/__tests__/is-sort-imports-ignored.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { extractASTNodes } from '../extract-ast-nodes';
import { getImportNodes } from '../get-import-nodes';
import { isSortImportsIgnored } from '../is-sort-imports-ignored';
import { parse } from '@babel/parser';

const codeIgnored = `// sort-imports-ignore
// second comment
Expand All @@ -10,14 +12,30 @@ const codeNotIgnored = `// second comment
import z from 'z';
`;

const codeIgnoredWithInterpreterCommand = `#!/usr/bin/env node
// sort-imports-ignore

import z from 'z';
`;

test('it should return true if specific leading comment detected', () => {
const importNodes = getImportNodes(codeIgnored);
const ast = parse(codeIgnored, { sourceType: 'module' });
const { importNodes } = extractASTNodes(ast);

expect(isSortImportsIgnored(importNodes)).toBeTruthy();
expect(isSortImportsIgnored(importNodes, !!ast.program.interpreter)).toBeTruthy();
});

test('it should return false if no specific leading comment detected', () => {
const importNodes = getImportNodes(codeNotIgnored);
const ast = parse(codeNotIgnored, { sourceType: 'module' });
const { importNodes } = extractASTNodes(ast);

expect(isSortImportsIgnored(importNodes)).toBeFalsy();
expect(isSortImportsIgnored(importNodes, !!ast.program.interpreter)).toBeFalsy();
});

test('it should return true if specific leading comment detected in a ts module', () => {
const ast = parse(codeIgnoredWithInterpreterCommand, { sourceType: 'module' });
const { importNodes } = extractASTNodes(ast);

expect(isSortImportsIgnored(importNodes, !!ast.program.interpreter)).toBeTruthy();
});

4 changes: 2 additions & 2 deletions src/utils/is-sort-imports-ignored.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Statement } from '@babel/types';
import { sortImportsIgnoredComment } from '../constants';
import { getAllCommentsFromNodes } from './get-all-comments-from-nodes';

export const isSortImportsIgnored = (nodes: Statement[]) =>
export const isSortImportsIgnored = (nodes: Statement[], hasInterpreter: boolean ) =>
getAllCommentsFromNodes(nodes).some(
(comment) =>
comment.loc &&
comment.loc.start.line === 1 &&
comment.loc.start.line === (hasInterpreter ? 2 : 1) &&
comment.value.includes(sortImportsIgnoredComment),
);