Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0351f0b

Browse files
committed
TS: Add test and documentation for private fields
1 parent 8531c11 commit 0351f0b

6 files changed

Lines changed: 307 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,11 @@ private Node convertNode(JsonObject node, String defaultKind) throws ParseError
336336
private Node convertNodeUntyped(JsonObject node, String defaultKind) throws ParseError {
337337
String kind = getKind(node);
338338
if (kind == null) kind = defaultKind;
339-
if (kind == null) kind = "Identifier";
339+
if (kind == null) {
340+
// Identifiers and PrivateIdentifiers do not have a "kind" property like other nodes.
341+
// Since we encode identifiers and private identifiers the same, default to Identifier.
342+
kind = "Identifier";
343+
}
340344
SourceLocation loc = getSourceLocation(node);
341345
switch (kind) {
342346
case "AnyKeyword":
@@ -443,6 +447,7 @@ private Node convertNodeUntyped(JsonObject node, String defaultKind) throws Pars
443447
case "FunctionType":
444448
return convertFunctionType(node, loc);
445449
case "Identifier":
450+
case "PrivateIdentifier":
446451
return convertIdentifier(node, loc);
447452
case "IfStatement":
448453
return convertIfStatement(node, loc);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class C {
2+
#foo;
3+
constructor() {
4+
this.#foo = 5;
5+
}
6+
}
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
#10000=@"/privateField.ts;sourcefile"
2+
files(#10000,"/privateField.ts","privateField","ts",0)
3+
#10001=@"/;folder"
4+
folders(#10001,"/","")
5+
containerparent(#10001,#10000)
6+
#10002=@"loc,{#10000},0,0,0,0"
7+
locations_default(#10002,#10000,0,0,0,0)
8+
hasLocation(#10000,#10002)
9+
#20000=@"global_scope"
10+
scopes(#20000,0)
11+
#20001=@"script;{#10000},1,1"
12+
#20002=*
13+
lines(#20002,#20001,"class C {","
14+
")
15+
#20003=@"loc,{#10000},1,1,1,9"
16+
locations_default(#20003,#10000,1,1,1,9)
17+
hasLocation(#20002,#20003)
18+
#20004=*
19+
lines(#20004,#20001," #foo;","
20+
")
21+
#20005=@"loc,{#10000},2,1,2,6"
22+
locations_default(#20005,#10000,2,1,2,6)
23+
hasLocation(#20004,#20005)
24+
indentation(#10000,2," ",1)
25+
#20006=*
26+
lines(#20006,#20001," constructor() {","
27+
")
28+
#20007=@"loc,{#10000},3,1,3,16"
29+
locations_default(#20007,#10000,3,1,3,16)
30+
hasLocation(#20006,#20007)
31+
indentation(#10000,3," ",1)
32+
#20008=*
33+
lines(#20008,#20001," this.#foo = 5;","
34+
")
35+
#20009=@"loc,{#10000},4,1,4,17"
36+
locations_default(#20009,#10000,4,1,4,17)
37+
hasLocation(#20008,#20009)
38+
indentation(#10000,4," ",3)
39+
#20010=*
40+
lines(#20010,#20001," }","
41+
")
42+
#20011=@"loc,{#10000},5,1,5,2"
43+
locations_default(#20011,#10000,5,1,5,2)
44+
hasLocation(#20010,#20011)
45+
indentation(#10000,5," ",1)
46+
#20012=*
47+
lines(#20012,#20001,"}","
48+
")
49+
#20013=@"loc,{#10000},6,1,6,1"
50+
locations_default(#20013,#10000,6,1,6,1)
51+
hasLocation(#20012,#20013)
52+
numlines(#20001,6,6,0)
53+
#20014=*
54+
tokeninfo(#20014,7,#20001,0,"class")
55+
#20015=@"loc,{#10000},1,1,1,5"
56+
locations_default(#20015,#10000,1,1,1,5)
57+
hasLocation(#20014,#20015)
58+
#20016=*
59+
tokeninfo(#20016,6,#20001,1,"C")
60+
#20017=@"loc,{#10000},1,7,1,7"
61+
locations_default(#20017,#10000,1,7,1,7)
62+
hasLocation(#20016,#20017)
63+
#20018=*
64+
tokeninfo(#20018,8,#20001,2,"{")
65+
#20019=@"loc,{#10000},1,9,1,9"
66+
locations_default(#20019,#10000,1,9,1,9)
67+
hasLocation(#20018,#20019)
68+
#20020=*
69+
tokeninfo(#20020,8,#20001,3,";")
70+
#20021=@"loc,{#10000},2,6,2,6"
71+
locations_default(#20021,#10000,2,6,2,6)
72+
hasLocation(#20020,#20021)
73+
#20022=*
74+
tokeninfo(#20022,7,#20001,4,"constructor")
75+
#20023=@"loc,{#10000},3,2,3,12"
76+
locations_default(#20023,#10000,3,2,3,12)
77+
hasLocation(#20022,#20023)
78+
#20024=*
79+
tokeninfo(#20024,8,#20001,5,"(")
80+
#20025=@"loc,{#10000},3,13,3,13"
81+
locations_default(#20025,#10000,3,13,3,13)
82+
hasLocation(#20024,#20025)
83+
#20026=*
84+
tokeninfo(#20026,8,#20001,6,")")
85+
#20027=@"loc,{#10000},3,14,3,14"
86+
locations_default(#20027,#10000,3,14,3,14)
87+
hasLocation(#20026,#20027)
88+
#20028=*
89+
tokeninfo(#20028,8,#20001,7,"{")
90+
#20029=@"loc,{#10000},3,16,3,16"
91+
locations_default(#20029,#10000,3,16,3,16)
92+
hasLocation(#20028,#20029)
93+
#20030=*
94+
tokeninfo(#20030,7,#20001,8,"this")
95+
#20031=@"loc,{#10000},4,4,4,7"
96+
locations_default(#20031,#10000,4,4,4,7)
97+
hasLocation(#20030,#20031)
98+
#20032=*
99+
tokeninfo(#20032,8,#20001,9,".")
100+
#20033=@"loc,{#10000},4,8,4,8"
101+
locations_default(#20033,#10000,4,8,4,8)
102+
hasLocation(#20032,#20033)
103+
#20034=*
104+
tokeninfo(#20034,8,#20001,10,"=")
105+
#20035=@"loc,{#10000},4,14,4,14"
106+
locations_default(#20035,#10000,4,14,4,14)
107+
hasLocation(#20034,#20035)
108+
#20036=*
109+
tokeninfo(#20036,3,#20001,11,"5")
110+
#20037=@"loc,{#10000},4,16,4,16"
111+
locations_default(#20037,#10000,4,16,4,16)
112+
hasLocation(#20036,#20037)
113+
#20038=*
114+
tokeninfo(#20038,8,#20001,12,";")
115+
#20039=@"loc,{#10000},4,17,4,17"
116+
locations_default(#20039,#10000,4,17,4,17)
117+
hasLocation(#20038,#20039)
118+
#20040=*
119+
tokeninfo(#20040,8,#20001,13,"}")
120+
#20041=@"loc,{#10000},5,2,5,2"
121+
locations_default(#20041,#10000,5,2,5,2)
122+
hasLocation(#20040,#20041)
123+
#20042=*
124+
tokeninfo(#20042,8,#20001,14,"}")
125+
hasLocation(#20042,#20013)
126+
#20043=*
127+
tokeninfo(#20043,0,#20001,15,"")
128+
#20044=@"loc,{#10000},7,1,7,0"
129+
locations_default(#20044,#10000,7,1,7,0)
130+
hasLocation(#20043,#20044)
131+
toplevels(#20001,0)
132+
#20045=@"loc,{#10000},1,1,7,0"
133+
locations_default(#20045,#10000,1,1,7,0)
134+
hasLocation(#20001,#20045)
135+
#20046=@"var;{C};{#20000}"
136+
variables(#20046,"C",#20000)
137+
#20047=@"local_type_name;{C};{#20000}"
138+
local_type_names(#20047,"C",#20000)
139+
#20048=*
140+
stmts(#20048,26,#20001,0,"class C ... 5;\n }\n}")
141+
#20049=@"loc,{#10000},1,1,6,1"
142+
locations_default(#20049,#10000,1,1,6,1)
143+
hasLocation(#20048,#20049)
144+
stmtContainers(#20048,#20001)
145+
#20050=*
146+
exprs(#20050,78,#20048,0,"C")
147+
hasLocation(#20050,#20017)
148+
enclosingStmt(#20050,#20048)
149+
exprContainers(#20050,#20001)
150+
literals("C","C",#20050)
151+
decl(#20050,#20046)
152+
typedecl(#20050,#20047)
153+
#20051=*
154+
scopes(#20051,10)
155+
scopenodes(#20048,#20051)
156+
scopenesting(#20051,#20000)
157+
#20052=*
158+
properties(#20052,#20048,2,8,"#foo;")
159+
#20053=@"loc,{#10000},2,2,2,6"
160+
locations_default(#20053,#10000,2,2,2,6)
161+
hasLocation(#20052,#20053)
162+
#20054=*
163+
#20055=*
164+
exprs(#20055,0,#20052,0,"#foo")
165+
#20056=@"loc,{#10000},2,2,2,5"
166+
locations_default(#20056,#10000,2,2,2,5)
167+
hasLocation(#20055,#20056)
168+
exprContainers(#20055,#20054)
169+
literals("#foo","#foo",#20055)
170+
#20057=*
171+
properties(#20057,#20048,3,0,"constru ... = 5;\n }")
172+
#20058=@"loc,{#10000},3,2,5,2"
173+
locations_default(#20058,#10000,3,2,5,2)
174+
hasLocation(#20057,#20058)
175+
#20059=*
176+
exprs(#20059,0,#20057,0,"constru ... = 5;\n }")
177+
hasLocation(#20059,#20058)
178+
enclosingStmt(#20059,#20048)
179+
exprContainers(#20059,#20001)
180+
literals("constructor","constructor",#20059)
181+
exprs(#20054,9,#20057,1,"constru ... = 5;\n }")
182+
hasLocation(#20054,#20058)
183+
enclosingStmt(#20054,#20048)
184+
exprContainers(#20054,#20001)
185+
#20060=*
186+
scopes(#20060,1)
187+
scopenodes(#20054,#20060)
188+
scopenesting(#20060,#20051)
189+
#20061=@"var;{arguments};{#20060}"
190+
variables(#20061,"arguments",#20060)
191+
isArgumentsObject(#20061)
192+
#20062=*
193+
stmts(#20062,1,#20054,-2,"{\n th ... = 5;\n }")
194+
#20063=@"loc,{#10000},3,16,5,2"
195+
locations_default(#20063,#10000,3,16,5,2)
196+
hasLocation(#20062,#20063)
197+
stmtContainers(#20062,#20054)
198+
#20064=*
199+
stmts(#20064,2,#20062,0,"this.#foo = 5;")
200+
#20065=@"loc,{#10000},4,4,4,17"
201+
locations_default(#20065,#10000,4,4,4,17)
202+
hasLocation(#20064,#20065)
203+
stmtContainers(#20064,#20054)
204+
#20066=*
205+
exprs(#20066,47,#20064,0,"this.#foo = 5")
206+
#20067=@"loc,{#10000},4,4,4,16"
207+
locations_default(#20067,#10000,4,4,4,16)
208+
hasLocation(#20066,#20067)
209+
enclosingStmt(#20066,#20064)
210+
exprContainers(#20066,#20054)
211+
#20068=*
212+
exprs(#20068,14,#20066,0,"this.#foo")
213+
#20069=@"loc,{#10000},4,4,4,12"
214+
locations_default(#20069,#10000,4,4,4,12)
215+
hasLocation(#20068,#20069)
216+
enclosingStmt(#20068,#20064)
217+
exprContainers(#20068,#20054)
218+
#20070=*
219+
exprs(#20070,6,#20068,0,"this")
220+
hasLocation(#20070,#20031)
221+
enclosingStmt(#20070,#20064)
222+
exprContainers(#20070,#20054)
223+
#20071=*
224+
exprs(#20071,0,#20068,1,"#foo")
225+
#20072=@"loc,{#10000},4,9,4,12"
226+
locations_default(#20072,#10000,4,9,4,12)
227+
hasLocation(#20071,#20072)
228+
enclosingStmt(#20071,#20064)
229+
exprContainers(#20071,#20054)
230+
literals("#foo","#foo",#20071)
231+
#20073=*
232+
exprs(#20073,3,#20066,1,"5")
233+
hasLocation(#20073,#20037)
234+
enclosingStmt(#20073,#20064)
235+
exprContainers(#20073,#20054)
236+
literals("5","5",#20073)
237+
isMethod(#20057)
238+
#20074=*
239+
entry_cfg_node(#20074,#20001)
240+
#20075=@"loc,{#10000},1,1,1,0"
241+
locations_default(#20075,#10000,1,1,1,0)
242+
hasLocation(#20074,#20075)
243+
#20076=*
244+
exit_cfg_node(#20076,#20001)
245+
hasLocation(#20076,#20044)
246+
successor(#20055,#20052)
247+
successor(#20054,#20057)
248+
#20077=*
249+
entry_cfg_node(#20077,#20054)
250+
#20078=@"loc,{#10000},3,2,3,1"
251+
locations_default(#20078,#10000,3,2,3,1)
252+
hasLocation(#20077,#20078)
253+
successor(#20052,#20062)
254+
#20079=*
255+
exit_cfg_node(#20079,#20054)
256+
#20080=@"loc,{#10000},5,3,5,2"
257+
locations_default(#20080,#10000,5,3,5,2)
258+
hasLocation(#20079,#20080)
259+
successor(#20062,#20064)
260+
successor(#20064,#20070)
261+
successor(#20073,#20066)
262+
successor(#20071,#20068)
263+
successor(#20070,#20071)
264+
successor(#20068,#20073)
265+
successor(#20066,#20079)
266+
successor(#20077,#20055)
267+
successor(#20059,#20054)
268+
successor(#20057,#20048)
269+
successor(#20050,#20059)
270+
successor(#20048,#20076)
271+
successor(#20074,#20050)
272+
numlines(#10000,6,6,0)
273+
filetype(#10000,"typescript")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
propAccess
2+
| #privateField | tst.ts:5:9:5:26 | this.#privateField |
3+
| #privateField | tst.ts:6:9:6:26 | this.#privateField |
4+
fieldDecl
5+
| #privateField | tst.ts:2:5:2:23 | #privateField: any; |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import javascript
2+
3+
query PropAccess propAccess(string name) {
4+
result.getPropertyName() = name
5+
}
6+
7+
query FieldDeclaration fieldDecl(string name) {
8+
result.getName() = name
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class C {
2+
#privateField: any;
3+
4+
constructor(x: any) {
5+
this.#privateField = x;
6+
this.#privateField(y);
7+
}
8+
}

0 commit comments

Comments
 (0)