Skip to content

Commit

Permalink
feat: support Flat ESLint config; fixes #1131
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Aug 20, 2023
1 parent 48cc6be commit 918dbed
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
25 changes: 25 additions & 0 deletions .README/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ npm install --save-dev eslint-plugin-jsdoc

## Configuration

### Flat config

```js
import jsdoc from 'eslint-plugin-jsdoc';

const config = [
// configuration included in plugin
jsdoc.configs['flat/recommended'],
// other configuration objects...
{
files: ['**/*.js'],
plugins: {
jsdoc,
},
rules: {
'jsdoc/require-description': 'warn'
}
}
];

export default config;
```

### `eslintrc`

Add `plugins` section to [.eslintrc.*](https://eslint.org/docs/user-guide/configuring#configuration-file-formats)
and specify `eslint-plugin-jsdoc` as a plugin.

Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ JSDoc linting rules for ESLint.
* [eslint-plugin-jsdoc](#user-content-eslint-plugin-jsdoc)
* [Installation](#user-content-eslint-plugin-jsdoc-installation)
* [Configuration](#user-content-eslint-plugin-jsdoc-configuration)
* [Flat config](#user-content-eslint-plugin-jsdoc-configuration-flat-config)
* [`eslintrc`](#user-content-eslint-plugin-jsdoc-configuration-eslintrc)
* [Options](#user-content-eslint-plugin-jsdoc-options)
* [Settings](#user-content-eslint-plugin-jsdoc-settings)
* [Advanced](#user-content-eslint-plugin-jsdoc-advanced)
Expand Down Expand Up @@ -40,6 +42,35 @@ npm install --save-dev eslint-plugin-jsdoc
<a name="eslint-plugin-jsdoc-configuration"></a>
## Configuration

<a name="user-content-eslint-plugin-jsdoc-configuration-flat-config"></a>
<a name="eslint-plugin-jsdoc-configuration-flat-config"></a>
### Flat config

```js
import jsdoc from 'eslint-plugin-jsdoc';

const config = [
// configuration included in plugin
jsdoc.configs['flat/recommended'],
// other configuration objects...
{
files: ['**/*.js'],
plugins: {
jsdoc,
},
rules: {
'jsdoc/require-description': 'warn'
}
}
];

export default config;
```

<a name="user-content-eslint-plugin-jsdoc-configuration-eslintrc"></a>
<a name="eslint-plugin-jsdoc-configuration-eslintrc"></a>
### <code>eslintrc</code>

Add `plugins` section to [.eslintrc.*](https://eslint.org/docs/user-guide/configuring#configuration-file-formats)
and specify `eslint-plugin-jsdoc` as a plugin.

Expand Down
32 changes: 22 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import validTypes from './rules/validTypes.js';

/**
* @type {import('eslint').ESLint.Plugin & {
* configs: Record<string, import('eslint').ESLint.ConfigData>
* configs: Record<string, import('eslint').ESLint.ConfigData|{}>
* }}
*/
const index = {
Expand Down Expand Up @@ -118,11 +118,14 @@ const index = {

/**
* @param {"warn"|"error"} warnOrError
* @returns {import('eslint').ESLint.ConfigData}
* @param {boolean} [flat]
* @returns {import('eslint').ESLint.ConfigData | {plugins: {}, rules: {}}}
*/
const createRecommendedRuleset = (warnOrError) => {
const createRecommendedRuleset = (warnOrError, flat) => {
return {
plugins: [
plugins: flat ? {
jsdoc: index,
} : [
'jsdoc',
],
rules: {
Expand Down Expand Up @@ -185,10 +188,11 @@ const createRecommendedRuleset = (warnOrError) => {

/**
* @param {"warn"|"error"} warnOrError
* @returns {import('eslint').ESLint.ConfigData}
* @param {boolean} [flat]
* @returns {import('eslint').ESLint.ConfigData|{}}
*/
const createRecommendedTypeScriptRuleset = (warnOrError) => {
const ruleset = createRecommendedRuleset(warnOrError);
const createRecommendedTypeScriptRuleset = (warnOrError, flat) => {
const ruleset = createRecommendedRuleset(warnOrError, flat);

return {
...ruleset,
Expand All @@ -212,10 +216,11 @@ const createRecommendedTypeScriptRuleset = (warnOrError) => {

/**
* @param {"warn"|"error"} warnOrError
* @returns {import('eslint').ESLint.ConfigData}
* @param {boolean} [flat]
* @returns {import('eslint').ESLint.ConfigData|{}}
*/
const createRecommendedTypeScriptFlavorRuleset = (warnOrError) => {
const ruleset = createRecommendedRuleset(warnOrError);
const createRecommendedTypeScriptFlavorRuleset = (warnOrError, flat) => {
const ruleset = createRecommendedRuleset(warnOrError, flat);

return {
...ruleset,
Expand All @@ -240,4 +245,11 @@ index.configs['recommended-typescript-error'] = createRecommendedTypeScriptRules
index.configs['recommended-typescript-flavor'] = createRecommendedTypeScriptFlavorRuleset('warn');
index.configs['recommended-typescript-flavor-error'] = createRecommendedTypeScriptFlavorRuleset('error');

index.configs['flat/recommended'] = createRecommendedRuleset('warn', true);
index.configs['flat/recommended-error'] = createRecommendedRuleset('error', true);
index.configs['flat/recommended-typescript'] = createRecommendedTypeScriptRuleset('warn', true);
index.configs['flat/recommended-typescript-error'] = createRecommendedTypeScriptRuleset('error', true);
index.configs['flat/recommended-typescript-flavor'] = createRecommendedTypeScriptFlavorRuleset('warn', true);
index.configs['flat/recommended-typescript-flavor-error'] = createRecommendedTypeScriptFlavorRuleset('error', true);

export default index;

0 comments on commit 918dbed

Please sign in to comment.