Skip to content

Commit cfc32d1

Browse files
review comments
1 parent c92bbf4 commit cfc32d1

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/astcreation/AstForTypesCreator.scala

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,9 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
368368
fullName: String,
369369
returnType: String
370370
): Ast = {
371-
val staticModifier = NewModifier().modifierType(ModifierTypes.STATIC)
372-
val blockNode = NewBlock().typeFullName(Defines.Any)
371+
val modifiers =
372+
List(NewModifier().modifierType(ModifierTypes.STATIC), NewModifier().modifierType(ModifierTypes.CONSTRUCTOR))
373+
val blockNode = NewBlock().typeFullName(Defines.Any)
373374
val methodNode_ = methodNode(node, io.joern.x2cpg.Defines.StaticInitMethodName, fullName, "", parserResult.filename)
374375

375376
methodAstParentStack.push(methodNode_)
@@ -390,7 +391,7 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
390391
scope.popScope()
391392

392393
val methodReturn = methodReturnNode(node, returnType)
393-
methodAst(methodNode_, Nil, body, methodReturn, List(staticModifier))
394+
methodAst(methodNode_, Nil, body, methodReturn, modifiers)
394395
}
395396

396397
private def staticEnumInitMethodAst(
@@ -400,8 +401,9 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
400401
fullName: String,
401402
returnType: String
402403
): Ast = {
403-
val staticModifier = NewModifier().modifierType(ModifierTypes.STATIC)
404-
val blockNode = NewBlock().typeFullName(Defines.Any)
404+
val modifiers =
405+
List(NewModifier().modifierType(ModifierTypes.STATIC), NewModifier().modifierType(ModifierTypes.CONSTRUCTOR))
406+
val blockNode = NewBlock().typeFullName(Defines.Any)
405407
val methodNode_ = methodNode(node, io.joern.x2cpg.Defines.StaticInitMethodName, fullName, "", parserResult.filename)
406408

407409
methodAstParentStack.push(methodNode_)
@@ -424,7 +426,7 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
424426
scope.popScope()
425427

426428
val methodReturn = methodReturnNode(node, returnType)
427-
methodAst(methodNode_, Nil, body, methodReturn, List(staticModifier))
429+
methodAst(methodNode_, Nil, body, methodReturn, modifiers)
428430
}
429431

430432
protected def astForClass(clazz: BabelNodeInfo, shouldCreateAssignmentCall: Boolean = false): Ast = {
@@ -528,12 +530,12 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
528530
clazz.columnNumber
529531
)
530532

531-
val classAnnotationAst = createClassAnnotationAst(clazz, classIdNode)
532-
val propertyAnnotationAsts = createPropertyAnnotationAsts(clazz, classIdNode)
533-
val methodAnnotationAsts = createMethodAnnotationAsts(clazz, classIdNode)
534-
if (classAnnotationAst.root.isDefined || propertyAnnotationAsts.nonEmpty || methodAnnotationAsts.nonEmpty) {
533+
val classDecorationAst = createClassDecorationAst(clazz, classIdNode)
534+
val propertyDecorationAsts = createPropertyDecorationAsts(clazz, classIdNode)
535+
val methodDecorationAsts = createMethodDecorationAsts(clazz, classIdNode)
536+
if (classDecorationAst.root.isDefined || propertyDecorationAsts.nonEmpty || methodDecorationAsts.nonEmpty) {
535537
val blockNode_ = blockNode(clazz)
536-
val childrenAsts = List(assignmentAst, classAnnotationAst) ++ propertyAnnotationAsts ++ methodAnnotationAsts
538+
val childrenAsts = List(assignmentAst, classDecorationAst) ++ propertyDecorationAsts ++ methodDecorationAsts
537539
setArgumentIndices(childrenAsts)
538540
Ast(blockNode_).withChildren(childrenAsts)
539541
} else assignmentAst
@@ -542,7 +544,7 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
542544
}
543545
}
544546

