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

Skip to content

Commit 16a2a60

Browse files
committed
JS: Add AngularPipeRef
1 parent 928a382 commit 16a2a60

9 files changed

Lines changed: 79 additions & 3 deletions

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
import com.semmle.js.ast.AngularPipeRef;
67
import com.semmle.js.ast.CallExpression;
78
import com.semmle.js.ast.Expression;
9+
import com.semmle.js.ast.Identifier;
810
import com.semmle.js.ast.Position;
911
import com.semmle.js.ast.SourceLocation;
1012

@@ -38,7 +40,10 @@ protected Expression buildBinary(
3840
arguments.add(parseExprOp(arg, argStartPos, argStartLocation, TokenType.plusMin.binop, true));
3941
}
4042
SourceLocation loc = new SourceLocation(startLoc);
41-
return this.finishNode(new CallExpression(loc, right, null, arguments, false, false));
43+
if (right instanceof Identifier) {
44+
right = new AngularPipeRef(right.getLoc(), (Identifier)right);
45+
}
46+
return this.finishNode(new CallExpression(loc, right, null, arguments, false, false));
4247
}
4348
return super.buildBinary(startPos, startLoc, left, right, op, logical);
4449
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.semmle.js.ast;
2+
3+
/**
4+
* An identifier occurring as the right operand of an Angular pipe expression,
5+
* which has been desugared to a function call with this expression as the callee.
6+
* <p>
7+
* For example, <code>x | f:y</code> is desugared to <code>f(x, y)</code> where the
8+
* <code>f</code> is an instance of {@link AngularPipeRef}, and evaluates to the function
9+
* being invoked.
10+
*/
11+
public class AngularPipeRef extends Expression {
12+
private final Identifier identifier;
13+
14+
public AngularPipeRef(SourceLocation loc, Identifier identifier) {
15+
super("AngularPipeRef", loc);
16+
this.identifier = identifier;
17+
}
18+
19+
@Override
20+
public <Q, A> A accept(Visitor<Q, A> v, Q q) {
21+
return v.visit(this, q);
22+
}
23+
24+
public Identifier getIdentifier() {
25+
return identifier;
26+
}
27+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public R visit(IFunction nd, C c) {
9393
else return visit((Expression) nd, c);
9494
}
9595

96+
@Override
97+
public R visit(AngularPipeRef nd, C c) {
98+
return visit((Expression) nd, c);
99+
}
100+
96101
@Override
97102
public R visit(AssignmentExpression nd, C c) {
98103
return visit((Expression) nd, c);

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

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

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
import com.semmle.js.ast.jsx.JSXAttribute;
47
import com.semmle.js.ast.jsx.JSXClosingElement;
58
import com.semmle.js.ast.jsx.JSXElement;
@@ -48,8 +51,6 @@
4851
import com.semmle.ts.ast.UnaryTypeExpr;
4952
import com.semmle.ts.ast.UnionTypeExpr;
5053
import com.semmle.util.data.IntList;
51-
import java.util.ArrayList;
52-
import java.util.List;
5354

5455
/** Deep cloning of AST nodes. */
5556
public class NodeCopier implements Visitor<Void, INode> {
@@ -77,6 +78,11 @@ private IntList copy(IntList list) {
7778
return new IntList(list);
7879
}
7980

81+
@Override
82+
public INode visit(AngularPipeRef nd, Void q) {
83+
return new AngularPipeRef(nd.getLoc(), copy(nd.getIdentifier()));
84+
}
85+
8086
@Override
8187
public AssignmentExpression visit(AssignmentExpression nd, Void q) {
8288
return new AssignmentExpression(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
* <p>Visit methods take a context argument of type {@link C} and return a result of type {@link R}.
5555
*/
5656
public interface Visitor<C, R> {
57+
public R visit(AngularPipeRef nd, C q);
58+
5759
public R visit(AssignmentExpression nd, C q);
5860

5961
public R visit(AssignmentPattern nd, C q);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.semmle.js.ast.AClass;
1010
import com.semmle.js.ast.AFunction;
1111
import com.semmle.js.ast.AFunctionExpression;
12+
import com.semmle.js.ast.AngularPipeRef;
1213
import com.semmle.js.ast.ArrayExpression;
1314
import com.semmle.js.ast.ArrayPattern;
1415
import com.semmle.js.ast.ArrowFunctionExpression;
@@ -2126,6 +2127,13 @@ public Label visit(AssignmentPattern nd, Context c) {
21262127
new ParseError("Unexpected assignment pattern.", nd.getLoc().getStart()));
21272128
return super.visit(nd, c);
21282129
}
2130+
2131+
@Override
2132+
public Label visit(AngularPipeRef nd, Context c) {
2133+
Label key = super.visit(nd, c);
2134+
visit(nd.getIdentifier(), key, 0, IdContext.label);
2135+
return key;
2136+
}
21292137
}
21302138

21312139
public List<ParseError> extract(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public class ExprKinds {
148148
exprKinds.put("BindExpression", 97);
149149
exprKinds.put("ExternalModuleReference", 98);
150150
exprKinds.put("NonNullAssertion", 105);
151+
exprKinds.put("AngularPipeRef", 119);
151152
}
152153

153154
private static final Map<IdContext, Integer> idKinds =

javascript/ql/src/semmle/javascript/frameworks/Angular2.qll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,25 @@ module Angular2 {
218218
private class DomAdapterLocation extends DOM::LocationSource::Range {
219219
DomAdapterLocation() { this = domAdapter().getAMethodCall("getLocation") }
220220
}
221+
222+
/**
223+
* A reference to a pipe function, occurring in an Angular pipe expression
224+
* that has been desugared to a function call.
225+
*
226+
* For example, the expression `x | f: y` is desugared to `f(x, y)` where
227+
* `f` is a `PipeRefExpr`.
228+
*/
229+
class PipeRefExpr extends Expr, @angular_pipe_ref {
230+
/** Gets the identifier node naming the pipe. */
231+
Identifier getIdentifier() {
232+
result = getChildExpr(0)
233+
}
234+
235+
/** Gets the name of the pipe being referenced. */
236+
string getName() { result = getIdentifier().getName() }
237+
238+
override string getAPrimaryQlClass() {
239+
result = "Angular2::PipeRefExpr"
240+
}
241+
}
221242
}

javascript/ql/src/semmlecode.javascript.dbscheme

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ case @expr.kind of
354354
| 116 = @assignlogandexpr
355355
| 117 = @assignlogorexpr
356356
| 118 = @assignnullishcoalescingexpr
357+
| 119 = @angular_pipe_ref
357358
;
358359

359360
@varaccess = @proper_varaccess | @export_varaccess;

0 commit comments

Comments
 (0)