diff --git a/.README/README.md b/.README/README.md index 52e71e4e1..6ef0f05f2 100644 --- a/.README/README.md +++ b/.README/README.md @@ -608,6 +608,7 @@ selector). {"gitdown": "include", "file": "./rules/multiline-blocks.md"} {"gitdown": "include", "file": "./rules/no-bad-blocks.md"} {"gitdown": "include", "file": "./rules/no-blank-block-descriptions.md"} +{"gitdown": "include", "file": "./rules/no-blank-blocks.md"} {"gitdown": "include", "file": "./rules/no-defaults.md"} {"gitdown": "include", "file": "./rules/no-missing-syntax.md"} {"gitdown": "include", "file": "./rules/no-multi-asterisks.md"} diff --git a/.README/rules/no-blank-blocks.md b/.README/rules/no-blank-blocks.md new file mode 100644 index 000000000..96da97840 --- /dev/null +++ b/.README/rules/no-blank-blocks.md @@ -0,0 +1,19 @@ +### `no-blank-blocks` + +Reports and optionally removes blocks with whitespace only. + +#### Options + +##### `enableFixer` + +Whether or not to auto-remove the blank block. Defaults to `false`. + +||| +|---|---| +|Context|everywhere| +|Tags|N/A| +|Recommended|false| +|Settings|| +|Options|`enableFixer`| + + diff --git a/README.md b/README.md index 02d13f1a2..cebb03c96 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ JSDoc linting rules for ESLint. * [`multiline-blocks`](#user-content-eslint-plugin-jsdoc-rules-multiline-blocks) * [`no-bad-blocks`](#user-content-eslint-plugin-jsdoc-rules-no-bad-blocks) * [`no-blank-block-descriptions`](#user-content-eslint-plugin-jsdoc-rules-no-blank-block-descriptions) + * [`no-blank-blocks`](#user-content-eslint-plugin-jsdoc-rules-no-blank-blocks) * [`no-defaults`](#user-content-eslint-plugin-jsdoc-rules-no-defaults) * [`no-missing-syntax`](#user-content-eslint-plugin-jsdoc-rules-no-missing-syntax) * [`no-multi-asterisks`](#user-content-eslint-plugin-jsdoc-rules-no-multi-asterisks) @@ -9468,6 +9469,84 @@ function functionWithClearName() {} ```` + + +### no-blank-blocks + +Reports and optionally removes blocks with whitespace only. + + + +#### Options + + + +##### enableFixer + +Whether or not to auto-remove the blank block. Defaults to `false`. + +||| +|---|---| +|Context|everywhere| +|Tags|N/A| +|Recommended|false| +|Settings|| +|Options|`enableFixer`| + +The following patterns are considered problems: + +````js +/** */ +// "jsdoc/no-blank-blocks": ["error"|"warn", {"enableFixer":true}] +// Message: No empty blocks + +/** + */ +// "jsdoc/no-blank-blocks": ["error"|"warn", {"enableFixer":true}] +// Message: No empty blocks + +/** + * + */ +// "jsdoc/no-blank-blocks": ["error"|"warn", {"enableFixer":true}] +// Message: No empty blocks + +/** + * + * + */ +// "jsdoc/no-blank-blocks": ["error"|"warn", {"enableFixer":true}] +// Message: No empty blocks + +/** + * + * + */ +// "jsdoc/no-blank-blocks": ["error"|"warn", {"enableFixer":false}] +// Message: No empty blocks + +/** + * + * + */ +// Message: No empty blocks +```` + +The following patterns are not considered problems: + +````js +/** @tag */ + +/** + * Text + */ + +/** + * @tag + */ +```` + + ### no-defaults @@ -9484,12 +9563,12 @@ tag is attached). Unless your `@default` is on a function, you will need to set `contexts` to an appropriate context, including, if you wish, "any". - - + + #### Options - - + + ##### noOptionalParamNames Set this to `true` to report the presence of optional parameters. May be @@ -9498,8 +9577,8 @@ the presence of ES6 default parameters (bearing in mind that such "defaults" are only applied when the supplied value is missing or `undefined` but not for `null` or other "falsey" values). - - + + ##### contexts Set this to an array of strings representing the AST context (or an object with @@ -9685,12 +9764,12 @@ which are not adequate to satisfy a condition, e.g., not report if there were only a function declaration of the name "ignoreMe" (though it would report by function declarations of other names). - - + + #### Options - - + + ##### contexts Set this to an array of strings representing the AST context (or an object with @@ -9906,12 +9985,12 @@ Note that if you wish to prevent multiple asterisks at the very beginning of the jsdoc block, you should use `no-bad-blocks` (as that is not proper jsdoc and that rule is for catching blocks which only seem like jsdoc). - - + + #### Options - - + + ##### allowWhitespace (defaults to false) Set to `true` if you wish to allow asterisks after a space (as with Markdown): @@ -9922,8 +10001,8 @@ Set to `true` if you wish to allow asterisks after a space (as with Markdown): */ ``` - - + + ##### preventAtMiddleLines (defaults to true) Prevent the likes of this: @@ -9935,8 +10014,8 @@ Prevent the likes of this: */ ``` - - + + ##### preventAtEnd (defaults to true) Prevent the likes of this: @@ -10171,12 +10250,12 @@ structures, (whether or not you add a specific `comment` condition). Note that if your parser supports comment AST (as [jsdoc-eslint-parser](https://github.com/brettz9/jsdoc-eslint-parser) is designed to do), you can just use ESLint's rule. - - + + #### Options - - + + ##### contexts Set this to an array of strings representing the AST context (or an object with @@ -10501,12 +10580,12 @@ This rule reports types being used on `@param` or `@returns`. The rule is intended to prevent the indication of types on tags where the type information would be redundant with TypeScript. - - + + #### Options - - + + ##### contexts Set this to an array of strings representing the AST context (or an object with @@ -10680,8 +10759,8 @@ reporting on use of that namepath elsewhere) and/or that a tag's `type` is `false` (and should not be checked for types). If the `type` is an array, that array's items will be considered as defined for the purposes of that tag. - - + + #### Options An option object may have the following key: @@ -11349,8 +11428,8 @@ class Foo { Requires that each JSDoc line starts with an `*`. - - + + #### Options This rule allows an optional string argument. If it is `"always"` then a @@ -11361,8 +11440,8 @@ and use the `tags` option to apply to specific tags only. After the string option, one may add an object with the following. - - + + ##### tags If you want different values to apply to specific tags, you may use @@ -11646,12 +11725,12 @@ If sentences do not end with terminal punctuation, a period will be added. If sentences do not start with an uppercase character, the initial letter will be capitalized. - - + + #### Options - - + + ##### tags If you want additional tags to be checked for their descriptions, you may @@ -11675,16 +11754,16 @@ its "description" (e.g., for `@returns {someType} some description`, the description is `some description` while for `@some-tag xyz`, the description is `xyz`). - - + + ##### abbreviations You can provide an `abbreviations` options array to avoid such strings of text being treated as sentence endings when followed by dots. The `.` is not necessary at the end of the array items. - - + + ##### newlineBeforeCapsAssumesBadSentenceEnd When `false` (the new default), we will not assume capital letters after @@ -12428,8 +12507,8 @@ Requires that all functions have a description. is `"tag"`) must have a non-empty description that explains the purpose of the method. - - + + #### Options An options object may have any of the following properties: @@ -12990,14 +13069,14 @@ Requires that all functions have examples. * Every example tag must have a non-empty description that explains the method's usage. - - + + #### Options This rule has an object option. - - + + ##### exemptedBy Array of tags (e.g., `['type']`) whose presence on the document @@ -13006,15 +13085,15 @@ block avoids the need for an `@example`. Defaults to an array with so be sure to add back `inheritdoc` if you wish its presence to cause exemption of the rule. - - + + ##### exemptNoArguments Boolean to indicate that no-argument functions should not be reported for missing `@example` declarations. - - + + ##### contexts Set this to an array of strings representing the AST context (or an object with @@ -13026,27 +13105,27 @@ want the rule to apply to any jsdoc block throughout your files. See the ["AST and Selectors"](#user-content-eslint-plugin-jsdoc-advanced-ast-and-selectors) section of our README for more on the expected format. - - + + ##### checkConstructors A value indicating whether `constructor`s should be checked. Defaults to `true`. - - + + ##### checkGetters A value indicating whether getters should be checked. Defaults to `false`. - - + + ##### checkSetters A value indicating whether setters should be checked. Defaults to `false`. - - + + ##### enableFixer A boolean on whether to enable the fixer (which adds an empty `@example` block). @@ -13359,12 +13438,12 @@ Checks that: as being when the overview tag is not preceded by anything other than a comment. - - + + #### Options - - + + ##### tags The keys of this object are tag names, and the values are configuration @@ -13648,8 +13727,8 @@ function quux () { Requires (or disallows) a hyphen before the `@param` description. - - + + #### Options This rule takes one optional string argument and an optional options object. @@ -13881,14 +13960,14 @@ function main(argv) { Checks for presence of jsdoc comments, on class declarations as well as functions. - - + + #### Options Accepts one optional options object with the following optional keys. - - + + ##### publicOnly This option will insist that missing jsdoc blocks are only reported for @@ -13904,8 +13983,8 @@ otherwise noted): - `cjs` - CommonJS exports are checked for JSDoc comments (Defaults to `true`) - `window` - Window global exports are checked for JSDoc comments - - + + ##### require An object with the following optional boolean keys which all default to @@ -13918,8 +13997,8 @@ An object with the following optional boolean keys which all default to - `FunctionExpression` - `MethodDefinition` - - + + ##### contexts Set this to an array of strings or objects representing the additional AST @@ -13936,8 +14015,8 @@ if you are specifying a more precise form in `contexts` (e.g., `MethodDefinition See the ["AST and Selectors"](#user-content-eslint-plugin-jsdoc-advanced-ast-and-selectors) section of our README for more on the expected format. - - + + ##### exemptEmptyConstructors Default: true @@ -13946,8 +14025,8 @@ When `true`, the rule will not report missing jsdoc blocks above constructors with no parameters or return values (this is enabled by default as the class name or description should be seen as sufficient to convey intent). - - + + ##### exemptEmptyFunctions Default: false. @@ -13956,16 +14035,16 @@ When `true`, the rule will not report missing jsdoc blocks above functions/methods with no parameters or return values (intended where function/method names are sufficient for themselves as documentation). - - + + ##### checkConstructors A value indicating whether `constructor`s should be checked. Defaults to `true`. When `true`, `exemptEmptyConstructors` may still avoid reporting when no parameters or return values are found. - - + + ##### checkGetters A value indicating whether getters should be checked. Besides setting as a @@ -13974,8 +14053,8 @@ getters should be checked but only when there is no setter. This may be useful if one only wishes documentation on one of the two accessors. Defaults to `false`. - - + + ##### checkSetters A value indicating whether setters should be checked. Besides setting as a @@ -13984,15 +14063,15 @@ setters should be checked but only when there is no getter. This may be useful if one only wishes documentation on one of the two accessors. Defaults to `false`. - - + + ##### enableFixer A boolean on whether to enable the fixer (which adds an empty jsdoc block). Defaults to `true`. - - + + ##### minLineCount An integer to indicate a minimum number of lines expected for a node in order @@ -15684,12 +15763,12 @@ Will exempt destructured roots and their children if `@param {object} props` will be exempted from requiring a description given `function someFunc ({child1, child2})`). - - + + #### Options - - + + ##### setDefaultDestructuredRootDescription Whether to set a default destructured root description. For example, you may @@ -15698,15 +15777,15 @@ corresponding to a destructured root object as it should always be the same type of object. Uses `defaultDestructuredRootDescription` for the description string. Defaults to `false`. - - + + ##### defaultDestructuredRootDescription The description string to set by default for destructured roots. Defaults to "The root object". - - + + ##### contexts Set this to an array of strings representing the AST context (or an object with @@ -15899,12 +15978,12 @@ Requires that all function parameters have names. > > [JSDoc](https://jsdoc.app/tags-param.html#overview) - - + + #### Options - - + + ##### contexts Set this to an array of strings representing the AST context (or an object with @@ -16043,12 +16122,12 @@ Will exempt destructured roots and their children if `@param props` will be exempted from requiring a type given `function someFunc ({child1, child2})`). - - + + #### Options - - + + ##### setDefaultDestructuredRootType Whether to set a default destructured root type. For example, you may wish @@ -16057,14 +16136,14 @@ corresponding to a destructured root object as it is always going to be an object. Uses `defaultDestructuredRootType` for the type string. Defaults to `false`. - - + + ##### defaultDestructuredRootType The type string to set by default for destructured roots. Defaults to "object". - - + + ##### contexts Set this to an array of strings representing the AST context (or an object with @@ -16429,35 +16508,35 @@ other properties, so in looking at the docs alone without looking at the function signature, it may appear that there is an actual property named `extra`. - - + + #### Options An options object accepts the following optional properties: - - + + ##### enableFixer Whether to enable the fixer. Defaults to `true`. - - + + ##### enableRootFixer Whether to enable the auto-adding of incrementing roots (see the "Fixer" section). Defaults to `true`. Has no effect if `enableFixer` is set to `false`. - - + + ##### enableRestElementFixer Whether to enable the rest element fixer (see "Rest Element (`RestElement`) insertions"). Defaults to `true`. - - + + ##### checkRestProperty If set to `true`, will report (and add fixer insertions) for missing rest @@ -16511,15 +16590,15 @@ function quux ({num, ...extra}) { } ``` - - + + ##### autoIncrementBase Numeric to indicate the number at which to begin auto-incrementing roots. Defaults to `0`. - - + + ##### unnamedRootBase An array of root names to use in the fixer when roots are missing. Defaults @@ -16545,8 +16624,8 @@ function quux ({foo}, [bar], {baz}) { */ ``` - - + + ##### exemptedBy Array of tags (e.g., `['type']`) whose presence on the document block @@ -16555,8 +16634,8 @@ avoids the need for a `@param`. Defaults to an array with so be sure to add back `inheritdoc` if you wish its presence to cause exemption of the rule. - - + + ##### checkTypesPattern When one specifies a type, unless it is of a generic type, like `object` @@ -16591,8 +16670,8 @@ You could set this regular expression to a more expansive list, or you could restrict it such that even types matching those strings would not need destructuring. - - + + ##### contexts Set this to an array of strings representing the AST context (or an object with @@ -16604,33 +16683,33 @@ which are checked. See the ["AST and Selectors"](#user-content-eslint-plugin-jsdoc-advanced-ast-and-selectors) section of our README for more on the expected format. - - + + ##### checkConstructors A value indicating whether `constructor`s should be checked. Defaults to `true`. - - + + ##### checkGetters A value indicating whether getters should be checked. Defaults to `false`. - - + + ##### checkSetters A value indicating whether setters should be checked. Defaults to `false`. - - + + ##### checkDestructured Whether to require destructured properties. Defaults to `true`. - - + + ##### checkDestructuredRoots Whether to check the existence of a corresponding `@param` for root objects @@ -16643,8 +16722,8 @@ implied to be `false` (i.e., the inside of the roots will not be checked either, e.g., it will also not complain if `a` or `b` do not have their own documentation). Defaults to `true`. - - + + ##### useDefaultObjectProperties Set to `true` if you wish to expect documentation of properties on objects @@ -18329,8 +18408,8 @@ is found. Also reports if `@returns {never}` is discovered with a return value. Will also report if multiple `@returns` tags are present. - - + + #### Options - `exemptGenerators`- Because a generator might be labeled as having a @@ -19356,12 +19435,12 @@ Requires that the `@returns` tag has a `description` value. The error will not be reported if the return value is `void` or `undefined` or if it is `Promise` or `Promise`. - - + + #### Options - - + + ##### contexts Set this to an array of strings representing the AST context (or an object with @@ -19515,12 +19594,12 @@ function quux () { Requires that `@returns` tag has `type` value. - - + + #### Options - - + + ##### contexts Set this to an array of strings representing the AST context (or an object with @@ -19641,8 +19720,8 @@ Requires that returns are documented. Will also report if multiple `@returns` tags are present. - - + + #### Options - `checkConstructors` - A value indicating whether `constructor`s should @@ -20791,8 +20870,8 @@ for our desire for a separate tag to document rejection types and see [this discussion](https://stackoverflow.com/questions/50071115/typescript-promise-rejection-type) on why TypeScript doesn't offer such a feature. - - + + #### Options - `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the @@ -21096,8 +21175,8 @@ Will also report if multiple `@yields` tags are present. See the `next`, `forceRequireNext`, and `nextWithGeneratorTag` options for an option to expect a non-standard `@next` tag. - - + + #### Options - `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the @@ -21908,8 +21987,8 @@ function bodies. Will also report if multiple `@yields` tags are present. - - + + #### Options - `checkGeneratorsOnly` - Avoids checking the function body and merely insists @@ -22433,12 +22512,12 @@ line break setting this rule will attempt to do when not disabled. You may, however, safely set the "any" option in that rule along with `startLines` and/or `endLines`. - - + + #### Options - - + + ##### tagSequence An array of tag group objects indicating the preferred sequence for sorting tags. @@ -22622,8 +22701,8 @@ a fixed order that doesn't change into the future, supply your own ]}]; ``` - - + + ##### alphabetizeExtras Defaults to `false`. Alphabetizes any items not within `tagSequence` after any @@ -22633,16 +22712,16 @@ are sorted. If you want all your tags alphabetized, you can supply an empty array for `tagSequence` along with setting this option to `true`. - - + + ##### linesBetween Indicates the number of lines to be added between tag groups. Defaults to 1. Do not set to 0 or 2+ if you are using `tag-lines` and `"always"` and do not set to 1+ if you are using `tag-lines` and `"never"`. - - + + ##### reportTagGroupSpacing Whether to enable reporting and fixing of line breaks between tag groups @@ -22650,8 +22729,8 @@ as set by `linesBetween`. Defaults to `true`. Note that the very last tag will not have spacing applied regardless. For adding line breaks there, you may wish to use the `endLines` option of the `tag-lines` rule. - - + + ##### reportIntraTagGroupSpacing Whether to enable reporting and fixing of line breaks within tags of a given @@ -23029,8 +23108,8 @@ The "always" or "never" options of this rule should not be used with the linebreak-setting options of the `sort-tags` rule as both may try to impose a conflicting number of lines. - - + + #### Options The first option is a single string set to "always", "never", or "any" @@ -23044,36 +23123,36 @@ between tags. The second option is an object with the following optional properties. - - + + ##### count (defaults to 1) Use with "always" to indicate the number of lines to require be present. - - + + ##### applyToEndTag (defaults to true) Set to `false` and use with "always" to indicate the normal lines to be added after tags should not be added after the final tag. - - + + ##### startLines (defaults to 0) If not set to `null`, will enforce end lines to the given count before the first tag only, unless there is only whitespace content, in which case, a line count will not be enforced. - - + + ##### endLines (defaults to 0) If not set to `null`, will enforce end lines to the given count on the final tag only. - - + + ##### tags (default to empty object) Overrides the default behavior depending on specific tags. @@ -23548,19 +23627,19 @@ Markdown and you therefore do not wish for it to be accidentally interpreted as such by the likes of Visual Studio Code or if you wish to view it escaped within it or your documentation. - - + + #### Options - - + + ##### escapeHTML This option escapes all `<` and `&` characters (except those followed by whitespace which are treated as literals by Visual Studio Code). - - + + ##### escapeMarkdown This option escapes the first backtick (`` ` ``) in a paired sequence. @@ -23767,8 +23846,8 @@ for valid types (based on the tag's `type` value), and either portion checked for presence (based on `false` `name` or `type` values or their `required` value). See the setting for more details. - - + + #### Options - `allowEmptyNamepaths` (default: true) - Set to `false` to bulk disallow diff --git a/src/index.js b/src/index.js index 2964dced5..257f0245c 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,7 @@ import matchName from './rules/matchName'; import multilineBlocks from './rules/multilineBlocks'; import noBadBlocks from './rules/noBadBlocks'; import noBlankBlockDescriptions from './rules/noBlankBlockDescriptions'; +import noBlankBlocks from './rules/noBlankBlocks'; import noDefaults from './rules/noDefaults'; import noMissingSyntax from './rules/noMissingSyntax'; import noMultiAsterisks from './rules/noMultiAsterisks'; @@ -72,6 +73,7 @@ const index = { 'multiline-blocks': multilineBlocks, 'no-bad-blocks': noBadBlocks, 'no-blank-block-descriptions': noBlankBlockDescriptions, + 'no-blank-blocks': noBlankBlocks, 'no-defaults': noDefaults, 'no-missing-syntax': noMissingSyntax, 'no-multi-asterisks': noMultiAsterisks, @@ -132,6 +134,7 @@ const createRecommendedRuleset = (warnOrError) => { 'jsdoc/multiline-blocks': warnOrError, 'jsdoc/no-bad-blocks': 'off', 'jsdoc/no-blank-block-descriptions': 'off', + 'jsdoc/no-blank-blocks': 'off', 'jsdoc/no-defaults': 'off', 'jsdoc/no-missing-syntax': 'off', 'jsdoc/no-multi-asterisks': warnOrError, diff --git a/src/rules/noBlankBlocks.js b/src/rules/noBlankBlocks.js new file mode 100644 index 000000000..28f5e88cc --- /dev/null +++ b/src/rules/noBlankBlocks.js @@ -0,0 +1,53 @@ +import iterateJsdoc from '../iterateJsdoc'; + +export default iterateJsdoc(({ + context, + jsdoc, + utils, +}) => { + if (jsdoc.tags.length) { + return; + } + + const { + description, + lastDescriptionLine, + } = utils.getDescription(); + if (description.trim()) { + return; + } + + const { + enableFixer, + } = context.options[0] || {}; + + utils.reportJSDoc( + 'No empty blocks', + { + line: lastDescriptionLine, + }, + enableFixer ? () => { + jsdoc.source.splice(0, jsdoc.source.length); + } : null, + ); +}, { + iterateAllJsdocs: true, + meta: { + docs: { + description: 'Removes empty blocks with nothing but possibly line breaks', + url: 'https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-no-blank-blocks', + }, + fixable: 'code', + schema: [ + { + additionalProperties: false, + properties: { + enableFixer: { + type: 'boolean', + }, + }, + }, + ], + type: 'suggestion', + }, +}); diff --git a/test/rules/assertions/noBlankBlocks.js b/test/rules/assertions/noBlankBlocks.js new file mode 100644 index 000000000..93a8ef7f0 --- /dev/null +++ b/test/rules/assertions/noBlankBlocks.js @@ -0,0 +1,136 @@ +export default { + invalid: [ + { + code: ` + /** */ + `, + errors: [ + { + line: 2, + message: 'No empty blocks', + }, + ], + options: [ + { + enableFixer: true, + }, + ], + output: ` + `, + }, + { + code: ` + /** + */ + `, + errors: [ + { + line: 2, + message: 'No empty blocks', + }, + ], + options: [ + { + enableFixer: true, + }, + ], + output: ` + `, + }, + { + code: ` + /** + * + */ + `, + errors: [ + { + line: 3, + message: 'No empty blocks', + }, + ], + options: [ + { + enableFixer: true, + }, + ], + output: ` + `, + }, + { + code: ` + /** + * + * + */ + `, + errors: [ + { + line: 4, + message: 'No empty blocks', + }, + ], + options: [ + { + enableFixer: true, + }, + ], + output: ` + `, + }, + { + code: ` + /** + * + * + */ + `, + errors: [ + { + line: 4, + message: 'No empty blocks', + }, + ], + options: [ + { + enableFixer: false, + }, + ], + }, + { + code: ` + /** + * + * + */ + `, + errors: [ + { + line: 4, + message: 'No empty blocks', + }, + ], + }, + ], + valid: [ + { + code: ` + /** @tag */ + `, + }, + { + code: ` + /** + * Text + */ + `, + }, + { + code: ` + /** + * @tag + */ + `, + }, + ], +}; diff --git a/test/rules/ruleNames.json b/test/rules/ruleNames.json index f4a84e27d..7cff81eab 100644 --- a/test/rules/ruleNames.json +++ b/test/rules/ruleNames.json @@ -18,6 +18,7 @@ "multiline-blocks", "no-bad-blocks", "no-blank-block-descriptions", + "no-blank-blocks", "no-defaults", "no-missing-syntax", "no-multi-asterisks",