Skip to content

Commit

Permalink
fix: description start with markdown
Browse files Browse the repository at this point in the history
issue: #201
  • Loading branch information
hosseinmd committed Aug 18, 2023
1 parent 3ffa8c7 commit 9129957
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/descriptionFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ async function formatDescription(

let tableIndex = 0;

text = text.replace(
new RegExp("\\n" + "\\s".repeat(beginningSpace.length), "g"),
"\n",
);

const rootAst = fromMarkdown(text);

async function stringifyASTWithoutChildren(
Expand Down
4 changes: 1 addition & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export const getParser = (originalParse: Parser["parse"], parserName: string) =>
return {
type,
name,
description: description.trim(),
description,
tag,
...rest,
};
Expand Down Expand Up @@ -408,8 +408,6 @@ function normalizeTags(parsed: Block): void {
type = "";
}

description = description.trim();

return {
tag,
type,
Expand Down
11 changes: 8 additions & 3 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ const stringify = async (
.trimEnd();
} // Add description (complicated because of text wrap)
else if (description) {
let descriptionString = "";
if (useTagTitle) tagString += gap + " ".repeat(descGapAdj);
if (
TAGS_PEV_FORMATE_DESCRIPTION.includes(tag) ||
!TAGS_ORDER.includes(tag)
) {
// Avoid wrapping
tagString += description;
descriptionString = description;
} else {
const [, firstWord] = /^\s*(\S+)/.exec(description) || ["", ""];

Expand All @@ -141,20 +142,24 @@ const stringify = async (
[REMARKS, PRIVATE_REMARKS].includes(tag)
) {
// the tag is already longer than we are allowed to, so let's start at a new line
tagString +=
descriptionString =
`\n${beginningSpace}` +
(await formatDescription(tag, description, options, {
beginningSpace,
}));
} else {
// append the description to the tag
tagString += await formatDescription(tag, description, options, {
descriptionString = await formatDescription(tag, description, options, {
// 1 is `\n` which added to tagString
tagStringLength: tagString.length - 1,
beginningSpace,
});
}
}

tagString += descriptionString.startsWith("\n")
? descriptionString.replace(/^\n[\s]+\n/g, "\n")
: descriptionString.trimStart();
}

// Add empty line after some tags if there is something below
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ async function formatCode(
}`,
)
.join("\n")}\n`;

result = result.replace(/^\n[\s]+\n/g, "\n");
}

return result;
Expand Down
3 changes: 3 additions & 0 deletions tests/__snapshots__/compatibleWithPlugins.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import a from \\"a\\";
* @memberof test
* @example
* //prettier-plugin-fake
*
* var one = 5;
* var two = 10;
*
Expand Down Expand Up @@ -105,7 +106,9 @@ import a from \\"a\\";
* @memberof test
* @example
* //prettier-plugin-fake
*
* //prettier-plugin-fake
*
* var one = 5;
* var two = 10;
*
Expand Down
37 changes: 37 additions & 0 deletions tests/__snapshots__/remarks.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Description start with markdown 1`] = `
"/**
* Just a simple test
*
* @remarks
* - Remark 1
* - Remark 2
* - Remark 3
*/
"
`;

exports[`Description start with markdown 2`] = `
"/**
* Just a simple test
*
* @remarks
* - Remark 1
* - Remark 2
* - Remark 3
*/
"
`;

exports[`JS code should be formatted as usuall 1`] = `
"/**
* Just a simple test
*
* @remarks
* - Remark 1
* - Remark 2
* - Remark 3
*/
"
`;
82 changes: 82 additions & 0 deletions tests/files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as prettier from "prettier";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { AllOptions } from "../src/types";

import "jest-specific-snapshot";

function subjectFiles(relativePath: string, options: Partial<AllOptions> = {}) {
const filepath = resolve(process.cwd(), "tests", relativePath);

try {
const code = readFileSync(filepath).toString();

return prettier.format(code, {
plugins: ["prettier-plugin-jsdoc"],
jsdocSpaces: 1,
trailingComma: "all",
filepath,
...options,
} as AllOptions);
} catch (error) {
console.error(error);
}
}

const PrismOptions = {
arrowParens: "avoid",
printWidth: 120,
quoteProps: "preserve",
semi: true,
singleQuote: true,
tabWidth: 4,
trailingComma: "none",
useTabs: true,
jsdocKeepUnParseAbleExampleIndent: true,
} as const;

/**
* @type {TestFile[]}
*
* @typedef TestFile
* @property {string} name
* @property {import("prettier").Options} [options]
*/
const files: {
name: string;
options?: Partial<AllOptions>;
}[] = [
{ name: "typeScript.js" },
{ name: "typeScript.js" },
{ name: "typeScript.ts" },
{ name: "types.ts" },
{ name: "order.jsx" },
{ name: "create-ignorer.js" },
{
name: "prism-core.js",
options: PrismOptions,
},
{
name: "prism-dependencies.js",
options: {
jsdocSeparateTagGroups: true,
...PrismOptions,
},
},
{
name: "tsdoc.ts",
options: {
tsdoc: true,
},
},
];

for (let i = 0; i < files.length; i++) {
const { name, options } = files[i];
test(`File: ${name}`, async () => {
const result = await subjectFiles("./files/" + name, options);
(expect(result) as any).toMatchSpecificSnapshot(
`./__snapshots__/files/${name}.shot`,
);
});
}
48 changes: 48 additions & 0 deletions tests/remarks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { format } from "prettier";
import { AllOptions } from "../src/types";

function subject(code: string, options: Partial<AllOptions> = {}) {
return format(code, {
parser: "babel",
plugins: ["prettier-plugin-jsdoc"],
...options,
} as AllOptions);
}

test("Description start with markdown", async () => {
const result = await subject(
`
/**
* Just a simple test
*
* @remarks
* - Remark 1
* - Remark 2
* - Remark 3
*/
`,
{
tsdoc: true,
},
);

expect(result).toMatchSnapshot();

const result2 = await subject(
`
/**
* Just a simple test
*
* @remarks
* - Remark 1
* - Remark 2
* - Remark 3
*/
`,
{
tsdoc: false,
},
);

expect(result2).toMatchSnapshot();
});

0 comments on commit 9129957

Please sign in to comment.