gajus / eslint-plugin-jsdoc

JSDoc specific linting rules for ESLint.
Other
1.1k stars 159 forks source link

How to restore require-jsdoc behavior from ^46.8.2 #1292

Closed krutoo closed 3 months ago

krutoo commented 3 months ago

Hi, today I upgraded eslint-plugin-jsdoc from ^46.8.2 to ^48.11.0

I noticed that now require-jsdoc rule works differently.

Previously I used this config for require-jsdoc:

{
  'require-jsdoc': [
    'error',
    {
      require: {
        ArrowFunctionExpression: true,
        ClassDeclaration: true,
        FunctionDeclaration: true,
        FunctionExpression: true,
        MethodDefinition: true,
      },
    },
  ]
};

In old version this code provides error:

// Error: requires JSDoc (and its ok, i need it)
export const foo = () => {
  return 1;
};

export const bar = {
  // Error: requires JSDoc (and its ok, i need it)
  hello () {
    return 'hi'
  },

  // No error here (and its ok, i dont need it here)
  baz: () => {
    // Error: requires JSDoc (and its ok, i need it)
    const id = a => a;

    return id(2);
  },
};

But now all this arrow functions don't provides errors about JSDoc missing.

How can I restore behavior from version ^46.8.2?

brettz9 commented 3 months ago

This code provides 4 errors in the latest version (including above baz).

I think the issue may be that your config has changed. You are using require-jsdoc, and it should be jsdoc/require-jsdoc. ESLint has its own version of the function, but it was deprecated and removed in ESLint 9.

krutoo commented 3 months ago

@brettz9 Oh, its true, I mixed up require-jsdoc and jsdoc/require-jsdoc, sorry

Previously I used plugin:jsdoc/recommended and require-jsdoc

Now I remove require-jsdoc because its deprecated

Is it possible to make jsdoc/require-jsdoc works as require-jsdoc?

brettz9 commented 3 months ago

Yes, jsdoc/require-jsdoc should work similar to require-jsdoc, just with more features. It inherited some of the same codebase and just expanded on it.

krutoo commented 3 months ago

@brettz9 Looks like jsdoc/require-jsdoc now provides error for arrow functions in properties of objects

{
  // Error: Requires JSDoc (I don`t need it here)
  foo: () => 123,

  // Error: Requires JSDoc (But here is ok, i need it here)
  bar () {
    return 234;
  }
}

Can you suggest how to configure this rule for disable requiring JSDoc in such cases?

brettz9 commented 3 months ago

You can add the following object option:

        {
          contexts: [
            ':not(Property) > ArrowFunctionExpression',
          ],
          require: {
            ArrowFunctionExpression: false,
            ClassDeclaration: true,
            FunctionDeclaration: true,
            FunctionExpression: true,
            MethodDefinition: true,
          },
        }
krutoo commented 3 months ago

@brettz9 can you provide some docs about expressions in contexts option?

Looks like your example dont works as well (maybe i did something wrong):

image

Also now I have some new places where JSDoc is required:

brettz9 commented 3 months ago

@brettz9 can you provide some docs about expressions in contexts option?

The advanced page of our docs covers this a little, but as mentioned in the docs, your best bet is probably just experimenting with https://astexplorer.net/

Looks like your example dont works as well (maybe i did something wrong):

image

I can't see the full object to know exactly what you're working with.

Also now I have some new places where JSDoc is required:

  • in default values of destructured object argument:
function MyReactComponent ({ 
  getName = data => data.name // Error: Missing JSDoc (but I dont need it here)
}) {
  // ...
}
  • For arrow functions as arguments:
[1,2,3].map(() => {}); // Error: Missing JSDoc (but I dont need it here) 

While you could exclude more types than Property, you might just want to whitelist the types of allowing parents. You can add additional contexts to do so. The following mimics how the rule probably worked when it was part of ESLint (i.e., without the Property parent).

        {
          contexts: [
            'VariableDeclarator > ArrowFunctionExpression',
            'AssignmentExpression > ArrowFunctionExpression',
            'ExportDefaultDeclaration > ArrowFunctionExpression',
          ],
          require: {
            ArrowFunctionExpression: false,
            ClassDeclaration: true,
            FunctionDeclaration: true,
            FunctionExpression: true,
            MethodDefinition: true,
          },
        }
krutoo commented 3 months ago

I can't see the full object to know exactly what you're working with.

@brettz9 You don`t need full object, the error is exactly on this line with arrow function as default value (If I understand what do you mean)

brettz9 commented 3 months ago

Closing as the above should resolve, but feel free to comment further as needed.