Skip to content

Commit 66464b5

Browse files
authored
Merge pull request #1574 from xiemaisi/js/more-examples
Approved by esben-semmle
2 parents d3a880e + 1dc685b commit 66464b5

File tree

3 files changed

+601
-50
lines changed

3 files changed

+601
-50
lines changed

javascript/ql/src/semmle/javascript/AMD.qll

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import javascript
1111
* Example:
1212
*
1313
* ```
14-
* define(['a', 'b'], function(a, b) {
14+
* define(['fs', 'express'], function(fs, express) {
1515
* ...
1616
* });
1717
* ```
1818
*
1919
* The first argument is an (optional) array of dependencies,
2020
* the second a factory method or object.
2121
*
22-
* We also recognize the three-argument form `define('m', ['a', 'b'], ...)`
22+
* We also recognize the three-argument form `define('m', ['fs', 'express'], ...)`
2323
* where the first argument is the module name, the second argument an
2424
* array of dependencies, and the third argument a factory method or object.
2525
*/
@@ -274,6 +274,14 @@ private class AmdDependencyImport extends Import {
274274

275275
/**
276276
* An AMD-style module.
277+
*
278+
* Example:
279+
*
280+
* ```
281+
* define(['fs', 'express'], function(fs, express) {
282+
* ...
283+
* });
284+
* ```
277285
*/
278286
class AmdModule extends Module {
279287
AmdModule() { strictcount(AmdModuleDefinition def | amdModuleTopLevel(def, this)) = 1 }

javascript/ql/src/semmle/javascript/AST.qll

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import javascript
1010
*
1111
* This class provides generic traversal methods applicable to all AST nodes,
1212
* such as obtaining the children of an AST node.
13+
*
14+
* Examples:
15+
*
16+
* ```
17+
* function abs(x) {
18+
* return x < 0 ? -x : x;
19+
* }
20+
* abs(-42);
21+
* ```
1322
*/
1423
class ASTNode extends @ast_node, Locatable {
1524
override Location getLocation() { hasLocation(this, result) }
@@ -128,6 +137,14 @@ class ASTNode extends @ast_node, Locatable {
128137
* A toplevel syntactic unit; that is, a stand-alone script, an inline script
129138
* embedded in an HTML `<script>` tag, a code snippet assigned to an HTML event
130139
* handler attribute, or a `javascript:` URL.
140+
*
141+
* Example:
142+
*
143+
* ```
144+
* <script>
145+
* window.done = true;
146+
* </script>
147+
* ```
131148
*/
132149
class TopLevel extends @toplevel, StmtContainer {
133150
/** Holds if this toplevel is minified. */
@@ -185,51 +202,118 @@ class TopLevel extends @toplevel, StmtContainer {
185202

186203
/**
187204
* A stand-alone file or script originating from an HTML `<script>` element.
205+
*
206+
* Example:
207+
*
208+
* ```
209+
* <script>
210+
* window.done = true;
211+
* </script>
212+
* ```
188213
*/
189214
class Script extends TopLevel {
190215
Script() { this instanceof @script or this instanceof @inline_script }
191216
}
192217

193218
/**
194219
* A stand-alone file or an external script originating from an HTML `<script>` element.
220+
*
221+
* Example:
222+
*
223+
* ```
224+
* #! /usr/bin/node
225+
* console.log("Hello, world!");
226+
* ```
195227
*/
196228
class ExternalScript extends @script, Script { }
197229

198230
/**
199231
* A script embedded inline in an HTML `<script>` element.
232+
*
233+
* Example:
234+
*
235+
* ```
236+
* <script>
237+
* window.done = true;
238+
* </script>
239+
* ```
200240
*/
201241
class InlineScript extends @inline_script, Script { }
202242

203243
/**
204244
* A code snippet originating from an HTML attribute value.
245+
*
246+
* Examples:
247+
*
248+
* ```
249+
* <div onclick="alert('hi')">Click me</div>
250+
* <a href="javascript:alert('hi')">Click me</a>
251+
* ```
205252
*/
206253
class CodeInAttribute extends TopLevel {
207254
CodeInAttribute() { this instanceof @event_handler or this instanceof @javascript_url }
208255
}
209256

210257
/**
211258
* A code snippet originating from an event handler attribute.
259+
*
260+
* Example:
261+
*
262+
* ```
263+
* <div onclick="alert('hi')">Click me</div>
264+
* ```
212265
*/
213266
class EventHandlerCode extends @event_handler, CodeInAttribute { }
214267

215268
/**
216269
* A code snippet originating from a URL with the `javascript:` URL scheme.
270+
*
271+
* Example:
272+
*
273+
* ```
274+
* <a href="javascript:alert('hi')">Click me</a>
275+
* ```
217276
*/
218277
class JavaScriptURL extends @javascript_url, CodeInAttribute { }
219278

220279
/**
221280
* A toplevel syntactic entity containing Closure-style externs definitions.
281+
*
282+
* Example:
283+
*
284+
* <pre>
285+
* /** @externs *&#47;
286+
* /** @typedef {String} *&#47;
287+
* var MyString;
288+
* </pre>
222289
*/
223290
class Externs extends TopLevel {
224291
Externs() { isExterns() }
225292
}
226293

227-
/** A program element that is either an expression or a statement. */
294+
/**
295+
* A program element that is either an expression or a statement.
296+
*
297+
* Examples:
298+
*
299+
* ```
300+
* var i = 0;
301+
* i = 9
302+
* ```
303+
*/
228304
class ExprOrStmt extends @exprorstmt, ControlFlowNode, ASTNode { }
229305

230306
/**
231307
* A program element that contains statements, but isn't itself
232308
* a statement, in other words a toplevel or a function.
309+
*
310+
* Example:
311+
*
312+
* ```
313+
* function f() {
314+
* g();
315+
* }
316+
* ```
233317
*/
234318
class StmtContainer extends @stmt_container, ASTNode {
235319
/** Gets the innermost enclosing container in which this container is nested. */
@@ -316,6 +400,14 @@ module AST {
316400
* A program element that evaluates to a value at runtime. This includes expressions,
317401
* but also function and class declaration statements, as well as TypeScript
318402
* namespace and enum declarations.
403+
*
404+
* Examples:
405+
*
406+
* ```
407+
* 0 // expression
408+
* (function id(x) { return x; }) // parenthesized function expression
409+
* function id(x) { return x; } // function declaration
410+
* ```
319411
*/
320412
class ValueNode extends ASTNode, @dataflownode {
321413
/** Gets type inference results for this element. */

0 commit comments

Comments
 (0)