Skip to content

Commit

Permalink
Using Block Scope fix (#109)
Browse files Browse the repository at this point in the history
* fix methodFullName inside using block

* code refactoring

* Try handling change

* added curly braces
  • Loading branch information
ankit-privado authored Oct 10, 2024
1 parent 148ed9e commit 2811110
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,13 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
.code(code(usingStmt))
.lineNumber(line(usingStmt))
.columnNumber(column(usingStmt))
val declAst = Try(createDotNetNodeInfo(usingStmt.json(ParserKeys.Declaration))) match {
case Success(declNodevalue) => astForNode(declNodevalue)
case _ => Seq.empty[Ast]
}

val tryNodeInfo = createDotNetNodeInfo(usingStmt.json(ParserKeys.Statement))
val tryAst = astForBlock(tryNodeInfo, Option("try"))
val declNode = createDotNetNodeInfo(usingStmt.json(ParserKeys.Declaration))
val declAst = astForNode(declNode)

val finallyAst = declAst.flatMap(_.nodes).collectFirst { case x: NewIdentifier => x.copy }.map { id =>
val callCode = s"${id.name}.Dispose()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,37 @@ class ControlStructureTests extends CSharpCode2CpgFixture {

}

"having using statement" should {
val cpg = code("""
|namespace other
|{
| public class General
| {
| public static void Call(string name)
| {
| using (SqlConnection connection = new SqlConnection(name))
| {
| try
| {
| connection.Open();
| }
| catch (Exception ex)
| {
| Console.WriteLine(ex.Message);
| connection.Close();
| }
| }
| }
| }
|}
|""".stripMargin)

"resolve methodFullName" in {
inside(cpg.call.name("Open").methodFullName.l) {
case x :: Nil => x shouldBe "SqlConnection.Open:<unresolvedSignature>"
case _ => fail("Unexpected call node structure")
}
}
}

}

0 comments on commit 2811110

Please sign in to comment.