Skip to content

Commit

Permalink
fix(require-jsdoc): avoid erring on #-marked private methods; fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Mar 7, 2024
1 parent e948bee commit 783b4e9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
7 changes: 7 additions & 0 deletions docs/rules/require-jsdoc.md
Expand Up @@ -1898,5 +1898,12 @@ export default {
}
};
// "jsdoc/require-jsdoc": ["error"|"warn", {"contexts":["ExportDefaultDeclaration > ObjectExpression > Property[key.name!=/^(created|beforeUpdate)$/] > FunctionExpression","ExportDefaultDeclaration > ObjectExpression > Property[key.name!=/^(watch|computed|methods)$/] > ObjectExpression > Property > FunctionExpression"]}]

export class MyClass {
#myPrivateMethod(): void { }

#myPrivateProp = 5;
}
// "jsdoc/require-jsdoc": ["error"|"warn", {"publicOnly":true,"contexts":["PropertyDefinition"],"require":{"MethodDefinition":true}}]
````

15 changes: 10 additions & 5 deletions src/exportParser.js
Expand Up @@ -899,9 +899,14 @@ const accessibilityNodes = new Set([
* @param {import('eslint').Rule.Node} node
* @returns {boolean}
*/
const hasAccessibility = (node) => {
return accessibilityNodes.has(node.type) && 'accessibility' in node &&
node.accessibility !== 'public' && node.accessibility !== undefined;
const isPrivate = (node) => {
return accessibilityNodes.has(node.type) &&
(
'accessibility' in node &&
node.accessibility !== 'public' && node.accessibility !== undefined
) ||
'key' in node &&
node.key.type === 'PrivateIdentifier';
};

/**
Expand All @@ -916,8 +921,8 @@ const isUncommentedExport = function (node, sourceCode, opt, settings) {
// console.log({node});
// Optimize with ancestor check for esm
if (opt.esm) {
if (hasAccessibility(node) ||
node.parent && hasAccessibility(node.parent)) {
if (isPrivate(node) ||
node.parent && isPrivate(node.parent)) {
return false;
}

Expand Down
21 changes: 21 additions & 0 deletions test/rules/assertions/requireJsdoc.js
Expand Up @@ -6342,5 +6342,26 @@ function quux (foo) {
parser: typescriptEslintParser,
}
},
{
code: `
export class MyClass {
#myPrivateMethod(): void { }
#myPrivateProp = 5;
}
`,
languageOptions: {
parser: typescriptEslintParser,
},
options: [
{
publicOnly: true,
contexts: ['PropertyDefinition'],
require: {
MethodDefinition: true,
},
},
],
},
],
};

0 comments on commit 783b4e9

Please sign in to comment.