Merge pull request #345 from esben-semmle/js/intro-getUnderlying

Approved by xiemaisi
This commit is contained in:
semmle-qlci
2018-10-30 10:34:00 +00:00
committed by GitHub
18 changed files with 161 additions and 33 deletions

View File

@@ -127,14 +127,32 @@ module SyntacticConstants {
class WrappedConstant extends SyntacticConstant {
WrappedConstant() {
stripParens() instanceof SyntacticConstant or
this.(SeqExpr).getLastOperand() instanceof SyntacticConstant or
this.(TypeAssertion).getExpression() instanceof SyntacticConstant or
this.(Assignment).getRhs() instanceof SyntacticConstant
getUnderlyingValue() instanceof SyntacticConstant
}
}
/**
* Holds if `c` evaluates to `undefined`.
*/
predicate isUndefined(SyntacticConstant c) {
c.getUnderlyingValue() instanceof UndefinedConstant
}
/**
* Holds if `c` evaluates to `null`.
*/
predicate isNull(SyntacticConstant c) {
c.getUnderlyingValue() instanceof NullConstant
}
/**
* Holds if `c` evaluates to `null` or `undefined`.
*/
predicate isNullOrUndefined(SyntacticConstant c) {
isUndefined(c) or isNull(c)
}
}
/**

View File

@@ -45,8 +45,45 @@ class ExprOrType extends @exprortype, Documentable {
)
}
/** Gets this expression or type, with any surrounding parentheses removed. */
/**
* Gets this expression or type, with any surrounding parentheses removed.
*
* Also see `getUnderlyingValue` and `getUnderlyingReference`.
*/
ExprOrType stripParens() { result = this }
/**
* Gets the innermost reference that this expression evaluates to, if any.
*
* Examples:
*
* - a variable or property access: the access itself.
* - a parenthesized expression `(e)`: the underlying reference of `e`.
* - a TypeScript type assertion `e as T`: the underlying reference of `e`.
*
* Also see `getUnderlyingValue` and `stripParens`.
*/
Expr getUnderlyingReference() {
none()
}
/**
* Gets the innermost expression that this expression evaluates to.
*
* Examples:
*
* - a parenthesised expression `(e)`: the underlying value of `e`.
* - a sequence expression `e1, e2`: the underlying value of `e2`.
* - an assignment expression `v = e`: the underlying value of `e`.
* - a TypeScript type assertion `e as T`: the underlying value of `e`.
* - any other expression: the expression itself.
*
* Also see `getUnderlyingReference` and `stripParens`.
*/
Expr getUnderlyingValue() {
result = this
}
}
/** An expression. */
@@ -210,6 +247,15 @@ class ParExpr extends @parexpr, Expr {
override predicate isImpure() {
getExpression().isImpure()
}
override Expr getUnderlyingValue() {
result = getExpression().getUnderlyingValue()
}
override Expr getUnderlyingReference() {
result = getExpression().getUnderlyingReference()
}
}
/** A `null` literal. */
@@ -617,6 +663,11 @@ class SeqExpr extends @seqexpr, Expr {
override string getStringValue() {
result = getLastOperand().getStringValue()
}
override Expr getUnderlyingValue() {
result = getLastOperand().getUnderlyingValue()
}
}
/** A conditional expression. */
@@ -862,6 +913,11 @@ class PropAccess extends @propaccess, Expr {
override ControlFlowNode getFirstControlFlowNode() {
result = getBase().getFirstControlFlowNode()
}
override Expr getUnderlyingReference() {
result = this
}
}
/** A dot expression. */
@@ -1284,10 +1340,17 @@ class Assignment extends @assignment, Expr {
override ControlFlowNode getFirstControlFlowNode() {
result = getLhs().getFirstControlFlowNode()
}
}
/** A simple assignment expression. */
class AssignExpr extends @assignexpr, Assignment {}
class AssignExpr extends @assignexpr, Assignment {
override Expr getUnderlyingValue() {
result = getRhs().getUnderlyingValue()
}
}
/** A compound assign expression. */
abstract class CompoundAssignExpr extends Assignment {}

View File

@@ -1315,6 +1315,15 @@ class TypeAssertion extends Expr, @typeassertion {
override ControlFlowNode getFirstControlFlowNode() {
result = getExpression().getFirstControlFlowNode()
}
override Expr getUnderlyingValue() {
result = getExpression().getUnderlyingValue()
}
override Expr getUnderlyingReference() {
result = getExpression().getUnderlyingReference()
}
}
/**

View File

@@ -298,6 +298,11 @@ class VarRef extends @varref, Identifier, BindingPattern, LexicalRef {
override VarRef getABindingVarRef() { result = this }
override predicate isImpure() { none() }
override Expr getUnderlyingReference() {
result = this
}
}
/** An identifier that refers to a variable in a non-declaring position. */