Skip to content

Commit da3d139

Browse files
brettz9golopot
authored andcommitted
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 314557c commit da3d139

File tree

10 files changed

+315
-51
lines changed

10 files changed

+315
-51
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: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,7 +2252,7 @@ The default is this basic expression to match English sentences (Support
22522252
for Unicode upper case may be added in a future version when it can be handled
22532253
by our supported Node versions):
22542254

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

22572257
<a name="eslint-plugin-jsdoc-rules-match-description-options-1"></a>
22582258
#### Options
@@ -2367,7 +2367,7 @@ function quux () {
23672367
// Message: JSDoc description does not satisfy the regex pattern.
23682368

23692369
/**
2370-
* тест.
2370+
* Abc.
23712371
*/
23722372
function quux () {
23732373

@@ -2530,6 +2530,33 @@ class quux {
25302530
// Options: [{"contexts":["ClassDeclaration"],"noDefaults":true}]
25312531
>>>>>>> feat(match-description): allow `main description: string|boolean` to override or disable main description separate from default
25322532
// Message: JSDoc description does not satisfy the regex pattern.
2533+
2534+
class MyClass {
2535+
/**
2536+
* Abc
2537+
*/
2538+
myClassField = 1
2539+
}
2540+
// Options: [{"contexts":["ClassProperty"],"noDefaults":true}]
2541+
// Message: JSDoc description does not satisfy the regex pattern.
2542+
2543+
/**
2544+
* foo.
2545+
*/
2546+
interface quux {
2547+
2548+
}
2549+
// Options: [{"contexts":["TSInterfaceDeclaration"],"noDefaults":true}]
2550+
// Message: JSDoc description does not satisfy the regex pattern.
2551+
2552+
const myObject = {
2553+
/**
2554+
* Bad description
2555+
*/
2556+
myProp: true
2557+
};
2558+
// Options: [{"contexts":["Property"],"noDefaults":true}]
2559+
// Message: JSDoc description does not satisfy the regex pattern.
25332560
````
25342561

25352562
The following patterns are not considered problems:
@@ -2682,6 +2709,30 @@ class quux {
26822709

26832710
}
26842711
// Options: [{"tags":{"main description":true}}]
2712+
2713+
class MyClass {
2714+
/**
2715+
* Abc.
2716+
*/
2717+
myClassField = 1
2718+
}
2719+
// Options: [{"contexts":["ClassProperty"],"noDefaults":true}]
2720+
2721+
/**
2722+
* Foo.
2723+
*/
2724+
interface quux {
2725+
2726+
}
2727+
// Options: [{"contexts":["TSInterfaceDeclaration"],"noDefaults":true}]
2728+
2729+
const myObject = {
2730+
/**
2731+
* Bad description
2732+
*/
2733+
myProp: true
2734+
};
2735+
// Options: [{"contexts":[],"noDefaults":true}]
26852736
````
26862737

26872738

@@ -3490,6 +3541,7 @@ interface quux {
34903541
}
34913542
// Options: [{"contexts":["TSInterfaceDeclaration"],"noDefaults":true}]
34923543
// Message: Missing JSDoc @description declaration.
3544+
<<<<<<< HEAD
34933545

34943546
/**
34953547
*
@@ -3508,6 +3560,8 @@ var quux = {
35083560
};
35093561
// Options: [{"contexts":["ObjectExpression"]}]
35103562
// Message: Missing JSDoc @description declaration.
3563+
=======
3564+
>>>>>>> fix(match-description): tighten default regex to require punctuation at the end even if only a single character
35113565
````
35123566

35133567
The following patterns are not considered problems:
@@ -3583,6 +3637,7 @@ var quux = class {
35833637
var quux = {
35843638

35853639
};
3640+
// Message: Missing JSDoc @description declaration.
35863641
````
35873642

35883643

@@ -3850,6 +3905,11 @@ be checked by the rule.
38503905
- `FunctionExpression`
38513906
- `MethodDefinition`
38523907

3908+
- `contexts` - Set this to a string or array of strings representing the additional
3909+
AST context where you wish the rule to be applied (e.g., `Property` for properties).
3910+
Note that unlike `require-description` and `match-description`, this rule has no
3911+
`noDefaults` option because its defaults are instead set up by `require`.
3912+
38533913
|||
38543914
|---|---|
38553915
|Context|`ArrowFunctionExpression`, `ClassDeclaration`, `ClassExpression`, `FunctionDeclaration`, `FunctionExpression`|
@@ -4181,6 +4241,12 @@ export function someMethod() {
41814241
}
41824242
// Options: [{"publicOnly":{"cjs":false,"esm":true,"window":false},"require":{"FunctionDeclaration":true}}]
41834243
// Message: Missing JSDoc comment.
4244+
4245+
const myObject = {
4246+
myProp: true
4247+
};
4248+
// Options: [{"contexts":["Property"]}]
4249+
// Message: Missing JSDoc comment.
41844250
````
41854251

41864252
The following patterns are not considered problems:
@@ -4650,6 +4716,11 @@ exports.someMethod = function() {
46504716

46514717
}
46524718
// Options: [{"publicOnly":{"cjs":false,"esm":true,"window":false},"require":{"FunctionExpression":true}}]
4719+
4720+
const myObject = {
4721+
myProp: true
4722+
};
4723+
// Options: [{"contexts":[]}]
46534724
````
46544725

46554726

src/iterateJsdoc.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,7 @@ export default function iterateJsdoc (iterator, ruleConfig) {
389389
};
390390
}
391391

392-
return contexts.reduce((obj, prop) => {
393-
obj[prop] = checkJsdoc;
394-
395-
return obj;
396-
}, {});
392+
return jsdocUtils.getContextObject(contexts, checkJsdoc);
397393
},
398394
meta: ruleConfig.meta
399395
};

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)