Skip to content

feat: allow empty line between jsdoc and jscode #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {

};
Expand Down Expand Up @@ -5181,6 +5207,39 @@ 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}}

/** @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}}]

Expand Down
8 changes: 5 additions & 3 deletions src/eslint/getJSDocComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
Expand Down
14 changes: 11 additions & 3 deletions src/iterateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ const getUtils = (
tagNamePreference,
overrideReplacesDocs,
implementsReplacesDocs,
augmentsExtendsReplacesDocs
augmentsExtendsReplacesDocs,
maxLines,
minLines
},
report,
context
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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') || {};
Expand Down Expand Up @@ -418,6 +425,7 @@ const iterateAllJsdocs = (iterator, ruleConfig) => {
};

export {
getSettings,
parseComment
};

Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/rules/requireJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion test/eslint/getJSDocComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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;
}
Expand Down
125 changes: 125 additions & 0 deletions test/rules/assertions/requireJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -1130,6 +1190,71 @@ 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: `
/** @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: [{
Expand Down