gajus / eslint-plugin-jsdoc

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

[`check-template-names`] @property is ignored #1269

Closed regseb closed 4 months ago

regseb commented 4 months ago

Expected behavior

eslint-plugin-jsdoc should support two @template in one file.

Actual behavior

Oops! Something went wrong! :(

ESLint: 9.7.0

Error: No parslet found for token: 'EOF'
Occurred while linting /home/regseb/testcase/index.js:1
Rule: "jsdoc/check-template-names"
    at Parser.parseIntermediateType (/home/regseb/testcase/node_modules/jsdoc-type-pratt-parser/dist/index.js:364:23)
    at Parser.parseType (/home/regseb/testcase/node_modules/jsdoc-type-pratt-parser/dist/index.js:355:42)
    at Parser.parse (/home/regseb/testcase/node_modules/jsdoc-type-pratt-parser/dist/index.js:345:33)
    at parse (/home/regseb/testcase/node_modules/jsdoc-type-pratt-parser/dist/index.js:1599:68)
    at iterateJsdoc.iterateAllJsdocs (file:///home/regseb/testcase/node_modules/eslint-plugin-jsdoc/src/rules/checkTemplateNames.js:70:5)
    at iterate (file:///home/regseb/testcase/node_modules/eslint-plugin-jsdoc/src/iterateJsdoc.js:2049:3)
    at callIterator (file:///home/regseb/testcase/node_modules/eslint-plugin-jsdoc/src/iterateJsdoc.js:2225:7)
    at Program:exit (file:///home/regseb/testcase/node_modules/eslint-plugin-jsdoc/src/iterateJsdoc.js:2318:11)
    at ruleErrorHandler (/home/regseb/testcase/node_modules/eslint/lib/linter/linter.js:1117:48)
    at /home/regseb/testcase/node_modules/eslint/lib/linter/safe-emitter.js:45:58

ESLint Config

import jsdoc from "eslint-plugin-jsdoc";

export default [
    {
        plugins: { jsdoc },
        rules: {
            "jsdoc/check-template-names": "error",
        }
    }
];

ESLint sample

/**
 * @template {Object} T
 * @typedef Foo<T>
 * @prop {T} bar
 */

/**
 * @template {Object} T
 * @typedef Baz<T>
 * @prop {T} qux
 */

Environment

brettz9 commented 4 months ago

The issue is apparently instead that a generic cannot be indicated in the name like this: @typedef Foo<T>. See the other examples for what is currently allowable.

Does TypeScript accept this form of JSDoc? I don't see it at https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#template .

regseb commented 4 months ago

You're right, the error is reported with a single @typedef.

I tested the following file in TS Playground. Without a generic or with an invalid generic, TypeScript returns the same errors (number not assignable to string). The <T> shouldn't be added to the @typedef.

/**
 * @template {Object} T
 * @typedef Foo<OUPS_I_MAKE_A_TYPO>
 * @prop {T} bar
 */

/**
 * @type {Foo<string>}
 */
const fooValid = { bar: "" };

/**
 * @type {Foo<string>}
 */
const fooInvalid = { bar: 0 };
//                   ~~~

/**
 * @template {Object} T
 * @typedef Baz
 * @prop {T} qux
 */

/**
 * @type {Baz<string>}
 */
const bazValid = { qux: "" };

/**
 * @type {Baz<string>}
 */
const bazInvalid = { qux: 0 };
//                   ~~~

eslint-plugin-jsdoc should report a syntax error for a JSDoc comment with <T>.

brettz9 commented 4 months ago

Yeah, I think that is a dupe of #374.

regseb commented 4 months ago

The No parslet found for token: 'EOF' error isn't related to @template. I have the error with the following code.

/**
 * @typedef Foo
 * @prop {string} bar
 */

And no error with:

/**
 * @typedef {Object} Foo
 * @prop {string} bar
 */

In the examples in the TypeScript documentation, the type is always specified. But tsc supports when it's not specified.

brettz9 commented 4 months ago

Ah, great to know the playground can have tsconfig set to JavaScript. Wasn't aware of that.

I have confirmed the bug as you report now, yes. For now, I can change to silently ignore such parse errors here as when there is no type present, but I'll leave the issue open to try to handle the case for use of @property types. That sound reasonable?

github-actions[bot] commented 4 months ago

:tada: This issue has been resolved in version 48.8.3 :tada:

The release is available on:

Your semantic-release bot :package::rocket: