Skip to content

Commit

Permalink
[jssrc2cpg] Fix for dynamicTypeHintFullName on this identifier (#4439)
Browse files Browse the repository at this point in the history
Follow-up for: #4426
  • Loading branch information
max-leuthaeuser authored Apr 9, 2024
1 parent 76c5d3c commit a966ed7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
base.node match {
case ThisExpression =>
val receiverAst = astForNodeWithFunctionReference(callee.json)
val baseNode = identifierNode(base, base.code)
.dynamicTypeHintFullName(this.rootTypeDecl.map(_.fullName).toSeq)
val baseNode = identifierNode(base, base.code).dynamicTypeHintFullName(typeHintForThisExpression())
scope.addVariableReference(base.code, baseNode)
(receiverAst, baseNode, member.code)
case Identifier =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ trait TypeHelper { this: AstCreator =>
private val ImportMatcher = Pattern.compile("(typeof )?import\\([\"'](.*)[\"']\\)")

private val TypeReplacements = Map(
" any" -> s" ${Defines.Any}",
" unknown" -> s" ${Defines.Unknown}",
" number" -> s" ${Defines.Number}",
" null" -> s" ${Defines.Null}",
" string" -> s" ${Defines.String}",
" boolean" -> s" ${Defines.Boolean}",
" bigint" -> s" ${Defines.BigInt}",
"{}" -> Defines.Object,
"typeof " -> ""
" any" -> s" ${Defines.Any}",
" unknown" -> s" ${Defines.Unknown}",
" void" -> s" ${Defines.Void}",
" never" -> s" ${Defines.Never}",
" undefined" -> s" ${Defines.Undefined}",
" number" -> s" ${Defines.Number}",
" null" -> s" ${Defines.Null}",
" string" -> s" ${Defines.String}",
" boolean" -> s" ${Defines.Boolean}",
" bigint" -> s" ${Defines.BigInt}",
"{}" -> Defines.Object,
"typeof " -> ""
)

protected def isPlainTypeAlias(alias: BabelNodeInfo): Boolean = if (hasKey(alias.json, "right")) {
Expand Down Expand Up @@ -134,8 +137,8 @@ trait TypeHelper { this: AstCreator =>
case Some(tpe) => Seq(tpe)
case None if node.isDefined =>
typeFor(node.get) match {
case t if t != Defines.Any && t != "this" => Seq(t)
case _ => rootTypeDecl.map(_.fullName).toSeq
case tpe if tpe != Defines.Any && tpe != "this" => if (Defines.isBuiltinType(tpe)) Seq(tpe) else Seq.empty
case _ => rootTypeDecl.map(_.fullName).toSeq
}
case None => rootTypeDecl.map(_.fullName).toSeq
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ object Defines {
val Math: String = "__ecma.Math"
val Symbol: String = "__ecma.Symbol"
val Console: String = "__whatwg.console"
val Object: String = "object"
val BigInt: String = "bigint"
val Unknown: String = "unknown"
val Void: String = "void"
val Never: String = "never"
val Undefined: String = "undefined"
val Object: String = "__ecma.Object"
val BigInt: String = "__ecma.BigInt"
val Unknown: String = Any
val Void: String = Any
val Never: String = Any
val Undefined: String = Any
val NodeModulesFolder: String = "node_modules"
val GlobalNamespace: String = NamespaceTraversal.globalNamespaceName

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TSTypesTests extends AstJsSrc2CpgSuite {
"have correct types for empty method with rest parameter" in {
val cpg = code("function method(x, ...args) {}").withConfig(Config().withTsTypes(true))
val List(method) = cpg.method.nameExact("method").l
method.methodReturn.typeFullName shouldBe "void"
method.methodReturn.typeFullName shouldBe Defines.Any

val List(t, x, args) = method.parameter.l
t.index shouldBe 0
Expand Down Expand Up @@ -57,7 +57,7 @@ class TSTypesTests extends AstJsSrc2CpgSuite {
"have correct types for empty method" in {
val cpg = code("function method(x) {}").withConfig(Config().withTsTypes(true))
val List(method) = cpg.method.nameExact("method").l
method.methodReturn.typeFullName shouldBe "void"
method.methodReturn.typeFullName shouldBe Defines.Any
}

"have types for identifiers with type inference" in {
Expand Down

0 comments on commit a966ed7

Please sign in to comment.