Skip to content

Commit d644a30

Browse files
authored
Merge pull request #4434 from erik-krogh/printAST
Approved by asgerf
2 parents 2e52cbe + ca0870d commit d644a30

File tree

28 files changed

+6738
-29
lines changed

28 files changed

+6738
-29
lines changed

javascript/ql/src/printAst.ql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @name Print AST
3+
* @description Outputs a representation of a file's Abstract Syntax Tree. This
4+
* query is used by the VS Code extension.
5+
* @id js/print-ast
6+
* @kind graph
7+
* @tags ide-contextual-queries/print-ast
8+
*/
9+
10+
import javascript
11+
import semmle.javascript.PrintAst
12+
import definitions
13+
14+
/**
15+
* The source file to generate an AST from.
16+
*/
17+
external string selectedSourceFile();
18+
19+
class PrintAstConfigurationOverride extends PrintAstConfiguration {
20+
/**
21+
* Holds if the location matches the selected file in the VS Code extension and
22+
* the element is not a synthetic constructor.
23+
*/
24+
override predicate shouldPrint(Locatable e, Location l) {
25+
super.shouldPrint(e, l) and
26+
l.getFile() = getEncodedFile(selectedSourceFile())
27+
}
28+
}

javascript/ql/src/semmle/javascript/Classes.qll

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ class ClassDefinition extends @class_definition, ClassOrInterface, AST::ValueNod
256256
ClassDefinition getSuperClassDefinition() {
257257
result = getSuperClass().analyze().getAValue().(AbstractClass).getClass()
258258
}
259+
260+
override string getAPrimaryQlClass() { result = "ClassDefinition" }
259261
}
260262

261263
/**
@@ -342,6 +344,8 @@ private class ClassInitializedMember extends MemberDeclaration {
342344
ClassInitializedMember() { this instanceof MethodDefinition or this.isStatic() }
343345

344346
int getIndex() { properties(this, _, result, _, _) }
347+
348+
override string getAPrimaryQlClass() { result = "ClassInitializedMember" }
345349
}
346350

347351
/**
@@ -361,6 +365,8 @@ class SuperExpr extends @super_expr, Expr {
361365
* which is the nearest enclosing non-arrow function.
362366
*/
363367
Function getBinder() { result = getEnclosingFunction().getThisBinder() }
368+
369+
override string getAPrimaryQlClass() { result = "SuperExpr" }
364370
}
365371

366372
/**
@@ -412,6 +418,8 @@ class SuperPropAccess extends PropAccess {
412418
*/
413419
class NewTargetExpr extends @newtarget_expr, Expr {
414420
override predicate isImpure() { none() }
421+
422+
override string getAPrimaryQlClass() { result = "NewTargetExpr" }
415423
}
416424

417425
/**
@@ -574,6 +582,8 @@ class MemberDeclaration extends @property, Documentable {
574582
* True if this is abstract, ambient, or an overload signature.
575583
*/
576584
predicate isSignature() { not isConcrete() }
585+
586+
override string getAPrimaryQlClass() { result = "MemberDeclaration" }
577587
}
578588

579589
/**
@@ -748,7 +758,9 @@ private predicate hasOverloadedConstructorCallSignature(ClassOrInterface type) {
748758
* }
749759
* ```
750760
*/
751-
class MethodDefinition extends MethodDeclaration, MemberDefinition { }
761+
class MethodDefinition extends MethodDeclaration, MemberDefinition {
762+
override string getAPrimaryQlClass() { result = "MethodDefinition" }
763+
}
752764

753765
/**
754766
* A method signature declared in a class or interface, that is, a method without a function body.
@@ -763,7 +775,9 @@ class MethodDefinition extends MethodDeclaration, MemberDefinition { }
763775
*
764776
* Note that TypeScript call signatures are not considered method signatures.
765777
*/
766-
class MethodSignature extends MethodDeclaration, MemberSignature { }
778+
class MethodSignature extends MethodDeclaration, MemberSignature {
779+
override string getAPrimaryQlClass() { result = "MethodSignature" }
780+
}
767781

768782
/**
769783
* A constructor declaration in a class, either a concrete definition or a signature without a body.
@@ -792,6 +806,8 @@ class ConstructorDeclaration extends MethodDeclaration {
792806

793807
/** Holds if this is a synthetic default constructor. */
794808
predicate isSynthetic() { getLocation().isEmpty() }
809+
810+
override string getAPrimaryQlClass() { result = "ConstructorDeclaration" }
795811
}
796812

797813
/**
@@ -813,7 +829,9 @@ class ConstructorDeclaration extends MethodDeclaration {
813829
* }
814830
* ```
815831
*/
816-
class ConstructorDefinition extends ConstructorDeclaration, MethodDefinition { }
832+
class ConstructorDefinition extends ConstructorDeclaration, MethodDefinition {
833+
override string getAPrimaryQlClass() { result = "ConstructorDefinition" }
834+
}
817835

818836
/**
819837
* A constructor signature declared in a class, that is, a constructor without a function body.
@@ -824,7 +842,9 @@ class ConstructorDefinition extends ConstructorDeclaration, MethodDefinition { }
824842
* }
825843
* ```
826844
*/
827-
class ConstructorSignature extends ConstructorDeclaration, MethodSignature { }
845+
class ConstructorSignature extends ConstructorDeclaration, MethodSignature {
846+
override string getAPrimaryQlClass() { result = "ConstructorSignature" }
847+
}
828848

