@@ -52,7 +52,7 @@ function assert(
52
52
condition : boolean ,
53
53
message : string ,
54
54
document : { location : Location } ,
55
- ) {
55
+ ) : asserts condition {
56
56
if ( ! condition ) {
57
57
diagnostics . push ( new DocumentError ( message , document ) ) ;
58
58
}
@@ -77,18 +77,17 @@ function isVoid(returnType: TsTypeDef) {
77
77
78
78
function assertHasReturnTag ( document : { jsDoc : JsDoc ; location : Location } ) {
79
79
const tag = document . jsDoc . tags ?. find ( ( tag ) => tag . kind === "return" ) ;
80
- if ( tag === undefined ) {
81
- diagnostics . push (
82
- new DocumentError ( "Symbol must have a @return or @returns tag" , document ) ,
83
- ) ;
84
- } else {
85
- assert (
86
- // @ts -ignore doc is defined
87
- tag . doc !== undefined ,
88
- "@return tag must have a description" ,
89
- document ,
90
- ) ;
91
- }
80
+ assert (
81
+ tag !== undefined ,
82
+ "Symbol must have a @return or @returns tag" ,
83
+ document ,
84
+ ) ;
85
+ if ( tag === undefined ) return ;
86
+ assert (
87
+ tag . doc !== undefined ,
88
+ "@return tag must have a description" ,
89
+ document ,
90
+ ) ;
92
91
}
93
92
94
93
/**
@@ -111,14 +110,11 @@ function assertHasParamDefinition(
111
110
return false ;
112
111
} ) ;
113
112
114
- if ( ! paramDoc ) {
115
- diagnostics . push (
116
- new DocumentError (
117
- `@param ${ param . name } must have a corresponding named function parameter definition.` ,
118
- document ,
119
- ) ,
120
- ) ;
121
- }
113
+ assert (
114
+ paramDoc !== undefined ,
115
+ `@param ${ param . name } must have a corresponding function parameter definition.` ,
116
+ document ,
117
+ ) ;
122
118
}
123
119
124
120
function assertHasParamTag (
@@ -128,37 +124,31 @@ function assertHasParamTag(
128
124
const tag = document . jsDoc . tags ?. find ( ( tag ) =>
129
125
tag . kind === "param" && tag . name === param
130
126
) ;
131
- if ( ! tag ) {
132
- diagnostics . push (
133
- new DocumentError ( `Symbol must have a @param tag for ${ param } ` , document ) ,
134
- ) ;
135
- } else {
136
- assert (
137
- // @ts -ignore doc is defined
138
- tag . doc !== undefined ,
139
- `@param tag for ${ param } must have a description` ,
140
- document ,
141
- ) ;
142
- }
127
+ assert (
128
+ tag !== undefined ,
129
+ `Symbol must have a @param tag for ${ param } ` ,
130
+ document ,
131
+ ) ;
132
+ if ( tag === undefined ) return ;
133
+ assert (
134
+ // @ts -ignore doc is defined
135
+ tag . doc !== undefined ,
136
+ `@param tag for ${ param } must have a description` ,
137
+ document ,
138
+ ) ;
143
139
}
144
140
145
141
function assertHasSnippets (
146
142
doc : string ,
147
143
document : { jsDoc : JsDoc ; location : Location } ,
148
- required = true ,
149
144
) {
150
145
const snippets = doc . match ( TS_SNIPPET ) ;
151
- if ( snippets === null ) {
152
- if ( required ) {
153
- diagnostics . push (
154
- new DocumentError (
155
- "@example tag must have a TypeScript code snippet" ,
156
- document ,
157
- ) ,
158
- ) ;
159
- }
160
- return ;
161
- }
146
+ assert (
147
+ snippets !== null ,
148
+ "@example tag must have a TypeScript code snippet" ,
149
+ document ,
150
+ ) ;
151
+ if ( snippets === null ) return ;
162
152
for ( let snippet of snippets ) {
163
153
const delim = snippet . split ( NEWLINE ) [ 0 ] ;
164
154
// Trim the code block delimiters
@@ -179,17 +169,7 @@ function assertHasExampleTag(
179
169
const exampleTags = document . jsDoc . tags ?. filter ( ( tag ) =>
180
170
tag . kind === "example"
181
171
) as JsDocTagDocRequired [ ] ;
182
- const hasNoExampleTags = exampleTags === undefined ||
183
- exampleTags . length === 0 ;
184
- if (
185
- hasNoExampleTags &&
186
- ! document . jsDoc . tags ?. some ( ( tag ) => tag . kind === "private" )
187
- ) {
188
- diagnostics . push (
189
- new DocumentError ( "Symbol must have an @example tag" , document ) ,
190
- ) ;
191
- return ;
192
- }
172
+ assert ( exampleTags ?. length > 0 , "Symbol must have an @example tag" , document ) ;
193
173
for ( const tag of exampleTags ) {
194
174
assert (
195
175
tag . doc !== undefined ,
@@ -216,21 +196,17 @@ function assertHasTypeParamTags(
216
196
const tag = document . jsDoc . tags ?. find ( ( tag ) =>
217
197
tag . kind === "template" && tag . name === typeParamName
218
198
) ;
219
- if ( tag === undefined ) {
220
- diagnostics . push (
221
- new DocumentError (
222
- `Symbol must have a @typeParam tag for ${ typeParamName } ` ,
223
- document ,
224
- ) ,
225
- ) ;
226
- } else {
227
- assert (
228
- // @ts -ignore doc is defined
229
- tag . doc !== undefined ,
230
- `@typeParam tag for ${ typeParamName } must have a description` ,
231
- document ,
232
- ) ;
233
- }
199
+ assert (
200
+ tag !== undefined ,
201
+ `Symbol must have a @typeParam tag for ${ typeParamName } ` ,
202
+ document ,
203
+ ) ;
204
+ assert (
205
+ // @ts -ignore doc is defined
206
+ tag . doc !== undefined ,
207
+ `@typeParam tag for ${ typeParamName } must have a description` ,
208
+ document ,
209
+ ) ;
234
210
}
235
211
236
212
/**
@@ -245,7 +221,6 @@ function assertHasTypeParamTags(
245
221
function assertFunctionDocs (
246
222
document : DocNodeWithJsDoc < DocNodeFunction | ClassMethodDef > ,
247
223
) {
248
- assertHasSnippets ( document . jsDoc . doc ! , document , false ) ;
249
224
for ( const param of document . functionDef . params ) {
250
225
if ( param . kind === "identifier" ) {
251
226
assertHasParamTag ( document , param . name ) ;
@@ -289,7 +264,6 @@ function assertFunctionDocs(
289
264
* - Documentation on all properties, methods, and constructors.
290
265
*/
291
266
function assertClassDocs ( document : DocNodeWithJsDoc < DocNodeClass > ) {
292
- assertHasSnippets ( document . jsDoc . doc ! , document , false ) ;
293
267
for ( const typeParam of document . classDef . typeParams ) {
294
268
assertHasTypeParamTags ( document , typeParam . name ) ;
295
269
}
@@ -299,41 +273,31 @@ function assertClassDocs(document: DocNodeWithJsDoc<DocNodeClass>) {
299
273
300
274
for ( const property of document . classDef . properties ) {
301
275
if ( property . jsDoc === undefined ) continue ; // this is caught by `deno doc --lint`
302
- if ( property . accessibility !== undefined ) {
303
- diagnostics . push (
304
- new DocumentError (
305
- "Do not use `public`, `protected`, or `private` fields in classes" ,
306
- property ,
307
- ) ,
308
- ) ;
309
- continue ;
310
- }
276
+ assert (
277
+ property . accessibility === undefined ,
278
+ "Do not use `public`, `protected`, or `private` fields in classes" ,
279
+ property ,
280
+ ) ;
311
281
assertClassPropertyDocs (
312
282
property as DocNodeWithJsDoc < ClassPropertyDef > ,
313
283
) ;
314
284
}
315
285
for ( const method of document . classDef . methods ) {
316
286
if ( method . jsDoc === undefined ) continue ; // this is caught by `deno doc --lint`
317
- if ( method . accessibility !== undefined ) {
318
- diagnostics . push (
319
- new DocumentError (
320
- "Do not use `public`, `protected`, or `private` methods in classes" ,
321
- method ,
322
- ) ,
323
- ) ;
324
- }
287
+ assert (
288
+ method . accessibility === undefined ,
289
+ "Do not use `public`, `protected`, or `private` methods in classes" ,
290
+ document ,
291
+ ) ;
325
292
assertFunctionDocs ( method as DocNodeWithJsDoc < ClassMethodDef > ) ;
326
293
}
327
294
for ( const constructor of document . classDef . constructors ) {
328
295
if ( constructor . jsDoc === undefined ) continue ; // this is caught by `deno doc --lint`
329
- if ( constructor . accessibility !== undefined ) {
330
- diagnostics . push (
331
- new DocumentError (
332
- "Do not use `public`, `protected`, or `private` constructors in classes" ,
333
- constructor ,
334
- ) ,
335
- ) ;
336
- }
296
+ assert (
297
+ constructor . accessibility === undefined ,
298
+ "Do not use `public`, `protected`, or `private` constructors in classes" ,
299
+ constructor ,
300
+ ) ;
337
301
assertConstructorDocs (
338
302
constructor as DocNodeWithJsDoc < ClassConstructorDef > ,
339
303
) ;
@@ -392,14 +356,11 @@ function assertModuleDoc(document: DocNodeWithJsDoc<DocNodeModuleDoc>) {
392
356
function assertHasDefaultTags ( document : DocNodeWithJsDoc < DocNodeInterface > ) {
393
357
for ( const prop of document . interfaceDef . properties ) {
394
358
if ( ! prop . optional ) continue ;
395
- if ( ! prop . jsDoc ?. tags ?. find ( ( tag ) => tag . kind === "default" ) ) {
396
- diagnostics . push (
397
- new DocumentError (
398
- "Optional interface properties should have default values" ,
399
- document ,
400
- ) ,
401
- ) ;
402
- }
359
+ assert (
360
+ prop . jsDoc ?. tags ?. find ( ( tag ) => tag . kind === "default" ) !== undefined ,
361
+ "Optional interface properties should have default values" ,
362
+ document ,
363
+ ) ;
403
364
}
404
365
}
405
366
@@ -417,14 +378,11 @@ function assertHasDeprecationDesc(document: DocNodeWithJsDoc<DocNode>) {
417
378
if ( ! tags ) return ;
418
379
for ( const tag of tags ) {
419
380
if ( tag . kind !== "deprecated" ) continue ;
420
- if ( tag . doc === undefined ) {
421
- diagnostics . push (
422
- new DocumentError (
423
- "@deprecated tag must have a description" ,
424
- document ,
425
- ) ,
426
- ) ;
427
- }
381
+ assert (
382
+ tag . doc !== undefined ,
383
+ "@deprecated tag must have a description" ,
384
+ document ,
385
+ ) ;
428
386
}
429
387
}
430
388
@@ -434,30 +392,24 @@ async function assertDocs(specifiers: string[]) {
434
392
if ( d . jsDoc === undefined || d . declarationKind !== "export" ) continue ; // this is caught by other checks
435
393
436
394
const document = d as DocNodeWithJsDoc < DocNode > ;
395
+ assertHasDeprecationDesc ( document ) ;
437
396
switch ( document . kind ) {
438
397
case "moduleDoc" : {
439
398
if ( document . location . filename . endsWith ( "/mod.ts" ) ) {
440
399
assertModuleDoc ( document ) ;
441
- assertHasDeprecationDesc ( document ) ;
442
400
}
443
401
break ;
444
402
}
445
403
case "function" : {
446
404
assertFunctionDocs ( document ) ;
447
- assertHasDeprecationDesc ( document ) ;
448
405
break ;
449
406
}
450
407
case "class" : {
451
408
assertClassDocs ( document ) ;
452
- assertHasDeprecationDesc ( document ) ;
453
409
break ;
454
410
}
455
411
case "interface" :
456
412
assertInterfaceDocs ( document ) ;
457
- assertHasDeprecationDesc ( document ) ;
458
- break ;
459
- case "variable" :
460
- assertHasDeprecationDesc ( document ) ;
461
413
break ;
462
414
}
463
415
}
0 commit comments