Skip to content

Nameless tags: description starting with [ is parsed as an optional name and widens the type with | undefined #269

Description

@dmnsgn

Reproduction

Input:

/**
 * @returns {number[]} [x, y]
 */
export function get() {}

Output (prettier-plugin-jsdoc 1.8.1, default options):

/**
 * @returns {number[] | undefined} X, y
 */
export function get() {}

Expected: unchanged. @returns takes no name, so [x, y] is plain description text.

Cause

  1. comment-parser's name tokenizer reads [x, y] as an optional name: { name: "x, y", optional: true, description: "" }.
  2. @returns is in TAGS_NAMELESS, so the name is folded back into the description, but optional is left true:
    https://github.com/hosseinmd/prettier-plugin-jsdoc/blob/75825bae4a9e0f74a0539aeeada777d3e54e38d2/src/parser.ts#L478-L481
  3. assignOptionalAndDefaultToName then sees optional with an empty name and appends | undefined to the type:
    https://github.com/hosseinmd/prettier-plugin-jsdoc/blob/75825bae4a9e0f74a0539aeeada777d3e54e38d2/src/parser.ts#L681
  4. The description formatter capitalises the first letter, giving X, y.

Every tag in TAGS_NAMELESS (@returns, @throws, @description, @example, …) whose description begins with [ is affected.

Suggested fix

Reset the optional/default state when the name is folded into the description, in src/parser.ts#L478-L481:

if (name && TAGS_NAMELESS.includes(tag)) {
  description = `${name} ${description}`;
  name = "";
  optional = false;
  default_ = undefined;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions