gajus / eslint-plugin-jsdoc

JSDoc specific linting rules for ESLint.
Other
1.09k stars 157 forks source link

Force consistent object typedef #1140

Closed remcohaszing closed 1 year ago

remcohaszing commented 1 year ago

Motivation

There are 3 ways to define object type defs (that I know of):

/**
 * @typedef Person
 * @property name
 */

/**
 * @typedef {object} Person
 * @property name
 */

/**
 * @typedef {Object} Person
 * @property name
 */

I would like to enforce one consistently. Personally I prefer the first variant.

Current behavior

N/A

Desired behavior

A new ESLint rule to enforce a consistent way to write object typedefs.

{
  "rules": {
    "jsdoc/object-style": [
      "error",
      // Default value
      null // or "object" or "Object"
    ]
  }
}

Alternatives considered

The name can be different. The options can be different.

brettz9 commented 1 year ago

I would suggest just using jsdoc/no-restricted-syntax. This won't have a fixer, but it can report the non-desired configurations. Look for the examples using JsdocTag.

remcohaszing commented 1 year ago

Thanks for the suggestion! This works for me:

module.exports = {
  rules: {
    'jsdoc/no-restricted-syntax': [
      'error',
      {
        contexts: [
          {
            comment: 'JsdocBlock:has(JsdocTag[tag=typedef][parsedType.value=/object/i])',
            message: 'Omit the object type from typedef'
          }
        ]
      }
    ]
  }
}