Skip to content

Commit

Permalink
Added implicit return for SingletonClass
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiDreyer committed Dec 3, 2024
1 parent 3200b19 commit b7ce714
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
val nilReturnSpan = node.span.spanStart("return nil")
val nilReturnLiteral = StaticLiteral(Defines.NilClass)(nilReturnSpan)
astsForFieldDeclarations(node) ++ astsForImplicitReturnStatement(nilReturnLiteral)
case node: SingletonClassDeclaration =>
astForAnonymousTypeDeclaration(node)
val nilReturnSpan = node.span.spanStart("return nil")
val nilReturnLiteral = StaticLiteral(Defines.NilClass)(nilReturnSpan)
astsForImplicitReturnStatement(nilReturnLiteral)
case node =>
logger.warn(
s" not supported yet: ${node.text} (${node.getClass.getSimpleName}), only generating statement (${this.relativeFileName})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1243,4 +1243,31 @@ class ClassTests extends RubyCode2CpgFixture {
case xs => fail(s"Expected 5 methods, got [${xs.code.mkString(",")}]")
}
}

"Implicit return of SingletonClassDeclaration" in {
val cpg = code("""
|module Taskbar::List
|
| included do
| class << self
| def trigger_list_update(user, app)
| end
| end
| end
|end
|""".stripMargin)
inside(cpg.typeDecl.name("List").l) {
case listTypeDecl :: Nil =>
val List(lambdaMethod) = listTypeDecl.astChildren.isMethod.isLambda.l

val List(lambdaReturn) = lambdaMethod.astChildren.isBlock.astChildren.isReturn.l
lambdaReturn.code shouldBe "return nil"

val List(lambdaTypeDecl, lambdaTypeDeclClass) = lambdaMethod.astChildren.isTypeDecl.l
lambdaTypeDecl.name shouldBe "<anon-class-0>"
lambdaTypeDeclClass.name shouldBe "<anon-class-0><class>"

case xs => fail(s"expected 1 type, got ${xs.size}: [${xs.code.mkString(", ")}]")
}
}
}

0 comments on commit b7ce714

Please sign in to comment.