Skip to content

Commit

Permalink
[ruby] fixed double _astIn reference to identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiDreyer committed Oct 2, 2024
1 parent b3e36b6 commit 4b62de6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ trait AstForControlStructuresCreator(implicit withSchemaValidation: ValidationMo
private def astForForExpression(node: ForExpression): Ast = {
val forEachNode = controlStructureNode(node, ControlStructureTypes.FOR, code(node))

val collectionAst = astForExpression(node.iterableVariable) // iterableAst
def collectionAst = astForExpression(node.iterableVariable)
val collectionNode = node.iterableVariable

val iterIdentifier =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,14 +651,43 @@ class ControlStructureTests extends RubyCode2CpgFixture {
}
}

"Something" in {
"ForEach loops" in {
val cpg = code("""
|fibNumbers = [0, 1, 1, 2, 3, 5, 8, 13]
|for num in fibNumbers
| puts num
|end
|""".stripMargin)

cpg.method.isModule.dotAst.l.foreach(println)
inside(cpg.method.isModule.controlStructure.l) {
case forEachNode :: Nil =>
forEachNode.controlStructureType shouldBe ControlStructureTypes.FOR

inside(forEachNode.astChildren.l) {
case (idxLocal: Local) :: (numLocal: Local) :: (initAssign: Call) :: (cond: Call) :: (update: Call) :: (forBlock: Block) :: Nil =>
idxLocal.name shouldBe "_idx_"
idxLocal.typeFullName shouldBe Defines.getBuiltInType(Defines.Integer)

numLocal.name shouldBe "num"

initAssign.code shouldBe "_idx_ = 0"
initAssign.name shouldBe Operators.assignment
initAssign.methodFullName shouldBe Operators.assignment

cond.code shouldBe "_idx_ < fibNumbers.length"
cond.name shouldBe Operators.lessThan
cond.methodFullName shouldBe Operators.lessThan

update.code shouldBe "num = fibNumbers[_idx_++]"
update.name shouldBe Operators.assignment
update.methodFullName shouldBe Operators.assignment

val List(putsCall) = cpg.call.nameExact("puts").l
putsCall.astParent shouldBe forBlock

case xs => fail(s"Expected 6 children for `forEachNode`, got [${xs.code.mkString(",")}]")
}
case xs => fail(s"Expected one node for `forEach` loop, got [${xs.code.mkString(",")}]")
}
}
}

0 comments on commit 4b62de6

Please sign in to comment.