diff --git a/.prettierrc b/.prettierrc index 37bad4b..25e2654 100755 --- a/.prettierrc +++ b/.prettierrc @@ -4,6 +4,5 @@ "useTabs": false, "tabWidth": 2, "bracketSpacing": true, - "trailingComma": "all", - "plugins": ["."] + "trailingComma": "all" } diff --git a/src/descriptionFormatter.ts b/src/descriptionFormatter.ts index 4938b2a..d23d6a0 100644 --- a/src/descriptionFormatter.ts +++ b/src/descriptionFormatter.ts @@ -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; @@ -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, @@ -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 }) @@ -137,8 +168,6 @@ function formatDescription( text = `- ${text}`; } - text = text.replace(/\n[\s|]+(\d+)[-.][\s+-.|]+/g, "\n$1. "); - text = format(text, { ...options, parser: "markdown", diff --git a/tests/__snapshots__/paragraph.test.js.snap b/tests/__snapshots__/paragraph.test.js.snap index 58027f2..e06a0b8 100644 --- a/tests/__snapshots__/paragraph.test.js.snap +++ b/tests/__snapshots__/paragraph.test.js.snap @@ -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 + */ +" +`; diff --git a/tests/paragraph.test.js b/tests/paragraph.test.js index 1f66401..1b4855d 100644 --- a/tests/paragraph.test.js +++ b/tests/paragraph.test.js @@ -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 ( @@ -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(); });