Skip to content

Commit 79dbdac

Browse files
committed
TS: Support declare modifier for fields
1 parent 9bc45f3 commit 79dbdac

9 files changed

Lines changed: 44 additions & 5 deletions

File tree

javascript/extractor/src/com/semmle/js/ast/DeclarationFlags.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ public class DeclarationFlags {
1717
public static final int protected_ = 1 << 6;
1818
public static final int optional = 1 << 7;
1919
public static final int definiteAssignmentAssertion = 1 << 8;
20+
public static final int declareKeyword = 1 << 9;
2021

2122
public static final int none = 0;
22-
public static final int numberOfFlags = 9;
23+
public static final int numberOfFlags = 10;
2324

2425
public static final List<String> names =
2526
Arrays.asList(
@@ -31,7 +32,8 @@ public class DeclarationFlags {
3132
"private",
3233
"protected",
3334
"optional",
34-
"definiteAssignmentAssertion");
35+
"definiteAssignmentAssertion",
36+
"declare");
3537

3638
public static final List<String> relationNames =
3739
Arrays.asList(
@@ -43,7 +45,8 @@ public class DeclarationFlags {
4345
"hasPrivateKeyword",
4446
"hasProtectedKeyword",
4547
"isOptionalMember",
46-
"hasDefiniteAssignmentAssertion");
48+
"hasDefiniteAssignmentAssertion",
49+
"hasDeclareKeyword");
4750

4851
public static boolean isComputed(int flags) {
4952
return (flags & computed) != 0;
@@ -81,6 +84,10 @@ public static boolean hasDefiniteAssignmentAssertion(int flags) {
8184
return (flags & definiteAssignmentAssertion) != 0;
8285
}
8386

87+
public static boolean hasDeclareKeyword(int flags) {
88+
return (flags & declareKeyword) != 0;
89+
}
90+
8491
/** Returns a mask with the computed bit set to the value of <tt>enable</tt>. */
8592
public static int getComputed(boolean enable) {
8693
return enable ? computed : 0;
@@ -128,6 +135,11 @@ public static int getDefiniteAssignmentAssertion(boolean enable) {
128135
return enable ? definiteAssignmentAssertion : 0;
129136
}
130137

138+
/** Returns a mask with the declare keyword bit set to the value of <tt>enable</tt>. */
139+
public static int getDeclareKeyword(boolean enable) {
140+
return enable ? declareKeyword : 0;
141+
}
142+
131143
/** Returns true if the <tt>n</tt>th bit is set in <tt>flags</tt>. */
132144
public static boolean hasNthFlag(int flags, int n) {
133145
return (flags & (1 << n)) != 0;

javascript/extractor/src/com/semmle/js/ast/MemberDefinition.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public boolean hasReadonlyKeyword() {
7575
return DeclarationFlags.isReadonly(flags);
7676
}
7777

78+
/** Returns true if this has the <tt>declare</tt> modifier. */
79+
public boolean hasDeclareKeyword() {
80+
return DeclarationFlags.hasDeclareKeyword(flags);
81+
}
82+
7883
/**
7984
* Returns the expression denoting the name of the member, or {@code null} if this is a
8085
* call/construct signature.

javascript/extractor/src/com/semmle/js/extractor/ASTExtractor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,10 @@ public Label visit(MemberDefinition<?> nd, Context c) {
14101410
}
14111411
}
14121412

1413+
if (nd.hasDeclareKeyword()) {
1414+
trapwriter.addTuple("hasDeclareKeyword", methkey);
1415+
}
1416+
14131417
return methkey;
14141418
}
14151419

javascript/extractor/src/com/semmle/js/parser/TypeScriptASTConverter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,9 @@ private Node convertPropertyDeclaration(JsonObject node, String kind, SourceLoca
20092009
if (node.get("exclamationToken") != null) {
20102010
flags |= DeclarationFlags.definiteAssignmentAssertion;
20112011
}
2012+
if (hasModifier(node, "DeclareKeyword")) {
2013+
flags |= DeclarationFlags.declareKeyword;
2014+
}
20122015
FieldDefinition fieldDefinition =
20132016
new FieldDefinition(
20142017
loc,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,12 @@ class FieldDeclaration extends MemberDeclaration, @field {
10591059

10601060
/** Holds if this is a TypeScript field marked as definitely assigned with the `!` operator. */
10611061
predicate hasDefiniteAssignmentAssertion() { hasDefiniteAssignmentAssertion(this) }
1062+
1063+
override predicate isAmbient() {
1064+
hasDeclareKeyword(this)
1065+
or
1066+
getParent().isAmbient()
1067+
}
10621068
}
10631069

10641070
/**

javascript/ql/src/semmlecode.javascript.dbscheme

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ case @stmt.kind of
200200

201201
isInstantiated(unique int decl: @namespacedeclaration ref);
202202

203-
@declarablestmt = @declstmt | @namespacedeclaration | @classdeclstmt | @functiondeclstmt | @enumdeclaration | @externalmoduledeclaration | @globalaugmentationdeclaration;
204-
hasDeclareKeyword(unique int stmt: @declarablestmt ref);
203+
@declarablenode = @declstmt | @namespacedeclaration | @classdeclstmt | @functiondeclstmt | @enumdeclaration | @externalmoduledeclaration | @globalaugmentationdeclaration | @field;
204+
hasDeclareKeyword(unique int stmt: @declarablenode ref);
205205

206206
isForAwaitOf(unique int forof: @forofstmt ref);
207207

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| tst.ts:2:5:2:22 | declare x: number; | true |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import javascript
2+
3+
from FieldDeclaration f, boolean ambient
4+
where if f.isAmbient() then ambient = true else ambient = false
5+
select f, ambient
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class C {
2+
declare x: number;
3+
}

0 commit comments

Comments
 (0)