Skip to content

Commit

Permalink
Accept default handling for MemberAccess and IndexAccess on assoc key (
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiDreyer authored Nov 12, 2024
1 parent b75354e commit df29596
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
case keyIdentifier: SimpleIdentifier => setArgumentName(value, keyIdentifier.text)
case symbol @ StaticLiteral(typ) if typ == getBuiltInType(Defines.Symbol) =>
setArgumentName(value, symbol.text.stripPrefix(":"))
case _: (LiteralExpr | RubyCall | ProcOrLambdaExpr) => astForExpression(assoc)
case _: (LiteralExpr | RubyCall | ProcOrLambdaExpr | MemberAccess | IndexAccess) => astForExpression(assoc)
case x =>
logger.warn(s"Not explicitly handled argument association key of type ${x.getClass.getSimpleName}")
astForExpression(assoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,48 @@ class CallTests extends RubyCode2CpgFixture(withPostProcessing = true) {
case xs => fail(s"Expected 6 parameters for call, got [${xs.code.mkString(", ")}]")
}
}

"Call with association IndexAccess key" in {
val cpg = code("""
|foo(bar[:baz] => nil)
|""".stripMargin)

inside(cpg.call.name("foo").argument.l) {
case _ :: (assocParam: Call) :: Nil =>
assocParam.methodFullName shouldBe RubyOperators.association
assocParam.code shouldBe "bar[:baz] => nil"

inside(assocParam.argument.l) {
case (lhs: Call) :: (rhs: Literal) :: Nil =>
lhs.methodFullName shouldBe Operators.indexAccess
lhs.code shouldBe "bar[:baz]"

rhs.code shouldBe "nil"
case xs => fail(s"Expected lhs and rhs for association, got [${xs.code.mkString(",")}]")
}
case xs => fail(s"Expected two params, got [${xs.code.mkString(",")}]")
}
}

"Call with association MemberAccess key" in {
val cpg = code("""
|foo(bar.baz => nil)
|""".stripMargin)

inside(cpg.call.name("foo").argument.l) {
case _ :: (assocParam: Call) :: Nil =>
assocParam.methodFullName shouldBe RubyOperators.association
assocParam.code shouldBe "bar.baz => nil"

inside(assocParam.argument.l) {
case (lhs: Call) :: (rhs: Literal) :: Nil =>
lhs.methodFullName shouldBe Operators.fieldAccess
lhs.code shouldBe "bar.baz"

rhs.code shouldBe "nil"
case xs => fail(s"Expected lhs and rhs for association, got [${xs.code.mkString(",")}]")
}
case xs => fail(s"Expected two params, got [${xs.code.mkString(",")}]")
}
}
}

0 comments on commit df29596

Please sign in to comment.