-
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 sort tags
- Loading branch information
Showing
3 changed files
with
124 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,29 @@ | ||
const R = require('ramda'); | ||
const { getIndexOrFallback } = require('./utils'); | ||
/** | ||
* @typedef {import('../types').CommentTag} CommentTag | ||
* @typedef {import('../types').PJPTagsOptions} PJPTagsOptions | ||
*/ | ||
|
||
/** | ||
* Creates the function used by `Array.sort` to actually sort the tags based on a reference list. | ||
* | ||
* @param {string[]} ref The reference list with the order for the tags. | ||
* @returns {Function} | ||
*/ | ||
const createSorter = (ref) => { | ||
const fallback = getIndexOrFallback(ref, ref.length, 'other'); | ||
const getTagWeight = getIndexOrFallback(ref, fallback); | ||
return (a, b) => getTagWeight(a.tag) - getTagWeight(b.tag); | ||
}; | ||
|
||
/** | ||
* Sorts a list of tags based on reference list from the plugin options. | ||
* | ||
* @param {CommentTag[]} tags The list of tags to sort. | ||
* @param {PJPTagsOptions} options The options that tell the function how to sort them. | ||
* @returns {CommentTag[]} | ||
*/ | ||
const sortTags = R.curry((tags, options) => R.sort(createSorter(options.jsdocTagsOrder))(tags)); | ||
|
||
module.exports.sortTags = sortTags; |
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
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,73 @@ | ||
jest.unmock('../../src/fns/sortTags'); | ||
jest.unmock('../../src/fns/utils'); | ||
|
||
const { sortTags } = require('../../src/fns/sortTags'); | ||
|
||
describe('sortTags', () => { | ||
const cases = [ | ||
{ | ||
it: 'should sort the tags and correctly use \'other\' as fallback', | ||
input: [ | ||
{ tag: 'param' }, | ||
{ tag: 'description' }, | ||
{ tag: 'param' }, | ||
{ tag: 'returns' }, | ||
{ tag: 'todo' }, | ||
{ tag: 'throws' }, | ||
], | ||
output: [ | ||
{ tag: 'description' }, | ||
{ tag: 'param' }, | ||
{ tag: 'param' }, | ||
{ tag: 'returns' }, | ||
{ tag: 'throws' }, | ||
{ tag: 'todo' }, | ||
], | ||
options: { | ||
jsdocTagsOrder: [ | ||
'description', | ||
'param', | ||
'returns', | ||
'other', | ||
'todo', | ||
], | ||
}, | ||
}, | ||
{ | ||
it: 'should send the tag to the end of the list of \'other\' is not present', | ||
input: [ | ||
{ tag: 'throws' }, | ||
{ tag: 'param' }, | ||
{ tag: 'description' }, | ||
{ tag: 'param' }, | ||
{ tag: 'returns' }, | ||
{ tag: 'todo' }, | ||
], | ||
output: [ | ||
{ tag: 'description' }, | ||
{ tag: 'param' }, | ||
{ tag: 'param' }, | ||
{ tag: 'returns' }, | ||
{ tag: 'todo' }, | ||
{ tag: 'throws' }, | ||
], | ||
options: { | ||
jsdocTagsOrder: [ | ||
'description', | ||
'param', | ||
'returns', | ||
'todo', | ||
], | ||
}, | ||
}, | ||
]; | ||
|
||
it.each(cases)('should correctly format the case %#', (caseInfo) => { | ||
// Given | ||
let result = null; | ||
// When | ||
result = sortTags(caseInfo.input, caseInfo.options); | ||
// Then | ||
expect(result).toEqual(caseInfo.output); | ||
}); | ||
}); |