Skip to content

Commit d282ba0

Browse files
author
Max Schaefer
committed
JavaScript: Add concrete syntax examples to JSDoc, RegExp, Tokens, Variables and XML.
1 parent 74397da commit d282ba0

File tree

5 files changed

+862
-82
lines changed

5 files changed

+862
-82
lines changed

javascript/ql/src/semmle/javascript/JSDoc.qll

Lines changed: 172 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import javascript
44

55
/**
66
* A JSDoc comment.
7+
*
8+
* Example:
9+
*
10+
* <pre>
11+
* /**
12+
* * An example JSDoc comment documenting a constructor function.
13+
* *
14+
* * @constructor
15+
* * @param {Object=} options An object literal with options.
16+
* *&#47;
17+
* </pre>
718
*/
819
class JSDoc extends @jsdoc, Locatable {
920
override Location getLocation() { hasLocation(this, result) }
@@ -28,6 +39,20 @@ class JSDoc extends @jsdoc, Locatable {
2839

2940
/**
3041
* A program element that can have a JSDoc comment.
42+
*
43+
* Example:
44+
*
45+
* <pre>
46+
* /**
47+
* * An example JSDoc comment documenting a constructor function.
48+
* *
49+
* * @constructor
50+
* * @param {Object=} options An object literal with options.
51+
* *&#47;
52+
* function MyConstructor(options) { // documentable
53+
* this.options = options || {};
54+
* }
55+
* </pre>
3156
*/
3257
abstract class Documentable extends ASTNode {
3358
/** Gets the JSDoc comment for this element, if any. */
@@ -38,6 +63,13 @@ abstract class Documentable extends ASTNode {
3863
/**
3964
* A syntactic element that a JSDoc type expression may be nested in, that is,
4065
* either a JSDoc tag or another JSDoc type expression.
66+
*
67+
* Examples:
68+
*
69+
* ```
70+
* // the `@param` tag and the `...=` type expressions are JSDoc type expression parents
71+
* @param {Object=} options An object literal with options.
72+
* ```
4173
*/
4274
class JSDocTypeExprParent extends @jsdoc_type_expr_parent, Locatable {
4375
override Location getLocation() { hasLocation(this, result) }
@@ -46,7 +78,14 @@ class JSDocTypeExprParent extends @jsdoc_type_expr_parent, Locatable {
4678
}
4779

4880
/**
49-
* A JSDoc tag such as `@param Object options An object literal with options.`
81+
* A JSDoc tag.
82+
*
83+
* Examples:
84+
*
85+
* ```
86+
* @param {Object=} options An object literal with options.
87+
* @return {!Server}
88+
* ```
5089
*/
5190
class JSDocTag extends @jsdoc_tag, JSDocTypeExprParent {
5291
/** Gets the tag title; for instance, the title of a `@param` tag is `"param"`. */
@@ -90,6 +129,12 @@ class JSDocTag extends @jsdoc_tag, JSDocTypeExprParent {
90129

91130
/**
92131
* A `@param` tag.
132+
*
133+
* Example:
134+
*
135+
* ```
136+
* @param {Object=} options An object literal with options.
137+
* ```
93138
*/
94139
class JSDocParamTag extends JSDocTag {
95140
JSDocParamTag() { getTitle().regexpMatch("param|arg(ument)?") }
@@ -104,6 +149,14 @@ class JSDocParamTag extends JSDocTag {
104149

105150
/**
106151
* A JSDoc type expression.
152+
*
153+
* Examples:
154+
*
155+
* ```
156+
* *
157+
* Array<string>
158+
* !Object
159+
* ```
107160
*/
108161
class JSDocTypeExpr extends @jsdoc_type_expr, JSDocTypeExprParent, TypeAnnotation {
109162
/**
@@ -142,32 +195,81 @@ class JSDocTypeExpr extends @jsdoc_type_expr, JSDocTypeExprParent, TypeAnnotatio
142195
override TopLevel getTopLevel() { result = getEnclosingStmt().getTopLevel() }
143196
}
144197

145-
/** An `any` type expression `*`. */
198+
/**
199+
* An `any` type expression.
200+
*
201+
* Example:
202+
*
203+
* ```
204+
* *
205+
* ```
206+
*/
146207
class JSDocAnyTypeExpr extends @jsdoc_any_type_expr, JSDocTypeExpr {
147208
override predicate isAny() { any() }
148209
}
149210

150-
/** A null type expression. */
211+
/**
212+
* A null type expression.
213+
*
214+
* Example:
215+
*
216+
* ```
217+
* null
218+
* ```
219+
*/
151220
class JSDocNullTypeExpr extends @jsdoc_null_type_expr, JSDocTypeExpr {
152221
override predicate isNull() { any() }
153222
}
154223

155-
/** A type expression representing the type of `undefined`. */
224+
/**
225+
* A type expression representing the type of `undefined`.
226+
*
227+
* Example:
228+
*
229+
* ```
230+
* undefined
231+
* ```
232+
*/
156233
class JSDocUndefinedTypeExpr extends @jsdoc_undefined_type_expr, JSDocTypeExpr {
157234
override predicate isUndefined() { any() }
158235
}
159236

160-
/** A type expression representing an unknown type `?`. */
237+
/**
238+
* A type expression representing an unknown type.
239+
*
240+
* Example:
241+
*
242+
* ```
243+
* ?
244+
* ```
245+
*/
161246
class JSDocUnknownTypeExpr extends @jsdoc_unknown_type_expr, JSDocTypeExpr {
162247
override predicate isUnknownKeyword() { any() }
163248
}
164249

165-
/** A type expression representing the void type. */
250+
/**
251+
* A type expression representing the void type.
252+
*
253+
* Example:
254+
*
255+
* ```
256+
* void
257+
* ```
258+
*/
166259
class JSDocVoidTypeExpr extends @jsdoc_void_type_expr, JSDocTypeExpr {
167260
override predicate isVoid() { any() }
168261
}
169262

170-
/** A type expression referring to a named type. */
263+
/**
264+
* A type expression referring to a named type.
265+
*
266+
* Example:
267+
*
268+
* ```
269+
* string
270+
* Object
271+
* ```
272+
*/
171273
class JSDocNamedTypeExpr extends @jsdoc_named_type_expr, JSDocTypeExpr {
172274
/** Gets the name of the type the expression refers to. */
173275
string getName() { result = toString() }
@@ -211,7 +313,13 @@ class JSDocNamedTypeExpr extends @jsdoc_named_type_expr, JSDocTypeExpr {
211313
}
212314

213315
/**
214-
* An applied type expression such as `Array<string>`.
316+
* An applied type expression.
317+
*
318+
* Example:
319+
*
320+
* ```
321+
* Array<string>
322+
* ```
215323
*/
216324
class JSDocAppliedTypeExpr extends @jsdoc_applied_type_expr, JSDocTypeExpr {
217325
/** Gets the head type expression, such as `Array` in `Array<string>`. */
@@ -237,7 +345,13 @@ class JSDocAppliedTypeExpr extends @jsdoc_applied_type_expr, JSDocTypeExpr {
237345
}
238346

239347
/**
240-
* A nullable type expression such as `?number`.
348+
* A nullable type expression.
349+
*
350+
* Example:
351+
*
352+
* ```
353+
* ?Array
354+
* ```
241355
*/
242356
class JSDocNullableTypeExpr extends @jsdoc_nullable_type_expr, JSDocTypeExpr {
243357
/** Gets the argument type expression. */
@@ -250,7 +364,13 @@ class JSDocNullableTypeExpr extends @jsdoc_nullable_type_expr, JSDocTypeExpr {
250364
}
251365

252366
/**
253-
* A non-nullable type expression such as `!number`.
367+
* A non-nullable type expression.
368+
*
369+
* Example:
370+
*
371+
* ```
372+
* !Array
373+
* ```
254374
*/
255375
class JSDocNonNullableTypeExpr extends @jsdoc_non_nullable_type_expr, JSDocTypeExpr {
256376
/** Gets the argument type expression. */
@@ -263,7 +383,13 @@ class JSDocNonNullableTypeExpr extends @jsdoc_non_nullable_type_expr, JSDocTypeE
263383
}
264384

265385
/**
266-
* A record type expression such as `{ x: number, y: string }`.
386+
* A record type expression.
387+
*
388+
* Example:
389+
*
390+
* ```
391+
* { x: number, y: string }
392+
* ```
267393
*/
268394
class JSDocRecordTypeExpr extends @jsdoc_record_type_expr, JSDocTypeExpr {
269395
/** Gets the name of the `i`th field of the record type. */
@@ -282,7 +408,13 @@ class JSDocRecordTypeExpr extends @jsdoc_record_type_expr, JSDocTypeExpr {
282408
}
283409

284410
/**
285-
* An array type expression such as `[string]`.
411+
* An array type expression.
412+
*
413+
* Example:
414+
*
415+
* ```
416+
* [string]
417+
* ```
286418
*/
287419
class JSDocArrayTypeExpr extends @jsdoc_array_type_expr, JSDocTypeExpr {
288420
/** Gets the type of the `i`th element of this array type. */
@@ -293,7 +425,13 @@ class JSDocArrayTypeExpr extends @jsdoc_array_type_expr, JSDocTypeExpr {
293425
}
294426

295427
/**
296-
* A union type expression such as `number|string`.
428+
* A union type expression.
429+
*
430+
* Example:
431+
*
432+
* ```
433+
* number|string
434+
* ```
297435
*/
298436
class JSDocUnionTypeExpr extends @jsdoc_union_type_expr, JSDocTypeExpr {
299437
/** Gets one of the type alternatives of this union type. */
@@ -303,7 +441,13 @@ class JSDocUnionTypeExpr extends @jsdoc_union_type_expr, JSDocTypeExpr {
303441
}
304442

305443
/**
306-
* A function type expression such as `function(string): number`.
444+
* A function type expression.
445+
*
446+
* Example:
447+
*
448+
* ```
449+
* function(string): number
450+
* ```
307451
*/
308452
class JSDocFunctionTypeExpr extends @jsdoc_function_type_expr, JSDocTypeExpr {
309453
/** Gets the result type of this function type. */
@@ -323,7 +467,13 @@ class JSDocFunctionTypeExpr extends @jsdoc_function_type_expr, JSDocTypeExpr {
323467
}
324468

325469
/**
326-
* An optional parameter type such as `number=`.
470+
* An optional parameter type.
471+
*
472+
* Example:
473+
*
474+
* ```
475+
* number=
476+
* ```
327477
*/
328478
class JSDocOptionalParameterTypeExpr extends @jsdoc_optional_type_expr, JSDocTypeExpr {
329479
/** Gets the underlying type of this optional type. */
@@ -333,7 +483,13 @@ class JSDocOptionalParameterTypeExpr extends @jsdoc_optional_type_expr, JSDocTyp
333483
}
334484

335485
/**
336-
* A rest parameter type such as `string...`.
486+
* A rest parameter type.
487+
*
488+
* Example:
489+
*
490+
* ```
491+
* string...
492+
* ```
337493
*/
338494
class JSDocRestParameterTypeExpr extends @jsdoc_rest_type_expr, JSDocTypeExpr {
339495
/** Gets the underlying type of this rest parameter type. */

0 commit comments

Comments
 (0)