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

Skip to content

Commit cc0961a

Browse files
committed
TS: translate logical operators correctly
1 parent 02b58a8 commit cc0961a

3 files changed

Lines changed: 359 additions & 0 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.semmle.js.ast.InvokeExpression;
6161
import com.semmle.js.ast.LabeledStatement;
6262
import com.semmle.js.ast.Literal;
63+
import com.semmle.js.ast.LogicalExpression;
6364
import com.semmle.js.ast.MemberDefinition;
6465
import com.semmle.js.ast.MemberExpression;
6566
import com.semmle.js.ast.DeclarationFlags;
@@ -839,6 +840,9 @@ private Node convertBinaryExpression(JsonObject node, SourceLocation loc) throws
839840
return new SequenceExpression(loc, expressions);
840841
} else {
841842
String operator = getSourceLocation(operatorToken).getSource();
843+
if ("||".equals(operator) || "&&".equals(operator)) {
844+
return new LogicalExpression(loc, operator, left, right);
845+
}
842846
if ("EqualsToken".equals(operatorKind))
843847
left = convertLValue(left);
844848
return new BinaryExpression(loc, operator, left, right);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function f(x,y) {
2+
if (x || y) {}
3+
if (x && y) {}
4+
}
Lines changed: 351 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,351 @@
1+
#10000=@"/logicalOr.ts;sourcefile"
2+
files(#10000,"/logicalOr.ts","logicalOr","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+
toplevels(#20001,0)
13+
#20002=@"loc,{#10000},1,1,5,0"
14+
locations_default(#20002,#10000,1,1,5,0)
15+
hasLocation(#20001,#20002)
16+
#20003=@"var;{f};{#20000}"
17+
variables(#20003,"f",#20000)
18+
#20004=*
19+
stmts(#20004,17,#20001,0,"functio ... y) {}\n}")
20+
#20005=@"loc,{#10000},1,1,4,1"
21+
locations_default(#20005,#10000,1,1,4,1)
22+
hasLocation(#20004,#20005)
23+
stmtContainers(#20004,#20001)
24+
#20006=*
25+
exprs(#20006,78,#20004,-1,"f")
26+
#20007=@"loc,{#10000},1,10,1,10"
27+
locations_default(#20007,#10000,1,10,1,10)
28+
hasLocation(#20006,#20007)
29+
exprContainers(#20006,#20004)
30+
literals("f","f",#20006)
31+
decl(#20006,#20003)
32+
#20008=*
33+
scopes(#20008,1)
34+
scopenodes(#20004,#20008)
35+
scopenesting(#20008,#20000)
36+
#20009=@"var;{x};{#20008}"
37+
variables(#20009,"x",#20008)
38+
#20010=*
39+
exprs(#20010,78,#20004,0,"x")
40+
#20011=@"loc,{#10000},1,12,1,12"
41+
locations_default(#20011,#10000,1,12,1,12)
42+
hasLocation(#20010,#20011)
43+
exprContainers(#20010,#20004)
44+
literals("x","x",#20010)
45+
decl(#20010,#20009)
46+
#20012=@"var;{y};{#20008}"
47+
variables(#20012,"y",#20008)
48+
#20013=*
49+
exprs(#20013,78,#20004,1,"y")
50+
#20014=@"loc,{#10000},1,14,1,14"
51+
locations_default(#20014,#10000,1,14,1,14)
52+
hasLocation(#20013,#20014)
53+
exprContainers(#20013,#20004)
54+
literals("y","y",#20013)
55+
decl(#20013,#20012)
56+
#20015=@"var;{arguments};{#20008}"
57+
variables(#20015,"arguments",#20008)
58+
isArgumentsObject(#20015)
59+
#20016=*
60+
stmts(#20016,1,#20004,-2,"{\n if ... y) {}\n}")
61+
#20017=@"loc,{#10000},1,17,4,1"
62+
locations_default(#20017,#10000,1,17,4,1)
63+
hasLocation(#20016,#20017)
64+
stmtContainers(#20016,#20004)
65+
#20018=*
66+
stmts(#20018,3,#20016,0,"if (x || y) {}")
67+
#20019=@"loc,{#10000},2,3,2,16"
68+
locations_default(#20019,#10000,2,3,2,16)
69+
hasLocation(#20018,#20019)
70+
stmtContainers(#20018,#20004)
71+
#20020=*
72+
exprs(#20020,45,#20018,0,"x || y")
73+
#20021=@"loc,{#10000},2,7,2,12"
74+
locations_default(#20021,#10000,2,7,2,12)
75+
hasLocation(#20020,#20021)
76+
enclosingStmt(#20020,#20018)
77+
exprContainers(#20020,#20004)
78+
#20022=*
79+
exprs(#20022,79,#20020,0,"x")
80+
#20023=@"loc,{#10000},2,7,2,7"
81+
locations_default(#20023,#10000,2,7,2,7)
82+
hasLocation(#20022,#20023)
83+
enclosingStmt(#20022,#20018)
84+
exprContainers(#20022,#20004)
85+
literals("x","x",#20022)
86+
bind(#20022,#20009)
87+
#20024=*
88+
exprs(#20024,79,#20020,1,"y")
89+
#20025=@"loc,{#10000},2,12,2,12"
90+
locations_default(#20025,#10000,2,12,2,12)
91+
hasLocation(#20024,#20025)
92+
enclosingStmt(#20024,#20018)
93+
exprContainers(#20024,#20004)
94+
literals("y","y",#20024)
95+
bind(#20024,#20012)
96+
#20026=*
97+
stmts(#20026,1,#20018,1,"{}")
98+
#20027=@"loc,{#10000},2,15,2,16"
99+
locations_default(#20027,#10000,2,15,2,16)
100+
hasLocation(#20026,#20027)
101+
stmtContainers(#20026,#20004)
102+
#20028=*
103+
stmts(#20028,3,#20016,1,"if (x && y) {}")
104+
#20029=@"loc,{#10000},3,3,3,16"
105+
locations_default(#20029,#10000,3,3,3,16)
106+
hasLocation(#20028,#20029)
107+
stmtContainers(#20028,#20004)
108+
#20030=*
109+
exprs(#20030,44,#20028,0,"x && y")
110+
#20031=@"loc,{#10000},3,7,3,12"
111+
locations_default(#20031,#10000,3,7,3,12)
112+
hasLocation(#20030,#20031)
113+
enclosingStmt(#20030,#20028)
114+
exprContainers(#20030,#20004)
115+
#20032=*
116+
exprs(#20032,79,#20030,0,"x")
117+
#20033=@"loc,{#10000},3,7,3,7"
118+
locations_default(#20033,#10000,3,7,3,7)
119+
hasLocation(#20032,#20033)
120+
enclosingStmt(#20032,#20028)
121+
exprContainers(#20032,#20004)
122+
literals("x","x",#20032)
123+
bind(#20032,#20009)
124+
#20034=*
125+
exprs(#20034,79,#20030,1,"y")
126+
#20035=@"loc,{#10000},3,12,3,12"
127+
locations_default(#20035,#10000,3,12,3,12)
128+
hasLocation(#20034,#20035)
129+
enclosingStmt(#20034,#20028)
130+
exprContainers(#20034,#20004)
131+
literals("y","y",#20034)
132+
bind(#20034,#20012)
133+
#20036=*
134+
stmts(#20036,1,#20028,1,"{}")
135+
#20037=@"loc,{#10000},3,15,3,16"
136+
locations_default(#20037,#10000,3,15,3,16)
137+
hasLocation(#20036,#20037)
138+
stmtContainers(#20036,#20004)
139+
numlines(#20004,4,4,0)
140+
#20038=*
141+
lines(#20038,#20001,"function f(x,y) {","
142+
")
143+
#20039=@"loc,{#10000},1,1,1,17"
144+
locations_default(#20039,#10000,1,1,1,17)
145+
hasLocation(#20038,#20039)
146+
#20040=*
147+
lines(#20040,#20001," if (x || y) {}","
148+
")
149+
#20041=@"loc,{#10000},2,1,2,16"
150+
locations_default(#20041,#10000,2,1,2,16)
151+
hasLocation(#20040,#20041)
152+
indentation(#10000,2," ",2)
153+
#20042=*
154+
lines(#20042,#20001," if (x && y) {}","
155+
")
156+
#20043=@"loc,{#10000},3,1,3,16"
157+
locations_default(#20043,#10000,3,1,3,16)
158+
hasLocation(#20042,#20043)
159+
indentation(#10000,3," ",2)
160+
#20044=*
161+
lines(#20044,#20001,"}","
162+
")
163+
#20045=@"loc,{#10000},4,1,4,1"
164+
locations_default(#20045,#10000,4,1,4,1)
165+
hasLocation(#20044,#20045)
166+
numlines(#20001,4,4,0)
167+
#20046=*
168+
tokeninfo(#20046,7,#20001,0,"function")
169+
#20047=@"loc,{#10000},1,1,1,8"
170+
locations_default(#20047,#10000,1,1,1,8)
171+
hasLocation(#20046,#20047)
172+
#20048=*
173+
tokeninfo(#20048,6,#20001,1,"f")
174+
hasLocation(#20048,#20007)
175+
#20049=*
176+
tokeninfo(#20049,8,#20001,2,"(")
177+
#20050=@"loc,{#10000},1,11,1,11"
178+
locations_default(#20050,#10000,1,11,1,11)
179+
hasLocation(#20049,#20050)
180+
#20051=*
181+
tokeninfo(#20051,6,#20001,3,"x")
182+
hasLocation(#20051,#20011)
183+
#20052=*
184+
tokeninfo(#20052,8,#20001,4,",")
185+
#20053=@"loc,{#10000},1,13,1,13"
186+
locations_default(#20053,#10000,1,13,1,13)
187+
hasLocation(#20052,#20053)
188+
#20054=*
189+
tokeninfo(#20054,6,#20001,5,"y")
190+
hasLocation(#20054,#20014)
191+
#20055=*
192+
tokeninfo(#20055,8,#20001,6,")")
193+
#20056=@"loc,{#10000},1,15,1,15"
194+
locations_default(#20056,#10000,1,15,1,15)
195+
hasLocation(#20055,#20056)
196+
#20057=*
197+
tokeninfo(#20057,8,#20001,7,"{")
198+
#20058=@"loc,{#10000},1,17,1,17"
199+
locations_default(#20058,#10000,1,17,1,17)
200+
hasLocation(#20057,#20058)
201+
#20059=*
202+
tokeninfo(#20059,7,#20001,8,"if")
203+
#20060=@"loc,{#10000},2,3,2,4"
204+
locations_default(#20060,#10000,2,3,2,4)
205+
hasLocation(#20059,#20060)
206+
#20061=*
207+
tokeninfo(#20061,8,#20001,9,"(")
208+
#20062=@"loc,{#10000},2,6,2,6"
209+
locations_default(#20062,#10000,2,6,2,6)
210+
hasLocation(#20061,#20062)
211+
#20063=*
212+
tokeninfo(#20063,6,#20001,10,"x")
213+
hasLocation(#20063,#20023)
214+
#20064=*
215+
tokeninfo(#20064,8,#20001,11,"||")
216+
#20065=@"loc,{#10000},2,9,2,10"
217+
locations_default(#20065,#10000,2,9,2,10)
218+
hasLocation(#20064,#20065)
219+
#20066=*
220+
tokeninfo(#20066,6,#20001,12,"y")
221+
hasLocation(#20066,#20025)
222+
#20067=*
223+
tokeninfo(#20067,8,#20001,13,")")
224+
#20068=@"loc,{#10000},2,13,2,13"
225+
locations_default(#20068,#10000,2,13,2,13)
226+
hasLocation(#20067,#20068)
227+
#20069=*
228+
tokeninfo(#20069,8,#20001,14,"{")
229+
#20070=@"loc,{#10000},2,15,2,15"
230+
locations_default(#20070,#10000,2,15,2,15)
231+
hasLocation(#20069,#20070)
232+
#20071=*
233+
tokeninfo(#20071,8,#20001,15,"}")
234+
#20072=@"loc,{#10000},2,16,2,16"
235+
locations_default(#20072,#10000,2,16,2,16)
236+
hasLocation(#20071,#20072)
237+
#20073=*
238+
tokeninfo(#20073,7,#20001,16,"if")
239+
#20074=@"loc,{#10000},3,3,3,4"
240+
locations_default(#20074,#10000,3,3,3,4)
241+
hasLocation(#20073,#20074)
242+
#20075=*
243+
tokeninfo(#20075,8,#20001,17,"(")
244+
#20076=@"loc,{#10000},3,6,3,6"
245+
locations_default(#20076,#10000,3,6,3,6)
246+
hasLocation(#20075,#20076)
247+
#20077=*
248+
tokeninfo(#20077,6,#20001,18,"x")
249+
hasLocation(#20077,#20033)
250+
#20078=*
251+
tokeninfo(#20078,8,#20001,19,"&&")
252+
#20079=@"loc,{#10000},3,9,3,10"
253+
locations_default(#20079,#10000,3,9,3,10)
254+
hasLocation(#20078,#20079)
255+
#20080=*
256+
tokeninfo(#20080,6,#20001,20,"y")
257+
hasLocation(#20080,#20035)
258+
#20081=*
259+
tokeninfo(#20081,8,#20001,21,")")
260+
#20082=@"loc,{#10000},3,13,3,13"
261+
locations_default(#20082,#10000,3,13,3,13)
262+
hasLocation(#20081,#20082)
263+
#20083=*
264+
tokeninfo(#20083,8,#20001,22,"{")
265+
#20084=@"loc,{#10000},3,15,3,15"
266+
locations_default(#20084,#10000,3,15,3,15)
267+
hasLocation(#20083,#20084)
268+
#20085=*
269+
tokeninfo(#20085,8,#20001,23,"}")
270+
#20086=@"loc,{#10000},3,16,3,16"
271+
locations_default(#20086,#10000,3,16,3,16)
272+
hasLocation(#20085,#20086)
273+
#20087=*
274+
tokeninfo(#20087,8,#20001,24,"}")
275+
hasLocation(#20087,#20045)
276+
#20088=*
277+
tokeninfo(#20088,0,#20001,25,"")
278+
#20089=@"loc,{#10000},5,1,5,0"
279+
locations_default(#20089,#10000,5,1,5,0)
280+
hasLocation(#20088,#20089)
281+
#20090=*
282+
entry_cfg_node(#20090,#20001)
283+
#20091=@"loc,{#10000},1,1,1,0"
284+
locations_default(#20091,#10000,1,1,1,0)
285+
hasLocation(#20090,#20091)
286+
#20092=*
287+
exit_cfg_node(#20092,#20001)
288+
hasLocation(#20092,#20089)
289+
successor(#20004,#20092)
290+
#20093=*
291+
entry_cfg_node(#20093,#20004)
292+
hasLocation(#20093,#20091)
293+
#20094=*
294+
exit_cfg_node(#20094,#20004)
295+
#20095=@"loc,{#10000},4,2,4,1"
296+
locations_default(#20095,#10000,4,2,4,1)
297+
hasLocation(#20094,#20095)
298+
successor(#20016,#20018)
299+
successor(#20028,#20030)
300+
successor(#20030,#20032)
301+
#20096=*
302+
guard_node(#20096,1,#20032)
303+
hasLocation(#20096,#20033)
304+
successor(#20096,#20034)
305+
#20097=*
306+
guard_node(#20097,0,#20032)
307+
hasLocation(#20097,#20033)
308+
successor(#20097,#20094)
309+
successor(#20032,#20096)
310+
successor(#20032,#20097)
311+
#20098=*
312+
guard_node(#20098,1,#20034)
313+
hasLocation(#20098,#20035)
314+
successor(#20098,#20036)
315+
#20099=*
316+
guard_node(#20099,0,#20034)
317+
hasLocation(#20099,#20035)
318+
successor(#20099,#20094)
319+
successor(#20034,#20098)
320+
successor(#20034,#20099)
321+
successor(#20036,#20094)
322+
successor(#20018,#20020)
323+
successor(#20020,#20022)
324+
#20100=*
325+
guard_node(#20100,1,#20022)
326+
hasLocation(#20100,#20023)
327+
successor(#20100,#20026)
328+
#20101=*
329+
guard_node(#20101,0,#20022)
330+
hasLocation(#20101,#20023)
331+
successor(#20101,#20024)
332+
successor(#20022,#20100)
333+
successor(#20022,#20101)
334+
#20102=*
335+
guard_node(#20102,1,#20024)
336+
hasLocation(#20102,#20025)
337+
successor(#20102,#20026)
338+
#20103=*
339+
guard_node(#20103,0,#20024)
340+
hasLocation(#20103,#20025)
341+
successor(#20103,#20028)
342+
successor(#20024,#20102)
343+
successor(#20024,#20103)
344+
successor(#20026,#20028)
345+
successor(#20013,#20016)
346+
successor(#20010,#20013)
347+
successor(#20093,#20010)
348+
successor(#20006,#20004)
349+
successor(#20090,#20006)
350+
numlines(#10000,4,4,0)
351+
filetype(#10000,"typescript")

0 commit comments

Comments
 (0)