10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
+ //==========================================================================//
14
+ // IMPORTANT: The macros defined in this file are intended to test the //
15
+ // behavior of MacroSystem. Many of them do not serve as good examples of //
16
+ // how macros should be written. In particular, they often lack error //
17
+ // handling because it is not needed in the few test cases in which these //
18
+ // macros are invoked. //
19
+ //==========================================================================//
20
+
13
21
import _SwiftSyntaxTestSupport
14
22
import SwiftDiagnostics
15
23
import SwiftParser
@@ -42,17 +50,13 @@ fileprivate struct DeclsFromStringsMacro: DeclarationMacro, MemberMacro {
42
50
private static func decls( from arguments: LabeledExprListSyntax ) -> [ DeclSyntax ] {
43
51
var strings : [ String ] = [ ]
44
52
for arg in arguments {
45
- guard
46
- let value = arg. expression. as ( StringLiteralExprSyntax . self) ? . representedLiteralValue
47
- else {
53
+ guard let value = arg. expression. as ( StringLiteralExprSyntax . self) ? . representedLiteralValue else {
48
54
continue
49
55
}
50
56
strings. append ( value)
51
57
}
52
58
53
- return strings. map {
54
- " \( raw: $0) "
55
- }
59
+ return strings. map { " \( raw: $0) " }
56
60
}
57
61
58
62
static func expansion(
@@ -98,14 +102,7 @@ fileprivate struct WrapAllProperties: MemberAttributeMacro {
98
102
return [ ]
99
103
}
100
104
101
- return [
102
- AttributeSyntax (
103
- attributeName: IdentifierTypeSyntax (
104
- name: . identifier( " Wrapper " )
105
- )
106
- )
107
- . with ( \. leadingTrivia, [ . newlines( 1 ) , . spaces( 2 ) ] )
108
- ]
105
+ return [ " @Wrapper " ]
109
106
}
110
107
}
111
108
@@ -123,7 +120,7 @@ final class MacroSystemTests: XCTestCase {
123
120
var argList = macro. argumentList
124
121
argList [ argList. startIndex] . label = . identifier( " _colorLiteralRed " )
125
122
let initSyntax : ExprSyntax = " .init( \( argList) ) "
126
- return initSyntax. with ( \ . leadingTrivia , macro . leadingTrivia )
123
+ return initSyntax
127
124
}
128
125
}
129
126
@@ -209,11 +206,10 @@ final class MacroSystemTests: XCTestCase {
209
206
of macro: some FreestandingMacroExpansionSyntax ,
210
207
in context: some MacroExpansionContext
211
208
) throws -> ExprSyntax {
212
- guard let sourceLoc: AbstractSourceLocation = context. location ( of: macro)
213
- else {
209
+ guard let sourceLoc: AbstractSourceLocation = context. location ( of: macro) else {
214
210
throw MacroExpansionErrorMessage ( " can't find location for macro " )
215
211
}
216
- return sourceLoc. column. with ( \ . leadingTrivia , macro . leadingTrivia )
212
+ return sourceLoc. column
217
213
}
218
214
}
219
215
@@ -222,11 +218,10 @@ final class MacroSystemTests: XCTestCase {
222
218
of macro: some FreestandingMacroExpansionSyntax ,
223
219
in context: some MacroExpansionContext
224
220
) throws -> ExprSyntax {
225
- guard let sourceLoc: AbstractSourceLocation = context. location ( of: macro)
226
- else {
221
+ guard let sourceLoc: AbstractSourceLocation = context. location ( of: macro) else {
227
222
throw MacroExpansionErrorMessage ( " can't find location for macro " )
228
223
}
229
- return sourceLoc. file. with ( \ . leadingTrivia , macro . leadingTrivia )
224
+ return sourceLoc. file
230
225
}
231
226
}
232
227
@@ -348,9 +343,7 @@ final class MacroSystemTests: XCTestCase {
348
343
of node: some FreestandingMacroExpansionSyntax ,
349
344
in context: some MacroExpansionContext
350
345
) throws -> [ DeclSyntax ] {
351
- guard let firstElement = node. argumentList. first,
352
- let stringLiteral = firstElement. expression
353
- . as ( StringLiteralExprSyntax . self) ,
346
+ guard let stringLiteral = node. argumentList. first? . expression. as ( StringLiteralExprSyntax . self) ,
354
347
stringLiteral. segments. count == 1 ,
355
348
case let . stringSegment( prefix) = stringLiteral. segments. first
356
349
else {
@@ -394,8 +387,7 @@ final class MacroSystemTests: XCTestCase {
394
387
providingAccessorsOf declaration: some DeclSyntaxProtocol ,
395
388
in context: some MacroExpansionContext
396
389
) throws -> [ AccessorDeclSyntax ] {
397
- guard let varDecl = declaration. as ( VariableDeclSyntax . self) ,
398
- let binding = varDecl. bindings. first,
390
+ guard let binding = declaration. as ( VariableDeclSyntax . self) ? . bindings. first,
399
391
let identifier = binding. pattern. as ( IdentifierPatternSyntax . self) ? . identifier,
400
392
binding. accessorBlock == nil
401
393
else {
@@ -421,8 +413,7 @@ final class MacroSystemTests: XCTestCase {
421
413
providingPeersOf declaration: some DeclSyntaxProtocol ,
422
414
in context: some MacroExpansionContext
423
415
) throws -> [ SwiftSyntax . DeclSyntax ] {
424
- guard let varDecl = declaration. as ( VariableDeclSyntax . self) ,
425
- let binding = varDecl. bindings. first,
416
+ guard let binding = declaration. as ( VariableDeclSyntax . self) ? . bindings. first,
426
417
let identifier = binding. pattern. as ( IdentifierPatternSyntax . self) ? . identifier,
427
418
let type = binding. typeAnnotation? . type,
428
419
binding. accessorBlock == nil
@@ -431,8 +422,7 @@ final class MacroSystemTests: XCTestCase {
431
422
}
432
423
433
424
guard case . argumentList( let arguments) = node. arguments,
434
- let wrapperTypeNameExpr = arguments. first? . expression,
435
- let stringLiteral = wrapperTypeNameExpr. as ( StringLiteralExprSyntax . self) ,
425
+ let stringLiteral = arguments. first? . expression. as ( StringLiteralExprSyntax . self) ,
436
426
stringLiteral. segments. count == 1 ,
437
427
case let . stringSegment( wrapperTypeNameSegment) ? = stringLiteral. segments. first
438
428
else {
@@ -715,33 +705,23 @@ final class MacroSystemTests: XCTestCase {
715
705
}
716
706
717
707
// Form the completion handler parameter.
718
- let resultType : TypeSyntax ? = funcDecl. signature. returnClause? . type. with ( \ . leadingTrivia , [ ] ) . with ( \ . trailingTrivia , [ ] )
708
+ let resultType : TypeSyntax ? = funcDecl. signature. returnClause? . type. trimmed
719
709
720
710
let completionHandlerParam =
721
711
FunctionParameterSyntax (
722
712
firstName: . identifier( " completionHandler " ) ,
723
713
colon: . colonToken( trailingTrivia: . space) ,
724
- type: " ( \( resultType ?? " " ) ) -> Void " as TypeSyntax
714
+ type: TypeSyntax ( " ( \( resultType ?? " " ) ) -> Void " )
725
715
)
726
716
727
717
// Add the completion handler parameter to the parameter list.
728
718
let parameterList = funcDecl. signature. parameterClause. parameters
729
- let newParameterList : FunctionParameterListSyntax
730
- if let lastParam = parameterList. last {
719
+ var newParameterList = parameterList
720
+ if ! parameterList. isEmpty {
731
721
// We need to add a trailing comma to the preceding list.
732
- let newParameterListElements =
733
- parameterList. dropLast ( )
734
- + [
735
- lastParam. with (
736
- \. trailingComma,
737
- . commaToken( trailingTrivia: . space)
738
- ) ,
739
- completionHandlerParam,
740
- ]
741
- newParameterList = FunctionParameterListSyntax ( newParameterListElements)
742
- } else {
743
- newParameterList = parameterList + [ completionHandlerParam]
722
+ newParameterList [ newParameterList. index ( before: newParameterList. endIndex) ] . trailingComma = . commaToken( trailingTrivia: . space)
744
723
}
724
+ newParameterList. append ( completionHandlerParam)
745
725
746
726
let callArguments : [ String ] = parameterList. map { param in
747
727
let argName = param. secondName ?? param. firstName
@@ -775,34 +755,13 @@ final class MacroSystemTests: XCTestCase {
775
755
return attribute. attributeName. as ( IdentifierTypeSyntax . self) ? . name == " addCompletionHandler "
776
756
}
777
757
778
- let newFunc =
779
- funcDecl
780
- . with (
781
- \. signature,
782
- funcDecl. signature
783
- . with (
784
- \. effectSpecifiers,
785
- funcDecl. signature. effectSpecifiers? . with ( \. asyncSpecifier, nil ) // drop async
786
- )
787
- . with ( \. returnClause, nil ) // drop result type
788
- . with (
789
- \. parameterClause, // add completion handler parameter
790
- funcDecl. signature. parameterClause. with ( \. parameters, newParameterList)
791
- . with ( \. trailingTrivia, [ ] )
792
- )
793
- )
794
- . with (
795
- \. body,
796
- CodeBlockSyntax (
797
- leftBrace: . leftBraceToken( leadingTrivia: . space) ,
798
- statements: CodeBlockItemListSyntax (
799
- [ CodeBlockItemSyntax ( item: . expr( newBody) ) ]
800
- ) ,
801
- rightBrace: . rightBraceToken( leadingTrivia: . newline)
802
- )
803
- )
804
- . with ( \. attributes, newAttributeList)
805
- . with ( \. leadingTrivia, . newlines( 2 ) )
758
+ var newFunc = funcDecl
759
+ newFunc. signature. effectSpecifiers? . asyncSpecifier = nil // drop async
760
+ newFunc. signature. returnClause = nil // drop result type
761
+ newFunc. signature. parameterClause. parameters = newParameterList
762
+ newFunc. signature. parameterClause. trailingTrivia = [ ]
763
+ newFunc. body = CodeBlockSyntax { newBody }
764
+ newFunc. attributes = newAttributeList
806
765
807
766
return [ DeclSyntax ( newFunc) ]
808
767
}
@@ -833,13 +792,8 @@ final class MacroSystemTests: XCTestCase {
833
792
of node: AttributeSyntax ,
834
793
providingMembersOf decl: some DeclGroupSyntax ,
835
794
in context: some MacroExpansionContext
836
- )
837
- throws -> [ DeclSyntax ]
838
- {
839
- let storage : DeclSyntax = " var _storage: Storage<Self> "
840
- return [
841
- storage. with ( \. leadingTrivia, [ . newlines( 1 ) , . spaces( 2 ) ] )
842
- ]
795
+ ) throws -> [ DeclSyntax ] {
796
+ return [ " var _storage: Storage<Self> " ]
843
797
}
844
798
}
845
799
@@ -963,14 +917,7 @@ final class MacroSystemTests: XCTestCase {
963
917
return [ ]
964
918
}
965
919
966
- return [
967
- AttributeSyntax (
968
- attributeName: IdentifierTypeSyntax (
969
- name: . identifier( " Wrapper " )
970
- )
971
- )
972
- . with ( \. leadingTrivia, [ . newlines( 1 ) , . spaces( 2 ) ] )
973
- ]
920
+ return [ " @Wrapper " ]
974
921
}
975
922
}
976
923
@@ -1021,13 +968,7 @@ final class MacroSystemTests: XCTestCase {
1021
968
providingAttributesFor member: some DeclSyntaxProtocol ,
1022
969
in context: some MacroExpansionContext
1023
970
) throws -> [ AttributeSyntax ] {
1024
- return [
1025
- AttributeSyntax (
1026
- attributeName: IdentifierTypeSyntax (
1027
- name: . identifier( " Wrapper " )
1028
- )
1029
- )
1030
- ]
971
+ return [ " @Wrapper " ]
1031
972
}
1032
973
}
1033
974
@@ -1116,15 +1057,7 @@ final class MacroSystemTests: XCTestCase {
1116
1057
providingAttributesFor member: some DeclSyntaxProtocol ,
1117
1058
in context: some MacroExpansionContext
1118
1059
) throws -> [ AttributeSyntax ] {
1119
- return [
1120
- AttributeSyntax (
1121
- leadingTrivia: . blockComment( " /* start */" ) ,
1122
- attributeName: IdentifierTypeSyntax (
1123
- name: . identifier( " Wrapper " )
1124
- ) ,
1125
- trailingTrivia: . blockComment( " /* end */" )
1126
- )
1127
- ]
1060
+ return [ " /* start */@Wrapper/* end */ " ]
1128
1061
}
1129
1062
}
1130
1063
@@ -1207,13 +1140,10 @@ final class MacroSystemTests: XCTestCase {
1207
1140
1208
1141
func testTypeWrapperTransform( ) {
1209
1142
struct CustomTypeWrapperMacro : MemberMacro , MemberAttributeMacro , AccessorMacro {
1210
- static func expansion<
1211
- Declaration: DeclGroupSyntax ,
1212
- Context: MacroExpansionContext
1213
- > (
1143
+ static func expansion(
1214
1144
of node: AttributeSyntax ,
1215
- providingMembersOf declaration: Declaration ,
1216
- in context: Context
1145
+ providingMembersOf declaration: some DeclGroupSyntax ,
1146
+ in context: some MacroExpansionContext
1217
1147
) throws -> [ DeclSyntax ] {
1218
1148
return [ " var _storage: Wrapper<Self> " ]
1219
1149
}
@@ -1224,14 +1154,7 @@ final class MacroSystemTests: XCTestCase {
1224
1154
providingAttributesFor member: some DeclSyntaxProtocol ,
1225
1155
in context: some MacroExpansionContext
1226
1156
) throws -> [ AttributeSyntax ] {
1227
- return [
1228
- AttributeSyntax (
1229
- attributeName: IdentifierTypeSyntax (
1230
- name: . identifier( " customTypeWrapper " )
1231
- )
1232
- )
1233
- . with ( \. leadingTrivia, [ . newlines( 1 ) , . spaces( 2 ) ] )
1234
- ]
1157
+ return [ " @customTypeWrapper " ]
1235
1158
}
1236
1159
1237
1160
static func expansion(
@@ -1342,15 +1265,8 @@ final class MacroSystemTests: XCTestCase {
1342
1265
)
1343
1266
}
1344
1267
1345
- return identifiers. map { identifier in
1346
- CodeBlockItemSyntax (
1347
- item: CodeBlockItemSyntax . Item. stmt (
1348
- """
1349
-
1350
- guard let \( raw: identifier. text) else \( elseBlock ( identifier) )
1351
- """
1352
- )
1353
- )
1268
+ return identifiers. map { ( identifier) -> CodeBlockItemSyntax in
1269
+ " guard let \( raw: identifier. text) else \( elseBlock ( identifier) ) "
1354
1270
}
1355
1271
}
1356
1272
}
@@ -1410,17 +1326,13 @@ final class MacroSystemTests: XCTestCase {
1410
1326
) throws -> [ DeclSyntax ] {
1411
1327
var strings : [ String ] = [ ]
1412
1328
for arg in node. argumentList {
1413
- guard
1414
- let value = arg. expression. as ( StringLiteralExprSyntax . self) ? . representedLiteralValue
1415
- else {
1329
+ guard let value = arg. expression. as ( StringLiteralExprSyntax . self) ? . representedLiteralValue else {
1416
1330
continue
1417
1331
}
1418
1332
strings. append ( value)
1419
1333
}
1420
1334
1421
- return strings. map {
1422
- " \( raw: $0) "
1423
- }
1335
+ return strings. map { " \( raw: $0) " }
1424
1336
}
1425
1337
}
1426
1338
0 commit comments