Skip to content

Commit

Permalink
fix(require-jsdoc): ensure conditionally checking Property/`Objec…
Browse files Browse the repository at this point in the history
…tProperty`/`ClassProperty` as parents for arrow function expressions; fixes #612

Also:
1. Ensures `ObjectProperty` and `ClassProperty` (as with `babel-eslint`) are conditionally checked as parents for non-arrow function expressions
2. Fixes comment placement with `ClassProperty`/`ObjectProperty` (as with `babel/parser`)
  • Loading branch information
brettz9 committed Jul 20, 2020
1 parent c44ba78 commit eac8357
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -9218,6 +9218,13 @@ class Test {
}
// Options: [{"checkConstructors":false,"require":{"ArrowFunctionExpression":true,"ClassDeclaration":false,"ClassExpression":true,"FunctionDeclaration":true,"FunctionExpression":true,"MethodDefinition":true}}]
// Message: Missing JSDoc comment.

class Test {
aFunc = () => {}
anotherFunc() {}
}
// Options: [{"require":{"ArrowFunctionExpression":true,"ClassDeclaration":false,"ClassExpression":true,"FunctionDeclaration":true,"FunctionExpression":true,"MethodDefinition":true}}]
// Message: Missing JSDoc comment.
````

The following patterns are not considered problems:
Expand Down
17 changes: 12 additions & 5 deletions src/eslint/getJSDocComment.js
Expand Up @@ -134,6 +134,16 @@ const getTSFunctionComment = function (astNode) {
}
};

const invokedExpression = new Set(['CallExpression', 'OptionalCallExpression', 'NewExpression']);
const allowableCommentNode = new Set([
'VariableDeclaration',
'ExpressionStatement',
'MethodDefinition',
'Property',
'ObjectProperty',
'ClassProperty',
]);

/* eslint-disable complexity */
/**
* Reduces the provided node to the appropriate node for evaluating JSDoc comment status.
Expand Down Expand Up @@ -162,15 +172,12 @@ const getReducedASTNode = function (node, sourceCode) {
case 'TSEmptyBodyFunctionExpression':
case 'FunctionExpression':
if (
!['CallExpression', 'OptionalCallExpression', 'NewExpression'].includes(parent.type)
!invokedExpression.has(parent.type)
) {
while (
!sourceCode.getCommentsBefore(parent).length &&
!/Function/u.test(parent.type) &&
parent.type !== 'VariableDeclaration' &&
parent.type !== 'ExpressionStatement' &&
parent.type !== 'MethodDefinition' &&
parent.type !== 'Property'
!allowableCommentNode.has(parent.type)
) {
parent = parent.parent;

Expand Down
11 changes: 6 additions & 5 deletions src/rules/requireJsdoc.js
Expand Up @@ -272,11 +272,12 @@ export default {
return;
}

if (!['VariableDeclarator', 'ExportDefaultDeclaration', 'AssignmentExpression'].includes(node.parent.type)) {
return;
if (
['VariableDeclarator', 'AssignmentExpression', 'ExportDefaultDeclaration'].includes(node.parent.type) ||
['Property', 'ObjectProperty', 'ClassProperty'].includes(node.parent.type) && node === node.parent.value
) {
checkJsDoc(node, true);
}

checkJsDoc(node, true);
},

ClassDeclaration (node) {
Expand Down Expand Up @@ -316,7 +317,7 @@ export default {

if (
['VariableDeclarator', 'AssignmentExpression', 'ExportDefaultDeclaration'].includes(node.parent.type) ||
node.parent.type === 'Property' && node === node.parent.value
['Property', 'ObjectProperty', 'ClassProperty'].includes(node.parent.type) && node === node.parent.value
) {
checkJsDoc(node, true);
}
Expand Down
38 changes: 38 additions & 0 deletions test/rules/assertions/requireJsdoc.js
Expand Up @@ -2458,6 +2458,44 @@ export default {
}
`,
},
{
code: `
class Test {
aFunc = () => {}
anotherFunc() {}
}
`,
errors: [{
line: 3,
message: 'Missing JSDoc comment.',
}, {
line: 4,
message: 'Missing JSDoc comment.',
}],
options: [{
require: {
ArrowFunctionExpression: true,
ClassDeclaration: false,
ClassExpression: true,
FunctionDeclaration: true,
FunctionExpression: true,
MethodDefinition: true,
},
}],
output: `
class Test {
/**
*
*/
aFunc = () => {}
/**
*
*/
anotherFunc() {}
}
`,
parser: require.resolve('babel-eslint'),
},
],
valid: [{
code: `
Expand Down

0 comments on commit eac8357

Please sign in to comment.