Skip to content

Commit 392e62d

Browse files
authored
Merge pull request #295 from fa93hws/master
fix: check for comments immediately above any node by default and for relevant parents of `ClassExpression` and `ObjectExpression` This fix is so that for rules allowing contexts not yet covered by `getJSDocComment` can at least lint comments immediately above the block
2 parents 383a557 + 588d40f commit 392e62d

File tree

5 files changed

+201
-4
lines changed

5 files changed

+201
-4
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3372,6 +3372,33 @@ function quux () {
33723372

33733373
}
33743374
// Message: Missing JSDoc @description description.
3375+
3376+
/**
3377+
*
3378+
*/
3379+
interface quux {
3380+
3381+
}
3382+
// Options: [{"contexts":["TSInterfaceDeclaration"],"noDefaults":true}]
3383+
// Message: Missing JSDoc @description declaration.
3384+
3385+
/**
3386+
*
3387+
*/
3388+
var quux = class {
3389+
3390+
};
3391+
// Options: [{"contexts":["ClassExpression"]}]
3392+
// Message: Missing JSDoc @description declaration.
3393+
3394+
/**
3395+
*
3396+
*/
3397+
var quux = {
3398+
3399+
};
3400+
// Options: [{"contexts":["ObjectExpression"]}]
3401+
// Message: Missing JSDoc @description declaration.
33753402
````
33763403

33773404
The following patterns are not considered problems:
@@ -3426,6 +3453,27 @@ function quux () {
34263453

34273454
}
34283455
// Options: [{"exemptedBy":["type"]}]
3456+
3457+
/**
3458+
*
3459+
*/
3460+
interface quux {
3461+
3462+
}
3463+
3464+
/**
3465+
*
3466+
*/
3467+
var quux = class {
3468+
3469+
};
3470+
3471+
/**
3472+
*
3473+
*/
3474+
var quux = {
3475+
3476+
};
34293477
````
34303478

34313479

src/eslint/getJSDocComment.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const looksLikeExport = function (astNode) {
2525
astNode.type === 'ExportAllDeclaration' || astNode.type === 'ExportSpecifier';
2626
};
2727

28+
/* eslint-disable complexity */
2829
/**
2930
* Retrieves the JSDoc comment for a given node.
3031
*
@@ -67,8 +68,7 @@ const getJSDocComment = function (sourceCode, node) {
6768
return findJSDocComment(looksLikeExport(parent) ? parent : node);
6869

6970
case 'ClassExpression':
70-
return findJSDocComment(parent.parent);
71-
71+
case 'ObjectExpression':
7272
case 'ArrowFunctionExpression':
7373
case 'FunctionExpression':
7474
if (
@@ -96,10 +96,14 @@ const getJSDocComment = function (sourceCode, node) {
9696

9797
return findJSDocComment(node);
9898

99-
// falls through
10099
default:
101-
return null;
100+
if (!node) {
101+
return null;
102+
}
103+
104+
return findJSDocComment(node);
102105
}
103106
};
107+
/* eslint-enable complexity */
104108

105109
export default getJSDocComment;

test/eslint/getJSDocComment.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
RuleTester
3+
} from 'eslint';
4+
import iterateJsdoc from '../../src/iterateJsdoc';
5+
import getJSDocComment from '../../src/eslint/getJSDocComment';
6+
7+
const rule = iterateJsdoc(null, {
8+
meta: {
9+
messages: {
10+
missingJsDoc: 'Missing JSDoc comment.'
11+
},
12+
type: 'layout'
13+
},
14+
returns (context, sourceCode) {
15+
return {
16+
ObjectExpression: (node) => {
17+
const comment = getJSDocComment(sourceCode, node);
18+
if (comment !== null) {
19+
return;
20+
}
21+
context.report({
22+
messageId: 'missingJsDoc',
23+
node
24+
});
25+
}
26+
};
27+
}
28+
});
29+
30+
const ruleTester = new RuleTester();
31+
32+
ruleTester.run('getJSDocComment', rule, {
33+
invalid: [{
34+
code: 'var a = {};',
35+
errors: [{messageId: 'missingJsDoc'}]
36+
}],
37+
valid: [{
38+
code: `
39+
/** Doc */
40+
var a = {};
41+
`
42+
}]
43+
});

test/rules/assertions/requireDescription.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,74 @@ export default {
9090
message: 'Missing JSDoc @description description.'
9191
}
9292
]
93+
},
94+
{
95+
code: `
96+
/**
97+
*
98+
*/
99+
interface quux {
100+
101+
}
102+
`,
103+
errors: [
104+
{
105+
message: 'Missing JSDoc @description declaration.'
106+
}
107+
],
108+
options: [
109+
{
110+
contexts: [
111+
'TSInterfaceDeclaration'
112+
],
113+
noDefaults: true
114+
}
115+
],
116+
parser: require.resolve('@typescript-eslint/parser')
117+
},
118+
{
119+
code: `
120+
/**
121+
*
122+
*/
123+
var quux = class {
124+
125+
};
126+
`,
127+
errors: [
128+
{
129+
message: 'Missing JSDoc @description declaration.'
130+
}
131+
],
132+
options: [
133+
{
134+
contexts: [
135+
'ClassExpression'
136+
]
137+
}
138+
]
139+
},
140+
{
141+
code: `
142+
/**
143+
*
144+
*/
145+
var quux = {
146+
147+
};
148+
`,
149+
errors: [
150+
{
151+
message: 'Missing JSDoc @description declaration.'
152+
}
153+
],
154+
options: [
155+
{
156+
contexts: [
157+
'ObjectExpression'
158+
]
159+
}
160+
]
93161
}
94162
],
95163
valid: [
@@ -168,6 +236,37 @@ export default {
168236
exemptedBy: ['type']
169237
}
170238
]
239+
},
240+
{
241+
code: `
242+
/**
243+
*
244+
*/
245+
interface quux {
246+
247+
}
248+
`,
249+
parser: require.resolve('@typescript-eslint/parser')
250+
},
251+
{
252+
code: `
253+
/**
254+
*
255+
*/
256+
var quux = class {
257+
258+
};
259+
`
260+
},
261+
{
262+
code: `
263+
/**
264+
*
265+
*/
266+
var quux = {
267+
268+
};
269+
`
171270
}
172271
]
173272
};

test/rules/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ const ruleTester = new RuleTester();
4949
});
5050

5151
assertions.valid = assertions.valid.map((assertion) => {
52+
if (assertion.errors) {
53+
throw new Error(`Valid assertions for rule ${ruleName} should not have an \`errors\` array.`);
54+
}
5255
assertion.parserOptions = _.defaultsDeep(assertion.parserOptions, parserOptions);
5356

5457
return assertion;

0 commit comments

Comments
 (0)