Skip to content

Commit

Permalink
Simplify Import Order Groups (#3)
Browse files Browse the repository at this point in the history
* Simplify imports

* Bump

* Add descriptions

* Tweaks

* Tweak

* Tweak

* Tweak
  • Loading branch information
PauloMFJ authored Jan 23, 2023
1 parent 87c38e7 commit 910a079
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 30 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,12 @@ This is used to enforce that all imports are used in the file. This is useful to
This is used to enforce a consistent import order. The following order has been defined:

1. External modules (e.g. `react`, `next` and `@buildinams/`)
2. `_server` imports
3. `_utils` imports
4. `_contexts` imports
5. `_hooks` imports
6. `_components` imports
7. `_styles` imports
8. Internal modules (e.g. `./`)

## Using with Prettier
2. Absolute imports (supports prefix of `_` and `~` e.g. `public/`, `_components` and `~/contexts`)
3. Relative imports (e.g. `../` and `./`)

**Note**: The biggest thing we enforce here is the use of prefixes (either `_` or `~`) for absolute imports. This is to make it clear that these are not external modules, and to make it easier to distinguish between the different modules when enforcing the import order.

## Using With Prettier

As you'll notice, this package doesn't contain any custom `.pretterrc.json` config to extend. We recommend not including it, and instead just inheriting the default config (by not creating / changing it).

Expand Down
18 changes: 0 additions & 18 deletions configs/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,6 @@ module.exports = {
// Turn on exports ordering
"simple-import-sort/exports": "error",

// Turn on imports ordering with custom groups
"simple-import-sort/imports": [
"error",
{
groups: [
["^react", "^@?\\w"],
["^_server"],
["^_utils"],
["^_contexts"],
["^_hooks"],
["^_components"],
["^_styles"],
["^\\."],
["^"],
],
},
],

// Prevent 'console.log(...)' from being committed
"no-console": ["error", { allow: ["warn", "error"] }],

Expand Down
15 changes: 15 additions & 0 deletions configs/next.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,20 @@ module.exports = {
rules: {
// Prevent 'forwardRef' from erroring
"react/display-name": "off",

// Turn on imports ordering with custom groups
"simple-import-sort/imports": [
"error",
{
groups: [
// Match 'react', 'next' and external imports
["^react", "^next", "^@?\\w"],
// Match 'public' and all absolute imports that begin with '_' or '~'
["^public", "[_~].*"],
// Match all relative imports that begin with '.'
["^\\."],
],
},
],
},
};
15 changes: 15 additions & 0 deletions configs/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,20 @@ module.exports = {

// Suppress missing 'import React' in files error
"react/react-in-jsx-scope": "off",

// Turn on imports ordering with custom groups
"simple-import-sort/imports": [
"error",
{
groups: [
// Match 'react' and external imports
["^react", "^@?\\w"],
// Match 'public' and all absolute imports that begin with '_' or '~'
["^public", "[_~].*"],
// Match all relative imports that begin with '.'
["^\\."],
],
},
],
},
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@buildinams/lint",
"description": "BiA's base lint config(s).",
"version": "0.0.2",
"version": "0.0.3",
"license": "MIT",
"author": "Build in Amsterdam <development@buildinamsterdam.com> (https://www.buildinamsterdam.com/)",
"main": "index.js",
Expand Down

0 comments on commit 910a079

Please sign in to comment.