Skip to content

Commit

Permalink
fix(require-jsdoc): detect ClassDeclaration as referenced public ex…
Browse files Browse the repository at this point in the history
…port and ClassExpression methods; fixes #648
  • Loading branch information
brettz9 committed Apr 11, 2022
1 parent 806deca commit 520c7be
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 4 deletions.
24 changes: 24 additions & 0 deletions README.md
Expand Up @@ -13652,6 +13652,30 @@ export class Component {
}
// "jsdoc/require-jsdoc": ["error"|"warn", {"checkConstructors":false,"contexts":["PropertyDefinition"],"publicOnly":true}]
// Message: Missing JSDoc comment.

class Utility {
/**
*
*/
mthd() {
return false;
}
}

module.exports = Utility;
// "jsdoc/require-jsdoc": ["error"|"warn", {"enableFixer":false,"publicOnly":true,"require":{"ArrowFunctionExpression":true,"ClassDeclaration":true,"ClassExpression":true,"FunctionDeclaration":true,"FunctionExpression":true,"MethodDefinition":true}}]
// Message: Missing JSDoc comment.

/**
*
*/
module.exports = class Utility {
mthd() {
return false;
}
};
// "jsdoc/require-jsdoc": ["error"|"warn", {"enableFixer":false,"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
12 changes: 8 additions & 4 deletions src/exportParser.js
Expand Up @@ -96,9 +96,13 @@ const getSymbol = function (node, globals, scope, opt) {
return null;
}

case 'ClassExpression': {
return getSymbol(node.body, globals, scope, opts);
}

case 'TSTypeAliasDeclaration':
case 'TSEnumDeclaration': case 'TSInterfaceDeclaration':
case 'ClassDeclaration': case 'ClassExpression':
case 'ClassDeclaration':
case 'FunctionExpression': case 'FunctionDeclaration':
case 'ArrowFunctionExpression': {
const val = createNode();
Expand All @@ -123,7 +127,7 @@ const getSymbol = function (node, globals, scope, opt) {
}

val.type = 'object';
val.value = node;
val.value = node.parent;

return val;
}
Expand Down Expand Up @@ -510,9 +514,9 @@ const findExportedNode = function (block, node, cache) {
};

const isNodeExported = function (node, globals, opt) {
const moduleExports = globals.props.module?.props?.exports;
if (
opt.initModuleExports && globals.props.module && globals.props.module.props.exports &&
findNode(node, globals.props.module.props.exports)
opt.initModuleExports && moduleExports && findNode(node, moduleExports)
) {
return true;
}
Expand Down
129 changes: 129 additions & 0 deletions test/rules/assertions/requireJsdoc.js
Expand Up @@ -3816,6 +3816,73 @@ function quux (foo) {
`,
parser: require.resolve('@typescript-eslint/parser'),
},

{
code: `
class Utility {
/**
*
*/
mthd() {
return false;
}
}
module.exports = Utility;
`,
errors: [
{
line: 2,
message: 'Missing JSDoc comment.',
},
],
options: [
{
enableFixer: false,
publicOnly: true,
require: {
ArrowFunctionExpression: true,
ClassDeclaration: true,
ClassExpression: true,
FunctionDeclaration: true,
FunctionExpression: true,
MethodDefinition: true,
},
},
],
},
{
code: `
/**
*
*/
module.exports = class Utility {
mthd() {
return false;
}
};
`,
errors: [
{
line: 6,
message: 'Missing JSDoc comment.',
},
],
options: [
{
enableFixer: false,
publicOnly: true,
require: {
ArrowFunctionExpression: true,
ClassDeclaration: true,
ClassExpression: true,
FunctionDeclaration: true,
FunctionExpression: true,
MethodDefinition: true,
},
},
],
},
],
valid: [
{
Expand Down Expand Up @@ -5702,5 +5769,67 @@ function quux (foo) {
sourceType: 'module',
},
},
{
code: `
function main () {
class Utility {
/**
*
*/
mthd() {
return false;
}
}
}
module.exports = main;
`,
ignoreReadme: true,
options: [
{
enableFixer: false,
publicOnly: true,
require: {
ArrowFunctionExpression: true,
ClassDeclaration: true,
ClassExpression: true,
FunctionDeclaration: false,
FunctionExpression: true,
MethodDefinition: true,
},
},
],
},
{
code: `
function main () {
const a = class Utility {
/**
*
*/
mthd() {
return false;
}
}
}
module.exports = main;
`,
ignoreReadme: true,
options: [
{
enableFixer: false,
publicOnly: true,
require: {
ArrowFunctionExpression: true,
ClassDeclaration: true,
ClassExpression: true,
FunctionDeclaration: false,
FunctionExpression: true,
MethodDefinition: true,
},
},
],
},
],
};

0 comments on commit 520c7be

Please sign in to comment.