hosseinmd / prettier-plugin-jsdoc

A Prettier plugin to format JSDoc comments.
MIT License
228 stars 29 forks source link

Long type references are broken up into multiple lines incorrectly with leading pipe character #181

Closed pahund closed 11 months ago

pahund commented 1 year ago

Before formatting, by JSDoc for a return statement looks like this:

/**
 * Gets a configuration object assembled from environment variables and .env configuration files.
 *
 * @memberof Config
 * @function getEnvConfig
 * @returns {Config.SomeConfiguration | Config.SomeOtherConfiguration | Config.AnotherConfiguration | Config.YetAnotherConfiguration } The environment configuration
 */
export default () => configurator.config;

After running Prettier, the JSDoc comment is formatted like this:

/**
 * Gets a configuration object assembled from environment variables and .env configuration files.
 *
 * @memberof Config
 * @function getEnvConfig
 * @returns {| Config.SomeConfiguration
 *   | Config.SomeOtherConfiguration
 *   | Config.AnotherConfiguration
 *   | Config.YetAnotherConfiguration}
 *   The environment configuration
 */
export default () => configurator.config;

Running JSDoc, this causes the following error:

ERROR: Unable to parse a tag's type expression for source file /Users/patrick.hund/IdeaProjects/code-kraken/src/config/getEnvConfig.mjs in line 41 with tag title "returns" and text "{| Config.SomeConfiguration
  | Config.SomeOtherConfiguration
  | Config.AnotherConfiguration
  | Config.YetAnotherConfiguration}
  The environment configuration": Invalid type expression "| Config.SomeConfiguration
  | Config.SomeOtherConfiguration
  | Config.AnotherConfiguration
  | Config.YetAnotherConfiguration": Expected "!", "$", "'", "(", "*", ".", "...", "0", "?", "@", "Function", "\"", "\\", "_", "break", "case", "catch", "class", "const", "continue", "debugger", "default", "delete", "do", "else", "enum", "export", "extends", "false", "finally", "for", "function", "if", "implements", "import", "in", "instanceof", "interface", "let", "new", "null", "package", "private", "protected", "public", "return", "static", "super", "switch", "this", "throw", "true", "try", "typeof", "undefined", "var", "void", "while", "with", "yield", "{", Unicode letter number, Unicode lowercase letter, Unicode modifier letter, Unicode other letter, Unicode titlecase letter, Unicode uppercase letter, or [1-9] but "|" found.

The culprit is the pipe character before Config.SomeConfiguration. If I manually format the JSDoc code like this, removing the pipe character, the syntax error does not happen:

/**
 * Gets a configuration object assembled from environment variables and .env configuration files.
 *
 * @memberof Config
 * @function getEnvConfig
 * @returns {Config.SomeConfiguration
 *   | Config.SomeOtherConfiguration
 *   | Config.AnotherConfiguration
 *   | Config.YetAnotherConfiguration}
 *   The environment configuration
 */
export default () => configurator.config;
hosseinmd commented 11 months ago

fixed: v1.0.5

pahund commented 4 months ago

Thanks @hosseinmd!