Skip to content

Commit

Permalink
Sort modules (#72)
Browse files Browse the repository at this point in the history
* Update configuration parameters and add sortModules

Co-authored-by: byara <[email protected]>
  • Loading branch information
ratierd and byara authored Oct 19, 2021
1 parent fe3707e commit 53d9789
Show file tree
Hide file tree
Showing 12 changed files with 5,791 additions and 1,279 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ uses [`new RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe
to evaluate regular expression. E.g. `node.source.value.match(new RegExp(val))` Here, `val`
is the string provided in import order.

#### `importOrderSeparation`
A boolean value to enable or disable the new line separation
between sorted import declarations. The separation takes place according to `importOrder`.

#### `importOrderCaseInsensitive`
A boolean value to enable case-insensitivity in the sorting algorithm
used to order imports within each match group.
Expand All @@ -71,6 +67,14 @@ import ExamplesList from './ExamplesList';
import ExampleView from './ExampleView';
```

#### `importOrderSeparation`
A boolean value to enable or disable the new line separation
between sorted import declarations. The separation takes place according to `importOrder`.

#### `sortModules`
A boolean value to enable or disable sorting of the imports module declarations.
It is disabled by default

#### `experimentalBabelParserPluginsList`
A collection of parser names for babel parser. The plugin passes this list to babel parser so it can understand the syntaxes used in the file being formatted. The plugin uses prettier itself to figure out the parser it needs to use but if that fails, you can use this field to enforce the usage of the plugins babel needs.

Expand Down
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { parsers as typescriptParsers } from 'prettier/parser-typescript';
import { preprocessor } from './preprocessor';

const options = {
experimentalBabelParserPluginsList: {
type: 'path',
category: 'Global',
array: true,
default: [{ value: [] }],
description: 'Provide a list of plugins for special syntax',
},
importOrder: {
type: 'path',
category: 'Global',
Expand All @@ -17,13 +24,12 @@ const options = {
default: false,
description: 'Should imports be separated by new line ?',
},
experimentalBabelParserPluginsList: {
type: 'path',
sortModules: {
type: 'boolean',
category: 'Global',
array: true,
default: [{ value: [] }],
description: 'Provide a list of plugins for special syntax',
}
default: false,
description: 'Should modules be sorted ?',
},
};

module.exports = {
Expand Down
13 changes: 7 additions & 6 deletions src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { getExperimentalParserPlugins } from './utils/get-experimental-parser-pl

export function preprocessor(code: string, options: PrettierOptions) {
const {
experimentalBabelParserPluginsList = [],
importOrder,
importOrderSeparation,
importOrderCaseInsensitive,
importOrderSeparation,
parser: prettierParser,
experimentalBabelParserPluginsList = [],
sortModules,
} = options;

const plugins = getParserPlugins(prettierParser);
Expand Down Expand Up @@ -44,12 +45,12 @@ export function preprocessor(code: string, options: PrettierOptions) {
// short-circuit if there are no import declaration
if (importNodes.length === 0) return code;

const allImports = getSortedNodes(
importNodes,
const allImports = getSortedNodes(importNodes, {
importOrder,
importOrderSeparation,
importOrderCaseInsensitive,
);
importOrderSeparation,
sortModules,
});

return getCodeFromAst(allImports, code, interpreter);
}
5 changes: 3 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { RequiredOptions } from 'prettier';

export interface PrettierOptions extends RequiredOptions {
experimentalBabelParserPluginsList: string[]; // should be of type ParserPlugin from '@babel/parser' but prettier does not support nested arrays in options
importOrder: string[];
importOrderSeparation: boolean;
importOrderCaseInsensitive: boolean;
experimentalBabelParserPluginsList: string[]; // should be of type ParserPlugin from '@babel/parser' but prettier does not support nested arrays in options
importOrderSeparation: boolean;
sortModules: boolean;
}
Loading

0 comments on commit 53d9789

Please sign in to comment.