Skip to content

Commit

Permalink
feat(prettier-plugin-jsdoc): add function to render example tags
Browse files Browse the repository at this point in the history
  • Loading branch information
homer0 committed Oct 22, 2020
1 parent d005cd0 commit 3e9cd9d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/fns/renderExampleTag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @typedef {import('../types').CommentTag} CommentTag
*/

/**
* Renders an `example` tag by just adding the code below the tag.
*
* @param {CommentTag} tag The tag to render.
* @returns {string[]}
*/
const renderExampleTag = (tag) => [
`@${tag.tag}`,
...tag.description.split('\n'),
];

module.exports.renderExampleTag = renderExampleTag;
23 changes: 23 additions & 0 deletions test/unit/fns/renderExampleTag.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
jest.unmock('../../../src/fns/renderExampleTag');

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

describe('renderExampleTag', () => {
it('should ignore a tag that\'s not @example', () => {
// Given
const input = {
tag: 'example',
description: 'const fn = (msg) => console.log(msg);\nfn(\'hello world!\');',
};
const output = [
'@example',
'const fn = (msg) => console.log(msg);',
'fn(\'hello world!\');',
];
let result = null;
// When
result = renderExampleTag(input);
// Then
expect(result).toEqual(output);
});
});

0 comments on commit 3e9cd9d

Please sign in to comment.