Skip to content

Commit

Permalink
fix: bug paraghrap with number in start of description
Browse files Browse the repository at this point in the history
  • Loading branch information
hosseinmd committed Jan 7, 2021
1 parent 9c2a33b commit 23297b8
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 35 deletions.
3 changes: 1 addition & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
"useTabs": false,
"tabWidth": 2,
"bracketSpacing": true,
"trailingComma": "all",
"plugins": ["."]
"trailingComma": "all"
}
91 changes: 60 additions & 31 deletions src/descriptionFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { Comment } from "comment-parser";
import { JsdocOptions } from "./types";
import { capitalizer } from "./utils";

const EMPTY_LINE_SIGNATURE = "a2@^5!~#sdE!_EMPTY_LINE_SIGNATURE";
const EMPTY_LINE_SIGNATURE = "2@^5!~#sdE!_EMPTY_LINE_SIGNATURE";
const NEW_LINE_START_THREE_SPACE_SIGNATURE =
"a2@^5!~#sdE!_NEW_LINE_START_THREE_SPACE_SIGNATURE";
const NEW_LINE_START_WITH_DASH = "a2@^5!~#sdE!_NEW_LINE_START_WITH_DASH";
"2@^5!~#sdE!_NEW_LINE_START_THREE_SPACE_SIGNATURE";
const NEW_LINE_START_WITH_DASH = "2@^5!~#sdE!_NEW_LINE_START_WITH_DASH";
const NEW_LINE_START_WITH_NUMBER = "2@^5!~#sdE!_NEW_LINE_START_WITH_NUMBER";
const NEW_PARAGRAPH_START_WITH_DASH =
"a2@^5!~#sdE!_NEW_PARAGRAPH_START_WITH_DASH";
"2@^5!~#sdE!_NEW_PARAGRAPH_START_WITH_DASH";
const NEW_PARAGRAPH_START_THREE_SPACE_SIGNATURE =
"a2@^5!~#sdE!_NEW_PARAGRAPH_START_THREE_SPACE_SIGNATURE";
"2@^5!~#sdE!_NEW_PARAGRAPH_START_THREE_SPACE_SIGNATURE";

