Skip to content
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

fix(require-jsdoc): do not report MethodDefinition with non-public accessibility #1125

Merged
merged 1 commit into from
Jun 28, 2023
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
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',
},
},
],
};