From f185c29691d22f673f35b7888d2fde784ecb5822 Mon Sep 17 00:00:00 2001 From: Bernhard Pottler Date: Fri, 31 May 2024 05:54:50 +0200 Subject: [PATCH] Add flat config to plugin --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ index.js | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index c18daf4..8b33dff 100755 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ ESLint plugin that checks for common chai.js `expect()` mistakes +> [!IMPORTANT] +> The `recommended` preset is for the ESLint legacy configuration system +> (`.eslintrc.json`). The `recommended-flat` configuration is for the new flat +> configuration system. ## Requirements @@ -20,6 +24,8 @@ npm install --save-dev eslint-plugin-chai-expect ## Configuration +### Legacy ESLint Configuration Format (.eslintrc.json) + Add a `plugins` section and specify `chai-expect` as a plugin: ```json @@ -52,6 +58,42 @@ and just extend the config: } ``` +### Flat ESLint Configuration Format (eslint.config.js) + +Add a `plugins` section and specify `chai-expect` as a plugin and enable the rules that you would like to use: + +```js +import chaiExpectPlugin from 'eslint-plugin-chai-expect'; + +export default [ + { + "plugins": { + "chai-expect": chaiExpectPlugin + }, + "rules": { + "chai-expect/no-inner-compare": 2, + "chai-expect/no-inner-literal": 2, + "chai-expect/missing-assertion": 2, + "chai-expect/terminating-properties": 2 + } + } +]; +``` + +Or, if you just want the above defaults, you can avoid all of the above +and just extend the config: + +```js +import chaiExpectPlugin from 'eslint-plugin-chai-expect'; + +export default [ + chaiExpectPlugin.configs["recommended-flat"], + { + // ... + }, +]; +``` + ## Rules - `no-inner-compare` - Prevent using comparisons in the `expect()` argument diff --git a/index.js b/index.js index 14ce1c2..d651096 100755 --- a/index.js +++ b/index.js @@ -1,21 +1,39 @@ 'use strict'; -module.exports = { - configs: { - recommended: { - plugins: ['chai-expect'], - rules: { - 'chai-expect/no-inner-compare': 'error', - 'chai-expect/no-inner-literal': 'error', - 'chai-expect/missing-assertion': 'error', - 'chai-expect/terminating-properties': 'error' - } - } +const pkg = require('./package.json'); + +let recommendedRules = { + 'chai-expect/no-inner-compare': 'error', + 'chai-expect/no-inner-literal': 'error', + 'chai-expect/missing-assertion': 'error', + 'chai-expect/terminating-properties': 'error', +}; + +const plugin = { + meta: { + name: pkg.name, + version: pkg.version, }, + configs: {}, rules: { 'no-inner-compare': require('./lib/rules/no-inner-compare'), 'no-inner-literal': require('./lib/rules/no-inner-literal'), 'missing-assertion': require('./lib/rules/missing-assertion'), 'terminating-properties': require('./lib/rules/terminating-properties') - } + }, + processors: {} +}; + +plugin.configs['recommended'] = { + plugins: ['chai-expect'], + rules: recommendedRules, }; + +plugin.configs['recommended-flat'] = { + plugins: { + 'chai-expect': plugin, + }, + rules: recommendedRules, +}; + +module.exports = plugin;