Skip to content

Commit

Permalink
[ruby] Handle Splatting Argument in Call (#4525)
Browse files Browse the repository at this point in the history
Handles splatting argument when given to a call.

Resolves #4437
  • Loading branch information
DavidBakerEffendi authored May 2, 2024
1 parent bdfe025 commit 3d4c58a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ object AntlrContextHelpers {
operatorExpressions ++ associations ++ splatting ++ block
case ctx: AssociationsArgumentListContext =>
Option(ctx.associationList()).map(_.associations).getOrElse(List.empty)
case ctx: SplattingArgumentArgumentListContext =>
Option(ctx.splattingArgument()).toList
case ctx =>
logger.warn(s"Unsupported element type ${ctx.getClass.getSimpleName}")
List()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.joern.rubysrc2cpg.querying

import io.joern.rubysrc2cpg.passes.Defines.RubyOperators
import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture
import io.joern.x2cpg.Defines
import io.shiftleft.codepropertygraph.generated.{DispatchTypes, Operators}
Expand Down Expand Up @@ -181,4 +182,19 @@ class CallTests extends RubyCode2CpgFixture {
}
}

"splatting argument for a call should be a single argument" in {
val cpg = code("""
|args = [1, 2]
|foo(*args)
|""".stripMargin)

inside(cpg.call("foo").argument.l) {
case _ :: (args: Call) :: Nil =>
args.methodFullName shouldBe RubyOperators.splat
args.code shouldBe "*args"
args.lineNumber shouldBe Some(3)
case xs => fail(s"Expected a single `*args` argument under `foo`, got [${xs.code.mkString(",")}]")
}
}

}

0 comments on commit 3d4c58a

Please sign in to comment.