Skip to content

Commit 8fe2d84

Browse files
committed
JS: Move template-related classes to Templating file
1 parent f26e94c commit 8fe2d84

3 files changed

Lines changed: 57 additions & 51 deletions

File tree

javascript/ql/src/semmle/javascript/dataflow/Sources.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ module SourceNode {
320320
astNode instanceof ImportSpecifier or
321321
astNode instanceof ImportMetaExpr or
322322
astNode instanceof TaggedTemplateExpr or
323-
astNode instanceof Angular2::PipeRefExpr or
324-
astNode instanceof Angular2::TemplateVarRefExpr or
323+
astNode instanceof Templating::PipeRefExpr or
324+
astNode instanceof Templating::TemplateVarRefExpr or
325325
astNode instanceof StringLiteral
326326
)
327327
or

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

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -230,46 +230,11 @@ module Angular2 {
230230
DomAdapterLocation() { this = domAdapter().getAMethodCall("getLocation") }
231231
}
232232

233-
/**
234-
* A reference to a pipe function, occurring in an Angular pipe expression
235-
* that has been desugared to a function call.
236-
*
237-
* For example, the expression `x | f: y` is desugared to `f(x, y)` where
238-
* `f` is a `PipeRefExpr`.
239-
*/
240-
class PipeRefExpr extends Expr, @template_pipe_ref {
241-
/** Gets the identifier node naming the pipe. */
242-
Identifier getIdentifier() { result = getChildExpr(0) }
243-
244-
/** Gets the name of the pipe being referenced. */
245-
string getName() { result = getIdentifier().getName() }
246-
247-
override string getAPrimaryQlClass() { result = "Angular2::PipeRefExpr" }
248-
}
233+
class PipeRefExpr = Templating::PipeRefExpr;
249234

250-
/**
251-
* A reference to a variable in a template expression, corresponding
252-
* to a property on the component class.
253-
*/
254-
class TemplateVarRefExpr extends Expr {
255-
TemplateVarRefExpr() { this = any(TemplateTopLevel tl).getScope().getAVariable().getAnAccess() }
256-
}
235+
class TemplateVarRefExpr = Templating::TemplateVarRefExpr;
257236

258-
/** The top-level containing an Angular expression. */
259-
class TemplateTopLevel extends TopLevel, @template_toplevel {
260-
/** Gets the expression in this top-level. */
261-
Expr getExpression() { result = getChildStmt(0).(ExprStmt).getExpr() }
262-
263-
/** Gets the data flow node representing the initialization of the given variable in this scope. */
264-
DataFlow::Node getVariableInit(string name) {
265-
result = DataFlow::ssaDefinitionNode(SSA::implicitInit(getScope().getVariable(name)))
266-
}
267-
268-
/** Gets a data flow node corresponding to a use of the given template variable within this top-level. */
269-
DataFlow::SourceNode getAVariableUse(string name) {
270-
result = getScope().getVariable(name).getAnAccess().flow()
271-
}
272-
}
237+
class TemplateTopLevel = Templating::TemplateTopLevel;
273238

274239
/** The RHS of a `templateUrl` property, seen as a path expression. */
275240
private class TemplateUrlPath extends PathExpr {
@@ -493,19 +458,10 @@ module Angular2 {
493458
}
494459
}
495460

496-
/**
497-
* Gets an invocation of the pipe of the given name.
498-
*
499-
* For example, the call generated from `items | async` would be found by `getAPipeCall("async")`.
500-
*/
501-
DataFlow::CallNode getAPipeCall(string name) {
502-
result.getCalleeNode().asExpr().(PipeRefExpr).getName() = name
503-
}
504-
505461
private class BuiltinPipeStep extends TaintTracking::SharedTaintStep {
506462
override predicate step(DataFlow::Node pred, DataFlow::Node succ) {
507463
exists(DataFlow::CallNode call, string name |
508-
call = getAPipeCall(name) and
464+
call = Templating::getAPipeCall(name) and
509465
succ = call
510466
|
511467
exists(int i | pred = call.getArgument(i) |

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,56 @@ module Templating {
6565
DataFlow::TemplatePlaceholderTagNode asDataFlowNode() { result.getTag() = this }
6666

6767
/** Gets the top-level containing the template expression to be inserted at this placeholder. */
68-
Angular2::TemplateTopLevel getInnerTopLevel() { toplevel_parent_xml_node(result, this) }
68+
TemplateTopLevel getInnerTopLevel() { toplevel_parent_xml_node(result, this) }
69+
}
70+
71+
/**
72+
* A reference to a pipe function, occurring in a pipe expression
73+
* that has been desugared to a function call.
74+
*
75+
* For example, the expression `x | f: y` is desugared to `f(x, y)` where
76+
* `f` is a `PipeRefExpr`.
77+
*/
78+
class PipeRefExpr extends Expr, @template_pipe_ref {
79+
/** Gets the identifier node naming the pipe. */
80+
Identifier getIdentifier() { result = getChildExpr(0) }
81+
82+
/** Gets the name of the pipe being referenced. */
83+
string getName() { result = getIdentifier().getName() }
84+
85+
override string getAPrimaryQlClass() { result = "Templating::PipeRefExpr" }
86+
}
87+
88+
/**
89+
* Gets an invocation of the pipe of the given name.
90+
*
91+
* For example, the call generated from `items | async` would be found by `getAPipeCall("async")`.
92+
*/
93+
DataFlow::CallNode getAPipeCall(string name) {
94+
result.getCalleeNode().asExpr().(PipeRefExpr).getName() = name
95+
}
96+
97+
/**
98+
* A reference to a variable in a template expression, corresponding
99+
* to a value plugged into the template.
100+
*/
101+
class TemplateVarRefExpr extends Expr {
102+
TemplateVarRefExpr() { this = any(TemplateTopLevel tl).getScope().getAVariable().getAnAccess() }
103+
}
104+
105+
/** The top-level containing the expression in a template placeholder. */
106+
class TemplateTopLevel extends TopLevel, @template_toplevel {
107+
/** Gets the expression in this top-level. */
108+
Expr getExpression() { result = getChildStmt(0).(ExprStmt).getExpr() }
109+
110+
/** Gets the data flow node representing the initialization of the given variable in this scope. */
111+
DataFlow::Node getVariableInit(string name) {
112+
result = DataFlow::ssaDefinitionNode(SSA::implicitInit(getScope().getVariable(name)))
113+
}
114+
115+
/** Gets a data flow node corresponding to a use of the given template variable within this top-level. */
116+
DataFlow::SourceNode getAVariableUse(string name) {
117+
result = getScope().getVariable(name).getAnAccess().flow()
118+
}
69119
}
70120
}

0 commit comments

Comments
 (0)