Skip to content

Commit f219b62

Browse files
authored
feat(require-jsdoc): add checkAllFunctionExpressions option to force check FunctionExpression's regardless of context; fixes #1613 (#1614)
1 parent 81cdb0a commit f219b62

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

docs/rules/require-jsdoc.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [Fixer](#user-content-require-jsdoc-fixer)
66
* [Options](#user-content-require-jsdoc-options)
7+
* [`checkAllFunctionExpressions`](#user-content-require-jsdoc-options-checkallfunctionexpressions)
78
* [`checkConstructors`](#user-content-require-jsdoc-options-checkconstructors)
89
* [`checkGetters`](#user-content-require-jsdoc-options-checkgetters)
910
* [`checkSetters`](#user-content-require-jsdoc-options-checksetters)
@@ -43,6 +44,14 @@ A single options object has the following properties.
4344
Has the following optional keys.
4445

4546

47+
<a name="user-content-require-jsdoc-options-checkallfunctionexpressions"></a>
48+
<a name="require-jsdoc-options-checkallfunctionexpressions"></a>
49+
### <code>checkAllFunctionExpressions</code>
50+
51+
Normally, when `FunctionExpression` is checked, additional checks are
52+
added to check the parent contexts where reporting is likely to be desired. If you really
53+
want to check *all* function expressions, then set this to `true`.
54+
4655
<a name="user-content-require-jsdoc-options-checkconstructors"></a>
4756
<a name="require-jsdoc-options-checkconstructors"></a>
4857
### <code>checkConstructors</code>
@@ -1151,6 +1160,15 @@ function myFunction(): void;
11511160
function myFunction(foo?: string) {}
11521161
// "jsdoc/require-jsdoc": ["error"|"warn", {"contexts":["TSDeclareFunction"],"exemptOverloadedImplementations":true,"skipInterveningOverloadedDeclarations":false}]
11531162
// Message: Missing JSDoc comment.
1163+
1164+
const foo = autolog(
1165+
function foo() {
1166+
log.debug('inside foo', 'this is a test helper function')
1167+
},
1168+
{ withGovernance: true, withProfiling: true },
1169+
)
1170+
// "jsdoc/require-jsdoc": ["error"|"warn", {"checkAllFunctionExpressions":true,"contexts":["FunctionExpression"],"require":{"FunctionExpression":false}}]
1171+
// Message: Missing JSDoc comment.
11541172
````
11551173

11561174

src/rules.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,12 @@ export interface Rules {
16391639
| []
16401640
| [
16411641
{
1642+
/**
1643+
* Normally, when `FunctionExpression` is checked, additional checks are
1644+
* added to check the parent contexts where reporting is likely to be desired. If you really
1645+
* want to check *all* function expressions, then set this to `true`.
1646+
*/
1647+
checkAllFunctionExpressions?: boolean;
16421648
/**
16431649
* A value indicating whether `constructor`s should be checked. Defaults to
16441650
* `true`. When `true`, `exemptEmptyConstructors` may still avoid reporting when

src/rules/requireJsdoc.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ const OPTIONS_SCHEMA = {
3636
additionalProperties: false,
3737
description: 'Has the following optional keys.\n',
3838
properties: {
39+
checkAllFunctionExpressions: {
40+
default: false,
41+
description: `Normally, when \`FunctionExpression\` is checked, additional checks are
42+
added to check the parent contexts where reporting is likely to be desired. If you really
43+
want to check *all* function expressions, then set this to \`true\`.`,
44+
type: 'boolean',
45+
},
3946
checkConstructors: {
4047
default: true,
4148
description: `A value indicating whether \`constructor\`s should be checked. Defaults to
@@ -378,6 +385,7 @@ const getOption = (context, baseObject, option, key) => {
378385
* @param {import('eslint').Rule.RuleContext} context
379386
* @param {import('../iterateJsdoc.js').Settings} settings
380387
* @returns {{
388+
* checkAllFunctionExpressions: boolean,
381389
* contexts: (string|{
382390
* context: string,
383391
* inlineCommentBlock: boolean,
@@ -396,6 +404,7 @@ const getOption = (context, baseObject, option, key) => {
396404
*/
397405
const getOptions = (context, settings) => {
398406
const {
407+
checkAllFunctionExpressions = false,
399408
contexts = settings.contexts || [],
400409
enableFixer = true,
401410
exemptEmptyConstructors = true,
@@ -408,6 +417,7 @@ const getOptions = (context, settings) => {
408417
} = context.options[0] || {};
409418

410419
return {
420+
checkAllFunctionExpressions,
411421
contexts,
412422
enableFixer,
413423
exemptEmptyConstructors,
@@ -503,7 +513,7 @@ const isFunctionWithOverload = (node) => {
503513
return false;
504514
}
505515

506-
const functionName = node.id.name;
516+
const functionName = node.id?.name;
507517

508518
const idx = parent.body.indexOf(child);
509519
const prevSibling = parent.body[idx - 1];
@@ -536,6 +546,7 @@ export default {
536546
const opts = getOptions(context, settings);
537547

538548
const {
549+
checkAllFunctionExpressions,
539550
contexts,
540551
enableFixer,
541552
exemptEmptyConstructors,
@@ -831,7 +842,7 @@ export default {
831842
return;
832843
}
833844

834-
if (
845+
if (checkAllFunctionExpressions ||
835846
[
836847
'AssignmentExpression', 'ExportDefaultDeclaration', 'VariableDeclarator',
837848
].includes(node.parent.type) ||

test/rules/assertions/requireJsdoc.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4420,6 +4420,44 @@ function quux (foo) {
44204420
function myFunction(foo?: string) {}
44214421
`,
44224422
},
4423+
{
4424+
code: `
4425+
const foo = autolog(
4426+
function foo() {
4427+
log.debug('inside foo', 'this is a test helper function')
4428+
},
4429+
{ withGovernance: true, withProfiling: true },
4430+
)
4431+
`,
4432+
errors: [
4433+
{
4434+
line: 3,
4435+
message: 'Missing JSDoc comment.',
4436+
},
4437+
],
4438+
options: [
4439+
{
4440+
checkAllFunctionExpressions: true,
4441+
contexts: [
4442+
'FunctionExpression',
4443+
],
4444+
require: {
4445+
FunctionExpression: false,
4446+
},
4447+
},
4448+
],
4449+
output: `
4450+
const foo = autolog(
4451+
/**
4452+
*
4453+
*/
4454+
function foo() {
4455+
log.debug('inside foo', 'this is a test helper function')
4456+
},
4457+
{ withGovernance: true, withProfiling: true },
4458+
)
4459+
`,
4460+
},
44234461
],
44244462
valid: [
44254463
{

0 commit comments

Comments
 (0)