Skip to content

Commit 722b1a2

Browse files
authored
Merge pull request #4087 from erik-krogh/thisJsx
Approved by asgerf
2 parents 844abc5 + 61d4648 commit 722b1a2

File tree

15 files changed

+205
-80
lines changed

15 files changed

+205
-80
lines changed

javascript/extractor/src/com/semmle/jcorn/jsx/JSXParser.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.semmle.js.ast.jsx.JSXNamespacedName;
3636
import com.semmle.js.ast.jsx.JSXOpeningElement;
3737
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
38+
import com.semmle.js.ast.jsx.JSXThisExpr;
3839
import com.semmle.util.data.Either;
3940
import java.util.ArrayList;
4041
import java.util.List;
@@ -225,22 +226,26 @@ private String getQualifiedJSXName(Object object) {
225226
}
226227

227228
/** Parse next token as JSX identifier */
228-
private JSXIdentifier jsx_parseIdentifier() {
229+
private IJSXName jsx_parseIdentifier(boolean expectThisExpr) {
229230
SourceLocation loc = new SourceLocation(this.startLoc);
230231
String name = null;
231232
if (this.type == jsxName) name = String.valueOf(this.value);
232233
else if (this.type.keyword != null) name = this.type.keyword;
233234
else this.unexpected();
234235
this.next();
235-
return this.finishNode(new JSXIdentifier(loc, name));
236+
if (expectThisExpr && name.equals("this")) {
237+
return this.finishNode(new JSXThisExpr(loc));
238+
} else {
239+
return this.finishNode(new JSXIdentifier(loc, name));
240+
}
236241
}
237242

238243
/** Parse namespaced identifier. */
239244
private IJSXName jsx_parseNamespacedName() {
240245
SourceLocation loc = new SourceLocation(this.startLoc);
241-
JSXIdentifier namespace = this.jsx_parseIdentifier();
242-
if (!((JSXOptions) options).allowNamespaces || !this.eat(colon)) return namespace;
243-
return this.finishNode(new JSXNamespacedName(loc, namespace, this.jsx_parseIdentifier()));
246+
IJSXName namespace = this.jsx_parseIdentifier(true);
247+
if (namespace instanceof JSXThisExpr || (!((JSXOptions) options).allowNamespaces || !this.eat(colon))) return namespace;
248+
return this.finishNode(new JSXNamespacedName(loc, (JSXIdentifier)namespace, (JSXIdentifier)this.jsx_parseIdentifier(false)));
244249
}
245250

246251
/**
@@ -258,7 +263,7 @@ private IJSXName jsx_parseElementName() {
258263
}
259264
while (this.eat(dot)) {
260265
SourceLocation loc = new SourceLocation(startPos);
261-
node = this.finishNode(new JSXMemberExpression(loc, node, this.jsx_parseIdentifier()));
266+
node = this.finishNode(new JSXMemberExpression(loc, node, (JSXIdentifier)this.jsx_parseIdentifier(false)));
262267
}
263268
return node;
264269
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.semmle.js.ast.jsx.JSXNamespacedName;
1616
import com.semmle.js.ast.jsx.JSXOpeningElement;
1717
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
18+
import com.semmle.js.ast.jsx.JSXThisExpr;
1819
import com.semmle.ts.ast.ExportWholeDeclaration;
1920
import com.semmle.ts.ast.ExternalModuleReference;
2021
import com.semmle.ts.ast.ImportWholeDeclaration;
@@ -635,6 +636,11 @@ public JsonElement visit(JSXIdentifier nd, Void c) {
635636
return result;
636637
}
637638

639+
@Override
640+
public JsonElement visit(JSXThisExpr nd, Void c) {
641+
return this.mkNode(nd, "JSXThisExpr");
642+
}
643+
638644
@Override
639645
public JsonElement visit(JSXMemberExpression nd, Void c) {
640646
JsonObject result = this.mkNode(nd);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.semmle.js.ast.jsx.JSXNamespacedName;
1515
import com.semmle.js.ast.jsx.JSXOpeningElement;
1616
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
17+
import com.semmle.js.ast.jsx.JSXThisExpr;
1718
import com.semmle.ts.ast.ArrayTypeExpr;
1819
import com.semmle.ts.ast.ConditionalTypeExpr;
1920
import com.semmle.ts.ast.DecoratorList;
@@ -498,6 +499,11 @@ public R visit(JSXIdentifier nd, C c) {
498499
return visit((IJSXName) nd, c);
499500
}
500501

502+
@Override
503+
public R visit(JSXThisExpr nd, C c) {
504+
return visit((IJSXName) nd, c);
505+
}
506+
501507
@Override
502508
public R visit(JSXMemberExpression nd, C c) {
503509
return visit((IJSXName) nd, c);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.semmle.js.ast.jsx.JSXNamespacedName;
1111
import com.semmle.js.ast.jsx.JSXOpeningElement;
1212
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
13+
import com.semmle.js.ast.jsx.JSXThisExpr;
1314
import com.semmle.ts.ast.ArrayTypeExpr;
1415
import com.semmle.ts.ast.ConditionalTypeExpr;
1516
import com.semmle.ts.ast.DecoratorList;
@@ -567,6 +568,11 @@ public INode visit(JSXIdentifier nd, Void c) {
567568
return new JSXIdentifier(visit(nd.getLoc()), nd.getName());
568569
}
569570

571+
@Override
572+
public INode visit(JSXThisExpr nd, Void c) {
573+
return new JSXThisExpr(visit(nd.getLoc()));
574+
}
575+
570576
@Override
571577
public INode visit(JSXMemberExpression nd, Void c) {
572578
return new JSXMemberExpression(visit(nd.getLoc()), copy(nd.getObject()), copy(nd.getName()));

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.semmle.js.ast.jsx.JSXNamespacedName;
1111
import com.semmle.js.ast.jsx.JSXOpeningElement;
1212
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
13+
import com.semmle.js.ast.jsx.JSXThisExpr;
1314
import com.semmle.ts.ast.ArrayTypeExpr;
1415
import com.semmle.ts.ast.ConditionalTypeExpr;
1516
import com.semmle.ts.ast.DecoratorList;
@@ -200,6 +201,8 @@ public interface Visitor<C, R> {
200201

201202
public R visit(JSXIdentifier nd, C c);
202203

204+
public R visit(JSXThisExpr nd, C c);
205+
203206
public R visit(JSXMemberExpression nd, C c);
204207

205208
public R visit(JSXNamespacedName nd, C c);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.semmle.js.ast.jsx;
2+
3+
import com.semmle.js.ast.ThisExpression;
4+
import com.semmle.js.ast.SourceLocation;
5+
import com.semmle.js.ast.Visitor;
6+
7+
public class JSXThisExpr extends ThisExpression implements IJSXName {
8+
public JSXThisExpr(SourceLocation loc) {
9+
super(loc);
10+
}
11+
12+
@Override
13+
public <C, R> R accept(Visitor<C, R> v, C c) {
14+
return v.visit(this, c);
15+
}
16+
17+
@Override
18+
public String getQualifiedName() {
19+
return "this";
20+
}
21+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import com.semmle.js.ast.TaggedTemplateExpression;
8383
import com.semmle.js.ast.TemplateElement;
8484
import com.semmle.js.ast.TemplateLiteral;
85+
import com.semmle.js.ast.ThisExpression;
8586
import com.semmle.js.ast.ThrowStatement;
8687
import com.semmle.js.ast.TryStatement;
8788
import com.semmle.js.ast.UnaryExpression;
@@ -105,6 +106,7 @@
105106
import com.semmle.js.ast.jsx.JSXNamespacedName;
106107
import com.semmle.js.ast.jsx.JSXOpeningElement;
107108
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
109+
import com.semmle.js.ast.jsx.JSXThisExpr;
108110
import com.semmle.js.extractor.ExtractionMetrics.ExtractionPhase;
109111
import com.semmle.js.extractor.ExtractorConfig.Platform;
110112
import com.semmle.js.extractor.ExtractorConfig.SourceType;
@@ -1645,6 +1647,11 @@ public Label visit(JSXIdentifier nd, Context c) {
16451647
return visit((Identifier) nd, c);
16461648
}
16471649

1650+
@Override
1651+
public Label visit(JSXThisExpr nd, Context c) {
1652+
return visit((ThisExpression) nd, c);
1653+
}
1654+
16481655
@Override
16491656
public Label visit(JSXMemberExpression nd, Context c) {
16501657
Label key = super.visit(nd, c);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.semmle.js.ast.LogicalExpression;
1919
import com.semmle.js.ast.MemberExpression;
2020
import com.semmle.js.ast.MetaProperty;
21+
import com.semmle.js.ast.ThisExpression;
2122
import com.semmle.js.ast.UnaryExpression;
2223
import com.semmle.js.ast.UpdateExpression;
2324
import com.semmle.js.ast.XMLAnyName;
@@ -28,6 +29,7 @@
2829
import com.semmle.js.ast.jsx.JSXIdentifier;
2930
import com.semmle.js.ast.jsx.JSXMemberExpression;
3031
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
32+
import com.semmle.js.ast.jsx.JSXThisExpr;
3133
import com.semmle.js.extractor.ASTExtractor.IdContext;
3234
import com.semmle.ts.ast.DecoratorList;
3335
import com.semmle.ts.ast.ExpressionWithTypeArguments;
@@ -191,6 +193,11 @@ public Integer visit(JSXIdentifier nd, Void c) {
191193
return visit((Identifier) nd, c);
192194
}
193195

196+
@Override
197+
public Integer visit(JSXThisExpr nd, Void c) {
198+
return visit((ThisExpression) nd, c);
199+
}
200+
194201
@Override
195202
public Integer visit(LogicalExpression nd, Void q) {
196203
return binOpKinds.get(nd.getOperator());

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
import com.semmle.js.ast.jsx.JSXMemberExpression;
111111
import com.semmle.js.ast.jsx.JSXOpeningElement;
112112
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
113+
import com.semmle.js.ast.jsx.JSXThisExpr;
113114
import com.semmle.js.parser.JSParser.Result;
114115
import com.semmle.ts.ast.ArrayTypeExpr;
115116
import com.semmle.ts.ast.ConditionalTypeExpr;
@@ -2356,7 +2357,7 @@ private IJSXName convertJSXName(Expression e) {
23562357
convertJSXName(me.getObject()),
23572358
(JSXIdentifier) convertJSXName(me.getProperty()));
23582359
}
2359-
if (e instanceof ThisExpression) return new JSXIdentifier(e.getLoc(), "this");
2360+
if (e instanceof ThisExpression) return new JSXThisExpr(e.getLoc());
23602361
return (IJSXName) e;
23612362
}
23622363

javascript/extractor/tests/ts/output/trap/tsx.tsx.trap

Lines changed: 69 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -301,94 +301,90 @@ hasLocation(#20099,#20100)
301301
enclosingStmt(#20099,#20077)
302302
exprContainers(#20099,#20001)
303303
#20101=*
304-
exprs(#20101,79,#20099,0,"this")
304+
exprs(#20101,6,#20099,0,"this")
305305
hasLocation(#20101,#20039)
306306
enclosingStmt(#20101,#20077)
307307
exprContainers(#20101,#20001)
308-
literals("this","this",#20101)
309-
#20102=@"var;{this};{#20000}"
310-
variables(#20102,"this",#20000)
311-
bind(#20101,#20102)
308+
#20102=*
309+
exprs(#20102,0,#20099,1,"props")
310+
hasLocation(#20102,#20043)
311+
enclosingStmt(#20102,#20077)
312+
exprContainers(#20102,#20001)
313+
literals("props","props",#20102)
312314
#20103=*
313-
exprs(#20103,0,#20099,1,"props")
314-
hasLocation(#20103,#20043)
315+
exprs(#20103,0,#20097,1,"icon")
316+
hasLocation(#20103,#20047)
315317
enclosingStmt(#20103,#20077)
316318
exprContainers(#20103,#20001)
317-
literals("props","props",#20103)
319+
literals("icon","icon",#20103)
318320
#20104=*
319-
exprs(#20104,0,#20097,1,"icon")
320-
hasLocation(#20104,#20047)
321+
exprs(#20104,4,#20079,-4,"")
322+
#20105=@"loc,{#10000},3,3,3,2"
323+
locations_default(#20105,#10000,3,3,3,2)
324+
hasLocation(#20104,#20105)
321325
enclosingStmt(#20104,#20077)
322326
exprContainers(#20104,#20001)
323-
literals("icon","icon",#20104)
324-
#20105=*
325-
exprs(#20105,4,#20079,-4,"")
326-
#20106=@"loc,{#10000},3,3,3,2"
327-
locations_default(#20106,#10000,3,3,3,2)
328-
hasLocation(#20105,#20106)
329-
enclosingStmt(#20105,#20077)
330-
exprContainers(#20105,#20001)
331327
literals("
332-
","",#20105)
333-
#20107=*
334-
regexpterm(#20107,14,#20105,0,"
328+
","",#20104)
329+
#20106=*
330+
regexpterm(#20106,14,#20104,0,"
335331
")
336-
#20108=@"loc,{#10000},3,4,3,6"
337-
locations_default(#20108,#10000,3,4,3,6)
338-
hasLocation(#20107,#20108)
339-
regexpConstValue(#20107,"
332+
#20107=@"loc,{#10000},3,4,3,6"
333+
locations_default(#20107,#10000,3,4,3,6)
334+
hasLocation(#20106,#20107)
335+
regexpConstValue(#20106,"
340336
")
341-
#20109=*
342-
exprs(#20109,89,#20079,-5,"<name-with-dashes/>")
343-
#20110=@"loc,{#10000},3,3,3,21"
344-
locations_default(#20110,#10000,3,3,3,21)
345-
hasLocation(#20109,#20110)
346-
enclosingStmt(#20109,#20077)
347-
exprContainers(#20109,#20001)
348-
#20111=*
349-
exprs(#20111,0,#20109,-1,"name-with-dashes")
350-
#20112=@"loc,{#10000},3,4,3,19"
351-
locations_default(#20112,#10000,3,4,3,19)
352-
hasLocation(#20111,#20112)
353-
enclosingStmt(#20111,#20077)
354-
exprContainers(#20111,#20001)
355-
literals("name-with-dashes","name-with-dashes",#20111)
356-
#20113=*
357-
exprs(#20113,4,#20079,-6,"")
358-
#20114=@"loc,{#10000},4,1,4,0"
359-
locations_default(#20114,#10000,4,1,4,0)
360-
hasLocation(#20113,#20114)
361-
enclosingStmt(#20113,#20077)
362-
exprContainers(#20113,#20001)
337+
#20108=*
338+
exprs(#20108,89,#20079,-5,"<name-with-dashes/>")
339+
#20109=@"loc,{#10000},3,3,3,21"
340+
locations_default(#20109,#10000,3,3,3,21)
341+
hasLocation(#20108,#20109)
342+
enclosingStmt(#20108,#20077)
343+
exprContainers(#20108,#20001)
344+
#20110=*
345+
exprs(#20110,0,#20108,-1,"name-with-dashes")
346+
#20111=@"loc,{#10000},3,4,3,19"
347+
locations_default(#20111,#10000,3,4,3,19)
348+
hasLocation(#20110,#20111)
349+
enclosingStmt(#20110,#20077)
350+
exprContainers(#20110,#20001)
351+
literals("name-with-dashes","name-with-dashes",#20110)
352+
#20112=*
353+
exprs(#20112,4,#20079,-6,"")
354+
#20113=@"loc,{#10000},4,1,4,0"
355+
locations_default(#20113,#10000,4,1,4,0)
356+
hasLocation(#20112,#20113)
357+
enclosingStmt(#20112,#20077)
358+
exprContainers(#20112,#20001)
363359
literals("
364-
","",#20113)
365-
#20115=*
366-
regexpterm(#20115,14,#20113,0,"
360+
","",#20112)
361+
#20114=*
362+
regexpterm(#20114,14,#20112,0,"
367363
")
368-
#20116=@"loc,{#10000},4,2,4,2"
369-
locations_default(#20116,#10000,4,2,4,2)
370-
hasLocation(#20115,#20116)
371-
regexpConstValue(#20115,"
364+
#20115=@"loc,{#10000},4,2,4,2"
365+
locations_default(#20115,#10000,4,2,4,2)
366+
hasLocation(#20114,#20115)
367+
regexpConstValue(#20114,"
372368
")
373-
#20117=*
374-
entry_cfg_node(#20117,#20001)
375-
#20118=@"loc,{#10000},1,1,1,0"
376-
locations_default(#20118,#10000,1,1,1,0)
377-
hasLocation(#20117,#20118)
378-
#20119=*
379-
exit_cfg_node(#20119,#20001)
380-
hasLocation(#20119,#20075)
369+
#20116=*
370+
entry_cfg_node(#20116,#20001)
371+
#20117=@"loc,{#10000},1,1,1,0"
372+
locations_default(#20117,#10000,1,1,1,0)
373+
hasLocation(#20116,#20117)
374+
#20118=*
375+
exit_cfg_node(#20118,#20001)
376+
hasLocation(#20118,#20075)
381377
successor(#20077,#20080)
382-
successor(#20113,#20079)
383-
successor(#20111,#20109)
384-
successor(#20109,#20113)
385-
successor(#20105,#20111)
386-
successor(#20104,#20097)
387-
successor(#20103,#20099)
388-
successor(#20101,#20103)
389-
successor(#20099,#20104)
378+
successor(#20112,#20079)
379+
successor(#20110,#20108)
380+
successor(#20108,#20112)
381+
successor(#20104,#20110)
382+
successor(#20103,#20097)
383+
successor(#20102,#20099)
384+
successor(#20101,#20102)
385+
successor(#20099,#20103)
390386
successor(#20097,#20095)
391-
successor(#20095,#20105)
387+
successor(#20095,#20104)
392388
successor(#20091,#20101)
393389
successor(#20089,#20086)
394390
successor(#20088,#20089)
@@ -397,7 +393,7 @@ successor(#20084,#20081)
397393
successor(#20083,#20084)
398394
successor(#20081,#20088)
399395
successor(#20080,#20083)
400-
successor(#20079,#20119)
401-
successor(#20117,#20077)
396+
successor(#20079,#20118)
397+
successor(#20116,#20077)
402398
numlines(#10000,4,4,0)
403399
filetype(#10000,"typescript")

0 commit comments

Comments
 (0)