Skip to content

Commit

Permalink
Merge pull request #349 from rhysd/flat-config
Browse files Browse the repository at this point in the history
Add support for eslint flat config format
  • Loading branch information
lo1tuma committed Mar 7, 2024
2 parents 6d7a3e5 + 6c65ca6 commit 1b550d6
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 72 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -15,6 +15,8 @@ This plugin requires ESLint `4.0.0` or later.
npm install --save-dev eslint-plugin-mocha
```

### `.eslintrc.json`

Then add a reference to this plugin and selected rules in your eslint config:

```json
Expand All @@ -25,6 +27,19 @@ Then add a reference to this plugin and selected rules in your eslint config:
}
```

### `eslint.config.js` (requires eslint >= 8.23.0)

To use this plugin with [the new eslint configuration format (flat config)](https://eslint.org/docs/latest/use/configure/configuration-files-new):

```js
import mochaPlugin from 'eslint-plugin-mocha';

export default [
mochaPlugin.configs.flat.recommended // or `mochaPlugin.configs.flat.all` to enable all
// ... Your configurations here
];
```

### Plugin Settings

This plugin supports the following settings, which are used by multiple rules:
Expand Down
128 changes: 74 additions & 54 deletions index.js
@@ -1,6 +1,62 @@
'use strict';

module.exports = {
const globals = require('globals');

const allRules = {
'mocha/handle-done-callback': 'error',
'mocha/max-top-level-suites': 'error',
'mocha/no-async-describe': 'error',
'mocha/no-exclusive-tests': 'error',
'mocha/no-exports': 'error',
'mocha/no-global-tests': 'error',
'mocha/no-hooks': 'error',
'mocha/no-hooks-for-single-case': 'error',
'mocha/no-identical-title': 'error',
'mocha/no-mocha-arrows': 'error',
'mocha/no-nested-tests': 'error',
'mocha/no-pending-tests': 'error',
'mocha/no-return-and-callback': 'error',
'mocha/no-return-from-async': 'error',
'mocha/no-setup-in-describe': 'error',
'mocha/no-sibling-hooks': 'error',
'mocha/no-skipped-tests': 'error',
'mocha/no-synchronous-tests': 'error',
'mocha/no-top-level-hooks': 'error',
'mocha/prefer-arrow-callback': 'error',
'mocha/valid-suite-description': 'error',
'mocha/valid-test-description': 'error',
'mocha/no-empty-description': 'error',
'mocha/consistent-spacing-between-blocks': 'error'
};

const recommendedRules = {
'mocha/handle-done-callback': 'error',
'mocha/max-top-level-suites': [ 'error', { limit: 1 } ],
'mocha/no-async-describe': 'error',
'mocha/no-exclusive-tests': 'warn',
'mocha/no-exports': 'error',
'mocha/no-global-tests': 'error',
'mocha/no-hooks': 'off',
'mocha/no-hooks-for-single-case': 'off',
'mocha/no-identical-title': 'error',
'mocha/no-mocha-arrows': 'error',
'mocha/no-nested-tests': 'error',
'mocha/no-pending-tests': 'warn',
'mocha/no-return-and-callback': 'error',
'mocha/no-return-from-async': 'off',
'mocha/no-setup-in-describe': 'error',
'mocha/no-sibling-hooks': 'error',
'mocha/no-skipped-tests': 'warn',
'mocha/no-synchronous-tests': 'off',
'mocha/no-top-level-hooks': 'warn',
'mocha/prefer-arrow-callback': 'off',
'mocha/valid-suite-description': 'off',
'mocha/valid-test-description': 'off',
'mocha/no-empty-description': 'error',
'mocha/consistent-spacing-between-blocks': 'error'
};

const mod = {
rules: {
'handle-done-callback': require('./lib/rules/handle-done-callback'),
'max-top-level-suites': require('./lib/rules/max-top-level-suites'),
Expand Down Expand Up @@ -31,63 +87,27 @@ module.exports = {
all: {
env: { mocha: true },
plugins: [ 'mocha' ],
rules: {
'mocha/handle-done-callback': 'error',
'mocha/max-top-level-suites': 'error',
'mocha/no-async-describe': 'error',
'mocha/no-exclusive-tests': 'error',
'mocha/no-exports': 'error',
'mocha/no-global-tests': 'error',
'mocha/no-hooks': 'error',
'mocha/no-hooks-for-single-case': 'error',
'mocha/no-identical-title': 'error',
'mocha/no-mocha-arrows': 'error',
'mocha/no-nested-tests': 'error',
'mocha/no-pending-tests': 'error',
'mocha/no-return-and-callback': 'error',
'mocha/no-return-from-async': 'error',
'mocha/no-setup-in-describe': 'error',
'mocha/no-sibling-hooks': 'error',
'mocha/no-skipped-tests': 'error',
'mocha/no-synchronous-tests': 'error',
'mocha/no-top-level-hooks': 'error',
'mocha/prefer-arrow-callback': 'error',
'mocha/valid-suite-description': 'error',
'mocha/valid-test-description': 'error',
'mocha/no-empty-description': 'error',
'mocha/consistent-spacing-between-blocks': 'error'
}
rules: allRules
},

recommended: {
env: { mocha: true },
plugins: [ 'mocha' ],
rules: {
'mocha/handle-done-callback': 'error',
'mocha/max-top-level-suites': [ 'error', { limit: 1 } ],
'mocha/no-async-describe': 'error',
'mocha/no-exclusive-tests': 'warn',
'mocha/no-exports': 'error',
'mocha/no-global-tests': 'error',
'mocha/no-hooks': 'off',
'mocha/no-hooks-for-single-case': 'off',
'mocha/no-identical-title': 'error',
'mocha/no-mocha-arrows': 'error',
'mocha/no-nested-tests': 'error',
'mocha/no-pending-tests': 'warn',
'mocha/no-return-and-callback': 'error',
'mocha/no-return-from-async': 'off',
'mocha/no-setup-in-describe': 'error',
'mocha/no-sibling-hooks': 'error',
'mocha/no-skipped-tests': 'warn',
'mocha/no-synchronous-tests': 'off',
'mocha/no-top-level-hooks': 'warn',
'mocha/prefer-arrow-callback': 'off',
'mocha/valid-suite-description': 'off',
'mocha/valid-test-description': 'off',
'mocha/no-empty-description': 'error',
'mocha/consistent-spacing-between-blocks': 'error'
}
rules: recommendedRules
}
}
};

mod.configs.flat = {
all: {
plugins: { mocha: mod },
languageOptions: { globals: globals.mocha },
rules: allRules
},
recommended: {
plugins: { mocha: mod },
languageOptions: { globals: globals.mocha },
rules: recommendedRules
}
};

module.exports = mod;
127 changes: 109 additions & 18 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -28,6 +28,7 @@
},
"dependencies": {
"eslint-utils": "^3.0.0",
"globals": "^14.0.0",
"rambda": "^7.4.0"
},
"devDependencies": {
Expand Down

0 comments on commit 1b550d6

Please sign in to comment.