Skip to content
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

Yet another <SEPARATOR> feature PR #339

Open
wants to merge 2 commits into
base: v6
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ between sorted import declarations group. The separation takes place according t
"importOrderSeparation": true,
```

If this option is enabled and `<SEPARATOR>` is used in the `importOrder` array, the plugin
will ONLY add newlines at those locations and at the end of the imports.

#### `importOrderSortSpecifiers`

**type**: `boolean`
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export const THIRD_PARTY_MODULES_SPECIAL_WORD = '<THIRD_PARTY_MODULES>';

export const THIRD_PARTY_TYPES_SPECIAL_WORD = '<THIRD_PARTY_TS_TYPES>';
export const TYPES_SPECIAL_WORD = '<TS_TYPES>';
export const SEPARATOR_SPECIAL_WORD = '<SEPARATOR>';

const PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE =
export const PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE =
'PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE';

export const newLineNode = expressionStatement(
Expand Down
138 changes: 138 additions & 0 deletions src/utils/__tests__/get-sorted-nodes-by-import-order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getImportNodes } from '../get-import-nodes';
import { getSortedNodes } from '../get-sorted-nodes';
import { getSortedNodesModulesNames } from '../get-sorted-nodes-modules-names';
import { getSortedNodesNames } from '../get-sorted-nodes-names';
import { PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE } from "../../constants"
import { ImportOrLine } from "../../types"

const code = `// first comment
// second comment
Expand Down Expand Up @@ -337,3 +339,139 @@ test('it returns all sorted nodes with namespace specifiers at the top', () => {
'z',
]);
});

test('it returns the default separations if `importOrderSeparation` is false', () => {
const result = getImportNodes(code);
const sorted = getSortedNodes(result, {
importOrder: ['<SEPARATOR>', '^a$', '^t$', '<SEPARATOR>', '^k$', '^B', '<SEPARATOR>'],
importOrderCaseInsensitive: false,
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSideEffects: true,
});
expect(getSeparationData(sorted)).toEqual([
{ type: "ImportDeclaration", value: 'XY' },
{ type: "ImportDeclaration", value: 'Xa' },
{ type: "ImportDeclaration", value: 'c' },
{ type: "ImportDeclaration", value: 'g' },
{ type: "ImportDeclaration", value: 'x' },
{ type: "ImportDeclaration", value: 'z' },
{ type: "ImportDeclaration", value: 'a' },
{ type: "ImportDeclaration", value: 't' },
{ type: "ImportDeclaration", value: 'k' },
{ type: "ImportDeclaration", value: 'BY' },
{ type: "ImportDeclaration", value: 'Ba' },
{ type: "ExpressionStatement", value: undefined },
]);
});

test('it returns default import module separations', () => {
const result = getImportNodes(code);
const sorted = getSortedNodes(result, {
importOrder: ['^a$', '^t$', '^k$', '^B'],
importOrderCaseInsensitive: false,
importOrderSeparation: true,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSideEffects: true,
});
expect(getSeparationData(sorted)).toEqual([
{ type: "ImportDeclaration", value: 'XY' },
{ type: "ImportDeclaration", value: 'Xa' },
{ type: "ImportDeclaration", value: 'c' },
{ type: "ImportDeclaration", value: 'g' },
{ type: "ImportDeclaration", value: 'x' },
{ type: "ImportDeclaration", value: 'z' },
{ type: "ExpressionStatement", value: undefined },
{ type: "ImportDeclaration", value: 'a' },
{ type: "ExpressionStatement", value: undefined },
{ type: "ImportDeclaration", value: 't' },
{ type: "ExpressionStatement", value: undefined },
{ type: "ImportDeclaration", value: 'k' },
{ type: "ExpressionStatement", value: undefined },
{ type: "ImportDeclaration", value: 'BY' },
{ type: "ImportDeclaration", value: 'Ba' },
{ type: "ExpressionStatement", value: undefined },
{ type: "ExpressionStatement", value: undefined },
]);
});

