Skip to content

Commit

Permalink
feat: added granular flat TypeScript configs
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Aug 8, 2024
1 parent 1cae2cb commit f2b1c1d
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,38 @@ const config = [
export default config;
```

The general starting rulesets you can extend from in flat config are:

* `jsdoc.configs['flat/recommended']`: Recommended starting rules for enforcing proper tag values, that common tags exist, and that tags are formatted and styled consistently
* `jsdoc.configs['flat/recommended-error']`: The same, reporting with failing errors instead of mere warnings
* `jsdoc.configs['flat/recommended-typescript']`: A similar recommended starting list, adjusted for projects using TypeScript syntax (and not just "typescript" `mode`)
* `jsdoc.configs['flat/recommended-typescript-error']`: The same, reporting with failing errors instead of mere warnings
* `jsdoc.configs['flat/recommended-typescript-flavor']`: A similar recommended starting list, adjusted for projects using JavaScript syntax (source files that are still `.js`) but using TypeScript flavor within JSDoc (i.e., the default "typescript" `mode` in `eslint-plugin-jsdoc`)
* `jsdoc.configs['flat/recommended-typescript-flavor-error']`: The same, reporting with failing errors instead of mere warnings

#### Granular Flat Configs

There also exist several more granular, standalone TypeScript rulesets you can extend from.
These each only enable a subset of the recommended starting rules:

* `jsdoc.configs['flat/logical-typescript']`: Includes only the rules that enforce proper tag values
* `jsdoc.configs['flat/logical-typescript-error']`: The same, reporting with failing errors instead of mere warnings
* `jsdoc.configs['flat/requirements-typescript']`: Includes only the rules that enforce tags exist
* `jsdoc.configs['flat/requirements-typescript-error']`: The same, reporting with failing errors instead of mere warnings
* `jsdoc.configs['flat/stylistic-typescript']`: Includes only the rules that enforce clear, consistent tag formatting and styles
* `jsdoc.configs['flat/stylistic-typescript-error']`: The same, reporting with failing errors instead of mere warnings

For example, to enforce only that any JSDoc tags are valid and styled consistently, without enforcing that they must always exist:

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

export default [
jsdoc.configs['flat/logical-typescript-error'],
jsdoc.configs['flat/stylistic-typescript-error'],
];
```

<a name="user-content-eslint-plugin-jsdoc-configuration-eslintrc"></a>
<a name="eslint-plugin-jsdoc-configuration-eslintrc"></a>
### <code>eslintrc</code>
Expand Down
66 changes: 66 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,65 @@ const createRecommendedTypeScriptFlavorRuleset = (warnOrError, flatName) => {
};
};

/**
* @param {string[]} ruleNames
*/
const createStandaloneRulesetFactory = (ruleNames) => {
/**
* @param {"warn"|"error"} warnOrError
* @param {string} [flatName]
* @returns {import('eslint').Linter.FlatConfig}
*/
return (warnOrError, flatName) => {
return {
name: 'jsdoc/' + flatName,
plugins: { jsdoc: index },
rules: Object.fromEntries(ruleNames.map(ruleName => [ruleName, warnOrError])),
};
};
}

const createLogicalTypescriptRuleset = createStandaloneRulesetFactory([
'jsdoc/check-access',
'jsdoc/check-param-names',
'jsdoc/check-property-names',
'jsdoc/check-tag-names',
'jsdoc/check-template-names',
'jsdoc/check-types',
'jsdoc/check-values',
'jsdoc/empty-tags',
'jsdoc/implements-on-classes',
'jsdoc/require-returns-check',
'jsdoc/require-yields-check',
'jsdoc/no-defaults',
'jsdoc/no-multi-asterisks',
'jsdoc/no-types',
'jsdoc/no-undefined-types',
'jsdoc/valid-types',
]);

const createRequirementsTypeScriptRuleset = createStandaloneRulesetFactory([
'jsdoc/require-example',
'jsdoc/require-jsdoc',
'jsdoc/require-param',
'jsdoc/require-param-description',
'jsdoc/require-param-name',
'jsdoc/require-property',
'jsdoc/require-property-description',
'jsdoc/require-property-name',
'jsdoc/require-returns',
'jsdoc/require-returns-description',
'jsdoc/require-yields',
]);

const createStylisticTypeScriptRuleset = createStandaloneRulesetFactory([
'jsdoc/check-alignment',
'jsdoc/lines-before-block',
'jsdoc/multiline-blocks',
'jsdoc/no-multi-asterisks',
'jsdoc/tag-lines',
]);

/* c8 ignore next 3 -- TS */
if (!index.configs) {
throw new Error('TypeScript guard');
Expand All @@ -279,6 +338,13 @@ index.configs['flat/recommended-typescript-error'] = createRecommendedTypeScript
index.configs['flat/recommended-typescript-flavor'] = createRecommendedTypeScriptFlavorRuleset('warn', 'flat/recommended-typescript-flavor');
index.configs['flat/recommended-typescript-flavor-error'] = createRecommendedTypeScriptFlavorRuleset('error', 'flat/recommended-typescript-flavor-error');

index.configs['flat/logical-typescript'] = createLogicalTypescriptRuleset('warn', 'flat/logical-typescript');
index.configs['flat/logical-typescript-error'] = createLogicalTypescriptRuleset('error', 'flat/logical-typescript-error');
index.configs['flat/requirements-typescript'] = createRequirementsTypeScriptRuleset('warn', 'flat/requirements-typescript');
index.configs['flat/requirements-typescript-error'] = createRequirementsTypeScriptRuleset('error', 'flat/requirements-typescript-error');
index.configs['flat/stylistic-typescript'] = createStylisticTypeScriptRuleset('warn', 'flat/stylistic-typescript');
index.configs['flat/stylistic-typescript-error'] = createStylisticTypeScriptRuleset('error', 'flat/stylistic-typescript-error');

index.configs.examples = /** @type {import('eslint').Linter.FlatConfig[]} */ ([
{
name: 'jsdoc/examples/processor',
Expand Down

0 comments on commit f2b1c1d

Please sign in to comment.