Skip to content

Commit

Permalink
feat(prettier-plugin-jsdoc): add function to prepare all tags
Browse files Browse the repository at this point in the history
  • Loading branch information
homer0 committed Oct 13, 2020
1 parent 2c4b0cd commit 5812fd6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/fns/prepareTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const R = require('ramda');
const { prepareTagName } = require('./prepareTagName');
const { prepareTagPrettyType } = require('./prepareTagPrettyType');

/**
* @typedef {import('../types').PrettierOptions} PrettierOptions
* @typedef {import('../types').CommentTag} CommentTag
*/

/**
* Takes a list of tags and runs them through all the preparations needed for them to be rendered.
* Preparations include adding the brackes for optional parameters and running Prettier for
* complex types.
*
* @callback PrepareTagsFn
* @param {CommentTag[]} tags The list of tags to format.
* @param {PrettierOptions} options The options sent to the plugin, in case they're needed for
* Prettier.
* @returns {CommentTag[]}
*/

/**
* @type {PrepareTagsFn}
*/
const prepareTags = R.curry((tags, options) => R.map(
R.compose(
prepareTagPrettyType(R.__, options),
prepareTagName,
),
tags,
));

module.exports.prepareTags = prepareTags;
68 changes: 68 additions & 0 deletions test/fns/prepareTags.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
jest.unmock('../../src/fns/prepareTags');
jest.unmock('../../src/fns/prepareTagName');
jest.unmock('../../src/fns/prepareTagPrettyType');
jest.unmock('../../src/fns/utils');
jest.mock('prettier');

const { format } = require('prettier');
const { prepareTags } = require('../../src/fns/prepareTags');

describe('prepareTags', () => {
beforeEach(() => {
format.mockClear();
});

it('should apply all the preparation functions on a tags list', () => {
// Given
const prettierResponse = 'prettier-response';
format.mockImplementationOnce((code) => code.replace(/=.*?$/, `= ${prettierResponse};`));
const input = [
{
name: 'someComponent',
type: 'React.FC<string>',
optional: true,
},
{
name: 'someName',
type: 'string',
optional: true,
default: '\'myName\'',
},
{
name: 'rest',
type: 'string',
},
];
const output = [
{
name: '[someComponent]',
type: prettierResponse,
optional: true,
},
{
name: '[someName=\'myName\']',
type: 'string',
optional: true,
default: '\'myName\'',
},
{
name: 'rest',
type: 'string',
},
];
const options = {
semi: true,
indent: 2,
};
let result = null;
// When
result = prepareTags(input, options);
// Then
expect(result).toEqual(output);
expect(format).toHaveBeenCalledTimes(1);
expect(format).toHaveBeenCalledWith(`type complex = ${input[0].type}`, {
...options,
parser: 'typescript',
});
});
});

0 comments on commit 5812fd6

Please sign in to comment.