From 9a71bdc9939feadfc79dca454d4a5a6e0af345f1 Mon Sep 17 00:00:00 2001 From: Nick Rolfe Date: Thu, 7 Jan 2021 17:48:51 +0000 Subject: [PATCH] Improvements from feedback on `case`/`when` classes. --- ql/src/codeql_ruby/ast/Control.qll | 16 ++++++++++++---- ql/src/codeql_ruby/ast/internal/Control.qll | 4 ---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/ql/src/codeql_ruby/ast/Control.qll b/ql/src/codeql_ruby/ast/Control.qll index 727e48eb678..26a8cefb90b 100644 --- a/ql/src/codeql_ruby/ast/Control.qll +++ b/ql/src/codeql_ruby/ast/Control.qll @@ -265,10 +265,10 @@ class CaseExpr extends ControlExpr, @case__ { final Expr getABranch() { result = this.getBranch(_) } /** Gets a `when` branch of this case expression. */ - final WhenExpr getAWhenBranch() { result = range.getAWhenBranch() } + final WhenExpr getAWhenBranch() { result = getABranch() } /** Gets the `else` branch of this case expression, if any. */ - final ExprSequence getElseBranch() { result = range.getElseBranch() } + final ExprSequence getElseBranch() { result = getABranch() } /** * Gets the number of branches of this case expression. @@ -280,7 +280,7 @@ class CaseExpr extends ControlExpr, @case__ { * A `when` branch of a `case` expression. * ```rb * case - * when a>b then x + * when a > b then x * end * ``` */ @@ -295,7 +295,15 @@ class WhenExpr extends Expr, @when { final ExprSequence getBody() { result = range.getBody() } /** - * Gets the `n`th pattern (or condition) in this case-when expression. + * Gets the `n`th pattern (or condition) in this case-when expression. In the + * following example, the 0th pattern is `x`, the 1st pattern is `y`, and the + * 2nd pattern is `z`. + * ```rb + * case foo + * when x, y, z + * puts 'x/y/z' + * end + * ``` */ final Expr getPattern(int n) { result = range.getPattern(n) } diff --git a/ql/src/codeql_ruby/ast/internal/Control.qll b/ql/src/codeql_ruby/ast/internal/Control.qll index 61ae48c4043..5b79884c860 100644 --- a/ql/src/codeql_ruby/ast/internal/Control.qll +++ b/ql/src/codeql_ruby/ast/internal/Control.qll @@ -126,10 +126,6 @@ module CaseExpr { final Expr getValue() { result = generated.getValue() } final Expr getBranch(int n) { result = generated.getChild(n) } - - final WhenExpr getAWhenBranch() { result = this.getBranch(_) } - - final ExprSequence getElseBranch() { result = this.getBranch(_) } } }