@@ -14,15 +14,13 @@ public struct AddInit: MemberMacro {
14
14
let ( parameters, body) = initBodyAndParams ( for: declaration)
15
15
let bodyExpr : ExprSyntax = " \( raw: body. joined ( separator: " \n " ) ) "
16
16
var parametersLiteral = " init( \( parameters. joined ( separator: " , " ) ) ) "
17
- if let modifiers = declaration. modifiers {
18
- parametersLiteral = " \( modifiers) \( parametersLiteral) "
19
- }
20
- let initDecl = try InitializerDeclSyntax ( PartialSyntaxNodeString ( stringLiteral: parametersLiteral) ,
17
+ parametersLiteral = " \( declaration. modifiers) \( parametersLiteral) "
18
+ let initDecl = try InitializerDeclSyntax ( SyntaxNodeString ( stringLiteral: parametersLiteral) ,
21
19
bodyBuilder: { bodyExpr } )
22
20
var result = [ DeclSyntax ( initDecl) ]
23
- if node. argument ( for: " withMock " ) ? . as ( BooleanLiteralExprSyntax . self) ? . booleanLiteral . tokenKind. keyword == . true {
21
+ if node. argument ( for: " withMock " ) ? . as ( BooleanLiteralExprSyntax . self) ? . literal . tokenKind. keyword == . true {
24
22
let randomValue = node. argument ( for: " randomMockValue " ) ? . as ( BooleanLiteralExprSyntax . self) ?
25
- . booleanLiteral . tokenKind. keyword != . false
23
+ . literal . tokenKind. keyword != . false
26
24
result. append ( mock ( basedOn: declaration, randomValue: randomValue) )
27
25
}
28
26
return result
@@ -45,16 +43,16 @@ public struct AddInit: MemberMacro {
45
43
parameter += " = nil "
46
44
}
47
45
parameters. append ( parameter)
48
- body. append ( " self. \( identifier) = \( identifier) " )
46
+ body. append ( " self.\( identifier) = \( identifier) " )
49
47
}
50
48
}
51
49
return ( params: parameters, body: body)
52
50
}
53
51
54
52
private static func mock( basedOn declaration: DeclGroupSyntax , randomValue: Bool ) -> DeclSyntax {
55
- let identifier = ( declaration as? StructDeclSyntax ) ? . identifier . text
56
- ?? ( declaration as? ClassDeclSyntax ) ? . identifier . text
57
- ?? ( declaration as? ActorDeclSyntax ) ? . identifier . text ?? " "
53
+ let identifier = ( declaration as? StructDeclSyntax ) ? . name . text
54
+ ?? ( declaration as? ClassDeclSyntax ) ? . name . text
55
+ ?? ( declaration as? ActorDeclSyntax ) ? . name . text ?? " "
58
56
let parameters = declaration. memberBlock. members. compactMap { member -> String ? in
59
57
guard let patternBinding = member. decl. as ( VariableDeclSyntax . self) ? . bindings
60
58
. as ( PatternBindingListSyntax . self) ? . first? . as ( PatternBindingSyntax . self) ,
@@ -66,23 +64,21 @@ public struct AddInit: MemberMacro {
66
64
return " \( identifier) : \( mockValue) "
67
65
}
68
66
var varDelcaration : DeclSyntax = " static let mock = \( raw: identifier) ( \( raw: parameters. joined ( separator: " , " ) ) ) "
69
- if let modifiers = declaration. modifiers {
70
- varDelcaration = " \( modifiers) varDelcaration "
71
- }
67
+ varDelcaration = " \( declaration. modifiers) \( varDelcaration) "
72
68
varDelcaration = " #if DEBUG \n \( varDelcaration) \n #endif "
73
69
return varDelcaration
74
70
}
75
71
}
76
72
77
73
extension AttributeSyntax {
78
74
func argument( for label: String ) -> ExprSyntax ? {
79
- argument ? . as ( TupleExprElementListSyntax . self) ? . filter ( { $0. label? . text == label } ) . first? . expression
75
+ arguments ? . as ( LabeledExprListSyntax . self) ? . filter ( { $0. label? . text == label } ) . first? . expression
80
76
}
81
77
}
82
78
83
- extension SimpleTypeIdentifierSyntax {
79
+ extension IdentifierTypeSyntax {
84
80
func mockValue( randomValue: Bool ) -> String ? {
85
- guard let type = self . as ( SimpleTypeIdentifierSyntax . self) ? . name. text else { return nil }
81
+ guard let type = self . as ( IdentifierTypeSyntax . self) ? . name. text else { return nil }
86
82
if let fun = mockFunctions [ type] {
87
83
return fun ( randomValue)
88
84
} else if name. text == " Void " {
@@ -107,30 +103,30 @@ extension OptionalTypeSyntax {
107
103
108
104
extension FunctionTypeSyntax {
109
105
func mockValue( randomValue: Bool ) -> String ? {
110
- let args = repeatElement ( " _ " , count: max ( arguments . count, 1 ) ) . joined ( separator: " , " )
111
- let returnValue = output . returnType . mockValue ( randomValue: randomValue) ?? " "
106
+ let args = repeatElement ( " _ " , count: max ( parameters . count, 1 ) ) . joined ( separator: " , " )
107
+ let returnValue = returnClause . type . mockValue ( randomValue: randomValue) ?? " "
112
108
return " { \( args) in return \( returnValue) } "
113
109
}
114
110
}
115
111
116
112
extension TypeSyntax {
117
113
func mockValue( randomValue: Bool ) -> String ? {
118
- if let mockValue = self . as ( SimpleTypeIdentifierSyntax . self) ? . mockValue ( randomValue: randomValue) {
114
+ if let mockValue = self . as ( IdentifierTypeSyntax . self) ? . mockValue ( randomValue: randomValue) {
119
115
return mockValue
120
116
} else if let type = self . as ( DictionaryTypeSyntax . self) {
121
- let mockKeyValue = type. keyType . mockValue ( randomValue: randomValue) ?? " "
122
- let mockValueValue = type. valueType . mockValue ( randomValue: randomValue) ?? " nil "
117
+ let mockKeyValue = type. key . mockValue ( randomValue: randomValue) ?? " "
118
+ let mockValueValue = type. value . mockValue ( randomValue: randomValue) ?? " nil "
123
119
return " [ \( mockKeyValue) : \( mockValueValue) ] "
124
120
} else if let mockValue = self . as ( FunctionTypeSyntax . self) ? . mockValue ( randomValue: randomValue) {
125
121
return mockValue
126
122
} else if let mockValue = self . as ( TupleTypeSyntax . self) ? . elements. first? . type
127
123
. as ( FunctionTypeSyntax . self) ? . mockValue ( randomValue: randomValue) {
128
124
return mockValue
129
- } else if let type = self . as ( ArrayTypeSyntax . self) ? . elementType {
125
+ } else if let type = self . as ( ArrayTypeSyntax . self) ? . element {
130
126
return " [ " + ( type. mockValue ( randomValue: randomValue) ?? " nil " ) + " ] "
131
- } else if let type = self . as ( SimpleTypeIdentifierSyntax . self) ,
127
+ } else if let type = self . as ( IdentifierTypeSyntax . self) ,
132
128
type. name. text == " Set " ,
133
- let genericType = type. genericArgumentClause? . arguments. first? . argumentType {
129
+ let genericType = type. genericArgumentClause? . arguments. first? . argument {
134
130
return " [ " + ( genericType. mockValue ( randomValue: randomValue) ?? " nil " ) + " ] "
135
131
}
136
132
return nil
0 commit comments