File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1010import java .util .Set ;
1111
1212import 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 ;
1317import com .semmle .js .parser .JcornWrapper ;
1418import com .semmle .util .data .StringUtil ;
1519import 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.
Original file line number Diff line number Diff 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 =
Original file line number Diff line number Diff line change 11package com .semmle .js .parser ;
22
3- import com .semmle .jcorn .CustomParser ;
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
46import com .semmle .jcorn .Options ;
57import com .semmle .jcorn .SyntaxError ;
68import com .semmle .jcorn .jsx .JSXOptions ;
1012import com .semmle .js .extractor .ExtractorConfig ;
1113import com .semmle .js .extractor .ExtractorConfig .ECMAVersion ;
1214import com .semmle .js .extractor .ExtractorConfig .SourceType ;
13- import java .util .ArrayList ;
14- import java .util .List ;
1515
1616public 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 }
You can’t perform that action at this time.
0 commit comments