Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-undefined-types: false positives inside functions for type parameters declared by template tags #559

Closed
snowteamer opened this issue Jun 2, 2020 · 5 comments

Comments

@snowteamer
Copy link

Expected behavior

No error.

Actual behavior

7:0 error The type 'T' is undefined jsdoc/no-undefined-types

ESLint Config

{
	"env": {
		"es6": true
	},
	"parserOptions": {
		"ecmaVersion": 2020,
		"sourceType": "module"
	},
	"plugins": ["jsdoc"],
	"root": true,
	"rules": {
		"jsdoc/no-undefined-types": "error"
	},
	"settings": {
		"jsdoc": {
			"mode": "typescript"
		}
	}
}

ESLint sample

/**
 * @template T
 * @param {T} arg
 */
function example(arg) {

	/** @param {T} */
	function inner(x) {
	}
}

Environment

  • Node version: v14.1.0
  • ESLint version: v7.1.0
  • eslint-plugin-jsdoc version: 27.0.0
@wycats
Copy link

wycats commented Jun 2, 2020

I hit this same issue with this reproduction:

class {
  /**
   * @template {object} T
   * @param {(node: QDigestNode) => T} callback
   * @returns {T[]}
   */
  map(callback) {
    /** @type {T[]} */
    let vals;
    vals = [];
    this.forEach((node) => vals.push(callback(node)));
    return vals;
  }
}

@snowteamer
Copy link
Author

This error apparently also occurs when the type parameter is referenced inside the function by using other TypeScript-supported tags like @type instead of @param:

/**
 * @template T
 * @param {T} arg
 * @returns {[T]}
 */
function genericFunctionExample(arg) {
	const result = /** @type {[T]} */ (new Array());
	result[0] = arg;
	return result;
}

@snowteamer
Copy link
Author

@brettz9 Regarding the scope of template tags, I could find some docs at
https://github.com/google/closure-compiler/wiki/Generic-Types#declaring-a-generic-type, I hope it helps

Otherwise, from my experience using TypeScript with JsDoc,
I think template type names defined in the JsDoc comment of a function can be used:

  • anywhere in that comment (which is already supported by jsdoc/plugin),
  • in any TS-supported tag (mainly @type, @param, @returns) attached to any AST node belonging to that function.

brettz9 added a commit to brettz9/eslint-plugin-jsdoc that referenced this issue Jun 3, 2020
@brettz9
Copy link
Collaborator

brettz9 commented Jun 3, 2020

Thank you for the reports and helpful information.

While I got some of the more low-hanging fruit by fixing the first problem case (with a nested function), fixing the other cases would seem to require creating our own scope detection based on following the AST parent chain. (ESLint's scopeManager.acquire(node) doesn't seem to get enclosing scopes for non-functions as we need.)

This is not currently something on my priority list (unless I may be able to get to it in the course of hopefully addressing #99 at some point), but PRs would be welcome.

@gajus
Copy link
Owner

gajus commented Jan 3, 2022

🎉 This issue has been resolved in version 37.5.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gajus gajus added the released label Jan 3, 2022
gobengo added a commit to gobengo/hd-scripts that referenced this issue Feb 1, 2023
wrt the mentioned issues with this rule:
* gajus/eslint-plugin-jsdoc#559 is resolved
* gajus/eslint-plugin-jsdoc#280 - this seems like we can [use this workaround](gajus/eslint-plugin-jsdoc#280 (comment)) including configuration for common builtin types here in hd-scripts
hugomrdias pushed a commit to hugomrdias/hd-scripts that referenced this issue Feb 1, 2023
wrt the mentioned issues with this rule:
* gajus/eslint-plugin-jsdoc#559 is resolved
* gajus/eslint-plugin-jsdoc#280 - this seems like we can [use this workaround](gajus/eslint-plugin-jsdoc#280 (comment)) including configuration for common builtin types here in hd-scripts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants