Skip to content

Commit

Permalink
fix(require-jsdoc): add fixer to add empty jsdoc block
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 authored and l1bbcsg committed Aug 8, 2019
1 parent 37e8300 commit b48cf1d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 22 deletions.
15 changes: 11 additions & 4 deletions README.md
Expand Up @@ -4769,8 +4769,8 @@ function myFunction() {
/**
* @func myFunction
*/


function myFunction() {

}
Expand Down Expand Up @@ -4839,6 +4839,13 @@ function quux (foo) {
// Options: [{"exemptEmptyFunctions":true}]
// Message: Missing JSDoc comment.

function quux (foo) {

}
// Settings: {"jsdoc":{"minLines":2}}
// Options: [{"exemptEmptyFunctions":true}]
// Message: Missing JSDoc comment.

function myFunction() {}
// Message: Missing JSDoc comment.

Expand Down Expand Up @@ -5231,10 +5238,10 @@ function myFunction() {
}
// Settings: {"jsdoc":{"maxLines":0,"minLines":0}}

/**
/**
* @func myFunction
*/

function myFunction() {

}
Expand Down
4 changes: 1 addition & 3 deletions src/iterateJsdoc.js
Expand Up @@ -91,9 +91,7 @@ const getUtils = (
const utils = {};

utils.stringify = (tagBlock) => {
let indent = sourceCode.text.match(/^\n*([ \t]+)/);
/* istanbul ignore next */
indent = indent ? indent[1] + indent[1].charAt() : ' ';
const indent = jsdocUtils.getIndent(sourceCode);

return commentStringify([tagBlock], {indent}).slice(indent.length - 1);
};
Expand Down
9 changes: 9 additions & 0 deletions src/jsdocUtils.js
Expand Up @@ -601,12 +601,21 @@ const getAncestor = (sourceCode, nde, depth, idx = 0) => {
return null;
};

const getIndent = (sourceCode) => {
let indent = sourceCode.text.match(/^\n*([ \t]+)/);
/* istanbul ignore next */
indent = indent ? indent[1] + indent[1].charAt() : ' ';

return indent;
};

export default {
enforcedContexts,
filterTags,
getAncestor,
getContextObject,
getFunctionParameterNames,
getIndent,
getJsdocParameterNames,
getJsdocParameterNamesDeep,
getPreferredTagName,
Expand Down
16 changes: 16 additions & 0 deletions src/rules/requireJsdoc.js
Expand Up @@ -125,6 +125,8 @@ export default {
url: 'https://github.com/gajus/eslint-plugin-jsdoc'
},

fixable: 'code',

messages: {
missingJsDoc: 'Missing JSDoc comment.'
},
Expand Down Expand Up @@ -158,6 +160,18 @@ export default {
}
}

const fix = (fixer) => {
// Default to one line break if the `minLines`/`maxLines` settings allow
const lines = settings.minLines === 0 && settings.maxLines >= 1 ? 1 : settings.minLines;
const indent = jsdocUtils.getIndent(sourceCode);
const insertion = `/**\n${indent}*\n${indent}*/${'\n'.repeat(lines)}${indent.slice(0, -1)}`;
const baseNode = [
'ExportDefaultDeclaration', 'ExportNamedDeclaration'
].includes(node.parent && node.parent.type) ? node.parent : node;

return fixer.insertTextBefore(baseNode, insertion);
};

if (publicOnly) {
const opt = {
ancestorsOnly: Boolean(_.get(publicOnly, 'ancestorsOnly', false)),
Expand All @@ -170,12 +184,14 @@ export default {

if (exported && !jsDocNode) {
context.report({
fix,
messageId: 'missingJsDoc',
node
});
}
} else {
context.report({
fix,
messageId: 'missingJsDoc',
node
});
Expand Down
71 changes: 56 additions & 15 deletions test/rules/assertions/requireJsdoc.js
Expand Up @@ -10,7 +10,7 @@ export default {
* @func myFunction
*/
function myFunction() {
}
`,
errors: [
Expand All @@ -30,10 +30,10 @@ export default {
/**
* @func myFunction
*/
function myFunction() {
}
`,
errors: [
Expand All @@ -50,7 +50,7 @@ export default {
{
code: `
/** @func myFunction */ function myFunction() {
}
`,
errors: [
Expand Down Expand Up @@ -245,6 +245,12 @@ export default {
ClassDeclaration: true
}
}],
output: `
/**
*
*/
export default class {}
`,
parserOptions: {
sourceType: 'module'
}
Expand Down Expand Up @@ -288,7 +294,42 @@ export default {
],
options: [
{exemptEmptyFunctions: true}
]
],
output: `
/**
*
*/
function quux (foo) {
}`
},
{
code: `
function quux (foo) {
}`,
errors: [
{
line: 2,
message: 'Missing JSDoc comment.'
}
],
options: [
{exemptEmptyFunctions: true}
],
output: `
/**
*
*/
function quux (foo) {
}`,
settings: {
jsdoc: {
minLines: 2
}
}
},
{
code: 'function myFunction() {}',
Expand Down Expand Up @@ -1195,9 +1236,9 @@ export default {
/**
* @func myFunction
*/
function myFunction() {
}
`,
settings: {
Expand All @@ -1212,10 +1253,10 @@ export default {
/**
* @func myFunction
*/
function myFunction() {
}
`,
settings: {
Expand All @@ -1228,7 +1269,7 @@ export default {
{
code: `
/** @func myFunction */ function myFunction() {
}
`,
settings: {
Expand All @@ -1240,12 +1281,12 @@ export default {
},
{
code: `
/**
/**
* @func myFunction
*/
function myFunction() {
}
`,
settings: {
Expand Down

0 comments on commit b48cf1d

Please sign in to comment.