Skip to content

Commit

Permalink
Merge pull request #45 from mskelton/import-separator
Browse files Browse the repository at this point in the history
Import separators
  • Loading branch information
mskelton committed Mar 11, 2023
2 parents e5a3793 + 057ac91 commit 4b26875
Show file tree
Hide file tree
Showing 23 changed files with 1,305 additions and 2,611 deletions.
19 changes: 2 additions & 17 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,13 @@
"prettier"
],
"env": {
"es6": true,
"node": true
},
"rules": {
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_"
}
{ "argsIgnorePattern": "^_" }
]
},
"overrides": [
{
"files": "**/__tests__/**",
"env": {
"jest": true
},
"rules": {
"@typescript-eslint/no-var-requires": "off"
}
}
]
}
}
5 changes: 5 additions & 0 deletions __mocks__/isomorphic-resolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function resolve(source) {
if (!source.startsWith("dependency-")) {
throw new Error(`Cannot resolve "${source}"`)
}
}
4 changes: 2 additions & 2 deletions docs/rules/destructuring-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ insensitive in ascending order.

Examples of **incorrect** code for this rule:

```js
```javascript
let { b, c, a } = {}
let { C, b } = {}
let { b: a, a: b } = {}
```

Examples of **correct** code for this rule:

```js
```javascript
let { a, b, c } = {}
let { b, C } = {}
let { a: b, b: a } = {}
Expand Down
4 changes: 2 additions & 2 deletions docs/rules/export-members.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Sorts export members alphabetically and case insensitive in ascending order.

Examples of **incorrect** code for this rule:

```js
```javascript
export { b, c, a } from "a"
export { C, b } from "a"
export { b as a, a as b } from "a"
```

Examples of **correct** code for this rule:

```js
```javascript
export { a, b, c } from "a"
export { b, C } from "a"
export { a as b, b as a } from "a"
Expand Down
6 changes: 3 additions & 3 deletions docs/rules/exports.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Sorts exports alphabetically and case insensitive in ascending order.

Example of **incorrect** code for this rule:

```js
```javascript
export { c } from "./c"
export default React
export * from "./b"
Expand All @@ -19,7 +19,7 @@ export { a } from "./a"

Example of **correct** code for this rule:

```js
```javascript
export { a } from "./a"
export * from "./b"
export { c } from "./c"
Expand Down Expand Up @@ -77,7 +77,7 @@ groups as well as a custom regex sort group.

This configuration would result in the following output.

```js
```javascript
export { a }
export { useState } from "react"
export App from "~/components"
Expand Down
4 changes: 2 additions & 2 deletions docs/rules/import-members.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Sorts import members alphabetically and case insensitive in ascending order.

Examples of **incorrect** code for this rule:

```js
```javascript
import { b, c, a } from "a"
import { C, b } from "a"
import { b as a, a as b } from "a"
```

Examples of **correct** code for this rule:

```js
```javascript
import { a, b, c } from "a"
import { b, C } from "a"
import { a as b, b as a } from "a"
Expand Down
49 changes: 46 additions & 3 deletions docs/rules/imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Sorts imports alphabetically and case insensitive in ascending order.

Example of **incorrect** code for this rule:

```js
```javascript
import c from "c"
import b from "b"
import a from "a"
```

Example of **correct** code for this rule:

```js
```javascript
import a from "a"
import b from "b"
import c from "c"
Expand All @@ -28,6 +28,7 @@ import c from "c"
This rule has an object with its properties as:

- `"groups"` (default: `[]`)
- `"separator"` (default: `""`)

### Groups

Expand Down Expand Up @@ -69,7 +70,7 @@ groups as well as a custom regex sort group.

This configuration would result in the following output.

```js
```javascript
import "index.css"
import React from "react"
import { createStore } from "redux"
Expand Down Expand Up @@ -101,6 +102,48 @@ The configuration example above shows how this works where the static asset
imports are the second sort group even though they have the highest order and
are thus the last sort group in the resulting code.

### Separator

You can customize the separator between sort groups using the `separator`
option. By default, there is no separator but you can specify one or more
newlines between sort groups.

```json
{
"sort/imports": [
"warn",
{
"groups": [
{ "type": "side-effect", "order": 1 },
{ "type": "other", "order": 3 }
],
"separator": "\n"
}
]
}
```

This configuration would result in the following output.

```javascript
import "index.css"

import React from "react"
import { createStore } from "redux"
import c from "c"

import a from "../a"
import b from "./b"

import image1 from "my-library/static/image.svg"
import image2 from "static/image.jpg"
import image3 from "static/image.png"
```

Note that the separator only applies if you have defined sort groups.
Additionally, extra newlines between imports in the _same sort group_ will be
removed.

## When Not To Use It

This rule is a formatting preference and not following it won't negatively
Expand Down
4 changes: 2 additions & 2 deletions docs/rules/object-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Sorts object properties alphabetically and case insensitive in ascending order.

Examples of **incorrect** code for this rule:

```js
```javascript
var a = { b: 1, c: 2, a: 3 }
var a = { C: 1, b: 2 }
var a = { C: 1, b: { y: 1, x: 2 } }
```

Examples of **correct** code for this rule:

```js
```javascript
var a = { a: 1, b: 2, c: 3 }
var a = { b: 1, C: 2 }
var a = { b: { x: 1, y: 2 }, C: 1 }
Expand Down
19 changes: 0 additions & 19 deletions jest.config.js

This file was deleted.

15 changes: 5 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"build": "tsup ./src/index.ts --format cjs,esm",
"format": "prettier --write .",
"lint": "eslint . --ext .ts",
"test": "NODE_OPTIONS=--experimental-vm-modules pnpm jest",
"test": "vitest",
"ts": "tsc"
},
"type": "module",
Expand All @@ -41,27 +41,22 @@
"natural-compare": "^1.4.0"
},
"devDependencies": {
"@babel/cli": "^7.21.0",
"@babel/core": "^7.21.0",
"@babel/preset-env": "^7.20.2",
"@babel/preset-typescript": "^7.21.0",
"@jest/globals": "^29.5.0",
"@mskelton/tsconfig": "^2.0.0",
"@types/dedent": "^0.7.0",
"@types/eslint": "^8.21.1",
"@types/estree": "^1.0.0",
"@types/jest": "^29.4.0",
"@types/natural-compare": "^1.4.1",
"@types/node": "^18.15.0",
"@typescript-eslint/eslint-plugin": "^5.54.1",
"@typescript-eslint/parser": "^5.54.1",
"babel-jest": "^29.5.0",
"dedent": "^0.7.0",
"eslint": "^8.36.0",
"eslint-config-prettier": "^8.7.0",
"jest": "^29.5.0",
"prettier": "^2.8.4",
"semantic-release": "^20.1.1",
"ts-jest": "^29.0.5",
"tsup": "^6.6.3",
"typescript": "^4.9.5"
"typescript": "^4.9.5",
"vitest": "^0.29.2"
}
}

0 comments on commit 4b26875

Please sign in to comment.