gajus / eslint-plugin-jsdoc

JSDoc specific linting rules for ESLint.
Other
1.07k stars 155 forks source link

Rule to limit the number of main tags per comment block #1207

Closed Ploppy3 closed 4 months ago

Ploppy3 commented 4 months ago

Motivation

There should be only 1 @typedef per comment block.

Current behavior

But there is no rule to limit this, therefore it can lead to invalid jsdoc.

Desired behavior

Add a new rules which limits the number of "main" tags (such as @typedef) that can be used in a single comment block. This could be autofixed by closing and opening another comment block.

Example:

Change:

/**
 * @typedef {object} Test1
 * @property {string} prop1
 * @typedef {object} Test2
 * @property {string} prop2
 */

Into:

/**
 * @typedef {object} Test1
 * @property {string} prop1
 */
/**
 * @typedef {object} Test2
 * @property {string} prop2
 */
brettz9 commented 4 months ago

You can currently do so as follows:

rules: {
  'jsdoc/no-restricted-syntax': ['error', {
    contexts: [
      {
        comment: 'JsdocBlock:has(JsdocTag[tag=typedef] ~ JsdocTag[tag=typedef])',
        context: 'any',
        message: 'No multiple typedefs within a single block; create a separate block',
      },
    ],
  }]
}
Ploppy3 commented 4 months ago

Thank you very much, it works, feel free to close it if necessary