From 5812fd6d7c3f732b8b468ffa71e2ae8452246a90 Mon Sep 17 00:00:00 2001 From: homer0 Date: Tue, 13 Oct 2020 03:37:45 -0300 Subject: [PATCH] feat(prettier-plugin-jsdoc): add function to prepare all tags --- src/fns/prepareTags.js | 33 +++++++++++++++++ test/fns/prepareTags.test.js | 68 ++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/fns/prepareTags.js create mode 100644 test/fns/prepareTags.test.js diff --git a/src/fns/prepareTags.js b/src/fns/prepareTags.js new file mode 100644 index 0000000..e0d8c88 --- /dev/null +++ b/src/fns/prepareTags.js @@ -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; diff --git a/test/fns/prepareTags.test.js b/test/fns/prepareTags.test.js new file mode 100644 index 0000000..2048a2d --- /dev/null +++ b/test/fns/prepareTags.test.js @@ -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', + 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', + }); + }); +});