Skip to content

Commit

Permalink
fix: jsdocKeepUnParseAbleExampleIndent
Browse files Browse the repository at this point in the history
issue: #69
  • Loading branch information
hosseinmd committed Feb 12, 2021
1 parent 4a9cbd2 commit 8f18200
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,20 @@ const stringify = (
tagString = `${tagString} ${exampleCaption[0]}`;
}

// Remove two space from lines, maybe added previous format
if (
description
.split("\n")
.slice(1)
.every((v) => !v.trim() || v.startsWith(" "))
) {
description = description.replace(/\n[^\S\r\n]{2}/g, "\n");
}

try {
let formattedExample = "";
const examplePrintWith = printWidth - " ".length;

description = description.replace(/\n[^\S\r\n]{2}/g, "\n"); // Remove two space from lines, maybe added previous format

// If example is a json
if (description.trim().startsWith("{")) {
formattedExample = format(description || "", {
Expand Down
14 changes: 14 additions & 0 deletions tests/__snapshots__/exampleTag.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ exports[`example json 1`] = `
"
`;

exports[`example unParseAble 1`] = `
"/**
* @example
* Prism.languages['css-with-colors'] = Prism.languages.extend('css', {
* // Prism.languages.css already has a 'comment' token, so this token will overwrite CSS' 'comment' token
* // at its original position
* 'comment': { ... },
* // CSS doesn't have a 'color' token, so this token will be appended
* 'color': /(?:red|green|blue)/
* });
*/
"
`;

exports[`examples Json 1`] = `
"/**
* @example
Expand Down
10 changes: 5 additions & 5 deletions tests/__snapshots__/files/prism-core.js.shot
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ var Prism = (function (_self) {
*
* @example
* Prism.languages['css-with-colors'] = Prism.languages.extend('css', {
* // Prism.languages.css already has a 'comment' token, so this token will overwrite CSS' 'comment' token
* // at its original position
* 'comment': { ... },
* // CSS doesn't have a 'color' token, so this token will be appended
* 'color': /\\\\b(?:red|green|blue)\\\\b/
* // Prism.languages.css already has a 'comment' token, so this token will overwrite CSS' 'comment' token
* // at its original position
* 'comment': { ... },
* // CSS doesn't have a 'color' token, so this token will be appended
* 'color': /\\\\b(?:red|green|blue)\\\\b/
* });
*
* @param {string} id The id of the language to extend. This has to be a key in \`Prism.languages\`.
Expand Down
26 changes: 26 additions & 0 deletions tests/exampleTag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,29 @@ test("example should be same after few time format ", () => {
expect(result).toEqual(result2);
expect(result).toEqual(result3);
});

test("example unParseAble", () => {
const result = subject(
`/**
* @example
* Prism.languages['css-with-colors'] = Prism.languages.extend('css', {
* // Prism.languages.css already has a 'comment' token, so this token will overwrite CSS' 'comment' token
* // at its original position
* 'comment': { ... },
* // CSS doesn't have a 'color' token, so this token will be appended
* 'color': /\b(?:red|green|blue)\b/
* });
*/`,
{
jsdocKeepUnParseAbleExampleIndent: true,
},
);

expect(result).toMatchSnapshot();
const result2 = subject(result, {
jsdocKeepUnParseAbleExampleIndent: true,
});

expect(result2).toEqual(result);
expect(subject(result)).not.toEqual(result);
});

0 comments on commit 8f18200

Please sign in to comment.