Skip to content

check-template-names (template not in use) when template is in use #1466

@OhiraKyou

Description

@OhiraKyou

Expected behavior

No error when using a generic template.

Actual behavior

ESLint claims that the template is not in use.

ESLint Config

eslint.config.mjs

import { defineConfig } from "eslint/config";
import jsdoc from "eslint-plugin-jsdoc";

export default defineConfig({
	plugins: { jsdoc },
	rules: { "jsdoc/check-template-names": "error" },
});

package.json

{
	"type": "module",
	"devDependencies": {
		"eslint": "^9.22.0",
		"eslint-plugin-jsdoc": "^54.3.0"
	}
}

ESLint sample

// Also fails: {object | undefined} [DataType=undefined]

/**
 * @template [ChannelDataType=undefined]
 * @param {string} messageType - A key used for sending and receiving messages.
 * @returns {MessageChannel<ChannelDataType>} A channel that can create messages of its
 * own type.
 */
export function createMessageChannel(messageType) {
	// Note: It should also infer the type if the new channel is returned
	// directly rather than returned as a typed variable.

	/** @type {MessageChannel<ChannelDataType>} */
	const messageChannel = new MessageChannel(messageType);

	return messageChannel;
}

/**
 * @template DataType Intentionally given a name that differs from that of the
 * template parameter used in the `createMessageChannel` function. This makes it
 * clear which template is being referred to when hovering over things.
 */
export class MessageChannel {
	/**
	 * @type {string}
	 * @private
	 */
	_messageType;

	/**
	 * @param {string} messageType
	 */
	constructor(messageType) {
		this._messageType = messageType;
	}

	/**
	 * @param {DataType} data
	 * @returns {MessageEvent<DataType>} A message event of this message
	 * creator's pre-configured type.
	 */
	createMessage(data) {
		return new MessageEvent(this._messageType, { data: data });
	}
}

Environment

  • Node version: v22.17.1
  • ESLint version v9.34.0
  • eslint-plugin-jsdoc version: 54.3.0

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions