Skip to content

Commit c2b9651

Browse files
authored
Merge pull request #221 from brettz9/avoidExampleOnConstructors
Avoid example on constructors
2 parents 5d5dc85 + 8b23024 commit c2b9651

File tree

4 files changed

+142
-2
lines changed

4 files changed

+142
-2
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,23 @@ function quux () {
18231823
*/
18241824
function quux () {
18251825

1826+
}
1827+
// Message: Missing JSDoc @example description.
1828+
1829+
/**
1830+
* @constructor
1831+
*/
1832+
function quux () {
1833+
1834+
}
1835+
// Message: Missing JSDoc @example declaration.
1836+
1837+
/**
1838+
* @constructor
1839+
* @example
1840+
*/
1841+
function quux () {
1842+
18261843
}
18271844
// Message: Missing JSDoc @example description.
18281845
````
@@ -1855,6 +1872,30 @@ function quux () {
18551872
*/
18561873
function quux () {
18571874

1875+
}
1876+
1877+
/**
1878+
* @constructor
1879+
*/
1880+
function quux () {
1881+
1882+
}
1883+
// Settings: {"jsdoc":{"avoidExampleOnConstructors":true}}
1884+
1885+
/**
1886+
* @constructor
1887+
* @example
1888+
*/
1889+
function quux () {
1890+
1891+
}
1892+
// Settings: {"jsdoc":{"avoidExampleOnConstructors":true}}
1893+
1894+
/**
1895+
* @inheritdoc
1896+
*/
1897+
function quux () {
1898+
18581899
}
18591900
````
18601901

src/iterateJsdoc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const curryUtils = (
4242
allowAugmentsExtendsWithoutParam,
4343
checkSeesForNamepaths,
4444
forceRequireReturn,
45+
avoidExampleOnConstructors,
4546
ancestors,
4647
sourceCode,
4748
context
@@ -142,6 +143,11 @@ const curryUtils = (
142143
utils.isNamepathType = (tagName) => {
143144
return jsdocUtils.isNamepathType(tagName, checkSeesForNamepaths);
144145
};
146+
147+
utils.avoidExampleOnConstructors = () => {
148+
return avoidExampleOnConstructors;
149+
};
150+
145151
utils.passesEmptyNamepathCheck = (tag) => {
146152
return !tag.name && allowEmptyNamepaths && _.includes([
147153
// These may serve some minor purpose when empty
@@ -243,6 +249,7 @@ export default (iterator, options) => {
243249
const allowAugmentsExtendsWithoutParam = Boolean(_.get(context, 'settings.jsdoc.allowAugmentsExtendsWithoutParam'));
244250
const checkSeesForNamepaths = Boolean(_.get(context, 'settings.jsdoc.checkSeesForNamepaths'));
245251
const forceRequireReturn = Boolean(_.get(context, 'settings.jsdoc.forceRequireReturn'));
252+
const avoidExampleOnConstructors = Boolean(_.get(context, 'settings.jsdoc.avoidExampleOnConstructors'));
246253

247254
const checkJsdoc = (node) => {
248255
const jsdocNode = sourceCode.getJSDocComment(node);
@@ -311,6 +318,7 @@ export default (iterator, options) => {
311318
allowAugmentsExtendsWithoutParam,
312319
checkSeesForNamepaths,
313320
forceRequireReturn,
321+
avoidExampleOnConstructors,
314322
ancestors,
315323
sourceCode
316324
);

src/rules/requireExample.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,30 @@ export default iterateJsdoc(({
1212
tag: targetTagName
1313
});
1414

15+
if (utils.hasATag([
16+
'inheritdoc',
17+
'override'
18+
])) {
19+
return;
20+
}
21+
22+
if (utils.avoidExampleOnConstructors() && (
23+
utils.hasATag([
24+
'class',
25+
'constructor'
26+
]) ||
27+
utils.isConstructor()
28+
)) {
29+
return;
30+
}
31+
1532
if (_.isEmpty(functionExamples)) {
16-
return report('Missing JSDoc @' + targetTagName + ' declaration.');
33+
report('Missing JSDoc @' + targetTagName + ' declaration.');
34+
35+
return;
1736
}
1837

19-
return _.forEach(functionExamples, (example) => {
38+
_.forEach(functionExamples, (example) => {
2039
const exampleContent = _.compact((example.name + ' ' + example.description).trim().split('\n'));
2140

2241
if (_.isEmpty(exampleContent)) {

test/rules/assertions/requireExample.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ export default {
2929
message: 'Missing JSDoc @example description.'
3030
}
3131
]
32+
},
33+
{
34+
code: `
35+
/**
36+
* @constructor
37+
*/
38+
function quux () {
39+
40+
}
41+
`,
42+
errors: [
43+
{
44+
message: 'Missing JSDoc @example declaration.'
45+
}
46+
]
47+
},
48+
{
49+
code: `
50+
/**
51+
* @constructor
52+
* @example
53+
*/
54+
function quux () {
55+
56+
}
57+
`,
58+
errors: [
59+
{
60+
message: 'Missing JSDoc @example description.'
61+
}
62+
]
3263
}
3364
],
3465
valid: [
@@ -67,6 +98,47 @@ export default {
6798
6899
}
69100
`
101+
},
102+
{
103+
code: `
104+
/**
105+
* @constructor
106+
*/
107+
function quux () {
108+
109+
}
110+
`,
111+
settings: {
112+
jsdoc: {
113+
avoidExampleOnConstructors: true
114+
}
115+
}
116+
},
117+
{
118+
code: `
119+
/**
120+
* @constructor
121+
* @example
122+
*/
123+
function quux () {
124+
125+
}
126+
`,
127+
settings: {
128+
jsdoc: {
129+
avoidExampleOnConstructors: true
130+
}
131+
}
132+
},
133+
{
134+
code: `
135+
/**
136+
* @inheritdoc
137+
*/
138+
function quux () {
139+
140+
}
141+
`
70142
}
71143
]
72144
};

0 commit comments

Comments
 (0)