Skip to content

Commit 96a0766

Browse files
author
Max Schaefer
committed
JavaScript: Add syntax examples in AMD.qll, AST.qll and Stmt.qll.
1 parent e9ba66f commit 96a0766

File tree

3 files changed

+598
-50
lines changed

3 files changed

+598
-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. */
@@ -181,51 +198,118 @@ class TopLevel extends @toplevel, StmtContainer {
181198

182199
/**
183200
* A stand-alone file or script originating from an HTML `<script>` element.
201+
*
202+
* Example:
203+
*
204+
* ```
205+
* <script>
206+
* window.done = true;
207+
* </script>
208+
* ```
184209
*/
185210
class Script extends TopLevel {
186211
Script() { this instanceof @script or this instanceof @inline_script }
187212
}
188213

189214
/**
190215
* A stand-alone file or an external script originating from an HTML `<script>` element.
216+
*
217+
* Example:
218+
*
219+
* ```
220+
* #! /usr/bin/node
221+
* console.log("Hello, world!");
222+
* ```
191223
*/
192224
class ExternalScript extends @script, Script { }
193225

194226
/**
195227
* A script embedded inline in an HTML `<script>` element.
228+
*
229+
* Example:
230+
*
231+
* ```
232+
* <script>
233+
* window.done = true;
234+
* </script>
235+
* ```
196236
*/
197237
class InlineScript extends @inline_script, Script { }
198238

199239
/**
200240
* A code snippet originating from an HTML attribute value.
241+
*
242+
* Examples:
243+
*
244+
* ```
245+
* <div onclick="alert('hi')">Click me</div>
246+
* <a href="javascript:alert('hi')">Click me</a>
247+
* ```
201248
*/
202249
class CodeInAttribute extends TopLevel {
203250
CodeInAttribute() { this instanceof @event_handler or this instanceof @javascript_url }
204251
}
205252

206253
/**
207254
* A code snippet originating from an event handler attribute.
255+
*
256+
* Example:
257+
*
258+
* ```
259+
* <div onclick="alert('hi')">Click me</div>
260+
* ```
208261
*/
209262
class EventHandlerCode extends @event_handler, CodeInAttribute { }
210263

211264
/**
212265
* A code snippet originating from a URL with the `javascript:` URL scheme.
266+
*
267+
* Example:
268+
*
269+
* ```
270+
* <a href="javascript:alert('hi')">Click me</a>
271+
* ```
213272
*/
214273
class JavaScriptURL extends @javascript_url, CodeInAttribute { }
215274

216275
/**
217276
* A toplevel syntactic entity containing Closure-style externs definitions.
277+
*
278+
* Example:
279+
*
280+
* <pre>
281+
* /** @externs *&#47;
282+
* /** @typedef {String} *&#47;
283+
* var MyString;
284+
* </pre>
218285
*/
219286
class Externs extends TopLevel {
220287
Externs() { isExterns() }
221288
}
222289

223-
/** A program element that is either an expression or a statement. */
290+
/**
291+
* A program element that is either an expression or a statement.
292+
*
293+
* Examples:
294+
*
295+
* ```
296+
* var i = 0;
297+
* i = 9
298+
* ```
299+
*/
224300
class ExprOrStmt extends @exprorstmt, ControlFlowNode, ASTNode { }
225301

226302
/**
227303
* A program element that contains statements, but isn't itself
228304
* a statement, in other words a toplevel or a function.
305+
*
306+
* Example:
307+
*
308+
* ```
309+
* function f() {
310+
* g();
311+
* }
312+
* ```
229313
*/
230314
class StmtContainer extends @stmt_container, ASTNode {
231315
/** Gets the innermost enclosing container in which this container is nested. */
@@ -312,6 +396,14 @@ module AST {
312396
* A program element that evaluates to a value at runtime. This includes expressions,
313397
* but also function and class declaration statements, as well as TypeScript
314398
* namespace and enum declarations.
399+
*
400+
* Examples:
401+
*
402+
* ```
403+
* 0 // expression
404+
* (function id(x) { return x; }) // parenthesized function expression
405+
* function id(x) { return x; } // function declaration
406+
* ```
315407
*/
316408
class ValueNode extends ASTNode, @dataflownode {
317409
/** Gets type inference results for this element. */

0 commit comments

Comments
 (0)