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

Skip to content

Commit c08ba14

Browse files
committed
JS: Add new SourceType for angular templates
1 parent b1d45a6 commit c08ba14

4 files changed

Lines changed: 40 additions & 8 deletions

File tree

javascript/extractor/src/com/semmle/jcorn/AngularExpressionParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* Parser for Angular template expressions, based on the JS parser with
1515
* modified handling of the pipe operator.
1616
*/
17-
public class AngularExpressionParser extends Parser {
18-
public AngularExpressionParser(String input, int startPos) {
19-
super(new Options(), input, startPos);
17+
public class AngularExpressionParser extends CustomParser {
18+
public AngularExpressionParser(Options options, String input, int startPos) {
19+
super(options, input, startPos);
2020
}
2121

2222
@Override

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
import java.util.Set;
1111

1212
import com.semmle.extractor.html.HtmlPopulator;
13+
import com.semmle.jcorn.AngularExpressionParser;
14+
import com.semmle.jcorn.CustomParser;
15+
import com.semmle.jcorn.Options;
16+
import com.semmle.jcorn.Parser;
1317
import com.semmle.js.parser.JcornWrapper;
1418
import com.semmle.util.data.StringUtil;
1519
import com.semmle.util.exception.UserError;
@@ -78,6 +82,9 @@ public static enum SourceType {
7882
/** A CommonJS module that is not also an ES2015 module. */
7983
COMMONJS_MODULE,
8084

85+
/** An Angular template expression. */
86+
ANGULAR_TEMPLATE,
87+
8188
/** Automatically determined source type. */
8289
AUTO;
8390

@@ -86,6 +93,18 @@ public String toString() {
8693
return StringUtil.lc(name());
8794
}
8895

96+
/**
97+
* Gets the parser to use for parsing this source type.
98+
*/
99+
public Parser createParser(Options options, String input, int startPos) {
100+
switch (this) {
101+
case ANGULAR_TEMPLATE:
102+
return new AngularExpressionParser(options, input, startPos);
103+
default:
104+
return new CustomParser(options, input, startPos);
105+
}
106+
}
107+
89108
/**
90109
* Returns true if this source is executed directly in the global scope, or false if it has its
91110
* own local scope.

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void handleElement(Element elt) {
9393

9494
String source = attr.getValue();
9595
RowColumnVector valueStart = attr.getValueSegment().getRowColumnVector();
96-
if (JS_ATTRIBUTE.matcher(attr.getName()).matches() || isAngularTemplateAttributeName(attr.getName())) {
96+
if (JS_ATTRIBUTE.matcher(attr.getName()).matches()) {
9797
snippetLoC =
9898
extractSnippet(
9999
TopLevelKind.eventHandler,
@@ -104,6 +104,19 @@ public void handleElement(Element elt) {
104104
valueStart.getRow(),
105105
valueStart.getColumn(),
106106
false /* isTypeScript */);
107+
} else if (isAngularTemplateAttributeName(attr.getName())) {
108+
// For an attribute *ngFor="let var of EXPR", start parsing at EXPR
109+
int offset = attr.getName().equals("*ngFor") ? source.indexOf(" of ") + " of ".length() : 0;
110+
snippetLoC =
111+
extractSnippet(
112+
TopLevelKind.eventHandler,
113+
config.withSourceType(SourceType.ANGULAR_TEMPLATE),
114+
scopeManager,
115+
textualExtractor,
116+
source,
117+
valueStart.getRow(),
118+
valueStart.getColumn() + offset,
119+
false /* isTypeScript */);
107120
} else if (source.startsWith("javascript:")) {
108121
source = source.substring(11);
109122
snippetLoC =

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.semmle.js.parser;
22

3-
import com.semmle.jcorn.CustomParser;
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
46
import com.semmle.jcorn.Options;
57
import com.semmle.jcorn.SyntaxError;
68
import com.semmle.jcorn.jsx.JSXOptions;
@@ -10,8 +12,6 @@
1012
import com.semmle.js.extractor.ExtractorConfig;
1113
import com.semmle.js.extractor.ExtractorConfig.ECMAVersion;
1214
import com.semmle.js.extractor.ExtractorConfig.SourceType;
13-
import java.util.ArrayList;
14-
import java.util.List;
1515

1616
public class JcornWrapper {
1717
/** Parse source code as a program. */
@@ -41,7 +41,7 @@ public static JSParser.Result parse(
4141
if (config.isTolerateParseErrors())
4242
options.onRecoverableError((err) -> errors.add(mkParseError(err)));
4343

44-
program = new CustomParser(options, source, 0).parse();
44+
program = sourceType.createParser(options, source, 0).parse();
4545
} catch (SyntaxError e) {
4646
errors.add(mkParseError(e));
4747
}

0 commit comments

Comments
 (0)