Merge pull request #4087 from erik-krogh/thisJsx

Approved by asgerf
This commit is contained in:
CodeQL CI
2020-08-25 10:20:32 +01:00
committed by GitHub
15 changed files with 205 additions and 80 deletions

View File

@@ -35,6 +35,7 @@ import com.semmle.js.ast.jsx.JSXMemberExpression;
import com.semmle.js.ast.jsx.JSXNamespacedName;
import com.semmle.js.ast.jsx.JSXOpeningElement;
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
import com.semmle.js.ast.jsx.JSXThisExpr;
import com.semmle.util.data.Either;
import java.util.ArrayList;
import java.util.List;
@@ -225,22 +226,26 @@ public class JSXParser extends Parser {
}
/** Parse next token as JSX identifier */
private JSXIdentifier jsx_parseIdentifier() {
private IJSXName jsx_parseIdentifier(boolean expectThisExpr) {
SourceLocation loc = new SourceLocation(this.startLoc);
String name = null;
if (this.type == jsxName) name = String.valueOf(this.value);
else if (this.type.keyword != null) name = this.type.keyword;
else this.unexpected();
this.next();
return this.finishNode(new JSXIdentifier(loc, name));
if (expectThisExpr && name.equals("this")) {
return this.finishNode(new JSXThisExpr(loc));
} else {
return this.finishNode(new JSXIdentifier(loc, name));
}
}
/** Parse namespaced identifier. */
private IJSXName jsx_parseNamespacedName() {
SourceLocation loc = new SourceLocation(this.startLoc);
JSXIdentifier namespace = this.jsx_parseIdentifier();
if (!((JSXOptions) options).allowNamespaces || !this.eat(colon)) return namespace;
return this.finishNode(new JSXNamespacedName(loc, namespace, this.jsx_parseIdentifier()));
IJSXName namespace = this.jsx_parseIdentifier(true);
if (namespace instanceof JSXThisExpr || (!((JSXOptions) options).allowNamespaces || !this.eat(colon))) return namespace;
return this.finishNode(new JSXNamespacedName(loc, (JSXIdentifier)namespace, (JSXIdentifier)this.jsx_parseIdentifier(false)));
}
/**
@@ -258,7 +263,7 @@ public class JSXParser extends Parser {
}
while (this.eat(dot)) {
SourceLocation loc = new SourceLocation(startPos);
node = this.finishNode(new JSXMemberExpression(loc, node, this.jsx_parseIdentifier()));
node = this.finishNode(new JSXMemberExpression(loc, node, (JSXIdentifier)this.jsx_parseIdentifier(false)));
}
return node;
}

View File

@@ -15,6 +15,7 @@ import com.semmle.js.ast.jsx.JSXMemberExpression;
import com.semmle.js.ast.jsx.JSXNamespacedName;
import com.semmle.js.ast.jsx.JSXOpeningElement;
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
import com.semmle.js.ast.jsx.JSXThisExpr;
import com.semmle.ts.ast.ExportWholeDeclaration;
import com.semmle.ts.ast.ExternalModuleReference;
import com.semmle.ts.ast.ImportWholeDeclaration;
@@ -635,6 +636,11 @@ public class AST2JSON extends DefaultVisitor<Void, JsonElement> {
return result;
}
@Override
public JsonElement visit(JSXThisExpr nd, Void c) {
return this.mkNode(nd, "JSXThisExpr");
}
@Override
public JsonElement visit(JSXMemberExpression nd, Void c) {
JsonObject result = this.mkNode(nd);

View File

@@ -14,6 +14,7 @@ import com.semmle.js.ast.jsx.JSXMemberExpression;
import com.semmle.js.ast.jsx.JSXNamespacedName;
import com.semmle.js.ast.jsx.JSXOpeningElement;
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
import com.semmle.js.ast.jsx.JSXThisExpr;
import com.semmle.ts.ast.ArrayTypeExpr;
import com.semmle.ts.ast.ConditionalTypeExpr;
import com.semmle.ts.ast.DecoratorList;
@@ -498,6 +499,11 @@ public class DefaultVisitor<C, R> implements Visitor<C, R> {
return visit((IJSXName) nd, c);
}
@Override
public R visit(JSXThisExpr nd, C c) {
return visit((IJSXName) nd, c);
}
@Override
public R visit(JSXMemberExpression nd, C c) {
return visit((IJSXName) nd, c);

View File

@@ -10,6 +10,7 @@ import com.semmle.js.ast.jsx.JSXMemberExpression;
import com.semmle.js.ast.jsx.JSXNamespacedName;
import com.semmle.js.ast.jsx.JSXOpeningElement;
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
import com.semmle.js.ast.jsx.JSXThisExpr;
import com.semmle.ts.ast.ArrayTypeExpr;
import com.semmle.ts.ast.ConditionalTypeExpr;
import com.semmle.ts.ast.DecoratorList;
@@ -567,6 +568,11 @@ public class NodeCopier implements Visitor<Void, INode> {
return new JSXIdentifier(visit(nd.getLoc()), nd.getName());
}
@Override
public INode visit(JSXThisExpr nd, Void c) {
return new JSXThisExpr(visit(nd.getLoc()));
}
@Override
public INode visit(JSXMemberExpression nd, Void c) {
return new JSXMemberExpression(visit(nd.getLoc()), copy(nd.getObject()), copy(nd.getName()));

View File

@@ -10,6 +10,7 @@ import com.semmle.js.ast.jsx.JSXMemberExpression;
import com.semmle.js.ast.jsx.JSXNamespacedName;
import com.semmle.js.ast.jsx.JSXOpeningElement;
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
import com.semmle.js.ast.jsx.JSXThisExpr;
import com.semmle.ts.ast.ArrayTypeExpr;
import com.semmle.ts.ast.ConditionalTypeExpr;
import com.semmle.ts.ast.DecoratorList;
@@ -200,6 +201,8 @@ public interface Visitor<C, R> {
public R visit(JSXIdentifier nd, C c);
public R visit(JSXThisExpr nd, C c);
public R visit(JSXMemberExpression nd, C c);
public R visit(JSXNamespacedName nd, C c);

View File

@@ -0,0 +1,21 @@
package com.semmle.js.ast.jsx;
import com.semmle.js.ast.ThisExpression;
import com.semmle.js.ast.SourceLocation;
import com.semmle.js.ast.Visitor;
public class JSXThisExpr extends ThisExpression implements IJSXName {
public JSXThisExpr(SourceLocation loc) {
super(loc);
}
@Override
public <C, R> R accept(Visitor<C, R> v, C c) {
return v.visit(this, c);
}
@Override
public String getQualifiedName() {
return "this";
}
}

View File

@@ -82,6 +82,7 @@ import com.semmle.js.ast.SwitchStatement;
import com.semmle.js.ast.TaggedTemplateExpression;
import com.semmle.js.ast.TemplateElement;
import com.semmle.js.ast.TemplateLiteral;
import com.semmle.js.ast.ThisExpression;
import com.semmle.js.ast.ThrowStatement;
import com.semmle.js.ast.TryStatement;
import com.semmle.js.ast.UnaryExpression;
@@ -105,6 +106,7 @@ import com.semmle.js.ast.jsx.JSXMemberExpression;
import com.semmle.js.ast.jsx.JSXNamespacedName;
import com.semmle.js.ast.jsx.JSXOpeningElement;
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
import com.semmle.js.ast.jsx.JSXThisExpr;
import com.semmle.js.extractor.ExtractionMetrics.ExtractionPhase;
import com.semmle.js.extractor.ExtractorConfig.Platform;
import com.semmle.js.extractor.ExtractorConfig.SourceType;
@@ -1645,6 +1647,11 @@ public class ASTExtractor {
return visit((Identifier) nd, c);
}
@Override
public Label visit(JSXThisExpr nd, Context c) {
return visit((ThisExpression) nd, c);
}
@Override
public Label visit(JSXMemberExpression nd, Context c) {
Label key = super.visit(nd, c);

View File

@@ -18,6 +18,7 @@ import com.semmle.js.ast.Literal;
import com.semmle.js.ast.LogicalExpression;
import com.semmle.js.ast.MemberExpression;
import com.semmle.js.ast.MetaProperty;
import com.semmle.js.ast.ThisExpression;
import com.semmle.js.ast.UnaryExpression;
import com.semmle.js.ast.UpdateExpression;
import com.semmle.js.ast.XMLAnyName;
@@ -28,6 +29,7 @@ import com.semmle.js.ast.XMLQualifiedIdentifier;
import com.semmle.js.ast.jsx.JSXIdentifier;
import com.semmle.js.ast.jsx.JSXMemberExpression;
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
import com.semmle.js.ast.jsx.JSXThisExpr;
import com.semmle.js.extractor.ASTExtractor.IdContext;
import com.semmle.ts.ast.DecoratorList;
import com.semmle.ts.ast.ExpressionWithTypeArguments;
@@ -191,6 +193,11 @@ public class ExprKinds {
return visit((Identifier) nd, c);
}
@Override
public Integer visit(JSXThisExpr nd, Void c) {
return visit((ThisExpression) nd, c);
}
@Override
public Integer visit(LogicalExpression nd, Void q) {
return binOpKinds.get(nd.getOperator());

View File

@@ -110,6 +110,7 @@ import com.semmle.js.ast.jsx.JSXIdentifier;
import com.semmle.js.ast.jsx.JSXMemberExpression;
import com.semmle.js.ast.jsx.JSXOpeningElement;
import com.semmle.js.ast.jsx.JSXSpreadAttribute;
import com.semmle.js.ast.jsx.JSXThisExpr;
import com.semmle.js.parser.JSParser.Result;
import com.semmle.ts.ast.ArrayTypeExpr;
import com.semmle.ts.ast.ConditionalTypeExpr;
@@ -2356,7 +2357,7 @@ public class TypeScriptASTConverter {
convertJSXName(me.getObject()),
(JSXIdentifier) convertJSXName(me.getProperty()));
}
if (e instanceof ThisExpression) return new JSXIdentifier(e.getLoc(), "this");
if (e instanceof ThisExpression) return new JSXThisExpr(e.getLoc());
return (IJSXName) e;
}

View File

@@ -301,94 +301,90 @@ hasLocation(#20099,#20100)
enclosingStmt(#20099,#20077)
exprContainers(#20099,#20001)
#20101=*
exprs(#20101,79,#20099,0,"this")
exprs(#20101,6,#20099,0,"this")
hasLocation(#20101,#20039)
enclosingStmt(#20101,#20077)
exprContainers(#20101,#20001)
literals("this","this",#20101)
#20102=@"var;{this};{#20000}"
variables(#20102,"this",#20000)
bind(#20101,#20102)
#20102=*
exprs(#20102,0,#20099,1,"props")
hasLocation(#20102,#20043)
enclosingStmt(#20102,#20077)
exprContainers(#20102,#20001)
literals("props","props",#20102)
#20103=*
exprs(#20103,0,#20099,1,"props")
hasLocation(#20103,#20043)
exprs(#20103,0,#20097,1,"icon")
hasLocation(#20103,#20047)
enclosingStmt(#20103,#20077)
exprContainers(#20103,#20001)
literals("props","props",#20103)
literals("icon","icon",#20103)
#20104=*
exprs(#20104,0,#20097,1,"icon")
hasLocation(#20104,#20047)
exprs(#20104,4,#20079,-4,"")
#20105=@"loc,{#10000},3,3,3,2"
locations_default(#20105,#10000,3,3,3,2)
hasLocation(#20104,#20105)
enclosingStmt(#20104,#20077)
exprContainers(#20104,#20001)
literals("icon","icon",#20104)
#20105=*
exprs(#20105,4,#20079,-4,"")
#20106=@"loc,{#10000},3,3,3,2"
locations_default(#20106,#10000,3,3,3,2)
hasLocation(#20105,#20106)
enclosingStmt(#20105,#20077)
exprContainers(#20105,#20001)
literals("
","",#20105)
#20107=*
regexpterm(#20107,14,#20105,0,"
","",#20104)
#20106=*
regexpterm(#20106,14,#20104,0,"
")
#20108=@"loc,{#10000},3,4,3,6"
locations_default(#20108,#10000,3,4,3,6)
hasLocation(#20107,#20108)
regexpConstValue(#20107,"
#20107=@"loc,{#10000},3,4,3,6"
locations_default(#20107,#10000,3,4,3,6)
hasLocation(#20106,#20107)
regexpConstValue(#20106,"
")
#20109=*
exprs(#20109,89,#20079,-5,"<name-with-dashes/>")
#20110=@"loc,{#10000},3,3,3,21"
locations_default(#20110,#10000,3,3,3,21)
hasLocation(#20109,#20110)
enclosingStmt(#20109,#20077)
exprContainers(#20109,#20001)
#20111=*
exprs(#20111,0,#20109,-1,"name-with-dashes")
#20112=@"loc,{#10000},3,4,3,19"
locations_default(#20112,#10000,3,4,3,19)
hasLocation(#20111,#20112)
enclosingStmt(#20111,#20077)
exprContainers(#20111,#20001)
literals("name-with-dashes","name-with-dashes",#20111)
#20113=*
exprs(#20113,4,#20079,-6,"")
#20114=@"loc,{#10000},4,1,4,0"
locations_default(#20114,#10000,4,1,4,0)
hasLocation(#20113,#20114)
enclosingStmt(#20113,#20077)
exprContainers(#20113,#20001)
#20108=*
exprs(#20108,89,#20079,-5,"<name-with-dashes/>")
#20109=@"loc,{#10000},3,3,3,21"
locations_default(#20109,#10000,3,3,3,21)
hasLocation(#20108,#20109)
enclosingStmt(#20108,#20077)
exprContainers(#20108,#20001)
#20110=*
exprs(#20110,0,#20108,-1,"name-with-dashes")
#20111=@"loc,{#10000},3,4,3,19"
locations_default(#20111,#10000,3,4,3,19)
hasLocation(#20110,#20111)
enclosingStmt(#20110,#20077)
exprContainers(#20110,#20001)
literals("name-with-dashes","name-with-dashes",#20110)
#20112=*
exprs(#20112,4,#20079,-6,"")
#20113=@"loc,{#10000},4,1,4,0"
locations_default(#20113,#10000,4,1,4,0)
hasLocation(#20112,#20113)
enclosingStmt(#20112,#20077)
exprContainers(#20112,#20001)
literals("
","",#20113)
#20115=*
regexpterm(#20115,14,#20113,0,"
","",#20112)
#20114=*
regexpterm(#20114,14,#20112,0,"
")
#20116=@"loc,{#10000},4,2,4,2"
locations_default(#20116,#10000,4,2,4,2)
hasLocation(#20115,#20116)
regexpConstValue(#20115,"
#20115=@"loc,{#10000},4,2,4,2"
locations_default(#20115,#10000,4,2,4,2)
hasLocation(#20114,#20115)
regexpConstValue(#20114,"
")
#20117=*
entry_cfg_node(#20117,#20001)
#20118=@"loc,{#10000},1,1,1,0"
locations_default(#20118,#10000,1,1,1,0)
hasLocation(#20117,#20118)
#20119=*
exit_cfg_node(#20119,#20001)
hasLocation(#20119,#20075)
#20116=*
entry_cfg_node(#20116,#20001)
#20117=@"loc,{#10000},1,1,1,0"
locations_default(#20117,#10000,1,1,1,0)
hasLocation(#20116,#20117)
#20118=*
exit_cfg_node(#20118,#20001)
hasLocation(#20118,#20075)
successor(#20077,#20080)
successor(#20113,#20079)
successor(#20111,#20109)
successor(#20109,#20113)
successor(#20105,#20111)
successor(#20104,#20097)
successor(#20103,#20099)
successor(#20101,#20103)
successor(#20099,#20104)
successor(#20112,#20079)
successor(#20110,#20108)
successor(#20108,#20112)
successor(#20104,#20110)
successor(#20103,#20097)
successor(#20102,#20099)
successor(#20101,#20102)
successor(#20099,#20103)
successor(#20097,#20095)
successor(#20095,#20105)
successor(#20095,#20104)
successor(#20091,#20101)
successor(#20089,#20086)
successor(#20088,#20089)
@@ -397,7 +393,7 @@ successor(#20084,#20081)
successor(#20083,#20084)
successor(#20081,#20088)
successor(#20080,#20083)
successor(#20079,#20119)
successor(#20117,#20077)
successor(#20079,#20118)
successor(#20116,#20077)
numlines(#10000,4,4,0)
filetype(#10000,"typescript")