Skip to content

Commit

Permalink
feat(prettier-plugin-jsdoc): add function to trim tags properties
Browse files Browse the repository at this point in the history
  • Loading branch information
homer0 committed Oct 22, 2020
1 parent f079621 commit f36904e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/fns/formatTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ const R = require('ramda');
const { formatAccessTag } = require('./formatAccessTag');
const { replaceTagsSynonyms } = require('./replaceTagsSynonyms');
const { sortTags } = require('./sortTags');
const { trimTagsProperties } = require('./trimTagsProperties');

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

/**
* Formats the tags' names and applies sorting, if enabled, to a list of tags.
* Formats the tags' names, trims the values, and applies sorting, if enabled, to a list of tags.
*
* @callback FormatTagsFn
* @param {CommentTag[]} tags The list to format.
Expand All @@ -21,7 +22,10 @@ const { sortTags } = require('./sortTags');
* @type {FormatTagsFn}
*/
const formatTags = R.curry((tags, options) => {
const fns = [formatAccessTag(R.__, options)];
const fns = [
trimTagsProperties,
formatAccessTag(R.__, options),
];

if (options.jsdocReplaceTagsSynonyms) {
fns.push(replaceTagsSynonyms);
Expand Down
23 changes: 23 additions & 0 deletions src/fns/trimTagsProperties.js
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;
1 change: 1 addition & 0 deletions test/unit/fns/formatTags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ jest.unmock('../../../src/fns/utils');
jest.unmock('../../../src/fns/formatAccessTag');
jest.unmock('../../../src/fns/replaceTagsSynonyms');
jest.unmock('../../../src/fns/sortTags');
jest.unmock('../../../src/fns/trimTagsProperties');

const { formatTags } = require('../../../src/fns/formatTags');

Expand Down
32 changes: 32 additions & 0 deletions test/unit/fns/trimTagProperties.test.js
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);
});
});

0 comments on commit f36904e

Please sign in to comment.