From c3fb1680d9fd20678d4fb4069395e14fc0440e19 Mon Sep 17 00:00:00 2001 From: Hossein Mohammadi Date: Fri, 5 Nov 2021 20:40:09 +0330 Subject: [PATCH] feat: add jsdocAddDefaultToDescription --- .eslintrc.js | 7 +- package.json | 3 +- src/custom.d.ts | 2 +- src/index.ts | 10 + src/parser.ts | 9 +- src/types.ts | 1 + .../files/create-ignorer.js.shot | 46 - tests/__snapshots__/files/order.jsx.shot | 19 - tests/__snapshots__/files/prism-core.js.shot | 1231 ----------------- .../files/prism-dependencies.js.shot | 480 ------- tests/__snapshots__/files/tsdoc.ts.shot | 113 -- tests/__snapshots__/files/typeScript.js.shot | 210 --- tests/__snapshots__/files/typeScript.ts.shot | 119 -- tests/__snapshots__/files/types.ts.shot | 52 - tests/__snapshots__/main.test.ts.snap | 11 + tests/files.test.ts | 2 +- tests/main.test.ts | 17 + tsconfig.json | 3 +- yarn.lock | 28 +- 19 files changed, 83 insertions(+), 2280 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index a3e36a1..5e76e9f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -9,7 +9,12 @@ module.exports = { ecmaVersion: "next", }, parser: "@typescript-eslint/parser", - extends: ["prettier", "eslint:recommended"], + plugins: ["@typescript-eslint"], + extends: [ + "prettier", + "eslint:recommended", + "plugin:@typescript-eslint/recommended", + ], rules: { "@typescript-eslint/no-explicit-any": "off", "no-unused-vars": "off", diff --git a/package.json b/package.json index f3af335..f71d4f9 100755 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "devDependencies": { "@commitlint/config-conventional": "^14.1.0", "@react-native-community/eslint-config": "^3.0.1", + "@typescript-eslint/eslint-plugin": "^5.3.0", "@types/jest": "^27.0.2", "@types/mdast": "^3.0.10", "@typescript-eslint/parser": "^5.3.0", @@ -76,4 +77,4 @@ "engines": { "node": ">=12.0.0" } -} +} \ No newline at end of file diff --git a/src/custom.d.ts b/src/custom.d.ts index 8088fd9..6f0e491 100644 --- a/src/custom.d.ts +++ b/src/custom.d.ts @@ -1,5 +1,5 @@ declare namespace jest { - interface Matchers { + interface Matchers { toMatchSpecificSnapshot(filename: string): R; } } diff --git a/src/index.ts b/src/index.ts index c52e836..b44022b 100755 --- a/src/index.ts +++ b/src/index.ts @@ -97,6 +97,14 @@ const options: Record = { description: "If You don't set value to jsdocPrintWidth, the printWidth will be use as jsdocPrintWidth.", }, + jsdocAddDefaultToDescription: { + since: "0.3.29", + name: "jsdocAddDefaultToDescription", + type: "boolean", + category: "jsdoc", + default: true, + description: "Add Default value of a param to end description", + }, }; const defaultOptions: JsdocOptions = { @@ -113,6 +121,8 @@ const defaultOptions: JsdocOptions = { jsdocSeparateTagGroups: options.jsdocSeparateTagGroups.default as boolean, jsdocCapitalizeDescription: options.jsdocCapitalizeDescription .default as boolean, + jsdocAddDefaultToDescription: options.jsdocAddDefaultToDescription + .default as boolean, tsdoc: options.tsdoc.default as boolean, }; diff --git a/src/parser.ts b/src/parser.ts index 25aa12a..ac58f87 100755 --- a/src/parser.ts +++ b/src/parser.ts @@ -130,9 +130,11 @@ export const getParser = (originalParse: Parser["parse"], parserName: string) => return [tag]; }); } + if (options.jsdocAddDefaultToDescription) { + tags = tags.map(addDefaultValueToDescription); + } tags = tags - .map(addDefaultValueToDescription) .map(assignOptionalAndDefaultToName) .map(({ type, name, description, tag, ...rest }) => { const isVerticallyAlignAbleTags = @@ -415,7 +417,6 @@ function convertCommentDescToDescTag(parsed: Block): void { * This is for find params of function name in code as strings of array */ function getParamsOrders(ast: AST, tokenIndex: number): string[] | undefined { - let paramsOrder: string[] | undefined; let params: Token[] | undefined; try { @@ -476,14 +477,14 @@ function getParamsOrders(ast: AST, tokenIndex: number): string[] | undefined { } } - paramsOrder = params + return params ?.filter(({ type }) => typeof type === "object" && type.label === "name") .map(({ value }) => value); } catch { // } - return paramsOrder; + return; } /** diff --git a/src/types.ts b/src/types.ts index fabfea3..11d420f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -11,6 +11,7 @@ export interface JsdocOptions { jsdocSingleLineComment: boolean; jsdocSeparateReturnsFromParam: boolean; jsdocSeparateTagGroups: boolean; + jsdocAddDefaultToDescription: boolean; jsdocCapitalizeDescription: boolean; tsdoc: boolean; } diff --git a/tests/__snapshots__/files/create-ignorer.js.shot b/tests/__snapshots__/files/create-ignorer.js.shot index eaafc15..6c16068 100644 --- a/tests/__snapshots__/files/create-ignorer.js.shot +++ b/tests/__snapshots__/files/create-ignorer.js.shot @@ -45,49 +45,3 @@ function _createIgnorer(ignoreContent, withNodeModules) { module.exports = createIgnorer; " `; - -exports[`File: create-ignorer.js index: 5 1`] = ` -"\\"use strict\\"; - -const path = require(\\"path\\"); -const ignore = require(\\"ignore\\"); -const getFileContentOrNull = require(\\"../utils/get-file-content-or-null\\"); - -/** - * @param {string | null} ignorePath - * @param {boolean | null} withNodeModules - */ -async function createIgnorer(ignorePath, withNodeModules) { - const ignoreContent = ignorePath - ? await getFileContentOrNull(path.resolve(ignorePath)) - : null; - - return _createIgnorer(ignoreContent, withNodeModules); -} - -/** - * @param {string | null} ignorePath - * @param {boolean | null} withNodeModules - */ -createIgnorer.sync = function (ignorePath, withNodeModules) { - const ignoreContent = !ignorePath - ? null - : getFileContentOrNull.sync(path.resolve(ignorePath)); - return _createIgnorer(ignoreContent, withNodeModules); -}; - -/** - * @param {null | string} ignoreContent - * @param {boolean | null} withNodeModules - */ -function _createIgnorer(ignoreContent, withNodeModules) { - const ignorer = ignore().add(ignoreContent || \\"\\"); - if (!withNodeModules) { - ignorer.add(\\"node_modules\\"); - } - return ignorer; -} - -module.exports = createIgnorer; -" -`; diff --git a/tests/__snapshots__/files/order.jsx.shot b/tests/__snapshots__/files/order.jsx.shot index d55ebc5..c3fce5a 100644 --- a/tests/__snapshots__/files/order.jsx.shot +++ b/tests/__snapshots__/files/order.jsx.shot @@ -18,22 +18,3 @@ exports[`File: order.jsx 1`] = ` */ " `; - -exports[`File: order.jsx index: 4 1`] = ` -"/** - * @callback ClassMapper - * @param {string} className - * @param {string} language - * @returns {string} - * - * @callback ClassAdder - * @param {ClassAdderEnvironment} env - * @returns {undefined | string | string[]} - * - * @typedef ClassAdderEnvironment - * @property {string} language - * @property {string} type - * @property {string} content - */ -" -`; diff --git a/tests/__snapshots__/files/prism-core.js.shot b/tests/__snapshots__/files/prism-core.js.shot index 4c79256..accee5d 100644 --- a/tests/__snapshots__/files/prism-core.js.shot +++ b/tests/__snapshots__/files/prism-core.js.shot @@ -1230,1234 +1230,3 @@ if (typeof global !== 'undefined') { */ " `; - -exports[`File: prism-core.js index: 6 1`] = ` -"/// - -var _self = - typeof window !== 'undefined' - ? window // if in browser - : typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope - ? self // if in worker - : {}; // if in node js - -/** - * Prism: Lightweight, robust, elegant syntax highlighting - * - * @license MIT [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT) - * @author Lea Verou [https://lea.verou.me](https://lea.verou.me) - * @namespace - * @public - */ -var Prism = (function (_self) { - // Private helper vars - var lang = /\\\\blang(?:uage)?-([\\\\w-]+)\\\\b/i; - var uniqueId = 0; - - var _ = { - /** - * By default, Prism will attempt to highlight all code elements (by calling {@link Prism.highlightAll}) on the - * current page after the page finished loading. This might be a problem if e.g. you wanted to asynchronously load - * additional languages or plugins yourself. - * - * By setting this value to \`true\`, Prism will not automatically highlight all code elements on the page. - * - * You obviously have to change this value before the automatic highlighting started. To do this, you can add an - * empty Prism object into the global scope before loading the Prism script like this: - * - * \`\`\`js - * window.Prism = window.Prism || {}; - * Prism.manual = true; - * // add a new