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

Introduce importOrderBuiltinModulesToTop for Node Builtins #10

Merged
merged 4 commits into from
May 12, 2022
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ with options as a JSON string of the plugin array:
importOrderParserPlugins: []
```


#### `importOrderBuiltinModulesToTop`

**type**: `boolean`

**default value:** `false`

A boolean value to enable sorting of builtins to the top of all import groups.


### How does import sort work ?

The plugin extracts the imports which are defined in `importOrder`. These imports are considered as _local imports_.
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ParserPlugin } from '@babel/parser';
import { expressionStatement, stringLiteral } from '@babel/types';
import { builtinModules } from 'module';

export const flow: ParserPlugin = 'flow';
export const typescript: ParserPlugin = 'typescript';
Expand All @@ -15,6 +16,7 @@ export const chunkTypeOther = 'other';
* where the not matched imports should be placed
*/
export const THIRD_PARTY_MODULES_SPECIAL_WORD = '<THIRD_PARTY_MODULES>';
export const BUILTIN_MODULES = builtinModules.join('|');

const PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE =
'PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE';
Expand Down
2 changes: 2 additions & 0 deletions src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function preprocessor(code: string, options: PrettierOptions): string {
importOrderSeparation,
importOrderGroupNamespaceSpecifiers,
importOrderSortSpecifiers,
importOrderBuiltinModulesToTop,
} = options;

const importNodes: ImportDeclaration[] = [];
Expand Down Expand Up @@ -50,6 +51,7 @@ export function preprocessor(code: string, options: PrettierOptions): string {
importOrderSeparation,
importOrderGroupNamespaceSpecifiers,
importOrderSortSpecifiers,
importOrderBuiltinModulesToTop,
});

return getCodeFromAst(allImports, code, directives, interpreter);
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { RequiredOptions } from 'prettier';
export interface PrettierOptions extends RequiredOptions {
importOrder: string[];
importOrderCaseInsensitive: boolean;
importOrderBuiltinModulesToTop: boolean;
// should be of type ParserPlugin from '@babel/parser' but prettier does not support nested arrays in options
importOrderParserPlugins: string[];
importOrderSeparation: boolean;
Expand All @@ -24,6 +25,7 @@ export type GetSortedNodes = (
options: Pick<
PrettierOptions,
| 'importOrder'
| 'importOrderBuiltinModulesToTop'
| 'importOrderCaseInsensitive'
| 'importOrderSeparation'
| 'importOrderGroupNamespaceSpecifiers'
Expand Down
1 change: 1 addition & 0 deletions src/utils/__tests__/get-all-comments-from-nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const getSortedImportNodes = (code: string, options?: ParserOptions) => {
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderBuiltinModulesToTop: false,
});
};

Expand Down
1 change: 1 addition & 0 deletions src/utils/__tests__/get-code-from-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import a from 'a';
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderBuiltinModulesToTop: false,
});
const formatted = getCodeFromAst(sortedNodes, code, [], undefined);
expect(format(formatted, { parser: 'babel' })).toEqual(
Expand Down
2 changes: 0 additions & 2 deletions src/utils/__tests__/get-import-nodes-matched-group.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { THIRD_PARTY_MODULES_SPECIAL_WORD } from '../../constants';
import { ImportGroups } from '../../types';
import { getImportNodes } from '../get-import-nodes';
import { getImportNodesMatchedGroup } from '../get-import-nodes-matched-group';

Expand Down
110 changes: 110 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 @@ -14,6 +14,9 @@ import { tC, tA, tB } from 't';
import k, { kE, kB } from 'k';
import * as a from 'a';
import * as x from 'x';
import path from 'path';
import url from 'node:url';
import * as fs from "node:fs/promises"
import BY from 'BY';
import Ba from 'Ba';
import XY from 'XY';
Expand All @@ -28,6 +31,7 @@ test('it returns all sorted nodes', () => {
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderBuiltinModulesToTop: false,
}) as ImportDeclaration[];

expect(getSortedNodesNames(sorted)).toEqual([
Expand All @@ -39,6 +43,9 @@ test('it returns all sorted nodes', () => {
'c',
'g',
'k',
'node:fs/promises',
'node:url',
'path',
't',
'x',
'z',
Expand All @@ -58,6 +65,9 @@ test('it returns all sorted nodes', () => {
['c', 'cD'],
['g'],
['k', 'kE', 'kB'],
['fs'],
['url'],
['path'],
['tC', 'tA', 'tB'],
['x'],
['z'],
Expand All @@ -72,6 +82,7 @@ test('it returns all sorted nodes case-insensitive', () => {
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderBuiltinModulesToTop: false,
}) as ImportDeclaration[];

expect(getSortedNodesNames(sorted)).toEqual([
Expand All @@ -81,6 +92,9 @@ test('it returns all sorted nodes case-insensitive', () => {
'c',
'g',
'k',
'node:fs/promises',
'node:url',
'path',
't',
'x',
'Xa',
Expand All @@ -100,6 +114,9 @@ test('it returns all sorted nodes case-insensitive', () => {
['c', 'cD'],
['g'],
['k', 'kE', 'kB'],
['fs'],
['url'],
['path'],
['tC', 'tA', 'tB'],
['x'],
['Xa'],
Expand All @@ -116,13 +133,17 @@ test('it returns all sorted nodes with sort order', () => {
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderBuiltinModulesToTop: false,
}) as ImportDeclaration[];

expect(getSortedNodesNames(sorted)).toEqual([
'XY',
'Xa',
'c',
'g',
'node:fs/promises',
'node:url',
'path',
'x',
'z',
'a',
Expand All @@ -142,6 +163,9 @@ test('it returns all sorted nodes with sort order', () => {
['Xa'],
['c', 'cD'],
['g'],
['fs'],
['url'],
['path'],
['x'],
['z'],
['a'],
Expand All @@ -160,10 +184,14 @@ test('it returns all sorted nodes with sort order case-insensitive', () => {
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderBuiltinModulesToTop: false,
}) as ImportDeclaration[];
expect(getSortedNodesNames(sorted)).toEqual([
'c',
'g',
'node:fs/promises',
'node:url',
'path',
'x',
'Xa',
'XY',
Expand All @@ -183,6 +211,9 @@ test('it returns all sorted nodes with sort order case-insensitive', () => {
).toEqual([
['c', 'cD'],
['g'],
['fs'],
['url'],
['path'],
['x'],
['Xa'],
['XY'],
Expand All @@ -203,12 +234,16 @@ test('it returns all sorted import nodes with sorted import specifiers', () => {
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: true,
importOrderBuiltinModulesToTop: false,
}) as ImportDeclaration[];
expect(getSortedNodesNames(sorted)).toEqual([
'XY',
'Xa',
'c',
'g',
'node:fs/promises',
'node:url',
'path',
'x',
'z',
'a',
Expand All @@ -228,6 +263,9 @@ test('it returns all sorted import nodes with sorted import specifiers', () => {
['Xa'],
['c', 'cD'],
['g'],
['fs'],
['url'],
['path'],
['x'],
['z'],
['a'],
Expand All @@ -246,10 +284,14 @@ test('it returns all sorted import nodes with sorted import specifiers with case
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: true,
importOrderBuiltinModulesToTop: false,
}) as ImportDeclaration[];
expect(getSortedNodesNames(sorted)).toEqual([
'c',
'g',
'node:fs/promises',
'node:url',
'path',
'x',
'Xa',
'XY',
Expand All @@ -269,6 +311,9 @@ test('it returns all sorted import nodes with sorted import specifiers with case
).toEqual([
['c', 'cD'],
['g'],
['fs'],
['url'],
['path'],
['x'],
['Xa'],
['XY'],
Expand All @@ -289,13 +334,17 @@ test('it returns all sorted nodes with custom third party modules', () => {
importOrderCaseInsensitive: true,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderBuiltinModulesToTop: false,
}) as ImportDeclaration[];
expect(getSortedNodesNames(sorted)).toEqual([
'a',
'Ba',
'BY',
'c',
'g',
'node:fs/promises',
'node:url',
'path',
'x',
'Xa',
'XY',
Expand All @@ -313,10 +362,12 @@ test('it returns all sorted nodes with namespace specifiers at the top', () => {
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: true,
importOrderSortSpecifiers: false,
importOrderBuiltinModulesToTop: false,
}) as ImportDeclaration[];

expect(getSortedNodesNames(sorted)).toEqual([
'a',
'node:fs/promises',
'x',
'BY',
'Ba',
Expand All @@ -325,7 +376,66 @@ test('it returns all sorted nodes with namespace specifiers at the top', () => {
'c',
'g',
'k',
'node:url',
'path',
't',
'z',
]);
});

test('it returns all sorted nodes with builtin specifiers at the top, ', () => {
const result = getImportNodes(code);
const sorted = getSortedNodes(result, {
importOrder: [],
importOrderCaseInsensitive: false,
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderBuiltinModulesToTop: true,
}) as ImportDeclaration[];

expect(getSortedNodesNames(sorted)).toEqual([
'node:fs/promises',
'node:url',
'path',
'BY',
'Ba',
'XY',
'Xa',
'a',
'c',
'g',
'k',
't',
'x',
'z',
]);
});

test('it returns all sorted nodes with custom third party modules and builtins at top', () => {
const result = getImportNodes(code);
const sorted = getSortedNodes(result, {
importOrder: ['^a$', '<THIRD_PARTY_MODULES>', '^t$', '^k$'],
importOrderSeparation: false,
importOrderCaseInsensitive: true,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderBuiltinModulesToTop: true,
}) as ImportDeclaration[];
expect(getSortedNodesNames(sorted)).toEqual([
'node:fs/promises',
'node:url',
'path',
'a',
'Ba',
'BY',
'c',
'g',
'x',
'Xa',
'XY',
'z',
't',
'k',
]);
});
4 changes: 4 additions & 0 deletions src/utils/__tests__/get-sorted-nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import "se4";
import "se1";
import * as a from 'a';
import * as x from 'x';
import path from 'path';
import BY from 'BY';
import Ba from 'Ba';
import XY from 'XY';
Expand All @@ -32,6 +33,7 @@ test('it returns all sorted nodes, preserving the order side effect nodes', () =
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderBuiltinModulesToTop: false,
}) as ImportDeclaration[];

expect(getSortedNodesNames(sorted)).toEqual([
Expand All @@ -48,6 +50,7 @@ test('it returns all sorted nodes, preserving the order side effect nodes', () =
'XY',
'Xa',
'a',
'path',
'x',
'se2',
]);
Expand All @@ -71,6 +74,7 @@ test('it returns all sorted nodes, preserving the order side effect nodes', () =
['XY'],
['Xa'],
['a'],
['path'],
['x'],
[],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ test('it should remove nodes from the original code', () => {
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderBuiltinModulesToTop: false,
});
const allCommentsFromImports = getAllCommentsFromNodes(sortedNodes);

Expand Down
Loading