Skip to content

Commit 50d3b4d

Browse files
committed
feat(match-description, require-description, require-example): allow "any" for contexts; fixes #325
1 parent cf37cc6 commit 50d3b4d

File tree

10 files changed

+181
-12
lines changed

10 files changed

+181
-12
lines changed

.README/rules/match-description.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ it by setting it to `false`.
8888

8989
Set this to an array of strings representing the AST context
9090
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
91-
Overrides the default contexts (see below).
91+
Overrides the default contexts (see below). Set to `"any"` if you want
92+
the rule to apply to any jsdoc block throughout your files.
9293

9394
|||
9495
|---|---|

.README/rules/require-description.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ An options object may have any of the following properties:
1414

1515
- `contexts` - Set to an array of strings representing the AST context
1616
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6
17-
classes). Overrides the default contexts (see below).
17+
classes). Overrides the default contexts (see below). Set to `"any"` if
18+
you want the rule to apply to any jsdoc block throughout your files.
1819
- `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the
1920
document block avoids the need for a `@description`. Defaults to an
2021
empty array.

.README/rules/require-example.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ Defaults to `false`.
2424

2525
Set this to an array of strings representing the AST context
2626
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
27-
Overrides the default contexts (see below).
27+
Overrides the default contexts (see below). Set to `"any"` if you want
28+
the rule to apply to any jsdoc block throughout your files.
2829

2930
#### Fixer
3031

README.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3973,7 +3973,8 @@ it by setting it to `false`.
39733973
39743974
Set this to an array of strings representing the AST context
39753975
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
3976-
Overrides the default contexts (see below).
3976+
Overrides the default contexts (see below). Set to `"any"` if you want
3977+
the rule to apply to any jsdoc block throughout your files.
39773978
39783979
|||
39793980
|---|---|
@@ -3995,6 +3996,18 @@ const q = class {
39953996
// Options: [{"contexts":["ClassExpression"]}]
39963997
// Message: JSDoc description does not satisfy the regex pattern.
39973998
3999+
/**
4000+
* foo.
4001+
*/
4002+
// Options: [{"contexts":["any"]}]
4003+
// Message: JSDoc description does not satisfy the regex pattern.
4004+
4005+
/**
4006+
* foo.
4007+
*/
4008+
// Options: [{"contexts":["any"]}]
4009+
// Message: JSDoc description does not satisfy the regex pattern.
4010+
39984011
/**
39994012
* foo.
40004013
*/
@@ -4305,6 +4318,17 @@ function quux (foo) {
43054318
The following patterns are not considered problems:
43064319
43074320
````js
4321+
/**
4322+
*
4323+
*/
4324+
4325+
/**
4326+
*
4327+
*/
4328+
function quux () {
4329+
4330+
}
4331+
43084332
/**
43094333
* @param foo - Foo.
43104334
*/
@@ -5952,7 +5976,8 @@ An options object may have any of the following properties:
59525976
59535977
- `contexts` - Set to an array of strings representing the AST context
59545978
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6
5955-
classes). Overrides the default contexts (see below).
5979+
classes). Overrides the default contexts (see below). Set to `"any"` if
5980+
you want the rule to apply to any jsdoc block throughout your files.
59565981
- `exemptedBy` - Array of tags (e.g., `['type']`) whose presence on the
59575982
document block avoids the need for a `@description`. Defaults to an
59585983
empty array.
@@ -6007,6 +6032,12 @@ class quux {
60076032
// Options: [{"contexts":["ClassDeclaration"],"descriptionStyle":"tag"}]
60086033
// Message: Missing JSDoc @description declaration.
60096034
6035+
/**
6036+
*
6037+
*/
6038+
// Options: [{"contexts":["any"],"descriptionStyle":"tag"}]
6039+
// Message: Missing JSDoc @description declaration.
6040+
60106041
/**
60116042
*
60126043
*/
@@ -6103,6 +6134,10 @@ function quux () {
61036134
The following patterns are not considered problems:
61046135
61056136
````js
6137+
/**
6138+
*
6139+
*/
6140+
61066141
/**
61076142
* @description
61086143
* // arbitrary description content
@@ -6253,7 +6288,8 @@ Defaults to `false`.
62536288
62546289
Set this to an array of strings representing the AST context
62556290
where you wish the rule to be applied (e.g., `ClassDeclaration` for ES6 classes).
6256-
Overrides the default contexts (see below).
6291+
Overrides the default contexts (see below). Set to `"any"` if you want
6292+
the rule to apply to any jsdoc block throughout your files.
62576293
62586294
<a name="eslint-plugin-jsdoc-rules-require-example-fixer"></a>
62596295
#### Fixer
@@ -6322,6 +6358,12 @@ class quux {
63226358
// Options: [{"contexts":["ClassDeclaration"]}]
63236359
// Message: Missing JSDoc @example declaration.
63246360
6361+
/**
6362+
*
6363+
*/
6364+
// Options: [{"contexts":["any"]}]
6365+
// Message: Missing JSDoc @example declaration.
6366+
63256367
/**
63266368
*
63276369
*/
@@ -6334,6 +6376,10 @@ function quux () {
63346376
The following patterns are not considered problems:
63356377
63366378
````js
6379+
/**
6380+
*
6381+
*/
6382+
63376383
/**
63386384
* @example
63396385
* // arbitrary example content
@@ -7092,6 +7138,10 @@ const myObject = {
70927138
The following patterns are not considered problems:
70937139
70947140
````js
7141+
/**
7142+
*
7143+
*/
7144+
70957145
var array = [1,2,3];
70967146
array.forEach(function() {});
70977147

src/iterateJsdoc.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,18 @@ export default function iterateJsdoc (iterator, ruleConfig) {
584584
* a list with parser callback function.
585585
*/
586586
create (context) {
587-
const sourceCode = context.getSourceCode();
587+
let contexts;
588+
if (ruleConfig.contextDefaults) {
589+
contexts = jsdocUtils.enforcedContexts(context, ruleConfig.contextDefaults);
590+
if (contexts.includes('any')) {
591+
return iterateAllJsdocs(iterator, ruleConfig).create(context);
592+
}
593+
}
588594

595+
const sourceCode = context.getSourceCode();
589596
const settings = getSettings(context);
590-
591597
const {lines} = sourceCode;
598+
592599
const checkJsdoc = (node) => {
593600
const jsdocNode = getJSDocComment(sourceCode, node, settings);
594601

@@ -603,8 +610,6 @@ export default function iterateJsdoc (iterator, ruleConfig) {
603610
};
604611

605612
if (ruleConfig.contextDefaults) {
606-
const contexts = jsdocUtils.enforcedContexts(context, ruleConfig.contextDefaults);
607-
608613
return jsdocUtils.getContextObject(contexts, checkJsdoc);
609614
}
610615

src/rules/requireJsdoc.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,10 @@ export default {
120120
warnRemovedSettings(context, 'require-jsdoc');
121121

122122
const sourceCode = context.getSourceCode();
123+
const settings = getSettings(context);
123124

124125
const {require: requireOption, publicOnly, exemptEmptyFunctions} = getOptions(context);
125126

126-
const settings = getSettings(context);
127-
128127
const checkJsDoc = (node) => {
129128
const jsDocNode = getJSDocComment(sourceCode, node, settings);
130129

test/rules/assertions/matchDescription.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,46 @@ export default {
2323
},
2424
],
2525
},
26+
{
27+
code: `
28+
/**
29+
* foo.
30+
*/
31+
`,
32+
errors: [
33+
{
34+
line: 3,
35+
message: 'JSDoc description does not satisfy the regex pattern.',
36+
},
37+
],
38+
options: [
39+
{
40+
contexts: [
41+
'any',
42+
],
43+
},
44+
],
45+
},
46+
{
47+
code: `
48+
/**
49+
* foo.
50+
*/
51+
`,
52+
errors: [
53+
{
54+
line: 3,
55+
message: 'JSDoc description does not satisfy the regex pattern.',
56+
},
57+
],
58+
options: [
59+
{
60+
contexts: [
61+
'any',
62+
],
63+
},
64+
],
65+
},
2666
{
2767
code: `
2868
/**
@@ -734,6 +774,23 @@ export default {
734774
},
735775
],
736776
valid: [
777+
{
778+
code: `
779+
/**
780+
*
781+
*/
782+
`,
783+
},
784+
{
785+
code: `
786+
/**
787+
*
788+
*/
789+
function quux () {
790+
791+
}
792+
`,
793+
},
737794
{
738795
code: `
739796
/**

test/rules/assertions/requireDescription.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ export default {
8181
},
8282
],
8383
},
84+
{
85+
code: `
86+
/**
87+
*
88+
*/
89+
`,
90+
errors: [
91+
{
92+
message: 'Missing JSDoc @description declaration.',
93+
},
94+
],
95+
options: [
96+
{
97+
contexts: ['any'],
98+
descriptionStyle: 'tag',
99+
},
100+
],
101+
},
84102
{
85103
code: `
86104
/**
@@ -319,6 +337,13 @@ export default {
319337
},
320338
],
321339
valid: [
340+
{
341+
code: `
342+
/**
343+
*
344+
*/
345+
`,
346+
},
322347
{
323348
code: `
324349
/**

test/rules/assertions/requireExample.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ export default {
112112
},
113113
],
114114
},
115+
{
116+
code: `
117+
/**
118+
*
119+
*/
120+
`,
121+
errors: [
122+
{
123+
message: 'Missing JSDoc @example declaration.',
124+
},
125+
],
126+
options: [
127+
{
128+
contexts: ['any'],
129+
},
130+
],
131+
},
115132
{
116133
code: `
117134
/**
@@ -134,6 +151,13 @@ export default {
134151
},
135152
],
136153
valid: [
154+
{
155+
code: `
156+
/**
157+
*
158+
*/
159+
`,
160+
},
137161
{
138162
code: `
139163
/**

test/rules/assertions/requireJsdoc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,12 @@ export default {
11421142
},
11431143
],
11441144
valid: [{
1145+
code: `
1146+
/**
1147+
*
1148+
*/
1149+
`,
1150+
}, {
11451151
code: `
11461152
var array = [1,2,3];
11471153
array.forEach(function() {});

0 commit comments

Comments
 (0)