545-
private def createClassAnnotationAst(classNodeInfo: BabelNodeInfo, classIdNode: NewIdentifier): Ast = {
547+
private def createClassDecorationAst(classNodeInfo: BabelNodeInfo, classIdNode: NewIdentifier): Ast = {
546548
val decoratorAsts = decoratorExpressionElements(classNodeInfo).map(e => astForNodeWithFunctionReference(e.json))
547549
if (decoratorAsts.nonEmpty) {
548550
val lhsAst = Ast(
@@ -744,7 +746,7 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
744746
)
745747
}
746748

747-
private def createPropertyAnnotationAsts(classNodeInfo: BabelNodeInfo, classIdNode: NewIdentifier): Seq[Ast] = {
749+
private def createPropertyDecorationAsts(classNodeInfo: BabelNodeInfo, classIdNode: NewIdentifier): Seq[Ast] = {
748750
val tsProperties = classMembers(classNodeInfo).flatMap { member =>
749751
val memberNodeInfo = createBabelNodeInfo(member)
750752
if (
@@ -800,7 +802,7 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
800802
}
801803
}
802804

803-
private def createMethodAnnotationAsts(classNodeInfo: BabelNodeInfo, classIdNode: NewIdentifier): Seq[Ast] = {
805+
private def createMethodDecorationAsts(classNodeInfo: BabelNodeInfo, classIdNode: NewIdentifier): Seq[Ast] = {
804806
val tsMethods = classMembers(classNodeInfo).flatMap { member =>
805807
val memberNodeInfo = createBabelNodeInfo(member)
806808
if (memberNodeInfo.node == ClassMethod) {
@@ -814,7 +816,7 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
814816
val decoratorAsts =
815817
decoratorExpressionElements(tsMethodNodeInfo).map(e => astForNodeWithFunctionReference(e.json))
816818
val (name, fullName) = calcMethodNameAndFullName(tsMethodNodeInfo)
817-
val methodTpe = typeFor(tsMethodNodeInfo)
819+
val methodTpe = typeFor(tsMethodNodeInfo).stripPrefix("__ecma.")
818820
val paramNodeInfos = if (hasKey(tsMethodNodeInfo.json, "parameters")) {
819821
tsMethodNodeInfo.json("parameters").arr.toSeq
820822
} else {

joern-cli/frontends/jssrc2cpg/src/test/scala/io/joern/jssrc2cpg/passes/ast/TsDecoratorAstCreationPassTests.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ class TsDecoratorAstCreationPassTests extends AstJsSrc2CpgSuite(".ts") {
348348
reqBAnnotationPut.name shouldBe "Put"
349349

350350
val List(decorateReqACall, decorateReqBCall) = cpg.call.name("__decorate").l
351-
decorateReqACall.code shouldBe """__decorate([Get("argA"), __param(0, Req("reqAParam")), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:type", __ecma.String)], Foo.prototype, 'reqA', null)"""
352-
decorateReqBCall.code shouldBe """__decorate([Get("argB"),Put("argC"), __param(0, Req("reqBParam1")),__param(1, Req("reqBParam2")), __metadata("design:type", Function), __metadata("design:paramtypes", [Object,Object]), __metadata("design:type", __ecma.Number)], Foo.prototype, 'reqB', null)"""
351+
decorateReqACall.code shouldBe """__decorate([Get("argA"), __param(0, Req("reqAParam")), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:type", String)], Foo.prototype, 'reqA', null)"""
352+
decorateReqBCall.code shouldBe """__decorate([Get("argB"),Put("argC"), __param(0, Req("reqBParam1")),__param(1, Req("reqBParam2")), __metadata("design:type", Function), __metadata("design:paramtypes", [Object,Object]), __metadata("design:type", Number)], Foo.prototype, 'reqB', null)"""
353353

354354
val List(recReqA) = decorateReqACall.receiver.isIdentifier.l
355355
recReqA.name shouldBe "__decorate"
@@ -409,9 +409,9 @@ class TsDecoratorAstCreationPassTests extends AstJsSrc2CpgSuite(".ts") {
409409
"__ecma.Array.factory()",
410410
"_tmp_0.push(Object)",
411411
"_tmp_0.push",
412-
"""_tmp_1.push(__metadata("design:type", __ecma.String))""",
412+
"""_tmp_1.push(__metadata("design:type", String))""",
413413
"_tmp_1.push",
414-
"""__metadata("design:type", __ecma.String)"""
414+
"""__metadata("design:type", String)"""
415415
)
416416

417417
decorateReqBCall.arguments(1).ast.isCall.code.l shouldBe List(
@@ -443,9 +443,9 @@ class TsDecoratorAstCreationPassTests extends AstJsSrc2CpgSuite(".ts") {
443443
"_tmp_2.push",
444444
"_tmp_2.push(Object)",
445445
"_tmp_2.push",
446-
"""_tmp_3.push(__metadata("design:type", __ecma.Number))""",
446+
"""_tmp_3.push(__metadata("design:type", Number))""",
447447
"_tmp_3.push",
448-
"""__metadata("design:type", __ecma.Number)"""
448+
"""__metadata("design:type", Number)"""
449449
)
450450
}
451451
}

0 commit comments

Comments
 (0)