Repository: joernio/joern (scala)

Issue:
jssrc: "this" is sometimes a literal instead of identifier
```
joern> importCode.jssrc.fromString("""function X() { this.foo = "1"; }""")
val res4: io.shiftleft.codepropertygraph.Cpg = Cpg (Graph [64 nodes])
                                                                                                                                                                                               
joern> cpg.fieldAccess.argument(1).l
val res4: List[io.shiftleft.codepropertygraph.generated.nodes.Expression] = ::(
  head -> Literal(
     = 11L,
     = 1,
     = None,
     = "this",
     = Some(value = 15),
     = ArraySeq("ANY"),
     = Some(value = 1),
     = 1,
     = "ANY"
  ),
  next -> List()
)

joern> cpg.method.name("X").parameter.name("this").l
val res36: 
  List[io.shiftleft.codepropertygraph.generated.nodes.MethodParameterIn] = ::(
  head -> MethodParameterIn(
     = 7L,
     = "this",
     = Some(value = 0),
     = ArraySeq(),
     = "BY_VALUE",
     = 0,
     = false,
     = Some(value = 1),
     = "this",
     = 0,
     = "ANY"
  ),
  next -> List()
)
                                                                                                                                                                                               
joern> cpg.method.name("X").parameter.name("this").referencingIdentifiers.l
val res37: List[io.shiftleft.codepropertygraph.generated.nodes.Identifier] = Nil()

```

We can't track the dataflows between these literals and `this` parameters. I think they should always be identifiers.

Pull request description:
Fixes: https://github.com/joernio/joern/issues/2047

Patch:
diff --git a/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/astcreation/AstForExpressionsCreator.scala b/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/astcreation/AstForExpressionsCreator.scala
index 85ca660aa513..9d0e8e5666ab 100644
--- a/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/astcreation/AstForExpressionsCreator.scala
+++ b/joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/astcreation/AstForExpressionsCreator.scala
@@ -86,15 +86,16 @@ trait AstForExpressionsCreator { this: AstCreator =>
     }
   }
 
-  protected def astForThisExpression(thisExpr: BabelNodeInfo): Ast =
-    Ast(
-      createLiteralNode(
-        thisExpr.code,
-        dynamicInstanceTypeStack.headOption.orElse(Some(Defines.ANY)),
-        thisExpr.lineNumber,
-        thisExpr.columnNumber
-      )
+  protected def astForThisExpression(thisExpr: BabelNodeInfo): Ast = {
+    val thisNode = createIdentifierNode(
+      thisExpr.code,
+      dynamicInstanceTypeStack.headOption.orElse(Some(Defines.ANY)),
+      thisExpr.lineNumber,
+      thisExpr.columnNumber
     )
+    scope.addVariableReference(thisExpr.code, thisNode)
+    Ast(thisNode)
+  }
 
   protected def astForNewExpression(newExpr: BabelNodeInfo): Ast = {
     val callee    = newExpr.json("callee")


Test patch:
diff --git a/joern-cli/frontends/jssrc2cpg/src/test/scala/io/joern/jssrc2cpg/passes/ast/SimpleAstCreationPassTest.scala b/joern-cli/frontends/jssrc2cpg/src/test/scala/io/joern/jssrc2cpg/passes/ast/SimpleAstCreationPassTest.scala
index 257cae112f3b..020dcea10eb6 100644
--- a/joern-cli/frontends/jssrc2cpg/src/test/scala/io/joern/jssrc2cpg/passes/ast/SimpleAstCreationPassTest.scala
+++ b/joern-cli/frontends/jssrc2cpg/src/test/scala/io/joern/jssrc2cpg/passes/ast/SimpleAstCreationPassTest.scala
@@ -515,6 +515,20 @@ class SimpleAstCreationPassTest extends AbstractPassTest {
       lambda2MethodRef.methodFullName shouldBe lambda2FullName
     }
 
+    "be correct for ThisExpression" in AstFixture("function foo() { this.bar = 1 }") { cpg =>
+      val List(thisIdentifier: Identifier) = cpg.fieldAccess.argument(1).l
+      thisIdentifier.name shouldBe "this"
+      thisIdentifier.code shouldBe "this"
+
+      val List(thisParameter: MethodParameterIn) = cpg.method.name("foo").parameter.l
+      thisParameter.name shouldBe "this"
+      thisParameter.code shouldBe "this"
+
+      val referencingIdentifiers =
+        cpg.method.name("foo").parameter.name("this").referencingIdentifiers.l
+      referencingIdentifiers shouldBe cpg.fieldAccess.argument(1).l
+    }
+
     "be correct for call expression" in AstFixture("""
          |function method(x) {
          |  foo(x);