Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jssrc2cpg] Fix for dynamicTypeHintFullName on this identifier #4439

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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