Skip to content

Commit

Permalink
feat!: make new config export
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jul 22, 2024
1 parent c0e8ab7 commit 2a7eec2
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 126 deletions.
8 changes: 0 additions & 8 deletions configs/recommended-alphabetical.ts

This file was deleted.

8 changes: 0 additions & 8 deletions configs/recommended-line-length.ts

This file was deleted.

8 changes: 0 additions & 8 deletions configs/recommended-natural.ts

This file was deleted.

6 changes: 3 additions & 3 deletions docs/content/configs/recommended-alphabetical.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ It makes it just a tiny bit faster to find a declaration in a large list. Rememb
{
source: dedent`
// eslint.config.js
import perfectionistAlphabetical from 'eslint-plugin-perfectionist/configs/recommended-alphabetical'
import perfectionist from 'eslint-plugin-perfectionist'
export default [
perfectionistAlphabetical,
perfectionist.configs['recommended-alphabetical'],
]
`,
name: 'Flat Config',
Expand All @@ -41,7 +41,7 @@ It makes it just a tiny bit faster to find a declaration in a large list. Rememb
// .eslintrc.js
export default {
extends: [
'plugin:perfectionist/recommended-alphabetical',
'plugin:perfectionist/recommended-alphabetical-legacy',
],
}
`,
Expand Down
6 changes: 3 additions & 3 deletions docs/content/configs/recommended-line-length.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ This configuration will make your code prettier and more pleasing to the eye.
{
source: dedent`
// eslint.config.js
import perfectionistLineLength from 'eslint-plugin-perfectionist/configs/recommended-line-length'
import perfectionist from 'eslint-plugin-perfectionist'
export default [
perfectionistLineLength,
perfectionist.configs['recommended-line-length'],
]
`,
name: 'Flat Config',
Expand All @@ -50,7 +50,7 @@ This configuration will make your code prettier and more pleasing to the eye.
// .eslintrc.js
export default {
extends: [
'plugin:perfectionist/recommended-line-length',
'plugin:perfectionist/recommended-line-length-legacy',
],
}
`,
Expand Down
6 changes: 3 additions & 3 deletions docs/content/configs/recommended-natural.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ This configuration will allow you to navigate through your code faster because a
{
source: dedent`
// eslint.config.js
import perfectionistNatural from 'eslint-plugin-perfectionist/configs/recommended-natural'
import perfectionist from 'eslint-plugin-perfectionist'
export default [
perfectionistNatural,
perfectionist.configs['recommended-natural'],
]
`,
name: 'Flat Config',
Expand All @@ -48,7 +48,7 @@ This configuration will allow you to navigate through your code faster because a
// .eslintrc.js
export default {
extends: [
'plugin:perfectionist/recommended-natural',
'plugin:perfectionist/recommended-natural-legacy',
],
}
`,
Expand Down
108 changes: 65 additions & 43 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,42 @@ import sortClasses, { RULE_NAME as sortClassesName } from './rules/sort-classes'
import sortEnums, { RULE_NAME as sortEnumsName } from './rules/sort-enums'
import sortMaps, { RULE_NAME as sortMapsName } from './rules/sort-maps'

interface BaseOptions {
type: 'alphabetical' | 'line-length' | 'natural'
order: 'desc' | 'asc'
}

type RuleSeverity = 'error' | 'warn' | 'off'

type RuleDeclaration = [RuleSeverity, { [key: string]: unknown }?]

let createConfigWithOptions = (options: {
type: 'alphabetical' | 'line-length' | 'natural'
order: 'desc' | 'asc'
ignoreCase?: boolean
}): {
let plugin = {
rules: {
[key: string]: RuleDeclaration
}
plugins: ['perfectionist']
[sortIntersectionTypesName]: sortIntersectionTypes,
[sortSvelteAttributesName]: sortSvelteAttributes,
[sortAstroAttributesName]: sortAstroAttributes,
[sortArrayIncludesName]: sortArrayIncludes,
[sortVueAttributesName]: sortVueAttributes,
[sortNamedExportsName]: sortNamedExports,
[sortNamedImportsName]: sortNamedImports,
[sortObjectTypesName]: sortObjectTypes,
[sortInterfacesName]: sortInterfaces,
[sortUnionTypesName]: sortUnionTypes,
[sortJsxPropsName]: sortJsxProps,
[sortClassesName]: sortClasses,
[sortExportsName]: sortExports,
[sortImportsName]: sortImports,
[sortObjectsName]: sortObjects,
[sortEnumsName]: sortEnums,
[sortMapsName]: sortMaps,
},
name: 'perfectionist',
}

let getRules = (
options: BaseOptions,
): {
[key: string]: RuleDeclaration
} => {
let recommendedRules: {
[key: string]: RuleDeclaration
Expand Down Expand Up @@ -97,53 +120,52 @@ let createConfigWithOptions = (options: {
[sortEnumsName]: ['error'],
[sortMapsName]: ['error'],
}
return {
rules: Object.fromEntries(
Object.entries(recommendedRules).map(([key, [message, baseOptions = {}]]) => [
`perfectionist/${key}`,
[message, Object.assign(baseOptions, options)],
]),
),
plugins: ['perfectionist'],
}
return Object.fromEntries(
Object.entries(recommendedRules).map(([key, [message, baseOptions = {}]]) => [
`perfectionist/${key}`,
[message, Object.assign(baseOptions, options)],
]),
)
}

/* eslint-disable perfectionist/sort-objects */
export default {
rules: {
[sortArrayIncludesName]: sortArrayIncludes,
[sortAstroAttributesName]: sortAstroAttributes,
[sortClassesName]: sortClasses,
[sortEnumsName]: sortEnums,
[sortExportsName]: sortExports,
[sortImportsName]: sortImports,
[sortInterfacesName]: sortInterfaces,
[sortJsxPropsName]: sortJsxProps,
[sortMapsName]: sortMaps,
[sortNamedExportsName]: sortNamedExports,
[sortNamedImportsName]: sortNamedImports,
[sortObjectTypesName]: sortObjectTypes,
[sortObjectsName]: sortObjects,
[sortSvelteAttributesName]: sortSvelteAttributes,
[sortIntersectionTypesName]: sortIntersectionTypes,
[sortUnionTypesName]: sortUnionTypes,
[sortVueAttributesName]: sortVueAttributes,
let createConfig = (options: BaseOptions) => ({
plugins: {
perfectionist: plugin,
},
rules: getRules(options),
})

let createLegacyConfig = (options: BaseOptions) => ({
plugins: ['perfectionist'],
rules: getRules(options),
})

export default {
...plugin,
configs: {
'recommended-alphabetical': createConfigWithOptions({
'recommended-alphabetical-legacy': createLegacyConfig({
type: 'alphabetical',
order: 'asc',
ignoreCase: true,
}),
'recommended-natural': createConfigWithOptions({
'recommended-line-length-legacy': createLegacyConfig({
type: 'line-length',
order: 'desc',
}),
'recommended-natural-legacy': createLegacyConfig({
type: 'natural',
order: 'asc',
ignoreCase: true,
}),
'recommended-line-length': createConfigWithOptions({
'recommended-alphabetical': createConfig({
type: 'alphabetical',
order: 'asc',
}),
'recommended-line-length': createConfig({
type: 'line-length',
order: 'desc',
}),
'recommended-natural': createConfig({
type: 'natural',
order: 'asc',
}),
},
name: 'eslint-plugin-perfectionist',
}
12 changes: 0 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./configs/recommended-alphabetical": {
"require": "./dist/configs/recommended-alphabetical.js",
"import": "./dist/configs/recommended-alphabetical.mjs"
},
"./configs/recommended-line-length": {
"require": "./dist/configs/recommended-line-length.js",
"import": "./dist/configs/recommended-line-length.mjs"
},
"./configs/recommended-natural": {
"require": "./dist/configs/recommended-natural.js",
"import": "./dist/configs/recommended-natural.mjs"
},
"./package.json": "./package.json"
},
"peerDependenciesMeta": {
Expand Down
64 changes: 32 additions & 32 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,7 @@ npm install --save-dev eslint-plugin-perfectionist

Add `eslint-plugin-perfectionist` to the plugins section of the ESLint configuration file and define the list of rules you will use.

### Legacy Config ([`.eslintrc`](https://eslint.org/docs/latest/use/configure/configuration-files))

<!-- prettier-ignore -->
```json
{
"plugins": [
"perfectionist"
],
"rules": {
"perfectionist/sort-objects": [
"error",
{
"type": "natural",
"order": "asc"
}
]
}
}
```

### Flat Config ([`eslint.config.js`](https://eslint.org/docs/latest/use/configure/configuration-files-new)) (requires eslint >= v8.23.0)
### Flat Config ([`eslint.config.js`](https://eslint.org/docs/latest/use/configure/configuration-files))

```js
import perfectionist from 'eslint-plugin-perfectionist'
Expand All @@ -98,32 +78,52 @@ export default [
]
```

## ⚙️ Configs

The easiest way to use `eslint-plugin-perfectionist` is to use ready-made configs. Config files use all the rules of the current plugin, but you can override them.

### Legacy Config ([`.eslintrc`](https://eslint.org/docs/latest/use/configure/configuration-files))
### Legacy Config ([`.eslintrc`](https://eslint.org/docs/latest/use/configure/configuration-files-deprecated))

<!-- prettier-ignore -->
```json
{
"extends": [
"plugin:perfectionist/recommended-natural"
]
"plugins": [
"perfectionist"
],
"rules": {
"perfectionist/sort-objects": [
"error",
{
"type": "natural",
"order": "asc"
}
]
}
}
```

### Flat Config ([`eslint.config.js`](https://eslint.org/docs/latest/use/configure/configuration-files-new))
## ⚙️ Configs

The easiest way to use `eslint-plugin-perfectionist` is to use ready-made configs. Config files use all the rules of the current plugin, but you can override them.

### Flat Config ([`eslint.config.js`](https://eslint.org/docs/latest/use/configure/configuration-files))

<!-- prettier-ignore -->
```js
import perfectionistNatural from 'eslint-plugin-perfectionist/configs/recommended-natural'
import perfectionist from 'eslint-plugin-perfectionist'

export default [
perfectionistNatural,
perfectionist.configs['recommended-natural'],
]
```

### Legacy Config ([`.eslintrc`](https://eslint.org/docs/latest/use/configure/configuration-files-deprecated))

<!-- prettier-ignore -->
```json
{
"extends": [
"plugin:perfectionist/recommended-natural-legacy"
]
}
```

### List of Configs

| Name | Description |
Expand Down
7 changes: 1 addition & 6 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import path from 'node:path'
export default defineConfig({
build: {
lib: {
entry: [
path.resolve(__dirname, 'configs/recommended-alphabetical.ts'),
path.resolve(__dirname, 'configs/recommended-line-length.ts'),
path.resolve(__dirname, 'configs/recommended-natural.ts'),
path.resolve(__dirname, 'index.ts'),
],
fileName: (format, entryName) => {
let directory = ''

Expand All @@ -19,6 +13,7 @@ export default defineConfig({

return `${directory}${entryName}.${format === 'es' ? 'mjs' : 'js'}`
},
entry: path.resolve(__dirname, 'index.ts'),
name: 'eslint-plugin-perfectionist',
formats: ['cjs', 'es'],
},
Expand Down

0 comments on commit 2a7eec2

Please sign in to comment.