interface DescriptionEndLineParams {
description: string;
Expand Down Expand Up @@ -56,8 +57,26 @@ function formatDescription(

if (!text) return text;

/**
* Description
*
* # Example
*
* Summry
*/
text = text.replace(/[\n\s]+([#]+)(.*)[\n\s]+/g, "\n\n$1 $2\n\n");

/**
* 1. a thing
*
* 2. another thing
*/
text = text.replace(/^(\d+)[-.][\s-.|]+/g, "$1. "); // Start
text = text.replace(
/\n?[\n\s]+(\d+)[-.][\s-.|]+/g,
NEW_LINE_START_WITH_NUMBER + "$1. ",
);

text = text.replace(
/(\n\n\s\s\s+)|(\n\s+\n\s\s\s+)/g,
NEW_PARAGRAPH_START_THREE_SPACE_SIGNATURE,
Expand Down Expand Up @@ -99,32 +118,44 @@ function formatDescription(
.map(
(newEmptyLineWithDash) =>
newEmptyLineWithDash
.split(NEW_PARAGRAPH_START_WITH_DASH)
.split(NEW_LINE_START_WITH_NUMBER)
.map(
(newLineWithDash) =>
newLineWithDash
.split(NEW_LINE_START_WITH_DASH)
.map((paragraph) => {
paragraph = paragraph.replace(/(\s+|)\n(\s+|)/g, " "); // Make single line

paragraph = capitalizer(paragraph);
if (options.jsdocDescriptionWithDot)
paragraph = paragraph.replace(/(\w)(?=$)/g, "$1."); // Insert dot if needed

return paragraph
.split(NEW_LINE_START_THREE_SPACE_SIGNATURE)
.map((value) =>
breakDescriptionToLines(
value,
maxWidth,
beginningSpace,
),
)
.join("\n "); // NEW_LINE_START_THREE_SPACE_SIGNATURE
})
.join("\n- "), // NEW_LINE_START_WITH_DASH
(newLineWithNumber) =>
newLineWithNumber
.split(NEW_PARAGRAPH_START_WITH_DASH)
.map(
(newLineWithDash) =>
newLineWithDash
.split(NEW_LINE_START_WITH_DASH)
.map((paragraph) => {
paragraph = paragraph.replace(
/(\s+|)\n(\s+|)/g,
" ",
); // Make single line

paragraph = capitalizer(paragraph);
if (options.jsdocDescriptionWithDot)
paragraph = paragraph.replace(
/(\w)(?=$)/g,
"$1.",
); // Insert dot if needed

return paragraph
.split(NEW_LINE_START_THREE_SPACE_SIGNATURE)
.map((value) =>
breakDescriptionToLines(
value,
maxWidth,
beginningSpace,
),
)
.join("\n "); // NEW_LINE_START_THREE_SPACE_SIGNATURE
})
.join("\n- "), // NEW_LINE_START_WITH_DASH
)
.join("\n\n- "), // NEW_PARAGRAPH_START_WITH_DASH
)
.join("\n\n- "), // NEW_PARAGRAPH_START_WITH_DASH
.join("\n"), // NEW_LINE_START_WITH_NUMBER
)
.join("\n\n"); // EMPTY_LINE_SIGNATURE
})
Expand All @@ -137,8 +168,6 @@ function formatDescription(
text = `- ${text}`;
}

text = text.replace(/\n[\s|]+(\d+)[-.][\s+-.|]+/g, "\n$1. ");

text = format(text, {
...options,
parser: "markdown",
Expand Down
30 changes: 30 additions & 0 deletions tests/__snapshots__/paragraph.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,33 @@ exports[`numbers and code in description 1`] = `
*/
"
`;
exports[`numbers and code in description 2`] = `
"/**
* 1. a keydown event occurred immediately before a focus event
* 2. a focus event happened on an element which requires keyboard interaction
* (e.g., a text field);
*/
"
`;
exports[`numbers and code in description 3`] = `
"/**
* The script uses two heuristics to determine whether the keyboard is being used:
*
* 1. a keydown event occurred lorem ipsum dolor sit amet, consectetur
* adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
* aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
* nisi ut aliqimmediately before a focus event;
* 2. a focus evenlorem ipsum dolor sit amet, consectetur adipiscing elit, sed
* do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
* minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliqt
* happened on an element which requires keyboard interaction (e.g., a text field);
*
* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
* tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
* quis nostrud exercitation ullamco laboris nisi ut aliq W3C Software Notice
* and License: https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
*/
"
`;
27 changes: 25 additions & 2 deletions tests/paragraph.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ test("numbers and code in description", () => {
*
* const pressResponder = new PressResponder(config);
*
* 2. Choose the rendered component who should collect the press events. On that
* 2. Choose the rendered component who should collect the press events. On that
* element, spread \`pressability.getEventHandlers()\` into its props.
*
* return (
Expand All @@ -178,6 +178,29 @@ test("numbers and code in description", () => {
*
*/
`);

expect(result1).toMatchSnapshot();

const result2 = subject(`
/**
* 1- a keydown event occurred immediately before a focus event
* 2- a focus event happened on an element which requires keyboard interaction (e.g., a text field);
*/
`);
expect(result2).toMatchSnapshot();

const result3 = subject(`
/**
* The script uses two heuristics to determine whether the keyboard is being used:
*
* 1. a keydown event occurred lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliqimmediately before a focus event;
* 2. a focus evenlorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliqt happened on an element which requires keyboard interaction (e.g., a text field);
*
* lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliq
* W3C Software Notice and License: https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
*
*/
`);
expect(result3).toMatchSnapshot();
});

0 comments on commit 23297b8

Please sign in to comment.