JS: Extract mapping from HTML node to parent Expression

This commit is contained in:
Asger Feldthaus
2020-12-14 12:32:38 +00:00
parent 3b666a5646
commit f24af58a60
3 changed files with 23 additions and 8 deletions

View File

@@ -1143,7 +1143,7 @@ public class ASTExtractor {
locationManager.emitNodeLocation(nd, propkey); locationManager.emitNodeLocation(nd, propkey);
visitAll(nd.getDecorators(), propkey, IdContext.varBind, -1, -1); visitAll(nd.getDecorators(), propkey, IdContext.varBind, -1, -1);
visit(nd.getKey(), propkey, 0, nd.isComputed() ? IdContext.varBind : IdContext.label); visit(nd.getKey(), propkey, 0, nd.isComputed() ? IdContext.varBind : IdContext.label);
visit(nd.getValue(), propkey, 1, c.idcontext); Label valueLabel = visit(nd.getValue(), propkey, 1, c.idcontext);
visit(nd.getDefaultValue(), propkey, 2, IdContext.varBind); visit(nd.getDefaultValue(), propkey, 2, IdContext.varBind);
if (nd.isComputed()) trapwriter.addTuple("is_computed", propkey); if (nd.isComputed()) trapwriter.addTuple("is_computed", propkey);
if (nd.isMethod()) trapwriter.addTuple("is_method", propkey); if (nd.isMethod()) trapwriter.addTuple("is_method", propkey);
@@ -1151,7 +1151,7 @@ public class ASTExtractor {
// Extract the value of a property named `template` as HTML, in order to support // Extract the value of a property named `template` as HTML, in order to support
// Angular2 components with an inline template. // Angular2 components with an inline template.
if (!nd.isComputed() && "template".equals(tryGetIdentifierName(nd.getKey()))) { if (!nd.isComputed() && "template".equals(tryGetIdentifierName(nd.getKey()))) {
extractStringValueAsHtml(nd.getValue()); extractStringValueAsHtml(nd.getValue(), valueLabel);
} }
return propkey; return propkey;
@@ -1160,7 +1160,7 @@ public class ASTExtractor {
/** /**
* Extracts the string value of <code>expr</code> as an HTML snippet. * Extracts the string value of <code>expr</code> as an HTML snippet.
*/ */
private void extractStringValueAsHtml(Expression expr) { private void extractStringValueAsHtml(Expression expr, Label exprLabel) {
TextualExtractor textualExtractor = lexicalExtractor.getTextualExtractor(); TextualExtractor textualExtractor = lexicalExtractor.getTextualExtractor();
if (textualExtractor.isSnippet()) { if (textualExtractor.isSnippet()) {
return; // do not create nested snippets return; // do not create nested snippets
@@ -1185,7 +1185,11 @@ public class ASTExtractor {
getMetrics(), getMetrics(),
vfile.toFile()); vfile.toFile());
HTMLExtractor html = HTMLExtractor.forEmbeddedHtml(config); HTMLExtractor html = HTMLExtractor.forEmbeddedHtml(config);
html.extract(innerTextualExtractor); List<Label> rootNodes = html.extractEx(innerTextualExtractor).fst();
int rootNodeIndex = 0;
for (Label rootNode : rootNodes) {
trapwriter.addTuple("xml_element_parent_expression", rootNode, exprLabel, rootNodeIndex++);
}
} }
private String tryGetIdentifierName(Expression e) { private String tryGetIdentifierName(Expression e) {

View File

@@ -1,7 +1,9 @@
package com.semmle.js.extractor; package com.semmle.js.extractor;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -180,9 +182,13 @@ public class HTMLExtractor implements IExtractor {
public static HTMLExtractor forEmbeddedHtml(ExtractorConfig config) { public static HTMLExtractor forEmbeddedHtml(ExtractorConfig config) {
return new HTMLExtractor(config, null, true); return new HTMLExtractor(config, null, true);
} }
@Override @Override
public LoCInfo extract(TextualExtractor textualExtractor) { public LoCInfo extract(TextualExtractor textualExtractor) throws IOException {
return extractEx(textualExtractor).snd();
}
public Pair<List<Label>, LoCInfo> extractEx(TextualExtractor textualExtractor) {
// Angular templates contain attribute names that are not valid HTML/XML, such as [foo], (foo), [(foo)], and *foo. // Angular templates contain attribute names that are not valid HTML/XML, such as [foo], (foo), [(foo)], and *foo.
// Allow a large number of errors in attribute names, so the Jericho parser does not give up. // Allow a large number of errors in attribute names, so the Jericho parser does not give up.
Attributes.setDefaultMaxErrorCount(100); Attributes.setDefaultMaxErrorCount(100);
@@ -198,9 +204,9 @@ public class HTMLExtractor implements IExtractor {
extractor.setStartOffset(locationManager.getStartLine() - 1, locationManager.getStartColumn() - 1); extractor.setStartOffset(locationManager.getStartLine() - 1, locationManager.getStartColumn() - 1);
extractor.doit(Option.some(eltHandler)); List<Label> rootNodes = extractor.doit(Option.some(eltHandler));
return locInfo; return Pair.make(rootNodes, locInfo);
} }
/** /**

View File

@@ -136,6 +136,11 @@ toplevel_parent_xml_node(
unique int toplevel: @toplevel ref, unique int toplevel: @toplevel ref,
int xmlnode: @xml_node_with_code ref); int xmlnode: @xml_node_with_code ref);
xml_element_parent_expression(
unique int xmlnode: @xmlelement ref,
int expression: @expr ref,
int index: int ref);
// statements // statements
#keyset[parent, idx] #keyset[parent, idx]
stmts (unique int id: @stmt, stmts (unique int id: @stmt,