File tree Expand file tree Collapse file tree 5 files changed +201
-4
lines changed Expand file tree Collapse file tree 5 files changed +201
-4
lines changed Original file line number Diff line number Diff line change @@ -3372,6 +3372,33 @@ function quux () {
3372
3372
3373
3373
}
3374
3374
// 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.
3375
3402
````
3376
3403
3377
3404
The following patterns are not considered problems:
@@ -3426,6 +3453,27 @@ function quux () {
3426
3453
3427
3454
}
3428
3455
// 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
+ };
3429
3477
````
3430
3478
3431
3479
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ const looksLikeExport = function (astNode) {
25
25
astNode . type === 'ExportAllDeclaration' || astNode . type === 'ExportSpecifier' ;
26
26
} ;
27
27
28
+ /* eslint-disable complexity */
28
29
/**
29
30
* Retrieves the JSDoc comment for a given node.
30
31
*
@@ -67,8 +68,7 @@ const getJSDocComment = function (sourceCode, node) {
67
68
return findJSDocComment ( looksLikeExport ( parent ) ? parent : node ) ;
68
69
69
70
case 'ClassExpression' :
70
- return findJSDocComment ( parent . parent ) ;
71
-
71
+ case 'ObjectExpression' :
72
72
case 'ArrowFunctionExpression' :
73
73
case 'FunctionExpression' :
74
74
if (
@@ -96,10 +96,14 @@ const getJSDocComment = function (sourceCode, node) {
96
96
97
97
return findJSDocComment ( node ) ;
98
98
99
- // falls through
100
99
default :
101
- return null ;
100
+ if ( ! node ) {
101
+ return null ;
102
+ }
103
+
104
+ return findJSDocComment ( node ) ;
102
105
}
103
106
} ;
107
+ /* eslint-enable complexity */
104
108
105
109
export default getJSDocComment ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -90,6 +90,74 @@ export default {
90
90
message : 'Missing JSDoc @description description.'
91
91
}
92
92
]
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
+ ]
93
161
}
94
162
] ,
95
163
valid : [
@@ -168,6 +236,37 @@ export default {
168
236
exemptedBy : [ 'type' ]
169
237
}
170
238
]
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
+ `
171
270
}
172
271
]
173
272
} ;
Original file line number Diff line number Diff line change @@ -49,6 +49,9 @@ const ruleTester = new RuleTester();
49
49
} ) ;
50
50
51
51
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
+ }
52
55
assertion . parserOptions = _ . defaultsDeep ( assertion . parserOptions , parserOptions ) ;
53
56
54
57
return assertion ;
You can’t perform that action at this time.
0 commit comments