Skip to content

Commit bc0fed4

Browse files
committed
Improvements to macros in MacroSystemTests
Some general improvements to the usage of SwiftSyntax in the macros in MacroSystemTests and removal of trivia handling in these macros because trivia is being added by MacroSystem now.
1 parent 54484fa commit bc0fed4

File tree

1 file changed

+46
-134
lines changed

1 file changed

+46
-134
lines changed

Tests/SwiftSyntaxMacroExpansionTest/MacroSystemTests.swift

Lines changed: 46 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

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+
1321
import _SwiftSyntaxTestSupport
1422
import SwiftDiagnostics
1523
import SwiftParser
@@ -42,17 +50,13 @@ fileprivate struct DeclsFromStringsMacro: DeclarationMacro, MemberMacro {
4250
private static func decls(from arguments: LabeledExprListSyntax) -> [DeclSyntax] {
4351
var strings: [String] = []
4452
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 {
4854
continue
4955
}
5056
strings.append(value)
5157
}
5258

53-
return strings.map {
54-
"\(raw: $0)"
55-
}
59+
return strings.map { "\(raw: $0)" }
5660
}
5761

5862
static func expansion(
@@ -98,14 +102,7 @@ fileprivate struct WrapAllProperties: MemberAttributeMacro {
98102
return []
99103
}
100104

101-
return [
102-
AttributeSyntax(
103-
attributeName: IdentifierTypeSyntax(
104-
name: .identifier("Wrapper")
105-
)
106-
)
107-
.with(\.leadingTrivia, [.newlines(1), .spaces(2)])
108-
]
105+
return ["@Wrapper"]
109106
}
110107
}
111108

@@ -123,7 +120,7 @@ final class MacroSystemTests: XCTestCase {
123120
var argList = macro.argumentList
124121
argList[argList.startIndex].label = .identifier("_colorLiteralRed")
125122
let initSyntax: ExprSyntax = ".init(\(argList))"
126-
return initSyntax.with(\.leadingTrivia, macro.leadingTrivia)
123+
return initSyntax
127124
}
128125
}
129126

@@ -209,11 +206,10 @@ final class MacroSystemTests: XCTestCase {
209206
of macro: some FreestandingMacroExpansionSyntax,
210207
in context: some MacroExpansionContext
211208
) throws -> ExprSyntax {
212-
guard let sourceLoc: AbstractSourceLocation = context.location(of: macro)
213-
else {
209+
guard let sourceLoc: AbstractSourceLocation = context.location(of: macro) else {
214210
throw MacroExpansionErrorMessage("can't find location for macro")
215211
}
216-
return sourceLoc.column.with(\.leadingTrivia, macro.leadingTrivia)
212+
return sourceLoc.column
217213
}
218214
}
219215

@@ -222,11 +218,10 @@ final class MacroSystemTests: XCTestCase {
222218
of macro: some FreestandingMacroExpansionSyntax,
223219
in context: some MacroExpansionContext
224220
) throws -> ExprSyntax {
225-
guard let sourceLoc: AbstractSourceLocation = context.location(of: macro)
226-
else {
221+
guard let sourceLoc: AbstractSourceLocation = context.location(of: macro) else {
227222
throw MacroExpansionErrorMessage("can't find location for macro")
228223
}
229-
return sourceLoc.file.with(\.leadingTrivia, macro.leadingTrivia)
224+
return sourceLoc.file
230225
}
231226
}
232227

