Skip to content

Commit

Permalink
Add support for eslint flat config format
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd authored and lo1tuma committed Mar 7, 2024
1 parent e3e51b3 commit b1fb03c
Show file tree
Hide file tree
Showing 4 changed files with 2,211 additions and 8,938 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit b1fb03c

Please sign in to comment.