From 791e5e42568fd342372f4b8a664e71b480e9a1f1 Mon Sep 17 00:00:00 2001 From: homer0 Date: Fri, 30 Oct 2020 04:22:58 -0300 Subject: [PATCH] fix(prettier-plugin-jsdoc): move names to a new line when the type is too long --- src/fns/renderTagInLine.js | 11 ++++++++++- test/e2e/fixtures/random-01.fixture.js | 25 +++++++++++++++++++++---- test/unit/fns/renderTagInLine.test.js | 18 ++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/fns/renderTagInLine.js b/src/fns/renderTagInLine.js index 4bef2fb..55b4f74 100644 --- a/src/fns/renderTagInLine.js +++ b/src/fns/renderTagInLine.js @@ -43,7 +43,16 @@ const renderTagInLine = R.curry((width, typePadding, namePadding, tag) => { const tagHeaderWithSpace = `${tagHeader}${useTypePadding}{${tag.type}}${useNamePadding}`; const nameWidth = width - tagHeaderWithSpace.length; const nameLines = get(splitText)(tag.name, nameWidth); - result = [`${tagHeaderWithSpace}${nameLines.shift()}`.trimRight()]; + const nameFirstLine = nameLines.shift(); + const topLine = `${tagHeaderWithSpace}${nameFirstLine}`.trimRight(); + if (topLine.length > width) { + result = [ + tagHeaderWithSpace.trimRight(), + nameFirstLine, + ]; + } else { + result = [topLine]; + } if (nameLines.length) { const namePaddingForLine = ' '.repeat(tagHeaderWithSpace.length); result.push(...nameLines.map((line) => `${namePaddingForLine}${line}`)); diff --git a/test/e2e/fixtures/random-01.fixture.js b/test/e2e/fixtures/random-01.fixture.js index 6c13c16..e6c9d7d 100644 --- a/test/e2e/fixtures/random-01.fixture.js +++ b/test/e2e/fixtures/random-01.fixture.js @@ -1,9 +1,10 @@ -module.exports = { - only: true, -}; - //# input +/** + * @external Jimple + * @see https://yarnpkg.com/en/package/jimple + */ + /** * @type {Object} Something * @description transform this into a sentence @@ -33,8 +34,18 @@ const log = (name = 'batman', logger) => {}; * @returns {User} some description for the return value */ +/** + * @typedef {StaticsControllerOptions & StaticsControllerWrapperOptionsProperties} StaticsControllerWrapperOptions + * @parent module:controllers + */ + //# output +/** + * @external Jimple + * @see https://yarnpkg.com/en/package/jimple + */ + /** * Transform this into a sentence. * @@ -84,3 +95,9 @@ const log = (name = 'batman', logger) => {}; * @summary * Something else. */ + +/** + * @typedef {StaticsControllerOptions & StaticsControllerWrapperOptionsProperties} + * StaticsControllerWrapperOptions + * @parent module:controllers + */ diff --git a/test/unit/fns/renderTagInLine.test.js b/test/unit/fns/renderTagInLine.test.js index 69d6c23..01bc9ad 100644 --- a/test/unit/fns/renderTagInLine.test.js +++ b/test/unit/fns/renderTagInLine.test.js @@ -141,6 +141,24 @@ describe('renderTagInLine', () => { namePadding: 1, }, }, + { + it: 'should move the name to a new line when the type is too long', + input: { + tag: 'typedef', + type: 'StaticsControllerOptions & StaticsControllerWrapperOptionsProperties', + name: 'StaticsControllerWrapperOptions', + description: '', + }, + output: [ + '@typedef {StaticsControllerOptions & StaticsControllerWrapperOptionsProperties}', + 'StaticsControllerWrapperOptions', + ], + options: { + width: 90, + typePadding: 1, + namePadding: 1, + }, + }, ]; it.each(cases)('should correctly format the case %#', (caseInfo) => {