Skip to content

Commit 3e0775a

Browse files
committed
feat(require-jsdoc): add contexts option to allow working with any node type
1 parent 58ca186 commit 3e0775a

File tree

5 files changed

+104
-46
lines changed

5 files changed

+104
-46
lines changed

.README/rules/require-jsdoc.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ be checked by the rule.
3434
- `FunctionExpression`
3535
- `MethodDefinition`
3636

37+
- `contexts` - Set this to a string or array of strings representing the additional
38+
AST context where you wish the rule to be applied (e.g., `Property` for properties).
39+
Note that unlike `require-description` and `match-description`, this rule has no
40+
`noDefaults` option because its defaults are instead set up by `require`.
41+
3742
|||
3843
|---|---|
3944
|Context|`ArrowFunctionExpression`, `ClassDeclaration`, `ClassExpression`, `FunctionDeclaration`, `FunctionExpression`|

src/iterateJsdoc.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,7 @@ export default function iterateJsdoc (iterator, ruleConfig) {
464464
};
465465
}
466466

467-
return contexts.reduce((obj, prop) => {
468-
obj[prop] = checkJsdoc;
469-
470-
return obj;
471-
}, {});
467+
return jsdocUtils.getContextObject(contexts, checkJsdoc);
472468
},
473469
meta: ruleConfig.meta
474470
};

src/jsdocUtils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,17 @@ const enforcedContexts = (context, defaultContexts) => {
511511
[...new Set([...defltContexts, ...contexts])];
512512
};
513513

514+
const getContextObject = (contexts, checkJsdoc) => {
515+
return contexts.reduce((obj, prop) => {
516+
obj[prop] = checkJsdoc;
517+
518+
return obj;
519+
}, {});
520+
};
521+
514522
export default {
515523
enforcedContexts,
524+
getContextObject,
516525
getFunctionParameterNames,
517526
getJsdocParameterNames,
518527
getJsdocParameterNamesDeep,

src/rules/requireJsdoc.js

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ import getJSDocComment from '../eslint/getJSDocComment';
77
const OPTIONS_SCHEMA = {
88
additionalProperties: false,
99
properties: {
10+
contexts: {
11+
oneOf: [
12+
{
13+
items: {
14+
type: 'string'
15+
},
16+
type: 'array'
17+
},
18+
{
19+
type: 'string'
20+
}
21+
]
22+
},
1023
publicOnly: {
1124
oneOf: [
1225
{
@@ -164,60 +177,64 @@ export default iterateJsdoc(null, {
164177
}
165178
};
166179

167-
return {
168-
ArrowFunctionExpression (node) {
169-
if (!requireOption.ArrowFunctionExpression) {
170-
return;
171-
}
180+
// eslint-disable-next-line fp/no-mutating-assign
181+
return Object.assign(
182+
jsdocUtils.getContextObject(jsdocUtils.enforcedContexts(context, []), checkJsDoc),
183+
{
184+
ArrowFunctionExpression (node) {
185+
if (!requireOption.ArrowFunctionExpression) {
186+
return;
187+
}
172188

173-
if (!['VariableDeclarator', 'ExportDefaultDeclaration'].includes(node.parent.type)) {
174-
return;
175-
}
189+
if (!['VariableDeclarator', 'ExportDefaultDeclaration'].includes(node.parent.type)) {
190+
return;
191+
}
176192

177-
checkJsDoc(node);
178-
},
179-
180-
ClassDeclaration (node) {
181-
if (!requireOption.ClassDeclaration) {
182-
return;
183-
}
193+
checkJsDoc(node);
194+
},
184195

185-
checkJsDoc(node);
186-
},
196+
ClassDeclaration (node) {
197+
if (!requireOption.ClassDeclaration) {
198+
return;
199+
}
187200

188-
ClassExpression (node) {
189-
if (!requireOption.ClassExpression) {
190-
return;
191-
}
201+
checkJsDoc(node);
202+
},
192203

193-
checkJsDoc(node);
194-
},
204+
ClassExpression (node) {
205+
if (!requireOption.ClassExpression) {
206+
return;
207+
}
195208

196-
FunctionDeclaration (node) {
197-
if (!requireOption.FunctionDeclaration) {
198-
return;
199-
}
209+
checkJsDoc(node);
210+
},
200211

201-
checkJsDoc(node);
202-
},
212+
FunctionDeclaration (node) {
213+
if (!requireOption.FunctionDeclaration) {
214+
return;
215+
}
203216

204-
FunctionExpression (node) {
205-
if (requireOption.MethodDefinition && node.parent.type === 'MethodDefinition') {
206217
checkJsDoc(node);
218+
},
207219

208-
return;
209-
}
220+
FunctionExpression (node) {
221+
if (requireOption.MethodDefinition && node.parent.type === 'MethodDefinition') {
222+
checkJsDoc(node);
210223

211-
if (!requireOption.FunctionExpression) {
212-
return;
213-
}
224+
return;
225+
}
214226

215-
if (['VariableDeclarator', 'AssignmentExpression', 'ExportDefaultDeclaration'].includes(node.parent.type)) {
216-
checkJsDoc(node);
217-
} else if (node.parent.type === 'Property' && node === node.parent.value) {
218-
checkJsDoc(node);
227+
if (!requireOption.FunctionExpression) {
228+
return;
229+
}
230+
231+
if (['VariableDeclarator', 'AssignmentExpression', 'ExportDefaultDeclaration'].includes(node.parent.type)) {
232+
checkJsDoc(node);
233+
} else if (node.parent.type === 'Property' && node === node.parent.value) {
234+
checkJsDoc(node);
235+
}
219236
}
220237
}
221-
};
238+
);
222239
}
223240
});

test/rules/assertions/requireJsdoc.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,26 @@ export default {
10101010
ecmaVersion: 6,
10111011
sourceType: 'module'
10121012
}
1013+
},
1014+
{
1015+
code: `
1016+
const myObject = {
1017+
myProp: true
1018+
};
1019+
`,
1020+
errors: [
1021+
{
1022+
message: 'Missing JSDoc comment.',
1023+
type: 'Property'
1024+
}
1025+
],
1026+
options: [
1027+
{
1028+
contexts: [
1029+
'Property'
1030+
]
1031+
}
1032+
]
10131033
}
10141034
],
10151035
valid: [{
@@ -1963,5 +1983,16 @@ export default {
19631983
FunctionExpression: true
19641984
}
19651985
}]
1986+
}, {
1987+
code: `
1988+
const myObject = {
1989+
myProp: true
1990+
};
1991+
`,
1992+
options: [
1993+
{
1994+
contexts: []
1995+
}
1996+
]
19661997
}]
19671998
};

0 commit comments

Comments
 (0)