Skip to content

Commit

Permalink
fix(require-jsdoc): do not report MethodDefinition with non-publi…
Browse files Browse the repository at this point in the history
…c `accessibility`; fixes #1124
  • Loading branch information
brettz9 committed Jun 28, 2023
1 parent 1e45ae4 commit 00922c3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
11 changes: 9 additions & 2 deletions docs/rules/require-jsdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ export default class Test {
this.a = a;
}
}
// "jsdoc/require-jsdoc": ["error"|"warn", {"contexts":["MethodDefinition:not([accessibility=\"private\"]) > FunctionExpression"],"publicOnly":true,"require":{"ArrowFunctionExpression":false,"ClassDeclaration":false,"ClassExpression":false,"FunctionDeclaration":false,"FunctionExpression":false,"MethodDefinition":false}}]
// "jsdoc/require-jsdoc": ["error"|"warn", {"contexts":["MethodDefinition > FunctionExpression"],"publicOnly":true,"require":{"ArrowFunctionExpression":false,"ClassDeclaration":false,"ClassExpression":false,"FunctionDeclaration":false,"FunctionExpression":false,"MethodDefinition":false}}]
// Message: Missing JSDoc comment.

e = function () {
Expand Down Expand Up @@ -1675,7 +1675,7 @@ export default class Test {
this.a = a;
}
}
// "jsdoc/require-jsdoc": ["error"|"warn", {"contexts":["MethodDefinition:not([accessibility=\"private\"]) > FunctionExpression"],"publicOnly":true,"require":{"ArrowFunctionExpression":false,"ClassDeclaration":false,"ClassExpression":false,"FunctionDeclaration":false,"FunctionExpression":false,"MethodDefinition":false}}]
// "jsdoc/require-jsdoc": ["error"|"warn", {"contexts":["MethodDefinition > FunctionExpression"],"publicOnly":true,"require":{"ArrowFunctionExpression":false,"ClassDeclaration":false,"ClassExpression":false,"FunctionDeclaration":false,"FunctionExpression":false,"MethodDefinition":false}}]

/**
* Basic application controller.
Expand Down Expand Up @@ -1846,5 +1846,12 @@ class A {
}
}
// "jsdoc/require-jsdoc": ["error"|"warn", {"contexts":[{"context":"MethodDefinition","minLineCount":4}],"require":{"ClassDeclaration":false,"FunctionExpression":false,"MethodDefinition":false}}]

export default class Test {
private abc(a) {
this.a = a;
}
}
// "jsdoc/require-jsdoc": ["error"|"warn", {"publicOnly":true,"require":{"ArrowFunctionExpression":false,"ClassDeclaration":false,"ClassExpression":false,"FunctionDeclaration":false,"FunctionExpression":false,"MethodDefinition":true}}]
````

21 changes: 17 additions & 4 deletions src/exportParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,21 @@ const parse = function (ast, node, opt) {
};
};

const accessibilityNodes = new Set([
'PropertyDefinition',
'MethodDefinition',
]);

/**
*
* @param {import('eslint').Rule.Node} node
* @returns {boolean}
*/
const hasAccessibility = (node) => {
return accessibilityNodes.has(node.type) && 'accessibility' in node &&
node.accessibility !== 'public';
};

/**
*
* @param {import('eslint').Rule.Node} node
Expand All @@ -905,10 +920,8 @@ const isUncommentedExport = function (node, sourceCode, opt, settings) {
// console.log({node});
// Optimize with ancestor check for esm
if (opt.esm) {
if (
node.type === 'PropertyDefinition' && 'accessibility' in node &&
node.accessibility !== 'public'
) {
if (hasAccessibility(node) ||
node.parent && hasAccessibility(node.parent)) {
return false;
}

Expand Down
30 changes: 28 additions & 2 deletions test/rules/assertions/requireJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2323,7 +2323,7 @@ function quux (foo) {
options: [
{
contexts: [
'MethodDefinition:not([accessibility="private"]) > FunctionExpression',
'MethodDefinition > FunctionExpression',
],
publicOnly: true,
require: {
Expand Down Expand Up @@ -5629,7 +5629,7 @@ function quux (foo) {
options: [
{
contexts: [
'MethodDefinition:not([accessibility="private"]) > FunctionExpression',
'MethodDefinition > FunctionExpression',
],
publicOnly: true,
require: {
Expand Down Expand Up @@ -6142,5 +6142,31 @@ function quux (foo) {
],
parser: require.resolve('@typescript-eslint/parser'),
},
{
code: `
export default class Test {
private abc(a) {
this.a = a;
}
}
`,
options: [
{
publicOnly: true,
require: {
ArrowFunctionExpression: false,
ClassDeclaration: false,
ClassExpression: false,
FunctionDeclaration: false,
FunctionExpression: false,
MethodDefinition: true,
},
},
],
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
sourceType: 'module',
},
},
],
};

0 comments on commit 00922c3

Please sign in to comment.