test('it returns targeted import module separations', () => {
const result = getImportNodes(code);
const sorted = getSortedNodes(result, {
importOrder: ['^a$', '<SEPARATOR>', '^t$', '<SEPARATOR>', '^k$', '^B'],
importOrderCaseInsensitive: false,
importOrderSeparation: true,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSideEffects: true,
});
expect(getSeparationData(sorted)).toEqual([
{ type: "ImportDeclaration", value: 'XY' },
{ type: "ImportDeclaration", value: 'Xa' },
{ type: "ImportDeclaration", value: 'c' },
{ type: "ImportDeclaration", value: 'g' },
{ type: "ImportDeclaration", value: 'x' },
{ type: "ImportDeclaration", value: 'z' },
{ type: "ImportDeclaration", value: 'a' },
{ type: "ExpressionStatement", value: undefined },
{ type: "ImportDeclaration", value: 't' },
{ type: "ExpressionStatement", value: undefined },
{ type: "ImportDeclaration", value: 'k' },
{ type: "ImportDeclaration", value: 'BY' },
{ type: "ImportDeclaration", value: 'Ba' },
{ type: "ExpressionStatement", value: undefined },
]);
});

test('it never returns a separation at the top of the list (leading separator)', () => {
const result = getImportNodes(`
import './test';
`.trim());
const sorted = getSortedNodes(result, {
importOrder: ['<SEPARATOR>', '^[./]'],
importOrderCaseInsensitive: false,
importOrderSeparation: true,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSideEffects: true,
});
expect(getSeparationData(sorted)).toEqual([
{ type: "ImportDeclaration", value: './test' },
{ type: "ExpressionStatement", value: undefined },
]);
});

test('it never returns a separation at the top of the list (zero preceding imports)', () => {
const result = getImportNodes(`
import './test';
`.trim());
const sorted = getSortedNodes(result, {
importOrder: ['^a.*$', '<SEPARATOR>', '^[./]'],
importOrderCaseInsensitive: false,
importOrderSeparation: true,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSideEffects: true,
});
expect(getSeparationData(sorted)).toEqual([
{ type: "ImportDeclaration", value: './test' },
{ type: "ExpressionStatement", value: undefined },
]);
});

// Focuses the nodes solely to the import declarations and the new lines
function getSeparationData(nodes: ImportOrLine[]): { type: "ImportDeclaration" | "ExpressionStatement", value?: string }[] {
return nodes
.filter(node => node.type === "ImportDeclaration"
|| (
node.type === "ExpressionStatement"
&& node.expression.type === "StringLiteral"
&& node.expression.value === PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE
))
.map(x => ({
type: x.type,
value: x.type === "ImportDeclaration" ? x.source.value : undefined
}));
}
10 changes: 8 additions & 2 deletions src/utils/get-sorted-nodes-by-import-order.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { clone } from 'lodash';

import { THIRD_PARTY_MODULES_SPECIAL_WORD, newLineNode } from '../constants';
import { THIRD_PARTY_MODULES_SPECIAL_WORD, newLineNode, SEPARATOR_SPECIAL_WORD } from '../constants';
import { naturalSort } from '../natural-sort';
import { GetSortedNodes, ImportGroups, ImportOrLine } from '../types';
import { getImportNodesMatchedGroup } from './get-import-nodes-matched-group';
Expand Down Expand Up @@ -51,9 +51,14 @@ export const getSortedNodesByImportOrder: GetSortedNodes = (nodes, options) => {
importOrderGroups[matchedGroup].push(node);
}

const hasUserProvidedSeparators = options.importOrder.includes(SEPARATOR_SPECIAL_WORD);
let safeToAddNewLine = false;
for (const group of importOrder) {
const groupNodes = importOrderGroups[group];

if (importOrderSeparation && group === SEPARATOR_SPECIAL_WORD && safeToAddNewLine) {
finalNodes.push(newLineNode);
}
if (groupNodes.length === 0) continue;

const sortedInsideGroup = getSortedNodesGroup(groupNodes, {
Expand All @@ -68,8 +73,9 @@ export const getSortedNodesByImportOrder: GetSortedNodes = (nodes, options) => {
}

finalNodes.push(...sortedInsideGroup);
safeToAddNewLine = true;

if (importOrderSeparation) {
if (importOrderSeparation && !hasUserProvidedSeparators) {
finalNodes.push(newLineNode);
}
}
Expand Down
Loading
Loading