Skip to content

Commit 81b78dc

Browse files
authored
Merge pull request #1603 from xiemaisi/js/more-examples
Approved by asger-semmle
2 parents de24bec + ae07546 commit 81b78dc

File tree

5 files changed

+255
-20
lines changed

5 files changed

+255
-20
lines changed

javascript/ql/src/semmle/javascript/Comments.qll

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@
22

33
import javascript
44

5-
/** A JavaScript source code comment. */
5+
/**
6+
* A JavaScript source-code comment.
7+
*
8+
* Examples:
9+
*
10+
* <pre>
11+
* // a line comment
12+
* /* a block
13+
* comment *&#47
14+
* &lt;!-- an HTML line comment
15+
* </pre>
16+
*/
617
class Comment extends @comment, Locatable {
718
override Location getLocation() { hasLocation(this, result) }
819

@@ -32,26 +43,95 @@ class Comment extends @comment, Locatable {
3243
}
3344
}
3445

35-
/** A line comment, that is, either an HTML comment or a `//` comment. */
46+
/**
47+
* A line comment, that is, either an HTML comment or a `//` comment.
48+
*
49+
* Examples:
50+
*
51+
* <pre>
52+
* // a line comment
53+
* &lt;!-- an HTML line comment
54+
* </pre>
55+
*/
3656
class LineComment extends @linecomment, Comment { }
3757

38-
/** An HTML comment start/end token interpreted as a line comment. */
58+
/**
59+
* An HTML comment start/end token interpreted as a line comment.
60+
*
61+
* Example:
62+
*
63+
* ```
64+
* &lt;!-- an HTML line comment
65+
* --> also an HTML line comment
66+
* ```
67+
*/
3968
class HtmlLineComment extends @htmlcomment, LineComment { }
4069

41-
/** An HTML comment start token interpreted as a line comment. */
70+
/**
71+
* An HTML comment start token interpreted as a line comment.
72+
*
73+
* Example:
74+
*
75+
* ```
76+
* &lt;!-- an HTML line comment
77+
* ```
78+
*/
4279
class HtmlCommentStart extends @htmlcommentstart, HtmlLineComment { }
4380

44-
/** An HTML comment end token interpreted as a line comment. */
81+
/**
82+
* An HTML comment end token interpreted as a line comment.
83+
*
84+
* Example:
85+
*
86+
* ```
87+
* --> also an HTML line comment
88+
* ```
89+
*/
4590
class HtmlCommentEnd extends @htmlcommentend, HtmlLineComment { }
4691

47-
/** A `//` comment. */
92+
/**
93+
* A `//` comment.
94+
*
95+
* Example:
96+
*
97+
* ```
98+
* // a line comment
99+
* ```
100+
*/
48101
class SlashSlashComment extends @slashslashcomment, LineComment { }
49102

50-
/** A block comment (which may be a JSDoc comment). */
103+
/**
104+
* A block comment (which may be a JSDoc comment).
105+
*
106+
* Examples:
107+
*
108+
* <pre>
109+
* /* a block comment
110+
* (but not a JSDoc comment) *&#47;
111+
* /** a JSDoc comment *&#47;
112+
* </pre>
113+
*/
51114
class BlockComment extends @blockcomment, Comment { }
52115

53-
/** A C-style block comment which is not a JSDoc comment. */
116+
/**
117+
* A C-style block comment which is not a JSDoc comment.
118+
*
119+
* Example:
120+
*
121+
* <pre>
122+
* /* a block comment
123+
* (but not a JSDoc comment) *&#47;
124+
* </pre>
125+
*/
54126
class SlashStarComment extends @slashstarcomment, BlockComment { }
55127

56-
/** A JSDoc comment. */
128+
/**
129+
* A JSDoc comment.
130+
*
131+
* Example:
132+
*
133+
* <pre>
134+
* /** a JSDoc comment *&#47;
135+
* </pre>
136+
*/
57137
class DocComment extends @doccomment, BlockComment { }

