Skip to content

Commit

Permalink
fix(prettier-plugin-jsdoc): use name and description columns for spec…
Browse files Browse the repository at this point in the history
…ial tags
  • Loading branch information
homer0 committed Oct 27, 2020
1 parent 192afb8 commit 53819ce
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/fns/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { isTag, ensureSentence } = require('./utils');
const { renderExampleTag } = require('./renderExampleTag');
const { renderTagInLine } = require('./renderTagInLine');
const { renderTagInColumns } = require('./renderTagInColumns');
const { TAGS_WITH_NAME_AS_DESCRIPTION } = require('../constants');

/**
* @typedef {import('../types').CommentBlock} CommentBlock
Expand Down Expand Up @@ -255,6 +256,10 @@ const calculateColumnsWidth = (options, data, width) => {
const getTagsData = (lengthByTag, width, options) => Object.entries(lengthByTag).reduce(
(acc, [tagName, tagInfo]) => {
const columnsWidth = calculateColumnsWidth(options, tagInfo, width);
if (TAGS_WITH_NAME_AS_DESCRIPTION.includes(tagName)) {
columnsWidth.description = 0;
columnsWidth.name = width - columnsWidth.tag - columnsWidth.type;
}
return {
...acc,
[tagName]: {
Expand All @@ -264,7 +269,10 @@ const getTagsData = (lengthByTag, width, options) => Object.entries(lengthByTag)
!options.jsdocAllowDescriptionOnNewLinesForTags.includes(tagName) ||
!tagInfo.hasADescriptionParagraph
) &&
columnsWidth.description >= options.jsdocDescriptionColumnMinLength
(
!columnsWidth.description ||
columnsWidth.description >= options.jsdocDescriptionColumnMinLength
)
),
columnsWidth,
},
Expand Down
72 changes: 72 additions & 0 deletions test/unit/fns/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ jest.unmock('../../../src/fns/renderExampleTag');
jest.unmock('../../../src/fns/splitText');
jest.unmock('../../../src/fns/utils');
jest.unmock('../../../src/options');
jest.unmock('../../../src/constants');

const { render } = require('../../../src/fns/render');
const { defaultOptions } = require('../../../src/options');
Expand Down Expand Up @@ -603,6 +604,77 @@ describe('render', () => {
],
},
},
{
it: 'something',
input: {
description: [
'lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas',
'sollicitudin non justo quis placerat. Quisque eu dignissim tellus, ut',
'sodales lectus',
].join(' '),
tags: [
{
tag: 'callback',
type: '',
name: 'LoremIpsumFn',
description: '',
},
{
tag: 'param',
type: 'string',
name: 'name',
description: 'Lorem ipsum description for the name',
},
{
tag: 'param',
type: 'number',
name: 'length',
description: [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas',
'sollicitudin non justo quis placerat.',
].join(' '),
},
{
tag: 'returns',
type: 'string',
name: '',
description: '',
},
{
tag: 'license',
type: '',
name: '',
description: 'Copyright (c) 2015 Example Corporation Inc.',
descriptionParagrah: true,
},
{
tag: 'see',
type: '',
name: 'Lorem ipsum description for the see tag and something else.',
description: '',
},
],
},
output: [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas',
'sollicitudin non justo quis placerat. Quisque eu dignissim tellus, ut sodales',
'lectus.',
'',
'@callback LoremIpsumFn',
'@param {string} name Lorem ipsum description for the name',
'@param {number} length Lorem ipsum dolor sit amet, consectetur adipiscing',
' elit. Maecenas sollicitudin non justo quis placerat.',
'@returns {string}',
'@license',
'Copyright (c) 2015 Example Corporation Inc.',
'@see Lorem ipsum description for the see tag and something else.',
],
column: 0,
options: {
...defaultOptions,
jsdocPrintWidth: 80,
},
},
];

it.each(cases)('should correctly format the case %#', (caseInfo) => {
Expand Down

0 comments on commit 53819ce

Please sign in to comment.