Skip to content

Commit

Permalink
[rubysrc2cpg] Fixed Duplicate Do-Blocks Due to Side-Effect (joernio#3720
Browse files Browse the repository at this point in the history
)

Removed adding do block functions from chained invocations to the block method map to reduce duplicate method creation.
  • Loading branch information
DavidBakerEffendi committed Oct 5, 2023
1 parent fc471ab commit 3f0e745
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,6 @@ class AstCreator(
columnEnd(ctx).head
)

blockMethodAsts.foreach { ast =>
ast.root match
case Some(_: NewMethod) => blockMethods.addOne(ast)
case _ =>
}

blockMethodAsts :+ blockMethodAsts
.flatMap(_.nodes)
.collectFirst { case methodRefNode: NewMethodRef =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,17 @@ class DoBlockTest extends RubyCode2CpgFixture {
}
}

"a do-block function wrapped within a chained invocation inside of a call argument" should {
val cpg = code("OpenStruct.new(obj.map { |key, val| [key, to_recursive_ostruct(val)] }.to_h)")

"create a do-block method named from the surrounding function" in {
val mapMethod :: _ = cpg.method.name("map.*").l: @unchecked
mapMethod.name should startWith("map")
mapMethod.parameter.size shouldBe 2
val k :: v :: _ = mapMethod.parameter.l: @unchecked
k.name shouldBe "key"
v.name shouldBe "val"
}
}

}

0 comments on commit 3f0e745

Please sign in to comment.