Skip to content

Commit

Permalink
Merge pull request #65 from risenforces/add-rest-special-word
Browse files Browse the repository at this point in the history
Add the ability to override default position of "3rd party" imports
  • Loading branch information
byara authored Oct 21, 2021
2 parents 25e89d4 + 9951ca8 commit 6c9bb36
Show file tree
Hide file tree
Showing 34 changed files with 4,304 additions and 1,905 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ module.exports = {
### APIs

#### `importOrder`
A collection of regular expressions in string format. The plugin
A collection of:
- Regular expressions in string format. The plugin
uses [`new RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)
to evaluate regular expression. E.g. `node.source.value.match(new RegExp(val))` Here, `val`
is the string provided in import order.
- Special words (optional):
- `<THIRD_PARTY_MODULES>` - marks the position between RegExps, where the not matched imports will be placed. By default, it's the top position (above all the groups in `importOrder`).

#### `importOrderCaseInsensitive`
A boolean value to enable case-insensitivity in the sorting algorithm
Expand Down Expand Up @@ -103,10 +106,12 @@ These imports are _local imports_. The imports which are not part of the
`importOrder` is considered as _3rd party imports_.

After, the plugin sorts the _local imports_ and _3rd party imports_ using
[natural sort algorithm](https://en.wikipedia.org/wiki/Natural_sort_order),
with case-insensitivity specified by `importOrderCaseInsensitive` (or false by default).
In the end, the plugin returns final imports with _3rd party imports_ on top and
_local imports_ at the end.

[natural sort algorithm](https://en.wikipedia.org/wiki/Natural_sort_order).

In the end, the plugin returns final imports with _3rd party imports_ on top and _local imports_ at the end.

The _3rd party imports_ position (it's top by default) can be overrided using the `<THIRD_PARTY_MODULES>` special word.

### FAQ / Troubleshooting

Expand Down Expand Up @@ -135,6 +140,20 @@ import z from '@server/z';
import s from './';
```

#### Q. How can I move `react` and `classnames` to place between my RegEx imports without hardcoding?
You can define the `<THIRD_PARTY_MODULES>` special word in the `importOrder`. For example above, the `importOrder` would be like `["^@ui/(.*)$", "^@server/(.*)$", "<THIRD_PARTY_MODULES>", '^[./]']`.
Now, the final output would be as follows:

```ecmascript 6
import p from '@ui/p';
import q from '@ui/q';
import a from '@server/a';
import z from '@server/z';
import classnames from 'classnames';
import React from 'react';
import s from './';
```

#### Q. How can I run examples in this project ?
There is a _examples_ directory. The examples file can be formatted by using
`npm run example` command.
Expand Down
6 changes: 6 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export const jsx: ParserPlugin = 'jsx';

export const newLineCharacters = '\n\n';

/*
* Used to mark the position between RegExps,
* where the not matched imports should be placed
*/
export const THIRD_PARTY_MODULES_SPECIAL_WORD = '<THIRD_PARTY_MODULES>'

const PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE =
'PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE';

Expand Down
Loading

0 comments on commit 6c9bb36

Please sign in to comment.