Remove PatternGuard class

This commit is contained in:
Arthur Baars
2021-11-25 13:43:48 +01:00
parent dca1e34cd8
commit 8b0bc677f4
3 changed files with 41 additions and 94 deletions

View File

@@ -539,15 +539,36 @@ class InClause extends Expr, TInClause {
final CasePattern getPattern() { toGenerated(result) = g.getPattern() }
/**
* Gets the pattern guard in this case-in expression. In the
* following example, the pattern guard is `x > 10`.
* Gets the pattern guard condition in this case-in expression. In the
* following example, the pattern guard condition is `x > 10`.
* ```rb
* case foo
* in [ x ] if x > 10 then ...
* in [ x ] unless x < 10 then ...
* end
* ```
*/
final Expr getCondition() { toGenerated(result) = g.getGuard().getAFieldOrChild() }
/**
* Holds if the pattern guard in this case-in expression is an `if` condition. For example:
* ```rb
* case foo
* in [ x ] if x > 10 then ...
* end
* ```
*/
final PatternGuard getPatternGuard() { toGenerated(result) = g.getGuard() }
predicate hasIfCondition() { g.getGuard() instanceof Ruby::IfGuard }
/**
* Holds if the pattern guard in this case-in expression is an `unless` condition. For example:
* ```rb
* case foo
* in [ x ] unless x < 10 then ...
* end
* ```
*/
predicate hasUnlessCondition() { g.getGuard() instanceof Ruby::UnlessGuard }
final override string toString() { result = "in ... then ..." }
@@ -558,75 +579,10 @@ class InClause extends Expr, TInClause {
or
pred = "getPattern" and result = this.getPattern()
or
pred = "getGuard" and result = this.getPatternGuard()
}
}
/**
* A guard used in pattern matching. For example:
* ```rb
* in pattern if guard
* in pattern unless guard
* ```
*/
class PatternGuard extends AstNode, TPatternGuard {
/**
* Gets the condition expression. For example, the result is `foo` in the
* following:
* ```rb
* if foo
* unless foo
* ```
*/
Expr getCondition() { none() }
override AstNode getAChild(string pred) {
result = super.getAChild(pred)
or
pred = "getCondition" and result = this.getCondition()
}
}
/**
* An `if` pattern guard. For example:
* ```rb
* case foo
* in [ bar] if bar > 10
* end
* ```
*/
class IfGuard extends PatternGuard, TIfGuard {
private Ruby::IfGuard g;
IfGuard() { this = TIfGuard(g) }
final override Expr getCondition() { toGenerated(result) = g.getCondition() }
final override string getAPrimaryQlClass() { result = "IfGuard" }
final override string toString() { result = "if ..." }
}
/**
* An `unless` pattern guard. For example:
* ```rb
* case foo
* in [ bar] unless bar > 10
* end
* ```
*/
class UnlessGuard extends PatternGuard, TUnlessGuard {
private Ruby::UnlessGuard g;
UnlessGuard() { this = TUnlessGuard(g) }
final override Expr getCondition() { toGenerated(result) = g.getCondition() }
final override string getAPrimaryQlClass() { result = "UnlessGuard" }
final override string toString() { result = "unless ..." }
}
/**
* A loop. That is, a `for` loop, a `while` or `until` loop, or their
* expression-modifier variants.

View File

@@ -161,7 +161,6 @@ private module Cached {
THereDoc(Ruby::HeredocBeginning g) or
TIdentifierMethodCall(Ruby::Identifier g) { isIdentifierMethodCall(g) } or
TIf(Ruby::If g) or
TIfGuard(Ruby::IfGuard g) or
TIfModifierExpr(Ruby::IfModifier g) or
TInClause(Ruby::InClause g) or
TInstanceVariableAccessReal(Ruby::InstanceVariable g, AST::InstanceVariable v) {
@@ -305,7 +304,6 @@ private module Cached {
TUnaryPlusExpr(Ruby::Unary g) { g instanceof @ruby_unary_plus } or
TUndefStmt(Ruby::Undef g) or
TUnlessExpr(Ruby::Unless g) or
TUnlessGuard(Ruby::UnlessGuard g) or
TUnlessModifierExpr(Ruby::UnlessModifier g) or
TUntilExpr(Ruby::Until g) or
TUntilModifierExpr(Ruby::UntilModifier g) or
@@ -332,15 +330,15 @@ private module Cached {
TForwardArgument or TGEExpr or TGTExpr or TGlobalVariableAccessReal or
THashKeySymbolLiteral or THashLiteral or THashPattern or THashSplatExpr or
THashSplatNilParameter or THashSplatParameter or THereDoc or TIdentifierMethodCall or TIf or
TIfGuard or TIfModifierExpr or TInClause or TInstanceVariableAccessReal or
TIntegerLiteralReal or TKeywordParameter or TLEExpr or TLShiftExprReal or TLTExpr or
TLambda or TLeftAssignmentList or TLine or TLocalVariableAccessReal or
TLogicalAndExprReal or TLogicalOrExprReal or TMethod or TModuleDeclaration or
TModuloExprReal or TMulExprReal or TNEExpr or TNextStmt or TNilLiteral or
TNoRegExpMatchExpr or TNotExpr or TOptionalParameter or TPair or TParenthesizedExpr or
TRShiftExprReal or TRangeLiteralReal or TRationalLiteral or TRedoStmt or TRegExpLiteral or
TRegExpMatchExpr or TRegularArrayLiteral or TRegularMethodCall or TRegularStringLiteral or
TRegularSuperCall or TRescueClause or TRescueModifierExpr or TRetryStmt or TReturnStmt or
TIfModifierExpr or TInClause or TInstanceVariableAccessReal or TIntegerLiteralReal or
TKeywordParameter or TLEExpr or TLShiftExprReal or TLTExpr or TLambda or
TLeftAssignmentList or TLine or TLocalVariableAccessReal or TLogicalAndExprReal or
TLogicalOrExprReal or TMethod or TModuleDeclaration or TModuloExprReal or TMulExprReal or
TNEExpr or TNextStmt or TNilLiteral or TNoRegExpMatchExpr or TNotExpr or
TOptionalParameter or TPair or TParenthesizedExpr or TRShiftExprReal or TRangeLiteralReal or
TRationalLiteral or TRedoStmt or TRegExpLiteral or TRegExpMatchExpr or
TRegularArrayLiteral or TRegularMethodCall or TRegularStringLiteral or TRegularSuperCall or
TRescueClause or TRescueModifierExpr or TRetryStmt or TReturnStmt or
TScopeResolutionConstantAccess or TScopeResolutionMethodCall or TSelfReal or
TSimpleParameterReal or TSimpleSymbolLiteral or TSingletonClass or TSingletonMethod or
TSpaceshipExpr or TSplatExprReal or TSplatParameter or TStringArrayLiteral or
@@ -348,9 +346,8 @@ private module Cached {
TStringTextComponent or TSubExprReal or TSubshellLiteral or TSymbolArrayLiteral or
TTernaryIfExpr or TThen or TTokenConstantAccess or TTokenMethodName or TTokenSuperCall or
TToplevel or TTrueLiteral or TTuplePatternParameter or TUnaryMinusExpr or TUnaryPlusExpr or
TUndefStmt or TUnlessExpr or TUnlessGuard or TUnlessModifierExpr or TUntilExpr or
TUntilModifierExpr or TVariableReferencePattern or TWhenExpr or TWhileExpr or
TWhileModifierExpr or TYieldCall;
TUndefStmt or TUnlessExpr or TUnlessModifierExpr or TUntilExpr or TUntilModifierExpr or
TVariableReferencePattern or TWhenExpr or TWhileExpr or TWhileModifierExpr or TYieldCall;
class TAstNodeSynth =
TAddExprSynth or TAssignExprSynth or TBitwiseAndExprSynth or TBitwiseOrExprSynth or
@@ -441,7 +438,6 @@ private module Cached {
n = THashSplatParameter(result) or
n = THereDoc(result) or
n = TIdentifierMethodCall(result) or
n = TIfGuard(result) or
n = TIfModifierExpr(result) or
n = TIf(result) or
n = TInClause(result) or
@@ -513,7 +509,6 @@ private module Cached {
n = TUnaryPlusExpr(result) or
n = TUndefStmt(result) or
n = TUnlessExpr(result) or
n = TUnlessGuard(result) or
n = TUnlessModifierExpr(result) or
n = TUntilExpr(result) or
n = TUntilModifierExpr(result) or
@@ -642,8 +637,6 @@ class TControlExpr = TConditionalExpr or TCaseExpr or TCaseMatch or TLoop;
class TConditionalExpr =
TIfExpr or TUnlessExpr or TIfModifierExpr or TUnlessModifierExpr or TTernaryIfExpr;
class TPatternGuard = TIfGuard or TUnlessGuard;
class TIfExpr = TIf or TElsif;
class TConditionalLoop = TWhileExpr or TUntilExpr or TWhileModifierExpr or TUntilModifierExpr;

View File

@@ -733,18 +733,16 @@ control/cases.rb:
# 31| getReceiver: [Self, SelfVariableAccess] self
# 32| getBranch/getInClause: [InClause] in ... then ...
# 32| getPattern: [LocalVariableAccess] x
# 32| getGuard: [UnlessGuard] unless ...
# 32| getCondition: [LTExpr] ... < ...
# 32| getAnOperand/getLeftOperand/getLesserOperand/getReceiver: [LocalVariableAccess] x
# 32| getAnOperand/getArgument/getGreaterOperand/getRightOperand: [IntegerLiteral] 0
# 32| getCondition: [LTExpr] ... < ...
# 32| getAnOperand/getLeftOperand/getLesserOperand/getReceiver: [LocalVariableAccess] x
# 32| getAnOperand/getArgument/getGreaterOperand/getRightOperand: [IntegerLiteral] 0
# 32| getBody: [StmtSequence] then ...
# 33| getStmt: [BooleanLiteral] true
# 34| getBranch/getInClause: [InClause] in ... then ...
# 34| getPattern: [LocalVariableAccess] x
# 34| getGuard: [IfGuard] if ...
# 34| getCondition: [LTExpr] ... < ...
# 34| getAnOperand/getLeftOperand/getLesserOperand/getReceiver: [LocalVariableAccess] x
# 34| getAnOperand/getArgument/getGreaterOperand/getRightOperand: [IntegerLiteral] 0
# 34| getCondition: [LTExpr] ... < ...
# 34| getAnOperand/getLeftOperand/getLesserOperand/getReceiver: [LocalVariableAccess] x
# 34| getAnOperand/getArgument/getGreaterOperand/getRightOperand: [IntegerLiteral] 0
# 34| getBody: [StmtSequence] then ...
# 35| getStmt: [BooleanLiteral] true
# 36| getBranch/getElseBranch: [StmtSequence] else ...