@@ -348,9 +343,7 @@ final class MacroSystemTests: XCTestCase {
348343
of node: some FreestandingMacroExpansionSyntax,
349344
in context: some MacroExpansionContext
350345
) 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),
354347
stringLiteral.segments.count == 1,
355348
case let .stringSegment(prefix) = stringLiteral.segments.first
356349
else {
@@ -394,8 +387,7 @@ final class MacroSystemTests: XCTestCase {
394387
providingAccessorsOf declaration: some DeclSyntaxProtocol,
395388
in context: some MacroExpansionContext
396389
) 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,
399391
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier,
400392
binding.accessorBlock == nil
401393
else {
@@ -421,8 +413,7 @@ final class MacroSystemTests: XCTestCase {
421413
providingPeersOf declaration: some DeclSyntaxProtocol,
422414
in context: some MacroExpansionContext
423415
) 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,
426417
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier,
427418
let type = binding.typeAnnotation?.type,
428419
binding.accessorBlock == nil
@@ -431,8 +422,7 @@ final class MacroSystemTests: XCTestCase {
431422
}
432423

433424
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),
436426
stringLiteral.segments.count == 1,
437427
case let .stringSegment(wrapperTypeNameSegment)? = stringLiteral.segments.first
438428
else {
@@ -715,33 +705,23 @@ final class MacroSystemTests: XCTestCase {
715705
}
716706

717707
// 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
719709

720710
let completionHandlerParam =
721711
FunctionParameterSyntax(
722712
firstName: .identifier("completionHandler"),
723713
colon: .colonToken(trailingTrivia: .space),
724-
type: "(\(resultType ?? "")) -> Void" as TypeSyntax
714+
type: TypeSyntax("(\(resultType ?? "")) -> Void")
725715
)
726716

727717
// Add the completion handler parameter to the parameter list.
728718
let parameterList = funcDecl.signature.parameterClause.parameters
729-
let newParameterList: FunctionParameterListSyntax
730-
if let lastParam = parameterList.last {
719+
var newParameterList = parameterList
720+
if !parameterList.isEmpty {
731721
// 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)
744723
}
724+
newParameterList.append(completionHandlerParam)
745725

746726
let callArguments: [String] = parameterList.map { param in
747727
let argName = param.secondName ?? param.firstName
@@ -775,34 +755,13 @@ final class MacroSystemTests: XCTestCase {
775755
return attribute.attributeName.as(IdentifierTypeSyntax.self)?.name == "addCompletionHandler"
776756
}
777757

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
806765

807766
return [DeclSyntax(newFunc)]
808767
}
@@ -833,13 +792,8 @@ final class MacroSystemTests: XCTestCase {
833792
of node: AttributeSyntax,
834793
providingMembersOf decl: some DeclGroupSyntax,
835794
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>"]
843797
}
844798
}
845799

@@ -963,14 +917,7 @@ final class MacroSystemTests: XCTestCase {
963917
return []
964918
}
965919

966-
return [
967-
AttributeSyntax(
968-
attributeName: IdentifierTypeSyntax(
969-
name: .identifier("Wrapper")
970-
)
971-
)
972-
.with(\.leadingTrivia, [.newlines(1), .spaces(2)])
973-
]
920+
return ["@Wrapper"]
974921
}
975922
}
976923

@@ -1021,13 +968,7 @@ final class MacroSystemTests: XCTestCase {
1021968
providingAttributesFor member: some DeclSyntaxProtocol,
1022969
in context: some MacroExpansionContext
1023970
) throws -> [AttributeSyntax] {
1024-
return [
1025-
AttributeSyntax(
1026-
attributeName: IdentifierTypeSyntax(
1027-
name: .identifier("Wrapper")
1028-
)
1029-
)
1030-
]
971+
return ["@Wrapper"]
1031972
}
1032973
}
1033974

@@ -1116,15 +1057,7 @@ final class MacroSystemTests: XCTestCase {
11161057
providingAttributesFor member: some DeclSyntaxProtocol,
11171058
in context: some MacroExpansionContext
11181059
) 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 */"]
11281061
}
11291062
}
11301063

@@ -1207,13 +1140,10 @@ final class MacroSystemTests: XCTestCase {
12071140

12081141
func testTypeWrapperTransform() {
12091142
struct CustomTypeWrapperMacro: MemberMacro, MemberAttributeMacro, AccessorMacro {
1210-
static func expansion<
1211-
Declaration: DeclGroupSyntax,
1212-
Context: MacroExpansionContext
1213-
>(
1143+
static func expansion(
12141144
of node: AttributeSyntax,
1215-
providingMembersOf declaration: Declaration,
1216-
in context: Context
1145+
providingMembersOf declaration: some DeclGroupSyntax,
1146+
in context: some MacroExpansionContext
12171147
) throws -> [DeclSyntax] {
12181148
return ["var _storage: Wrapper<Self>"]
12191149
}
@@ -1224,14 +1154,7 @@ final class MacroSystemTests: XCTestCase {
12241154
providingAttributesFor member: some DeclSyntaxProtocol,
12251155
in context: some MacroExpansionContext
12261156
) 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"]
12351158
}
12361159

12371160
static func expansion(
@@ -1342,15 +1265,8 @@ final class MacroSystemTests: XCTestCase {
13421265
)
13431266
}
13441267

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))"
13541270
}
13551271
}
13561272
}
@@ -1410,17 +1326,13 @@ final class MacroSystemTests: XCTestCase {
14101326
) throws -> [DeclSyntax] {
14111327
var strings: [String] = []
14121328
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 {
14161330
continue
14171331
}
14181332
strings.append(value)
14191333
}
14201334

1421-
return strings.map {
1422-
"\(raw: $0)"
1423-
}
1335+
return strings.map { "\(raw: $0)" }
14241336
}
14251337
}
14261338

0 commit comments

Comments
 (0)