Skip to content

Commit

Permalink
feat(prettier-plugin-jsdoc): allow inline comments
Browse files Browse the repository at this point in the history
  • Loading branch information
homer0 committed Oct 26, 2020
1 parent 1573db8 commit f386566
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/fns/createParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,17 @@ const getRenderer = (options) => {
return (column, block) => {
const padding = ' '.repeat(column + 1);
const prefix = `${padding}* `;
const lines = renderer(column, block)
const lines = renderer(column, block);

if (lines.length === 1 && options.jsdocUseInlineCommentForASingleTagBlock) {
return `* ${lines[0]} `;
}

const useLines = lines
.map((line) => `${prefix}${line}`)
.join('\n');
return `*\n${lines}\n${padding}`;

return `*\n${useLines}\n${padding}`;
};
};

Expand Down
6 changes: 6 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ const options = {
'If enabled, when evaluating the rule for consistent columns, tags with description ' +
'on a new line, allowed by `jsdocAllowDescriptionOnNewLinesForTags`, will be ignored.',
},
jsdocUseInlineCommentForASingleTagBlock: {
type: 'boolean',
category: 'jsdoc',
default: false,
description: 'Whether or not to use a single line JSDoc block when there\'s only one tag.',
},
};
/**
* @type {PJPOptions}
Expand Down
2 changes: 2 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@
* @property {boolean} jsdocIgnoreNewLineDescriptionsForConsistentColumns
* If enabled, when evaluating the rule for consistent columns, tags with description on a new line,
* allowed by `jsdocAllowDescriptionOnNewLinesForTags`, will be ignored. Default `true`.
* @property {boolean} jsdocUseInlineCommentForASingleTagBlock
* Whether or not to use a single line JSDoc block when there's only one tag. Default `false`.
*/

/* eslint-disable max-len */
Expand Down
63 changes: 63 additions & 0 deletions test/unit/fns/createParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,67 @@ describe('createParser', () => {
expect(formatDescriptionRest).toHaveBeenCalledTimes(1);
expect(formatDescriptionRest).toHaveBeenCalledWith(parsed[0]);
});

it('should render an inline comment', () => {
// Given
const commentStr = '*\n * @type {MyStr}\n ';
const column = 2;
const astBase = {
comments: [{
type: 'CommentBlock',
value: commentStr,
loc: {
start: {
column,
},
},
}],
};
const ast = R.clone(astBase);
const tagsList = [{
tag: 'type',
type: 'MyStr',
name: '',
description: '',
}];
const parsed = [{
description: '',
tags: tagsList,
}];
commentParser.mockImplementationOnce(() => parsed);
const formatTagsTypesRest = jest.fn((tags) => tags);
formatTagsTypes.mockImplementationOnce(() => formatTagsTypesRest);
const formatTagsRest = jest.fn((tags) => tags);
formatTags.mockImplementationOnce(() => formatTagsRest);
const formatDescriptionRest = jest.fn((tags) => tags);
formatDescription.mockImplementationOnce(() => formatDescriptionRest);
const prepareTagsRest = jest.fn((tags) => tags);
prepareTags.mockImplementationOnce(() => prepareTagsRest);
const renderRest = jest.fn(() => [
'@type {MyFormattedStr}',
]);
render.mockImplementationOnce(() => renderRest);

const originalParser = jest.fn(() => ast);
const text = 'lorem ipsum';
const parsers = ['babel'];
const options = {
printWidth: 80,
jsdocUseInlineCommentForASingleTagBlock: true,
};
// When
createParser(originalParser)(text, parsers, options);
// Then
expect(ast).toEqual({
comments: [{
type: 'CommentBlock',
value: '* @type {MyFormattedStr} ',
loc: {
start: {
column,
},
},
}],
});
});
});

0 comments on commit f386566

Please sign in to comment.