Skip to content

Commit 7daf934

Browse files
committed
fix(require-jsdoc): ensure exemptEmptyFunctions doesn't attempt to apply when non-function contexts are in use (causing errors); fixes #501
1 parent 3c9d20f commit 7daf934

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7772,6 +7772,12 @@ export interface Foo extends Bar {
77727772
}
77737773
// Options: [{"contexts":["TSMethodSignature"]}]
77747774
// Message: Missing JSDoc comment.
7775+
7776+
class MyClass {
7777+
someProperty: boolean; // Flow type annotation.
7778+
}
7779+
// Options: [{"exemptEmptyFunctions":true,"require":{"ClassDeclaration":true}}]
7780+
// Message: Missing JSDoc comment.
77757781
````
77767782
77777783
The following patterns are not considered problems:

src/rules/requireJsdoc.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ export default {
124124

125125
const {require: requireOption, publicOnly, exemptEmptyFunctions} = getOptions(context);
126126

127-
const checkJsDoc = (node) => {
127+
const checkJsDoc = (node, isFunctionContext) => {
128128
const jsDocNode = getJSDocComment(sourceCode, node, settings);
129129

130130
if (jsDocNode) {
131131
return;
132132
}
133133

134-
if (exemptEmptyFunctions) {
134+
if (exemptEmptyFunctions && isFunctionContext) {
135135
const functionParameterNames = jsdocUtils.getFunctionParameterNames(node);
136136
if (!functionParameterNames.length && !jsdocUtils.hasReturnValue(node, context)) {
137137
return;
@@ -194,7 +194,7 @@ export default {
194194
return;
195195
}
196196

197-
checkJsDoc(node);
197+
checkJsDoc(node, true);
198198
},
199199

200200
ClassDeclaration (node) {
@@ -218,12 +218,12 @@ export default {
218218
return;
219219
}
220220

221-
checkJsDoc(node);
221+
checkJsDoc(node, true);
222222
},
223223

224224
FunctionExpression (node) {
225225
if (requireOption.MethodDefinition && node.parent.type === 'MethodDefinition') {
226-
checkJsDoc(node);
226+
checkJsDoc(node, true);
227227

228228
return;
229229
}
@@ -236,7 +236,7 @@ export default {
236236
['VariableDeclarator', 'AssignmentExpression', 'ExportDefaultDeclaration'].includes(node.parent.type) ||
237237
node.parent.type === 'Property' && node === node.parent.value
238238
) {
239-
checkJsDoc(node);
239+
checkJsDoc(node, true);
240240
}
241241
},
242242
},

test/rules/assertions/requireJsdoc.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,25 @@ export default {
11661166
}],
11671167
parser: require.resolve('@typescript-eslint/parser'),
11681168
},
1169+
{
1170+
code: `
1171+
class MyClass {
1172+
someProperty: boolean; // Flow type annotation.
1173+
}
1174+
`,
1175+
errors: [
1176+
{
1177+
message: 'Missing JSDoc comment.',
1178+
},
1179+
],
1180+
options: [{
1181+
exemptEmptyFunctions: true,
1182+
require: {
1183+
ClassDeclaration: true,
1184+
},
1185+
}],
1186+
parser: require.resolve('babel-eslint'),
1187+
},
11691188
],
11701189
valid: [{
11711190
code: `

0 commit comments

Comments
 (0)