From 4f9ba4c86089ec12fee6fac9ce014661352ebf28 Mon Sep 17 00:00:00 2001 From: Hossein Mohammadi Date: Thu, 5 Oct 2023 23:55:31 +0330 Subject: [PATCH] fix: long union type issue: https://github.com/hosseinmd/prettier-plugin-jsdoc/issues/181 --- src/utils.ts | 9 ++++++--- tests/__snapshots__/typeScript.test.ts.snap | 19 ++++++++++++++++++- tests/typeScript.test.ts | 15 +++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 459e174..6666bf1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -108,9 +108,12 @@ async function formatType(type: string, options?: Options): Promise { filepath: "file.ts", }); pretty = pretty.slice(TYPE_START.length); - pretty = pretty.replace(/^\s*/g, ""); - pretty = pretty.replace(/[;\n]*$/g, ""); - pretty = pretty.trim(); + + pretty = pretty + .replace(/^\s*/g, "") + .replace(/[;\n]*$/g, "") + .replace(/^\|/g, "") + .trim(); if (rest) { pretty = "..." + pretty.replace(/\[\s*\]$/, ""); diff --git a/tests/__snapshots__/typeScript.test.ts.snap b/tests/__snapshots__/typeScript.test.ts.snap index b1ec7eb..a27e604 100644 --- a/tests/__snapshots__/typeScript.test.ts.snap +++ b/tests/__snapshots__/typeScript.test.ts.snap @@ -49,9 +49,26 @@ export let User; " `; +exports[`Long type Union types 1`] = ` +"/** + * 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; +" +`; + exports[`Union types 1`] = ` "/** - * @typedef {| { foo: string } + * @typedef {{ foo: string } * | { bar: string; manyMoreLongArguments: object } * | { baz: string }} Foo */ diff --git a/tests/typeScript.test.ts b/tests/typeScript.test.ts index 3c4144b..08df00a 100644 --- a/tests/typeScript.test.ts +++ b/tests/typeScript.test.ts @@ -175,3 +175,18 @@ test("Union types", async () => { expect(result).toMatchSnapshot(); }); + +test("Long type Union types", async () => { + const result = await subject(` + /** + * 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; +`); + + expect(result).toMatchSnapshot(); +});