javascript/ql/src/semmle/javascript/E4X.qll

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,25 @@ import javascript
77
module E4X {
88
/**
99
* An E4X wildcard pseudo-identifier.
10+
*
11+
* Example:
12+
*
13+
* ```
14+
* *
15+
* ```
1016
*/
1117
class XMLAnyName extends Expr, @e4x_xml_anyname {
1218
}
1319

1420
/**
15-
* An E4X qualified identifier of the form `q::n` or `q::[expr]`.
21+
* An E4X qualified identifier.
22+
*
23+
* Examples:
24+
*
25+
* ```
26+
* soap::encodingStyle
27+
* soap::["encodingStyle"]
28+
* ```
1629
*
1730
* Note that qualified identifiers are not currently supported by the parser, so snapshots
1831
* will not usually contain any.
@@ -43,7 +56,14 @@ module E4X {
4356
}
4457

4558
/**
46-
* An E4X attribute selector of the form `@name` or `@[expr]`.
59+
* An E4X attribute selector.
60+
*
61+
* Examples:
62+
*
63+
* ```
64+
* @border
65+
* @[p]
66+
* ```
4767
*/
4868
class XMLAttributeSelector extends Expr, @e4x_xml_attribute_selector {
4969
/**
@@ -65,7 +85,13 @@ module E4X {
6585
}
6686

6787
/**
68-
* An E4X filter expression of the form `left.(right)`.
88+
* An E4X filter expression.
89+
*
90+
* Example:
91+
*
92+
* ```
93+
* employees.(@id == 0 || @id == 1)
94+
* ```
6995
*/
7096
class XMLFilterExpression extends Expr, @e4x_xml_filter_expression {
7197
/**
@@ -84,7 +110,13 @@ module E4X {
84110
}
85111

86112
/**
87-
* An E4X "dot-dot" expression of the form `e..id`.
113+
* An E4X "dot-dot" expression.
114+
*
115+
* Example:
116+
*
117+
* ```
118+
* e..name
119+
* ```
88120
*/
89121
class XMLDotDotExpression extends Expr, @e4x_xml_dotdotexpr {
90122
/**

javascript/ql/src/semmle/javascript/Functions.qll

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,40 @@
22

33
import javascript
44

5-
/** A function as defined either by a function declaration or a function expression. */
5+
/**
6+
* A function as defined either by a function declaration or a function expression.
7+
*
8+
* Examples:
9+
*
10+
* ```
11+
* function greet() { // function declaration
12+
* console.log("Hi");
13+
* }
14+
*
15+
* var greet =
16+
* function() { // function expression
17+
* console.log("Hi");
18+
* };
19+
*
20+
* var greet2 =
21+
* () => console.log("Hi") // arrow function expression
22+
*
23+
* var o = {
24+
* m() { // function expression in a method definition in an object literal
25+
* return 0;
26+
* },
27+
* get x() { // function expression in a getter method definition in an object literal
28+
* return 1
29+
* }
30+
* };
31+
*
32+
* class C {
33+
* m() { // function expression in a method definition in a class
34+
* return 0;
35+
* }
36+
* }
37+
* ```
38+
*/
639
class Function extends @function, Parameterized, TypeParameterized, StmtContainer, Documentable,
740
AST::ValueNode {
841
/** Gets the `i`th parameter of this function. */
@@ -472,6 +505,22 @@ private module LinesOfComments {
472505

473506
/**
474507
* A method defined in a class or object expression.
508+
*
509+
* Examples:
510+
*
511+
* ```
512+
* var o = {
513+
* m() { // method defined in an object expression
514+
* return 0;
515+
* }
516+
* };
517+
*
518+
* class C {
519+
* m() { // method defined in a class
520+
* return 0;
521+
* }
522+
* }
523+
* ```
475524
*/
476525
class Method extends FunctionExpr {
477526
Method() {
@@ -483,6 +532,16 @@ class Method extends FunctionExpr {
483532

484533
/**
485534
* A constructor defined in a class.
535+
*
536+
* Example:
537+
*
538+
* ```
539+
* class Point {
540+
* constructor(x, y) { // constructor
541+
* this.x = x;
542+
* this.y = y;
543+
* }
544+
* }
486545
*/
487546
class Constructor extends FunctionExpr {
488547
Constructor() { exists(ConstructorDeclaration cd | this = cd.getBody()) }

javascript/ql/src/semmle/javascript/HTML.qll

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ module HTML {
1111
}
1212

1313
/**
14-
* An HTML element like `<a href="semmle.com">Semmle</a>`.
14+
* An HTML element.
15+
*
16+
* Example:
17+
*
18+
* ```
19+
* <a href="semmle.com">Semmle</a>
20+
* ```
1521
*/
1622
class Element extends Locatable, @xmlelement {
1723
Element() { exists(HtmlFile f | xmlElements(this, _, _, _, f)) }
@@ -79,8 +85,14 @@ module HTML {
7985
/**
8086
* An attribute of an HTML element.
8187
*
82-
* For example, the element `<a href ="semmle.com" target=_blank>Semmle</a>`
83-
* has two attributes: `href ="semmle.com"` and `target=_blank`.
88+
* Examples:
89+
*
90+
* ```
91+
* <a
92+
* href ="semmle.com" <!-- an attribute -->
93+
* target=_blank <!-- also an attribute -->
94+
* >Semmle</a>
95+
* ```
8496
*/
8597
class Attribute extends Locatable, @xmlattribute {
8698
Attribute() { exists(HtmlFile f | xmlAttrs(this, _, _, _, _, f)) }
@@ -116,13 +128,29 @@ module HTML {
116128

117129
/**
118130
* An HTML `<html>` element.
131+
*
132+
* Example:
133+
*
134+
* ```
135+
* <html>
136+
* <body>
137+
* This is a test.
138+
* </body>
139+
* </html>
140+
* ```
119141
*/
120142
class DocumentElement extends Element {
121143
DocumentElement() { getName() = "html" }
122144
}
123145

124146
/**
125147
* An HTML `<script>` element.
148+
*
149+
* Example:
150+
*
151+
* ```
152+
* <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
153+
* ```
126154
*/
127155
class ScriptElement extends Element {
128156
ScriptElement() { getName() = "script" }
@@ -222,7 +250,15 @@ module HTML {
222250
}
223251

224252
/**
225-
* An HTML text node like `<div>this-is-the-node</div>`.
253+
* An HTML text node.
254+
*
255+
* Example:
256+
*
257+
* ```
258+
* <div>
259+
* This text is represented as a text node.
260+
* </div>
261+
* ```
226262
*
227263
* Note that instances of this class are only available if extraction is done with `--html all` or `--experimental`.
228264
*/
@@ -257,7 +293,13 @@ module HTML {
257293
}
258294

259295
/**
260-
* An HTML comment like <code>&lt;!&hyphen;&hyphen; this &hyphen;&hyphen;&gt;</code>.
296+
* An HTML comment.
297+
*
298+
* Example:
299+
*
300+
* ```
301+
* <!-- this is a comment -->
302+
* ```
261303
*/
262304
class CommentNode extends Locatable, @xmlcomment {
263305
CommentNode() { exists(HtmlFile f | xmlComments(this, _, _, f)) }

0 commit comments

Comments
 (0)