Skip to content

Commit

Permalink
[ruby] Bug Fix - Generate Implicit Return for IndexAccess (joernio#4229)
Browse files Browse the repository at this point in the history
* [ruby] Added IndexAccess to ImplicitReturnStatement

* [ruby] Fixed ArgumentIndex for IndexAccess
  • Loading branch information
AndreiDreyer authored Feb 26, 2024
1 parent 7daaad1 commit 5584a64
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
val indexAsts = node.indices.map(astForExpression)
val targetAst = astForExpression(node.target)
val call = callNode(node, code(node), Operators.indexAccess, Operators.indexAccess, DispatchTypes.STATIC_DISPATCH)
callAst(call, indexAsts, Some(targetAst))

callAst(call, targetAst +: indexAsts)
}

protected def astForObjectInstantiation(node: ObjectInstantiation): Ast = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
private def astsForImplicitReturnStatement(node: RubyNode): List[Ast] = {
node match
case _: (ArrayLiteral | HashLiteral | StaticLiteral | BinaryExpression | UnaryExpression | SimpleIdentifier |
IfExpression | RescueExpression | SimpleCall) =>
IfExpression | RescueExpression | SimpleCall | IndexAccess) =>
astForReturnStatement(ReturnExpression(List(node))(node.span)) :: Nil
case node: SingleAssignment =>
astForSingleAssignment(node) :: List(astForReturnStatement(ReturnExpression(List(node.lhs))(node.span)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,27 @@ class MethodReturnTests extends RubyCode2CpgFixture {
c.methodFullName shouldBe Operators.arrayInitializer
}

"implicit RETURN node for index access exists" in {
val cpg = code("""
|def f
| l = [1, 2, 3]
| l[1]
|end
|""".stripMargin)

val List(f) = cpg.method.name("f").l
val List(r: Return) = f.methodReturn.cfgIn.l: @unchecked

r.code shouldBe "l[1]"
r.lineNumber shouldBe Some(4)

val List(c: Call) = r.astChildren.isCall.l
c.methodFullName shouldBe Operators.indexAccess
val List(arg1, arg2) = c.argument.l
arg1.argumentIndex shouldBe 1
arg2.argumentIndex shouldBe 2
}

"implicit RETURN node for `{x:0}` exists" in {
val cpg = code("""
|def f = {x:0}
Expand Down

0 comments on commit 5584a64

Please sign in to comment.