Skip to content

Commit 74397da

Browse files
author
Max Schaefer
committed
JavaScript: Add concrete syntax examples to ES2015Modules, Externs, JSON, YAML.
1 parent 12c906c commit 74397da

File tree

4 files changed

+669
-52
lines changed

4 files changed

+669
-52
lines changed

javascript/ql/src/semmle/javascript/ES2015Modules.qll

Lines changed: 220 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import javascript
44

55
/**
66
* An ECMAScript 2015 module.
7+
*
8+
* Example:
9+
*
10+
* ```
11+
* import console from 'console';
12+
*
13+
* console.log("Hello, world!");
14+
* ```
715
*/
816
class ES2015Module extends Module {
917
ES2015Module() { isES2015Module(this) }
@@ -32,7 +40,16 @@ class ES2015Module extends Module {
3240
}
3341
}
3442

35-
/** An import declaration. */
43+
/**
44+
* An import declaration.
45+
*
46+
* Examples:
47+
*
48+
* ```
49+
* import console, { log, error as fatal } from 'console';
50+
* import * as console from 'console';
51+
* ```
52+
*/
3653
class ImportDeclaration extends Stmt, Import, @importdeclaration {
3754
override ES2015Module getEnclosingModule() { result = getTopLevel() }
3855

@@ -71,19 +88,20 @@ private class LiteralImportPath extends PathExprInModule, ConstantString {
7188
/**
7289
* An import specifier in an import declaration.
7390
*
74-
* There are four kinds of import specifiers:
75-
*
76-
* - default import specifiers, which import the default export of a module
77-
* and make it available under a local name, as in `import` <u>`f`</u> `from 'a'`;
78-
* - namespace import specifiers, which import all exports of a module and
79-
* make them available through a local namespace object, as in
80-
* `import` <u>`* as ns`</u> `from 'a'`;
81-
* - named import specifiers, which import a named export of a module and
82-
* make it available in the importing module under the same name, as in
83-
* `import {` <u>`x`</u> `} from 'a'`;
84-
* - renaming import specifiers, which import a named export of a module and
85-
* make it available in the importing module under a different name, as in
86-
* `import {` <u>`x as y`</u> `} from 'a'`.
91+
* Examples:
92+
*
93+
* ```
94+
* import
95+
* console, // default import specifier
96+
* {
97+
* log, // named import specifier
98+
* error as fatal // renaming import specifier
99+
* } from 'console';
100+
*
101+
* import
102+
* * as console // namespace import specifier
103+
* from 'console';
104+
* ```
87105
*/
88106
class ImportSpecifier extends Expr, @importspecifier {
89107
/** Gets the imported symbol; undefined for default and namespace import specifiers. */
@@ -110,26 +128,74 @@ class ImportSpecifier extends Expr, @importspecifier {
110128
VarDecl getLocal() { result = getChildExpr(1) }
111129
}
112130

113-
/** A named import specifier. */
131+
/**
132+
* A named import specifier.
133+
*
134+
* Examples:
135+
*
136+
* ```
137+
* import
138+
* {
139+
* log, // named import specifier
140+
* error as fatal // renaming import specifier
141+
* } from 'console';
142+
* ```
143+
*/
114144
class NamedImportSpecifier extends ImportSpecifier, @namedimportspecifier { }
115145

116-
/** A default import specifier. */
146+
/**
147+
* A default import specifier.
148+
*
149+
* Example:
150+
*
151+
* ```
152+
* import
153+
* console // default import specifier
154+
* from 'console';
155+
* ```
156+
*/
117157
class ImportDefaultSpecifier extends ImportSpecifier, @importdefaultspecifier {
118158
override string getImportedName() { result = "default" }
119159
}
120160

121-
/** A namespace import specifier. */
161+
/**
162+
* A namespace import specifier.
163+
*
164+
* Example:
165+
*
166+
* ```
167+
* import
168+
* * as console // namespace import specifier
169+
* from 'console';
170+
* ```
171+
*/
122172
class ImportNamespaceSpecifier extends ImportSpecifier, @importnamespacespecifier { }
123173

124-
/** A bulk import that imports an entire module as a namespace. */
174+
/**
175+
* A bulk import that imports an entire module as a namespace.
176+
*
177+
* Example:
178+
*
179+
* ```
180+
* import * as console from 'console';
181+
* ```
182+
*/
125183
class BulkImportDeclaration extends ImportDeclaration {
126184
BulkImportDeclaration() { getASpecifier() instanceof ImportNamespaceSpecifier }
127185

128186
/** Gets the local namespace variable under which the module is imported. */
129187
VarDecl getLocal() { result = getASpecifier().getLocal() }
130188
}
131189

132-
/** A selective import that imports zero or more declarations. */
190+
/**
191+
* A selective import that imports zero or more declarations.
192+
*
193+
* Example:
194+
*
195+
* ```
196+
* import console, { log } from 'console';
197+
* ```
198+
*/
133199
class SelectiveImportDeclaration extends ImportDeclaration {
134200
SelectiveImportDeclaration() { not this instanceof BulkImportDeclaration }
135201

@@ -147,16 +213,20 @@ class SelectiveImportDeclaration extends ImportDeclaration {
147213
/**
148214
* An export declaration.
149215
*
150-
* There are three kinds of export declarations:
216+
* Examples:
151217
*
152-
* - a bulk re-export declaration of the form `export * from 'a'`, which re-exports
153-
* all exports of another module;
154-
* - a default export declaration of the form `export default var x = 42`, which exports
155-
* a local value or declaration as the default export;
156-
* - a named export declaration such as `export { x, y as z }`, which exports local
157-
* values or declarations under specific names; a named export declaration
158-
* may also export symbols itself imported from another module, as in
159-
* `export { x } from 'a'`.
218+
* ```
219+
* export * from 'a'; // bulk re-export declaration
220+
*
221+
* export default var x = 42; // default export declaration
222+
* export default function f() {}; // default export declaration
223+
* export default 42; // default export declaration
224+
*
225+
* export { x, y as z }; // named export declaration
226+
* export var x = 42; // named export declaration
227+
* export { x } from 'a'; // named re-export declaration
228+
* export x from 'a'; // default re-export declaration
229+
* ```
160230
*/
161231
abstract class ExportDeclaration extends Stmt, @exportdeclaration {
162232
/** Gets the module to which this export declaration belongs. */
@@ -192,6 +262,12 @@ abstract class ExportDeclaration extends Stmt, @exportdeclaration {
192262
/**
193263
* A bulk re-export declaration of the form `export * from 'a'`, which re-exports
194264
* all exports of another module.
265+
*
266+
* Examples:
267+
*
268+
* ```
269+
* export * from 'a'; // bulk re-export declaration
270+
* ```
195271
*/
196272
class BulkReExportDeclaration extends ReExportDeclaration, @exportalldeclaration {
197273
/** Gets the name of the module from which this declaration re-exports. */
@@ -229,8 +305,15 @@ private predicate isShadowedFromBulkExport(BulkReExportDeclaration reExport, str
229305
}
230306

231307
/**
232-
* A default export declaration such as `export default function f{}`
233-
* or `export default { x: 42 }`.
308+
* A default export declaration.
309+
*
310+
* Examples:
311+
*
312+
* ```
313+
* export default var x = 42;
314+
* export default function f() {};
315+
* export default 42;
316+
* ```
234317
*/
235318
class ExportDefaultDeclaration extends ExportDeclaration, @exportdefaultdeclaration {
236319
/** Gets the operand statement or expression that is exported by this declaration. */
@@ -253,7 +336,16 @@ class ExportDefaultDeclaration extends ExportDeclaration, @exportdefaultdeclarat
253336
}
254337
}
255338

256-
/** A named export declaration such as `export { x, y }` or `export var x = 42`. */
339+
/** A named export declaration.
340+
* *
341+
* Examples:
342+
*
343+
* ```
344+
* export { x, y as z };
345+
* export var x = 42;
346+
* export { x } from 'a';
347+
* ```
348+
* */
257349
class ExportNamedDeclaration extends ExportDeclaration, @exportnameddeclaration {
258350
/** Gets the operand statement or expression that is exported by this declaration. */
259351
ExprOrStmt getOperand() { result = getChild(-1) }
@@ -322,7 +414,30 @@ class ExportNamedDeclaration extends ExportDeclaration, @exportnameddeclaration
322414
}
323415
}
324416

325-
/** An export specifier in a named export declaration. */
417+
/**
418+
* An export specifier in an export declaration.
419+
*
420+
* Examples:
421+
*
422+
* ```
423+
* export
424+
* * // namespace export specifier
425+
* from 'a';
426+
*
427+
* export
428+
* default // default export specifier
429+
* var x = 42;
430+
*
431+
* export {
432+
* x, // named export specifier
433+
* y as z // named export specifier
434+
* };
435+
*
436+
* export
437+
* x // default re-export specifier
438+
* from 'a';
439+
* ```
440+
*/
326441
class ExportSpecifier extends Expr, @exportspecifier {
327442
/** Gets the declaration to which this specifier belongs. */
328443
ExportDeclaration getExportDeclaration() { result = getParent() }
@@ -380,21 +495,47 @@ class ExportSpecifier extends Expr, @exportspecifier {
380495
}
381496

382497
/**
383-
* A named export specifier, for example `v` in `export { v }`.
498+
* A named export specifier.
499+
*
500+
* Examples:
501+
*
502+
* ```
503+
* export {
504+
* x, // named export specifier
505+
* y as z // named export specifier
506+
* };
507+
* ```
384508
*/
385509
class NamedExportSpecifier extends ExportSpecifier, @namedexportspecifier { }
386510

387511
/**
388-
* A default export specifier, for example `default` in `export default 42`,
389-
* or `v` in `export v from "mod"`.
512+
* A default export specifier.
513+
*
514+
* Examples:
515+
*
516+
* ```
517+
* export
518+
* default // default export specifier
519+
* 42;
520+
* export
521+
* x // default re-export specifier
522+
* from 'a';
523+
* ```
390524
*/
391525
class ExportDefaultSpecifier extends ExportSpecifier, @exportdefaultspecifier {
392526
override string getExportedName() { result = "default" }
393527
}
394528

395529
/**
396-
* A default export specifier in a re-export declaration, for example `v` in
397-
* `export v from "mod"`.
530+
* A default export specifier in a re-export declaration.
531+
*
532+
* Example:
533+
*
534+
* ```
535+
* export
536+
* x // default re-export specifier
537+
* from 'a';
538+
* ```
398539
*/
399540
class ReExportDefaultSpecifier extends ExportDefaultSpecifier {
400541
ReExportDefaultSpecifier() { getExportDeclaration() instanceof ReExportDeclaration }
@@ -405,11 +546,29 @@ class ReExportDefaultSpecifier extends ExportDefaultSpecifier {
405546
}
406547

407548
/**
408-
* A namespace export specifier, for example `*` in `export * from "mod"`.
549+
* A namespace export specifier.
550+
*
551+
* Example:
552+
*
553+
* ```
554+
* export
555+
* * // namespace export specifier
556+
* from 'a';
557+
* ```
409558
*/
410559
class ExportNamespaceSpecifier extends ExportSpecifier, @exportnamespacespecifier { }
411560

412-
/** An export declaration that re-exports declarations from another module. */
561+
/**
562+
* An export declaration that re-exports declarations from another module.
563+
*
564+
* Examples:
565+
*
566+
* ```
567+
* export * from 'a'; // bulk re-export declaration
568+
* export { x } from 'a'; // named re-export declaration
569+
* export x from 'a'; // default re-export declaration
570+
* ```
571+
*/
413572
abstract class ReExportDeclaration extends ExportDeclaration {
414573
/** Gets the path of the module from which this declaration re-exports. */
415574
abstract ConstantString getImportedPath();
@@ -441,7 +600,15 @@ private class LiteralReExportPath extends PathExprInModule, ConstantString {
441600
override string getValue() { result = this.(ConstantString).getStringValue() }
442601
}
443602

444-
/** A named export declaration that re-exports symbols imported from another module. */
603+
/**
604+
* A named export declaration that re-exports symbols imported from another module.
605+
*
606+
* Example:
607+
*
608+
* ```
609+
* export { x } from 'a';
610+
* ```
611+
*/
445612
class SelectiveReExportDeclaration extends ReExportDeclaration, ExportNamedDeclaration {
446613
SelectiveReExportDeclaration() { exists(ExportNamedDeclaration.super.getImportedPath()) }
447614

@@ -451,7 +618,19 @@ class SelectiveReExportDeclaration extends ReExportDeclaration, ExportNamedDecla
451618
}
452619
}
453620

454-
/** An export declaration that exports zero or more declarations from the module it appears in. */
621+
/**
622+
* An export declaration that exports zero or more declarations from the module it appears in.
623+
*
624+
* Examples:
625+
*
626+
* ```
627+
* export default var x = 42;
628+
* export default function f() {};
629+
* export default 42;
630+
* export { x, y as z };
631+
* export var x = 42;
632+
* ```
633+
*/
455634
class OriginalExportDeclaration extends ExportDeclaration {
456635
OriginalExportDeclaration() { not this instanceof ReExportDeclaration }
457636

0 commit comments

Comments
 (0)