Skip to content

Commit

Permalink
[c#] fix anonymous functions' fullname (#5289)
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierpinho authored Feb 3, 2025
1 parent 320cbda commit 164087d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,12 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
paramTypeHint: Option[String] = None
): Seq[Ast] = {
// Create method declaration
val name = nextClosureName()
val fullName = s"${scope.surroundingScopeFullName.getOrElse(Defines.UnresolvedNamespace)}.$name"
val name = nextClosureName()
val fullName = {
val baseType = withoutSignature(scope.surroundingScopeFullName.getOrElse(Defines.UnresolvedNamespace))
val signature = Defines.UnresolvedSignature
composeMethodFullName(baseType, name, signature)
}
// Set parameter type if necessary, which may require the type hint
val paramType = paramTypeHint.flatMap(AstCreatorHelper.elementTypesFromCollectionType).headOption
val paramAsts = Try(lambdaExpression.json(ParserKeys.Parameter)).toOption match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LambdaTests extends CSharpCode2CpgFixture {
inside(cpg.method("Main").astChildren.collectAll[Method].l) {
case anon :: Nil =>
anon.name shouldBe "<lambda>0"
anon.fullName shouldBe "HelloWorld.Program.Main:System.Void(System.String[]).<lambda>0"
anon.fullName shouldBe "HelloWorld.Program.Main.<lambda>0:<unresolvedSignature>"

inside(anon.parameter.l) {
case x :: Nil =>
Expand All @@ -37,7 +37,7 @@ class LambdaTests extends CSharpCode2CpgFixture {
inside(cpg.method("Main").astChildren.collectAll[TypeDecl].l) {
case anon :: Nil =>
anon.name shouldBe "<lambda>0"
anon.fullName shouldBe "HelloWorld.Program.Main:System.Void(System.String[]).<lambda>0"
anon.fullName shouldBe "HelloWorld.Program.Main.<lambda>0:<unresolvedSignature>"
case xs => fail(s"Expected a single anonymous type declaration, got [${xs.code.mkString(",")}]")
}
}
Expand All @@ -48,7 +48,7 @@ class LambdaTests extends CSharpCode2CpgFixture {
numbers.name shouldBe "numbers"
numbers.typeFullName shouldBe s"${DotNetTypeMap(BuiltinTypes.Int)}[]"

closure.methodFullName shouldBe "HelloWorld.Program.Main:System.Void(System.String[]).<lambda>0"
closure.methodFullName shouldBe "HelloWorld.Program.Main.<lambda>0:<unresolvedSignature>"
closure.referencedMethod.name shouldBe "<lambda>0"
case xs => fail(s"Expected two `Select` call argument, got [${xs.code.mkString(",")}]")
}
Expand All @@ -69,7 +69,7 @@ class LambdaTests extends CSharpCode2CpgFixture {
inside(cpg.method("Main").astChildren.collectAll[Method].l) {
case anon :: Nil =>
anon.name shouldBe "<lambda>0"
anon.fullName shouldBe "HelloWorld.Program.Main:System.Void(System.String[]).<lambda>0"
anon.fullName shouldBe "HelloWorld.Program.Main.<lambda>0:<unresolvedSignature>"

inside(anon.parameter.l) {
case x :: y :: Nil =>
Expand All @@ -91,7 +91,7 @@ class LambdaTests extends CSharpCode2CpgFixture {
inside(cpg.method("Main").astChildren.collectAll[TypeDecl].l) {
case anon :: Nil =>
anon.name shouldBe "<lambda>0"
anon.fullName shouldBe "HelloWorld.Program.Main:System.Void(System.String[]).<lambda>0"
anon.fullName shouldBe "HelloWorld.Program.Main.<lambda>0:<unresolvedSignature>"
case xs => fail(s"Expected a single anonymous type declaration, got [${xs.code.mkString(",")}]")
}
}
Expand All @@ -102,7 +102,7 @@ class LambdaTests extends CSharpCode2CpgFixture {
numbers.name shouldBe "numbers"
numbers.typeFullName shouldBe s"${DotNetTypeMap(BuiltinTypes.Int)}[]"

closure.methodFullName shouldBe "HelloWorld.Program.Main:System.Void(System.String[]).<lambda>0"
closure.methodFullName shouldBe "HelloWorld.Program.Main.<lambda>0:<unresolvedSignature>"
closure.referencedMethod.name shouldBe "<lambda>0"
case xs => fail(s"Expected two `Select` call argument, got [${xs.code.mkString(",")}]")
}
Expand Down

0 comments on commit 164087d

Please sign in to comment.