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

[Feature] Add <SEPARATOR> special word #296

Open
wants to merge 7 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
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const chunkSideOtherNode = 'other-node';
* where the not matched imports should be placed
*/
export const THIRD_PARTY_MODULES_SPECIAL_WORD = '<THIRD_PARTY_MODULES>';
export const SEPARATOR_SPECIAL_WORD = '<SEPARATOR>';

export const THIRD_PARTY_TYPES_SPECIAL_WORD = '<THIRD_PARTY_TS_TYPES>';
export const TYPES_SPECIAL_WORD = '<TS_TYPES>';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/__tests__/get-code-from-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import a from 'a';`;
sourceType: 'module',
plugins: getExperimentalParserPlugins([]),
};
const ast = babelParser(code, parserOptions);
const ast: any = babelParser(code, parserOptions);
if (!ast) throw new Error('ast is null');
const { directives, importNodes } = extractASTNodes(ast);

Expand Down
14 changes: 12 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 @@ -40,7 +40,7 @@ export const getSortedNodesByImportOrder: GetSortedNodes = (nodes, options) => {
);

const importOrderWithOutThirdPartyPlaceholder = importOrder.filter(
(group) => group !== THIRD_PARTY_MODULES_SPECIAL_WORD,
(group) => group !== THIRD_PARTY_MODULES_SPECIAL_WORD && group !== SEPARATOR_SPECIAL_WORD,
);

for (const node of originalNodes) {
Expand All @@ -52,6 +52,16 @@ export const getSortedNodesByImportOrder: GetSortedNodes = (nodes, options) => {
}

for (const group of importOrder) {
if (group === SEPARATOR_SPECIAL_WORD) {
if (
finalNodes.length !== 0 &&
finalNodes[finalNodes.length - 1] !== newLineNode
) {
finalNodes.push(newLineNode);
}
continue;
}

const groupNodes = importOrderGroups[group];

if (groupNodes.length === 0) continue;
Expand Down
Loading
Loading