Skip to content

Commit

Permalink
feat: add support for flat config (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored Jul 1, 2024
1 parent 4dfc8a7 commit ecbce9f
Show file tree
Hide file tree
Showing 18 changed files with 277 additions and 111 deletions.
10 changes: 10 additions & 0 deletions .eslint-doc-generatorrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('eslint-doc-generator').GenerateOptions} */
module.exports = {
configEmoji: [
['recommended', '✅'],
['flat/recommended', '✅'],
],
postprocess: (doc) => {
return doc.replace(/✅\s*✅/gu, '✅')
},
}
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,23 @@ Then configure the rules you want to use under the rules section.

or start with the recommended rule set:

```json
{
"extends": ["plugin:promise/recommended"]
}
```
- `eslint.config.js`:

```js
import pluginPromise from 'eslint-plugin-promise'
export default [
// ...
pluginPromise.configs['flat/recommended'],
]
```

- `.eslintrc.*`:

```json
{
"extends": ["plugin:promise/recommended"]
}
```

## Rules

Expand All @@ -81,9 +93,9 @@ or start with the recommended rule set:
💼 Configurations enabled in.\
⚠️ Configurations set to warn in.\
🚫 Configurations disabled in.\
Set in the `flat/recommended` configuration.\
Set in the `recommended` configuration.\
🔧 Automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).

| Name                      | Description | 💼 | ⚠️ | 🚫 | 🔧 |
| :------------------------------------------------------------------- | :------------------------------------------------------------------------------------- | :-- | :-- | :-- | :-- |
Expand Down
8 changes: 8 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@
test('can require index file', () => {
expect(require('../index')).toBeInstanceOf(Object)
})

test('rule set', () => {
const plugin = require('../index')
expect(plugin.configs.recommended.rules).toEqual(
plugin.configs['flat/recommended'].rules
)
expect(plugin.configs['flat/recommended'].plugins.promise).toBe(plugin)
})
3 changes: 2 additions & 1 deletion docs/rules/always-return.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Require returning inside each `then()` to create readable and reusable Promise chains (`promise/always-return`)

💼 This rule is enabled in the ✅ `recommended` config.
💼 This rule is enabled in the following configs: ✅ `flat/recommended`, ✅
`recommended`.

<!-- end auto-generated rule header -->

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/avoid-new.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Disallow creating `new` promises outside of utility libs (use [pify][] instead) (`promise/avoid-new`)

🚫 This rule is _disabled_ in the ✅ `recommended` config.
🚫 This rule is _disabled_ in the following configs: ✅ `flat/recommended`, ✅
`recommended`.

<!-- end auto-generated rule header -->

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/catch-or-return.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Enforce the use of `catch()` on un-returned promises (`promise/catch-or-return`)

💼 This rule is enabled in the ✅ `recommended` config.
💼 This rule is enabled in the following configs: ✅ `flat/recommended`, ✅
`recommended`.

<!-- end auto-generated rule header -->

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/no-callback-in-promise.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Disallow calling `cb()` inside of a `then()` (use [nodeify][] instead) (`promise/no-callback-in-promise`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: ✅ `flat/recommended`, ✅
`recommended`.

<!-- end auto-generated rule header -->

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/no-native.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Require creating a `Promise` constructor before using it in an ES5 environment (`promise/no-native`)

🚫 This rule is _disabled_ in the ✅ `recommended` config.
🚫 This rule is _disabled_ in the following configs: ✅ `flat/recommended`, ✅
`recommended`.

<!-- end auto-generated rule header -->

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/no-nesting.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Disallow nested `then()` or `catch()` statements (`promise/no-nesting`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: ✅ `flat/recommended`, ✅
`recommended`.

<!-- end auto-generated rule header -->

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/no-new-statics.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Disallow calling `new` on a Promise static method (`promise/no-new-statics`)

💼 This rule is enabled in the ✅ `recommended` config.
💼 This rule is enabled in the following configs: ✅ `flat/recommended`, ✅
`recommended`.

🔧 This rule is automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
Expand Down
3 changes: 2 additions & 1 deletion docs/rules/no-promise-in-callback.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Disallow using promises inside of callbacks (`promise/no-promise-in-callback`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: ✅ `flat/recommended`, ✅
`recommended`.

<!-- end auto-generated rule header -->
3 changes: 2 additions & 1 deletion docs/rules/no-return-in-finally.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Disallow return statements in `finally()` (`promise/no-return-in-finally`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: ✅ `flat/recommended`, ✅
`recommended`.

<!-- end auto-generated rule header -->

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/no-return-wrap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Disallow wrapping values in `Promise.resolve` or `Promise.reject` when not needed (`promise/no-return-wrap`)

💼 This rule is enabled in the ✅ `recommended` config.
💼 This rule is enabled in the following configs: ✅ `flat/recommended`, ✅
`recommended`.

<!-- end auto-generated rule header -->

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/param-names.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Enforce consistent param names and ordering when creating new promises (`promise/param-names`)

💼 This rule is enabled in the ✅ `recommended` config.
💼 This rule is enabled in the following configs: ✅ `flat/recommended`, ✅
`recommended`.

<!-- end auto-generated rule header -->

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/valid-params.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Enforces the proper number of arguments are passed to Promise functions (`promise/valid-params`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: ✅ `flat/recommended`, ✅
`recommended`.

<!-- end auto-generated rule header -->

Expand Down
45 changes: 26 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
'use strict'

module.exports = {
const recommendedRules = {
'promise/always-return': 'error',
'promise/no-return-wrap': 'error',
'promise/param-names': 'error',
'promise/catch-or-return': 'error',
'promise/no-native': 'off',
'promise/no-nesting': 'warn',
'promise/no-promise-in-callback': 'warn',
'promise/no-callback-in-promise': 'warn',
'promise/avoid-new': 'off',
'promise/no-new-statics': 'error',
'promise/no-return-in-finally': 'warn',
'promise/valid-params': 'warn',
}

const pluginPromise = {
rules: {
'param-names': require('./rules/param-names'),
'no-return-wrap': require('./rules/no-return-wrap'),
Expand All @@ -25,23 +40,15 @@ module.exports = {
'no-native': 0,
'catch-or-return': 1,
},
configs: {
recommended: {
plugins: ['promise'],
rules: {
'promise/always-return': 'error',
'promise/no-return-wrap': 'error',
'promise/param-names': 'error',
'promise/catch-or-return': 'error',
'promise/no-native': 'off',
'promise/no-nesting': 'warn',
'promise/no-promise-in-callback': 'warn',
'promise/no-callback-in-promise': 'warn',
'promise/avoid-new': 'off',
'promise/no-new-statics': 'error',
'promise/no-return-in-finally': 'warn',
'promise/valid-params': 'warn',
},
},
}
pluginPromise.configs = {
recommended: {
plugins: ['promise'],
rules: recommendedRules,
},
'flat/recommended': {
plugins: { promise: pluginPromise },
rules: recommendedRules,
},
}
module.exports = pluginPromise
Loading

0 comments on commit ecbce9f

Please sign in to comment.