Skip to content

Commit

Permalink
fix(require-jsdoc): avoid reporting inner functions missing jsdoc w…
Browse files Browse the repository at this point in the history
…hen pubilc-only jsdoc is being checked and missing; fixes #812
  • Loading branch information
brettz9 committed Dec 15, 2021
1 parent f04a444 commit d62a2a7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12986,6 +12986,16 @@ export class InovaAutoCompleteComponent {
export default (arg) => arg;
// "jsdoc/require-jsdoc": ["error"|"warn", {"publicOnly":true,"require":{"ArrowFunctionExpression":true,"ClassDeclaration":true,"ClassExpression":true,"FunctionDeclaration":true,"FunctionExpression":true,"MethodDefinition":true}}]
// Message: Missing JSDoc comment.

export function outer() {
function inner() {
console.log('foo');
}

inner();
}
// "jsdoc/require-jsdoc": ["error"|"warn", {"publicOnly":true,"require":{"ArrowFunctionExpression":true,"ClassDeclaration":true,"ClassExpression":true,"FunctionDeclaration":true,"FunctionExpression":true,"MethodDefinition":true}}]
// Message: Missing JSDoc comment.
````

The following patterns are not considered problems:
Expand Down
7 changes: 7 additions & 0 deletions src/exportParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,19 @@ const findNode = function (node, block, cache) {
const exportTypes = new Set(['ExportNamedDeclaration', 'ExportDefaultDeclaration']);
const getExportAncestor = function (nde) {
let node = nde;
let idx = 0;
while (node) {
// Ignore functions nested more deeply than say `export default function () {}`
if (idx >= 2 && nde.type === 'FunctionDeclaration') {
break;
}

if (exportTypes.has(node.type)) {
return node;
}

node = node.parent;
idx++;
}

return false;
Expand Down
43 changes: 43 additions & 0 deletions test/rules/assertions/requireJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3312,6 +3312,49 @@ function quux (foo) {
sourceType: 'module',
},
},
{
code: `
export function outer() {
function inner() {
console.log('foo');
}
inner();
}
`,
errors: [
{
line: 2,
message: 'Missing JSDoc comment.',
},
],
options: [{
publicOnly: true,
require: {
ArrowFunctionExpression: true,
ClassDeclaration: true,
ClassExpression: true,
FunctionDeclaration: true,
FunctionExpression: true,
MethodDefinition: true,
},
}],
output: `
/**
*
*/
export function outer() {
function inner() {
console.log('foo');
}
inner();
}
`,
parserOptions: {
sourceType: 'module',
},
},
],
valid: [{
code: `
Expand Down

0 comments on commit d62a2a7

Please sign in to comment.