-
Notifications
You must be signed in to change notification settings - Fork 1
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 trim tags properties
- Loading branch information
Showing
4 changed files
with
62 additions
and
2 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
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,23 @@ | ||
/** | ||
* @typedef {import('../types').CommentTag} CommentTag | ||
*/ | ||
|
||
/** | ||
* Takes a list of tags and trims the values of the properties the plugins uses (type, name and | ||
* description). | ||
* | ||
* @param {CommentTag[]} tags The list of tags to format. | ||
* @returns {CommentTag[]} | ||
*/ | ||
const trimTagsProperties = (tags) => { | ||
const knownProperties = ['type', 'name', 'description']; | ||
return tags.map((tag) => Object.entries(tag).reduce( | ||
(acc, [key, value]) => ({ | ||
...acc, | ||
[key]: knownProperties.includes(key) ? value.trim() : value, | ||
}), | ||
{}, | ||
)); | ||
}; | ||
|
||
module.exports.trimTagsProperties = trimTagsProperties; |
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,32 @@ | ||
jest.unmock('../../../src/fns/trimTagsProperties'); | ||
|
||
const { trimTagsProperties } = require('../../../src/fns/trimTagsProperties'); | ||
|
||
describe('trimTagsProperties', () => { | ||
it('should correctly trim the properties of a tags list', () => { | ||
// Given | ||
const input = [ | ||
{ | ||
tag: 'param', | ||
type: ' string', | ||
name: 'myParam ', | ||
description: ' my description ', | ||
unknown: ' this will be ignored ', | ||
}, | ||
]; | ||
const output = [ | ||
{ | ||
tag: 'param', | ||
type: 'string', | ||
name: 'myParam', | ||
description: 'my description', | ||
unknown: ' this will be ignored ', | ||
}, | ||
]; | ||
let result = null; | ||
// When | ||
result = trimTagsProperties(input); | ||
// Then | ||
expect(result).toEqual(output); | ||
}); | ||
}); |