From 5cfff519bc378c03ab3c07f8accbb985f4d14f26 Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Tue, 23 Jul 2019 01:59:21 +0900 Subject: [PATCH 1/3] feat: allowing empty line via setting api --- src/eslint/getJSDocComment.js | 8 +++--- src/iterateJsdoc.js | 14 ++++++++--- src/rules/requireJsdoc.js | 5 +++- test/eslint/getJSDocComment.js | 4 ++- test/rules/assertions/requireJsdoc.js | 35 +++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/eslint/getJSDocComment.js b/src/eslint/getJSDocComment.js index 49fef7ca1..7e42875a2 100644 --- a/src/eslint/getJSDocComment.js +++ b/src/eslint/getJSDocComment.js @@ -32,12 +32,13 @@ const looksLikeExport = function (astNode) { * * @param {SourceCode} sourceCode The ESLint SourceCode * @param {ASTNode} node The AST node to get the comment for. + * @param {object} settings The settings in context * @returns {Token|null} The Block comment token containing the JSDoc comment * for the given node or null if not found. * @public * @deprecated */ -const getJSDocComment = function (sourceCode, node) { +const getJSDocComment = function (sourceCode, node, settings) { /** * Checks for the presence of a JSDoc comment for the given node and returns it. * @@ -48,13 +49,14 @@ const getJSDocComment = function (sourceCode, node) { */ const findJSDocComment = (astNode) => { const tokenBefore = sourceCode.getTokenBefore(astNode, {includeComments: true}); - + const {minLines, maxLines} = settings; if ( tokenBefore && isCommentToken(tokenBefore) && tokenBefore.type === 'Block' && tokenBefore.value.charAt(0) === '*' && - astNode.loc.start.line - tokenBefore.loc.end.line <= 1 + astNode.loc.start.line - tokenBefore.loc.end.line >= minLines && + astNode.loc.start.line - tokenBefore.loc.end.line <= maxLines ) { return tokenBefore; } diff --git a/src/iterateJsdoc.js b/src/iterateJsdoc.js index 0ee3c84e7..dda28d801 100644 --- a/src/iterateJsdoc.js +++ b/src/iterateJsdoc.js @@ -68,7 +68,9 @@ const getUtils = ( tagNamePreference, overrideReplacesDocs, implementsReplacesDocs, - augmentsExtendsReplacesDocs + augmentsExtendsReplacesDocs, + maxLines, + minLines }, report, context @@ -246,7 +248,10 @@ const getUtils = ( utils.getClassJsdoc = () => { const classNode = utils.getClassNode(); - const classJsdocNode = getJSDocComment(sourceCode, classNode); + const classJsdocNode = getJSDocComment(sourceCode, classNode, { + maxLines, + minLines + }); if (classJsdocNode) { const indent = ' '.repeat(classJsdocNode.loc.start.column); @@ -302,6 +307,8 @@ const getSettings = (context) => { // All rules settings.ignorePrivate = Boolean(_.get(context, 'settings.jsdoc.ignorePrivate')); + settings.minLines = Number(_.get(context, 'settings.jsdoc.minLines', 0)); + settings.maxLines = Number(_.get(context, 'settings.jsdoc.maxLines', 1)); // `check-tag-names` and many returns/param rules settings.tagNamePreference = _.get(context, 'settings.jsdoc.tagNamePreference') || {}; @@ -418,6 +425,7 @@ const iterateAllJsdocs = (iterator, ruleConfig) => { }; export { + getSettings, parseComment }; @@ -461,7 +469,7 @@ export default function iterateJsdoc (iterator, ruleConfig) { const settings = getSettings(context); const checkJsdoc = (node) => { - const jsdocNode = getJSDocComment(sourceCode, node); + const jsdocNode = getJSDocComment(sourceCode, node, settings); if (!jsdocNode) { return; diff --git a/src/rules/requireJsdoc.js b/src/rules/requireJsdoc.js index e4b5ebbca..abc6c0b70 100644 --- a/src/rules/requireJsdoc.js +++ b/src/rules/requireJsdoc.js @@ -3,6 +3,7 @@ import jsdocUtils from '../jsdocUtils'; import exportParser from '../exportParser'; import getJSDocComment from '../eslint/getJSDocComment'; import warnRemovedSettings from '../warnRemovedSettings'; +import {getSettings} from '../iterateJsdoc'; const OPTIONS_SCHEMA = { additionalProperties: false, @@ -141,8 +142,10 @@ export default { const {require: requireOption, publicOnly, exemptEmptyFunctions} = getOptions(context); + const settings = getSettings(context); + const checkJsDoc = (node) => { - const jsDocNode = getJSDocComment(sourceCode, node); + const jsDocNode = getJSDocComment(sourceCode, node, settings); if (jsDocNode) { return; diff --git a/test/eslint/getJSDocComment.js b/test/eslint/getJSDocComment.js index 82f2f4150..98df2d135 100644 --- a/test/eslint/getJSDocComment.js +++ b/test/eslint/getJSDocComment.js @@ -2,6 +2,7 @@ import { RuleTester } from 'eslint'; import getJSDocComment from '../../src/eslint/getJSDocComment'; +import {getSettings} from '../../src/iterateJsdoc'; /* eslint-disable sort-keys */ const rule = { @@ -13,10 +14,11 @@ const rule = { }, create (context) { const sourceCode = context.getSourceCode(); + const settings = getSettings(context); return { ObjectExpression: (node) => { - const comment = getJSDocComment(sourceCode, node); + const comment = getJSDocComment(sourceCode, node, settings); if (comment !== null) { return; } diff --git a/test/rules/assertions/requireJsdoc.js b/test/rules/assertions/requireJsdoc.js index 5bdebb74a..6d286a4a6 100644 --- a/test/rules/assertions/requireJsdoc.js +++ b/test/rules/assertions/requireJsdoc.js @@ -1130,6 +1130,41 @@ export default { Object.keys(object).forEach(function() {}) ` }, + { + code: ` + /** + * @func myFunction + */ + + function myFunction() { + + } + `, + settings: { + jsdoc: { + maxLines: 2, + minLines: 0 + } + } + }, + { + code: ` + /** + * @func myFunction + */ + + + function myFunction() { + + } + `, + settings: { + jsdoc: { + maxLines: 3, + minLines: 0 + } + } + }, { code: 'function myFunction() {}', options: [{ From f29f79afd0d1c59926ee624aba5c3ccb8757460c Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Tue, 23 Jul 2019 02:00:03 +0900 Subject: [PATCH 2/3] docs: generate docs --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 67f26b2d4..c0ab87dae 100644 --- a/README.md +++ b/README.md @@ -5181,6 +5181,25 @@ Object.keys(this.options.rules || {}).forEach(function(name) {}.bind(this)); var object = { name: 'key'}; Object.keys(object).forEach(function() {}) +/** + * @func myFunction + */ + +function myFunction() { + +} +// Settings: {"jsdoc":{"maxLines":2,"minLines":0}} + +/** + * @func myFunction + */ + + +function myFunction() { + +} +// Settings: {"jsdoc":{"maxLines":3,"minLines":0}} + function myFunction() {} // Options: [{"require":{"ClassDeclaration":true,"FunctionDeclaration":false,"MethodDefinition":true}}] From aba717da5ca033a683fbcc92f82687838fa33e2a Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Tue, 23 Jul 2019 20:50:21 +0900 Subject: [PATCH 3/3] feat: add test cases for min,max lines settings --- README.md | 40 ++++++++++++ test/rules/assertions/requireJsdoc.js | 90 +++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/README.md b/README.md index c0ab87dae..32cd188e1 100644 --- a/README.md +++ b/README.md @@ -4757,6 +4757,32 @@ Accepts one optional options object with the following optional keys. The following patterns are considered problems: ````js +/** + * @func myFunction + */ +function myFunction() { + +} +// Settings: {"jsdoc":{"maxLines":3,"minLines":2}} +// Message: Missing JSDoc comment. + +/** + * @func myFunction + */ + + +function myFunction() { + +} +// Settings: {"jsdoc":{"maxLines":2}} +// Message: Missing JSDoc comment. + +/** @func myFunction */ function myFunction() { + +} +// Settings: {"jsdoc":{"minLines":1}} +// Message: Missing JSDoc comment. + export var test = function () { }; @@ -5200,6 +5226,20 @@ function myFunction() { } // Settings: {"jsdoc":{"maxLines":3,"minLines":0}} +/** @func myFunction */ function myFunction() { + +} +// Settings: {"jsdoc":{"maxLines":0,"minLines":0}} + +/** + * @func myFunction + */ + +function myFunction() { + +} +// Settings: {"jsdoc":{"maxLines":3,"minLines":2}} + function myFunction() {} // Options: [{"require":{"ClassDeclaration":true,"FunctionDeclaration":false,"MethodDefinition":true}}] diff --git a/test/rules/assertions/requireJsdoc.js b/test/rules/assertions/requireJsdoc.js index 6d286a4a6..a6d215573 100644 --- a/test/rules/assertions/requireJsdoc.js +++ b/test/rules/assertions/requireJsdoc.js @@ -4,6 +4,66 @@ export default { invalid: [ + { + code: ` + /** + * @func myFunction + */ + function myFunction() { + + } + `, + errors: [ + { + message: 'Missing JSDoc comment.' + } + ], + settings: { + jsdoc: { + maxLines: 3, + minLines: 2 + } + } + }, + { + code: ` + /** + * @func myFunction + */ + + + function myFunction() { + + } + `, + errors: [ + { + message: 'Missing JSDoc comment.' + } + ], + settings: { + jsdoc: { + maxLines: 2 + } + } + }, + { + code: ` + /** @func myFunction */ function myFunction() { + + } + `, + errors: [ + { + message: 'Missing JSDoc comment.' + } + ], + settings: { + jsdoc: { + minLines: 1 + } + } + }, { code: ` export var test = function () { @@ -1165,6 +1225,36 @@ export default { } } }, + { + code: ` + /** @func myFunction */ function myFunction() { + + } + `, + settings: { + jsdoc: { + maxLines: 0, + minLines: 0 + } + } + }, + { + code: ` + /** + * @func myFunction + */ + + function myFunction() { + + } + `, + settings: { + jsdoc: { + maxLines: 3, + minLines: 2 + } + } + }, { code: 'function myFunction() {}', options: [{