Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v3.2.x' into issue-110-preserver…
Browse files Browse the repository at this point in the history
…-side-effect-import-order
  • Loading branch information
blutorange committed Jan 18, 2022
2 parents 9a43ff2 + 41fceff commit 812cbab
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 42 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
## Changelog


---
### v3.2.0
#### New features
- Group Namespace specifiers [#105](https://github.com/trivago/prettier-plugin-sort-imports/pull/105) by [Mattinton](https://github.com/Mattinton)

#### Chores
- Clean up unit test and snapshot test
- Add contribution guidelines for bug fixes and new features

### v3.1.1

- Fixes package management [#100](https://github.com/trivago/prettier-plugin-sort-imports/issues/100)

### v3.1.0

Expand Down
66 changes: 40 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ A prettier plugin to sort import declarations by provided Regular Expression ord
**Note: If you are migrating from v2.x.x to v3.x.x, [Please Read Migration Guidelines](./docs/MIGRATION.md)**

### Input

![input](./public/images/input-v3-1.png)

### Output
![output](./public/images/output-v3-1.png)

![output](./public/images/output-v3-1.png)

### Install

Expand All @@ -25,7 +26,6 @@ or, using yarn
yarn add --dev @trivago/prettier-plugin-sort-imports
```


**Note: If you are migrating from v2.x.x to v3.x.x, [Please Read Migration Guidelines](./docs/MIGRATION.md)**

### Usage
Expand All @@ -47,18 +47,19 @@ module.exports = {

### APIs

#### **`importOrder`**
#### **`importOrder`**

**type**: `Array<string>`

A collection of Regular expressions in string format.
A collection of Regular expressions in string format.

```
"importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
```

_Default behavior:_ The plugin moves the third party imports to the top which are not part of the `importOrder` list.
_Default behavior:_ The plugin moves the third party imports to the top which are not part of the `importOrder` list.
To move the third party imports at desired place, you can use `<THIRD_PARTY_MODULES>` to assign third party imports to the appropriate position:

```
"importOrder": ["^@core/(.*)$", "<THIRD_PARTY_MODULES>", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
```
Expand All @@ -69,7 +70,7 @@ To move the third party imports at desired place, you can use `<THIRD_PARTY_MODU

**default value**: `false`

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

```
Expand All @@ -84,6 +85,13 @@ between sorted import declarations group. The separation takes place according t

A boolean value to enable or disable sorting of the specifiers in an import declarations.

#### `importOrderGroupNamespaceSpecifiers`

**type**: `boolean`

**default value:** `false`

A boolean value to enable or disable sorting the namespace specifiers to the top of the import group.

#### `importOrderCaseInsensitive`

Expand Down Expand Up @@ -116,31 +124,33 @@ import ExampleView from './ExampleView';

Previously known as `experimentalBabelParserPluginsList`.

A collection of plugins for babel parser. The plugin passes this list to babel parser, so it can understand the syntaxes
A collection of plugins 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 parser needs.

**To pass the plugins to babel parser**:

```
"importOrderParserPlugins" : ["classProperties", "decorators-legacy"]
```

**To pass the options to the babel parser plugins**: Since prettier options are limited to string, you can pass plugins
with options as a JSON string of the plugin array:
**To pass the options to the babel parser plugins**: Since prettier options are limited to string, you can pass plugins
with options as a JSON string of the plugin array:
`"[\"plugin-name\", { \"pluginOption\": true }]"`.

```
"importOrderParserPlugins" : ["classProperties", "[\"decorators\", { \"decoratorsBeforeExport\": true }]"]
```

**To disable default plugins for babel parser, pass an empty array**:

```
importOrderParserPlugins: []
```

### How does import sort work ?

The plugin extracts the imports which are defined in `importOrder`. These imports are considered as _local imports_.
The plugin extracts the imports which are defined in `importOrder`. These imports are considered as _local imports_.
The imports which are not part of the `importOrder` is considered as _third party imports_.

First, the plugin checks for
Expand Down Expand Up @@ -181,33 +191,37 @@ The _third party imports_ position (it's top by default) can be overridden using

Having some trouble or an issue ? You can check [FAQ / Troubleshooting section](./docs/TROUBLESHOOTING.md).


### Compatibility
| Framework | Supported | Note |
|-----------|------------------------------------------|------------------------------|
| JS with ES Modules | ✅ Everything | - |
| NodeJS with ES Modules | ✅ Everything | - |
| React | ✅ Everything | - |
| Angular | ✅ Everything | Supported through `importOrderParserPlugins` API |
| Vue | ⚠️ Soon to be supported. | Any contribution is welcome. |
| Svelte | ⚠️ Soon to be supported. | Any contribution is welcome. |

| Framework | Supported | Note |
| ---------------------- | ------------------------ | ------------------------------------------------ |
| JS with ES Modules | ✅ Everything | - |
| NodeJS with ES Modules | ✅ Everything | - |
| React | ✅ Everything | - |
| Angular | ✅ Everything | Supported through `importOrderParserPlugins` API |
| Vue | ⚠️ Soon to be supported. | Any contribution is welcome. |
| Svelte | ⚠️ Soon to be supported. | Any contribution is welcome. |

### Used by

Want to highlight your project or company ? Adding your project / company name will help plugin to gain attraction and contribution.
Feel free to make a Pull Request to add your project / company name.
- [trivago](https://company.trivago.com)
- ADD YOUR PROJECT / COMPANY NAME

- [trivago](https://company.trivago.com)
- ADD YOUR PROJECT / COMPANY NAME

### Contribution

For more information regarding contribution, please check the [Contributing Guidelines](./CONTRIBUTING.md). If you are trying to
debug some code in the plugin, check [Debugging Guidelines](./docs/DEBUG.md)

### Maintainers

| [Ayush Sharma](https://github.com/ayusharma) | [Behrang Yarahmadi](https://github.com/byara)
|---|---|
| ![ayusharma](https://avatars2.githubusercontent.com/u/6918450?s=120&v=4) | ![@byara](https://avatars2.githubusercontent.com/u/6979966?s=120&v=4)
| [@ayusharma_](https://twitter.com/ayusharma_) | [@behrang_y](https://twitter.com/behrang_y)
| [Ayush Sharma](https://github.com/ayusharma) | [Behrang Yarahmadi](https://github.com/byara) |
| ------------------------------------------------------------------------ | --------------------------------------------------------------------- |
| ![ayusharma](https://avatars2.githubusercontent.com/u/6918450?s=120&v=4) | ![@byara](https://avatars2.githubusercontent.com/u/6979966?s=120&v=4) |
| [@ayusharma\_](https://twitter.com/ayusharma_) | [@behrang_y](https://twitter.com/behrang_y) |

### Disclaimer
This plugin modifies the AST which is against the rules of prettier.

This plugin modifies the AST which is against the rules of prettier.
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ const options = {
default: false,
description: 'Should imports be separated by new line?',
},
importOrderGroupNamespaceSpecifiers: {
type: 'boolean',
category: 'Global',
default: false,
description:
'Should namespace specifiers be grouped at the top of their group?',
},
importOrderSortSpecifiers: {
type: 'boolean',
category: 'Global',
Expand Down
2 changes: 2 additions & 0 deletions src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
importOrder,
importOrderCaseInsensitive,
importOrderSeparation,
importOrderGroupNamespaceSpecifiers,
importOrderSortSpecifiers,
} = options;

Expand Down Expand Up @@ -43,6 +44,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
importOrder,
importOrderCaseInsensitive,
importOrderSeparation,
importOrderGroupNamespaceSpecifiers,
importOrderSortSpecifiers,
});

Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface PrettierOptions extends RequiredOptions {
// should be of type ParserPlugin from '@babel/parser' but prettier does not support nested arrays in options
importOrderParserPlugins: string[];
importOrderSeparation: boolean;
importOrderGroupNamespaceSpecifiers: boolean;
importOrderSortSpecifiers: boolean;
}

Expand All @@ -25,6 +26,7 @@ export type GetSortedNodes = (
| 'importOrder'
| 'importOrderCaseInsensitive'
| 'importOrderSeparation'
| 'importOrderGroupNamespaceSpecifiers'
| 'importOrderSortSpecifiers'
>,
) => ImportOrLine[];
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 @@ -12,6 +12,7 @@ const getSortedImportNodes = (code: string, options?: ParserOptions) => {
importOrder: [],
importOrderCaseInsensitive: false,
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: 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 @@ -19,6 +19,7 @@ import a from 'a';
importOrder: [],
importOrderCaseInsensitive: false,
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
});
const formatted = getCodeFromAst(sortedNodes, code, null);
Expand Down
Loading

0 comments on commit 812cbab

Please sign in to comment.