Skip to content

Commit b7ce714

Browse files
committed
Added implicit return for SingletonClass
1 parent 3200b19 commit b7ce714

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

joern-cli/frontends/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/astcreation/AstForStatementsCreator.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
261261
val nilReturnSpan = node.span.spanStart("return nil")
262262
val nilReturnLiteral = StaticLiteral(Defines.NilClass)(nilReturnSpan)
263263
astsForFieldDeclarations(node) ++ astsForImplicitReturnStatement(nilReturnLiteral)
264+
case node: SingletonClassDeclaration =>
265+
astForAnonymousTypeDeclaration(node)
266+
val nilReturnSpan = node.span.spanStart("return nil")
267+
val nilReturnLiteral = StaticLiteral(Defines.NilClass)(nilReturnSpan)
268+
astsForImplicitReturnStatement(nilReturnLiteral)
264269
case node =>
265270
logger.warn(
266271
s" not supported yet: ${node.text} (${node.getClass.getSimpleName}), only generating statement (${this.relativeFileName})"

joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/querying/ClassTests.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,4 +1243,31 @@ class ClassTests extends RubyCode2CpgFixture {
12431243
case xs => fail(s"Expected 5 methods, got [${xs.code.mkString(",")}]")
12441244
}
12451245
}
1246+
1247+
"Implicit return of SingletonClassDeclaration" in {
1248+
val cpg = code("""
1249+
|module Taskbar::List
1250+
|
1251+
| included do
1252+
| class << self
1253+
| def trigger_list_update(user, app)
1254+
| end
1255+
| end
1256+
| end
1257+
|end
1258+
|""".stripMargin)
1259+
inside(cpg.typeDecl.name("List").l) {
1260+
case listTypeDecl :: Nil =>
1261+
val List(lambdaMethod) = listTypeDecl.astChildren.isMethod.isLambda.l
1262+
1263+
val List(lambdaReturn) = lambdaMethod.astChildren.isBlock.astChildren.isReturn.l
1264+
lambdaReturn.code shouldBe "return nil"
1265+
1266+
val List(lambdaTypeDecl, lambdaTypeDeclClass) = lambdaMethod.astChildren.isTypeDecl.l
1267+
lambdaTypeDecl.name shouldBe "<anon-class-0>"
1268+
lambdaTypeDeclClass.name shouldBe "<anon-class-0><class>"
1269+
1270+
case xs => fail(s"expected 1 type, got ${xs.size}: [${xs.code.mkString(", ")}]")
1271+
}
1272+
}
12461273
}

0 commit comments

Comments
 (0)