Skip to content

Commit

Permalink
fix(prettier-plugin-jsdoc): force space between tag name and type
Browse files Browse the repository at this point in the history
  • Loading branch information
homer0 committed Apr 21, 2022
1 parent 824df99 commit 3d94fff
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/fns/getParsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ const generateCommentData = (comment) => {
start: { column },
},
} = comment;
const [block] = commentParser(`/*${comment.value}*/`, {
const commentText = comment.value.replace(
/^(\s*\*\s*@[a-z]+){/gim,
(_, group) => `${group} {`,
);
const [block] = commentParser(`/*${commentText}*/`, {
dotted_names: false,
spacing: 'preserve',
});
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/fixtures/random-03.fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
* hide?: 'narrow' | 'other'
* overlap?: 'next' | 'previous'
* }} SectionValueProps
* @template{InputProps<Value>} [Props=React.ComponentProps<Input>]
*/

//# output
Expand All @@ -31,4 +32,5 @@ module.exports = {
* hide?: 'narrow' | 'other'
* overlap?: 'next' | 'previous'
* }} SectionValueProps
* @template {InputProps<Value>} [Props=React.ComponentProps<Input>]
*/
70 changes: 70 additions & 0 deletions test/unit/fns/getParsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,74 @@ describe('getParsers', () => {
],
});
});

it('should fix a tag without a space between name and type', () => {
// Given
const commentStr = '*\n * @typedef{string} MyStr\n ';
const column = 2;
const astBase = {
comments: [
{
type: 'CommentBlock',
value: commentStr,
loc: {
start: {
column,
},
},
},
],
};
const ast = R.clone(astBase);
const tagsList = [
{
tag: 'typedef',
type: 'string',
name: 'MyStr',
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(() => ['@typedef {string} MyFormattedStr']);
render.mockImplementationOnce(() => renderRest);
tsParser.parsers.typescript.parse.mockImplementationOnce(() => ast);
const text = 'lorem ipsum';
const parsers = ['ts'];
const options = {
jsdocPluginEnabled: true,
printWidth: 80,
};
let sut = null;
// When
sut = getParsers();
sut.typescript.parse(text, parsers, options);
// Then
expect(ast).toEqual({
comments: [
{
type: 'CommentBlock',
value: '*\n * @typedef {string} MyFormattedStr\n ',
loc: {
start: {
column,
},
},
},
],
});
});
});

0 comments on commit 3d94fff

Please sign in to comment.