Skip to content

Commit 91fda5c

Browse files
committed
fix(match-description): tighten default regex to require punctuation at the end even if only a single character
fix(match-description, require-description): allow `contexts` to work with any node type feat(require-jsdoc): add `contexts` option to allow working with any node type
1 parent e4450f2 commit 91fda5c

File tree

9 files changed

+311
-50
lines changed

9 files changed

+311
-50
lines changed

.README/rules/match-description.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The default is this basic expression to match English sentences (Support
66
for Unicode upper case may be added in a future version when it can be handled
77
by our supported Node versions):
88

9-
``^([A-Z]|[`\\d_])([\\s\\S]*[.?!`])?$``
9+
``^([A-Z]|[`\\d_])[\\s\\S]*[.?!`]$``
1010

1111
#### Options
1212

.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`|

README.md

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,7 +2285,7 @@ The default is this basic expression to match English sentences (Support
22852285
for Unicode upper case may be added in a future version when it can be handled
22862286
by our supported Node versions):
22872287

2288-
``^([A-Z]|[`\\d_])([\\s\\S]*[.?!`])?$``
2288+
``^([A-Z]|[`\\d_])[\\s\\S]*[.?!`]$``
22892289

22902290
<a name="eslint-plugin-jsdoc-rules-match-description-options-1"></a>
22912291
#### Options
@@ -2400,7 +2400,7 @@ function quux () {
24002400
// Message: JSDoc description does not satisfy the regex pattern.
24012401

24022402
/**
2403-
* тест.
2403+
* Abc.
24042404
*/
24052405
function quux () {
24062406

@@ -2563,6 +2563,33 @@ class quux {
25632563
// Options: [{"contexts":["ClassDeclaration"],"noDefaults":true}]
25642564
>>>>>>> feat(match-description): allow `main description: string|boolean` to override or disable main description separate from default
25652565
// Message: JSDoc description does not satisfy the regex pattern.
2566+
2567+
class MyClass {
2568+
/**
2569+
* Abc
2570+
*/
2571+
myClassField = 1
2572+
}
2573+
// Options: [{"contexts":["ClassProperty"],"noDefaults":true}]
2574+
// Message: JSDoc description does not satisfy the regex pattern.
2575+
2576+
/**
2577+
* foo.
2578+
*/
2579+
interface quux {
2580+
2581+
}
2582+
// Options: [{"contexts":["TSInterfaceDeclaration"],"noDefaults":true}]
2583+
// Message: JSDoc description does not satisfy the regex pattern.
2584+
2585+
const myObject = {
2586+
/**
2587+
* Bad description
2588+
*/
2589+
myProp: true
2590+
};
2591+
// Options: [{"contexts":["Property"],"noDefaults":true}]
2592+
// Message: JSDoc description does not satisfy the regex pattern.
25662593
````
25672594

25682595
The following patterns are not considered problems:
@@ -2715,6 +2742,30 @@ class quux {
27152742

27162743
}
27172744
// Options: [{"tags":{"main description":true}}]
2745+
2746+
class MyClass {
2747+
/**
2748+
* Abc.
2749+
*/
2750+
myClassField = 1
2751+
}
2752+
// Options: [{"contexts":["ClassProperty"],"noDefaults":true}]
2753+
2754+
/**
2755+
* Foo.
2756+
*/
2757+
interface quux {
2758+
2759+
}
2760+
// Options: [{"contexts":["TSInterfaceDeclaration"],"noDefaults":true}]
2761+
2762+
const myObject = {
2763+
/**
2764+
* Bad description
2765+
*/
2766+
myProp: true
2767+
};
2768+
// Options: [{"contexts":[],"noDefaults":true}]
27182769
````
27192770

27202771

@@ -3523,6 +3574,7 @@ interface quux {
35233574
}
35243575
// Options: [{"contexts":["TSInterfaceDeclaration"],"noDefaults":true}]
35253576
// Message: Missing JSDoc @description declaration.
3577+
<<<<<<< HEAD
35263578

35273579
/**
35283580
*
@@ -3541,6 +3593,8 @@ var quux = {
35413593
};
35423594
// Options: [{"contexts":["ObjectExpression"]}]
35433595
// Message: Missing JSDoc @description declaration.
3596+
=======
3597+
>>>>>>> fix(match-description): tighten default regex to require punctuation at the end even if only a single character
35443598
````
35453599

35463600
The following patterns are not considered problems:
@@ -3602,6 +3656,7 @@ function quux () {
36023656
interface quux {
36033657

36043658
}
3659+
<<<<<<< HEAD
36053660

36063661
/**
36073662
*
@@ -3616,6 +3671,9 @@ var quux = class {
36163671
var quux = {
36173672

36183673
};
3674+
=======
3675+
// Message: Missing JSDoc @description declaration.
3676+
>>>>>>> fix(match-description): tighten default regex to require punctuation at the end even if only a single character
36193677
````
36203678

36213679

@@ -3883,6 +3941,11 @@ be checked by the rule.
38833941
- `FunctionExpression`
38843942
- `MethodDefinition`
38853943

3944+
- `contexts` - Set this to a string or array of strings representing the additional
3945+
AST context where you wish the rule to be applied (e.g., `Property` for properties).
3946+
Note that unlike `require-description` and `match-description`, this rule has no
3947+
`noDefaults` option because its defaults are instead set up by `require`.
3948+
38863949
|||
38873950
|---|---|
38883951
|Context|`ArrowFunctionExpression`, `ClassDeclaration`, `ClassExpression`, `FunctionDeclaration`, `FunctionExpression`|
@@ -4214,6 +4277,12 @@ export function someMethod() {
42144277
}
42154278
// Options: [{"publicOnly":{"cjs":false,"esm":true,"window":false},"require":{"FunctionDeclaration":true}}]
42164279
// Message: Missing JSDoc comment.
4280+
4281+
const myObject = {
4282+
myProp: true
4283+
};
4284+
// Options: [{"contexts":["Property"]}]
4285+
// Message: Missing JSDoc comment.
42174286
````
42184287

42194288
The following patterns are not considered problems:
@@ -4683,6 +4752,11 @@ exports.someMethod = function() {
46834752

46844753
}
46854754
// Options: [{"publicOnly":{"cjs":false,"esm":true,"window":false},"require":{"FunctionExpression":true}}]
4755+
4756+
const myObject = {
4757+
myProp: true
4758+
};
4759+
// Options: [{"contexts":[]}]
46864760
````
46874761

46884762

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/matchDescription.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default iterateJsdoc(({
2323

2424
// If supporting Node >= 10, we could loosen to this for the
2525
// initial letter: \\p{Upper}
26-
) || '^[A-Z`\\d_](?:[\\s\\S]*[.?!`])?$',
26+
) || '^[A-Z`\\d_][\\s\\S]*[.?!`]$',
2727
'u'
2828
);
2929

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
});

0 commit comments

Comments
 (0)