-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prettier-plugin-jsdoc): add function to prepare all tags
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}); | ||
}); |