Repository: joernio/joern (scala)

Issue:
[Bug][rubysrc2cpg] - support for regular expression patter `/^ch_/` missing
**Describe the bug**
support for regular expression patter `/^ch_/`

**To Reproduce**
Sample code snippet to reproduce the error

```
    def find_stripe_object
      case params[:id]
      when /^ch_/
        Stripe::Charge.retrieve(params[:id])
      when /^pi_/
        Stripe::PaymentIntent.retrieve(params[:id]).charges.first
      end
    end
```

Entire code sample - https://github.com/pay-rails/pay/blob/master/test/dummy/app/controllers/stripe/charges/imports_controller.rb

Pull request:
Closes #3173

Patch:
diff --git a/joern-cli/frontends/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/parser/RubyLexerRegexHandling.scala b/joern-cli/frontends/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/parser/RubyLexerRegexHandling.scala
index 51efdb981e13..ce05de2d3309 100644
--- a/joern-cli/frontends/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/parser/RubyLexerRegexHandling.scala
+++ b/joern-cli/frontends/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/parser/RubyLexerRegexHandling.scala
@@ -22,6 +22,8 @@ trait RubyLexerRegexHandling { this: RubyLexerBase =>
     COMMA,
     // When '/' occurs after a ':'.
     COLON,
+    // When '/' occurs after 'when'.
+    WHEN,
     // When '/' occurs after an operator.
     EMARK,
     EMARKEQ,


Test patch:
diff --git a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/RegexTests.scala b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/RegexTests.scala
index 5efd241ed27d..6ac1c431452f 100644
--- a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/RegexTests.scala
+++ b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/RegexTests.scala
@@ -114,6 +114,56 @@ class RegexTests extends RubyParserAbstractTest {
             |    )""".stripMargin
       }
     }
+
+    "used in a `when` clause" should {
+      val code =
+        """case foo
+            |      when /^ch_/
+            |        bar
+            |end""".stripMargin
+
+      "be parsed as such" in {
+        printAst(_.primary(), code) shouldEqual
+          """CaseExpressionPrimary
+              | CaseExpression
+              |  case
+              |  WsOrNl
+              |  ExpressionExpressionOrCommand
+              |   PrimaryExpression
+              |    VariableReferencePrimary
+              |     VariableIdentifierVariableReference
+              |      VariableIdentifier
+              |       foo
+              |  Separators
+              |   Separator
+              |  WhenClause
+              |   when
+              |   WsOrNl
+              |   WhenArgument
+              |    Expressions
+              |     PrimaryExpression
+              |      LiteralPrimary
+              |       RegularExpressionLiteral
+              |        /
+              |        ^ch_
+              |        /
+              |   ThenClause
+              |    Separator
+              |    WsOrNl
+              |    CompoundStatement
+              |     Statements
+              |      ExpressionOrCommandStatement
+              |       ExpressionExpressionOrCommand
+              |        PrimaryExpression
+              |         VariableReferencePrimary
+              |          VariableIdentifierVariableReference
+              |           VariableIdentifier
+              |            bar
+              |     Separators
+              |      Separator
+              |  end""".stripMargin
+      }
+    }
   }
 
   "A non-interpolated regex literal" when {
diff --git a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/RubyLexerTests.scala b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/RubyLexerTests.scala
index 50996e35e362..6b9915928903 100644
--- a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/RubyLexerTests.scala
+++ b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/RubyLexerTests.scala
@@ -316,6 +316,18 @@ class RubyLexerTests extends AnyFlatSpec with Matchers {
     )
   }
 
+  "Non-empty regex literal after `when`" should "be recognized as such" in {
+    val code = "when /^ch_/"
+    tokenize(code) shouldBe Seq(
+      WHEN,
+      WS,
+      REGULAR_EXPRESSION_START,
+      REGULAR_EXPRESSION_BODY,
+      REGULAR_EXPRESSION_END,
+      EOF
+    )
+  }
+
   "Regex literals without metacharacters" should "be recognized as such" in {
     val eg = Seq("/regexp/", "/a regexp/")
     all(eg.map(tokenize)) shouldBe Seq(REGULAR_EXPRESSION_START, REGULAR_EXPRESSION_BODY, REGULAR_EXPRESSION_END, EOF)