829849
/**
830850
* A function generated by the extractor to implement a synthetic default constructor.
@@ -925,7 +945,9 @@ abstract class AccessorMethodSignature extends MethodSignature, AccessorMethodDe
925945
* }
926946
* ```
927947
*/
928-
class GetterMethodDeclaration extends AccessorMethodDeclaration, @property_getter { }
948+
class GetterMethodDeclaration extends AccessorMethodDeclaration, @property_getter {
949+
override string getAPrimaryQlClass() { result = "GetterMethodDeclaration" }
950+
}
929951

930952
/**
931953
* A concrete getter method definition in a class, that is, a getter method with a function body.
@@ -945,7 +967,9 @@ class GetterMethodDeclaration extends AccessorMethodDeclaration, @property_gette
945967
* }
946968
* ```
947969
*/
948-
class GetterMethodDefinition extends GetterMethodDeclaration, AccessorMethodDefinition { }
970+
class GetterMethodDefinition extends GetterMethodDeclaration, AccessorMethodDefinition {
971+
override string getAPrimaryQlClass() { result = "GetterMethodDefinition" }
972+
}
949973

950974
/**
951975
* A getter method signature declared in a class or interface, that is, a getter method without a function body.
@@ -958,7 +982,9 @@ class GetterMethodDefinition extends GetterMethodDeclaration, AccessorMethodDefi
958982
* }
959983
* ```
960984
*/
961-
class GetterMethodSignature extends GetterMethodDeclaration, AccessorMethodSignature { }
985+
class GetterMethodSignature extends GetterMethodDeclaration, AccessorMethodSignature {
986+
override string getAPrimaryQlClass() { result = "GetterMethodSignature" }
987+
}
962988

963989
/**
964990
* A setter method declaration in a class or interface, either a concrete definition or a signature without a body.
@@ -981,7 +1007,9 @@ class GetterMethodSignature extends GetterMethodDeclaration, AccessorMethodSigna
9811007
* }
9821008
* ```
9831009
*/
984-
class SetterMethodDeclaration extends AccessorMethodDeclaration, @property_setter { }
1010+
class SetterMethodDeclaration extends AccessorMethodDeclaration, @property_setter {
1011+
override string getAPrimaryQlClass() { result = "SetterMethodDeclaration" }
1012+
}
9851013

9861014
/**
9871015
* A concrete setter method definition in a class, that is, a setter method with a function body
@@ -1000,7 +1028,9 @@ class SetterMethodDeclaration extends AccessorMethodDeclaration, @property_sette
10001028
* }
10011029
* ```
10021030
*/
1003-
class SetterMethodDefinition extends SetterMethodDeclaration, AccessorMethodDefinition { }
1031+
class SetterMethodDefinition extends SetterMethodDeclaration, AccessorMethodDefinition {
1032+
override string getAPrimaryQlClass() { result = "SetterMethodDefinition" }
1033+
}
10041034

10051035
/**
10061036
* A setter method signature declared in a class or interface, that is, a setter method without a function body.
@@ -1013,7 +1043,9 @@ class SetterMethodDefinition extends SetterMethodDeclaration, AccessorMethodDefi
10131043
* }
10141044
* ```
10151045
*/
1016-
class SetterMethodSignature extends SetterMethodDeclaration, AccessorMethodSignature { }
1046+
class SetterMethodSignature extends SetterMethodDeclaration, AccessorMethodSignature {
1047+
override string getAPrimaryQlClass() { result = "SetterMethodSignature" }
1048+
}
10171049

10181050
/**
10191051
* A field declaration in a class or interface, either a concrete definition or an abstract or ambient field signature.
@@ -1047,6 +1079,8 @@ class FieldDeclaration extends MemberDeclaration, @field {
10471079

10481080
/** Holds if this is a TypeScript field marked as definitely assigned with the `!` operator. */
10491081
predicate hasDefiniteAssignmentAssertion() { has_definite_assignment_assertion(this) }
1082+
1083+
override string getAPrimaryQlClass() { result = "FieldDeclaration" }
10501084
}
10511085

10521086
/**
@@ -1203,4 +1237,6 @@ class IndexSignature extends @index_signature, MemberSignature {
12031237
override InterfaceDefinition getDeclaringType() {
12041238
result = MemberSignature.super.getDeclaringType()
12051239
}
1240+
1241+
override string getAPrimaryQlClass() { result = "IndexSignature" }
12061242
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class ImportDeclaration extends Stmt, Import, @import_declaration {
7979

8080
/** Holds if this is declared with the `type` keyword, so it only imports types. */
8181
predicate isTypeOnly() { has_type_keyword(this) }
82+
83+
override string getAPrimaryQlClass() { result = "ImportDeclaration" }
8284
}
8385

8486
/** A literal path expression appearing in an `import` declaration. */
@@ -129,6 +131,8 @@ class ImportSpecifier extends Expr, @import_specifier {
129131

130132
/** Gets the local variable into which this specifier imports. */
131133
VarDecl getLocal() { result = getChildExpr(1) }
134+
135+
override string getAPrimaryQlClass() { result = "ImportSpecifier" }
132136
}
133137

134138
/**
@@ -262,6 +266,8 @@ abstract class ExportDeclaration extends Stmt, @export_declaration {
262266

263267
/** Holds if is declared with the `type` keyword, so only types are exported. */
264268
predicate isTypeOnly() { has_type_keyword(this) }
269+
270+
override string getAPrimaryQlClass() { result = "ExportDeclaration" }
265271
}
266272

267273
/**
@@ -511,6 +517,8 @@ class ExportSpecifier extends Expr, @exportspecifier {
511517
* an exported name since it does not export a unique symbol.
512518
*/
513519
string getExportedName() { result = getExported().getName() }
520+
521+
override string getAPrimaryQlClass() { result = "ExportSpecifier" }
514522
}
515523

516524
/**

0 commit comments

Comments
 (0)