Skip to content

Commit

Permalink
[ruby] Temp Var Call code Fix (#4893)
Browse files Browse the repository at this point in the history
* [ruby] Temp Var Call `code` Fix
Fixed bug where splitting on `.` alone does not create a reasonable code property if parameters are not accounted for.

* Improved code property construction
  • Loading branch information
DavidBakerEffendi authored Sep 3, 2024
1 parent 72bc423 commit 911e15d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,10 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
val dispatchType = if (isStatic) DispatchTypes.STATIC_DISPATCH else DispatchTypes.DYNAMIC_DISPATCH

val callCode = if (baseCode.contains("<tmp-")) {
val rhsCode = if (n.methodName == "new") n.methodName else code(n).replace("::", ".").split('.').last
s"$baseCode.$rhsCode"
val rhsCode =
if (n.methodName == "new") n.methodName
else s"${n.methodName}(${n.arguments.map(code).mkString(", ")})"
s"$baseCode${n.op}$rhsCode"
} else {
code(n)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,17 @@ class CallTests extends RubyCode2CpgFixture(withPostProcessing = true) {
case xs => fail(s"Expected one call for foo, got ${xs.code.mkString}")
}
}

"Calls separated by `tmp` should render correct `code` properties" in {
val cpg = code("""
|User.find_by(auth_token: cookies[:auth_token].to_s)
|""".stripMargin)

cpg.call("find_by").code.head shouldBe "(<tmp-0> = User).find_by(auth_token: cookies[:auth_token].to_s)"
cpg.call(Operators.indexAccess).code.head shouldBe "cookies[:auth_token]"
cpg.fieldAccess
.where(_.fieldIdentifier.canonicalNameExact("@to_s"))
.code
.head shouldBe "(<tmp-1> = cookies[:auth_token]).to_s"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ class ClassTests extends RubyCode2CpgFixture {
case Some(bodyCall) =>
bodyCall.dispatchType shouldBe DispatchTypes.STATIC_DISPATCH
bodyCall.methodFullName shouldBe s"Test0.rb:$Main.Foo.${RubyDefines.TypeDeclBody}"
bodyCall.code shouldBe "(<tmp-0> = self::Foo).<body>"
bodyCall.code shouldBe "(<tmp-0> = self::Foo)::<body>()"
bodyCall.receiver.isEmpty shouldBe true
bodyCall.argument(0).code shouldBe "<tmp-0>"
case None => fail("Expected <body> call")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,9 @@ class MethodTests extends RubyCode2CpgFixture {
inside(cpg.method.name(RDefines.Main).filename("t1.rb").block.astChildren.isCall.l) {
case (a1: Call) :: (a2: Call) :: (a3: Call) :: (a4: Call) :: (a5: Call) :: Nil =>
a1.code shouldBe "self.A = module A (...)"
a2.code shouldBe "(<tmp-0> = self::A).<body>"
a2.code shouldBe "(<tmp-0> = self::A)::<body>()"
a3.code shouldBe "self.B = class B (...)"
a4.code shouldBe "(<tmp-1> = self::B).<body>"
a4.code shouldBe "(<tmp-1> = self::B)::<body>()"
a5.code shouldBe "self.c = def c (...)"
case xs => fail(s"Expected assignments to appear before definitions, instead got [${xs.mkString("\n")}]")
}
Expand Down Expand Up @@ -795,7 +795,7 @@ class MethodTests extends RubyCode2CpgFixture {
inside(cpg.call.name(".*retry!").l) {
case batchCall :: Nil =>
batchCall.name shouldBe "retry!"
batchCall.code shouldBe "(<tmp-0> = batch).retry!()"
batchCall.code shouldBe "(<tmp-0> = batch)::retry!()"

inside(batchCall.receiver.l) {
case (receiverCall: Call) :: Nil =>
Expand Down Expand Up @@ -851,7 +851,7 @@ class MethodTests extends RubyCode2CpgFixture {
inside(cpg.call.name(".*retry!").l) {
case batchCall :: Nil =>
batchCall.name shouldBe "retry!"
batchCall.code shouldBe "(<tmp-0> = retry).retry!()"
batchCall.code shouldBe "(<tmp-0> = retry)::retry!()"

inside(batchCall.receiver.l) {
case (receiverCall: Call) :: Nil =>
Expand Down

0 comments on commit 911e15d

Please sign in to comment.