Skip to content

Commit 237efb9

Browse files
committed
Add a documentation requirement to the Macro protocol
With the Macro protocol being the way in which macros are declared, with no in-source representation, we lost the ability to have a documentation comment. Add that back with a new (defaulted) requirement to provide documentation.
1 parent 9d0d99e commit 237efb9

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

Sources/_SwiftSyntaxMacros/Macro.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public protocol Macro {
1616
/// The name of this macro.
1717
static var name: String { get }
1818

19+
/// Documentation for this macro.
20+
///
21+
/// This documentation should be written in the Markdown format used for
22+
/// commenting Swift declarations.
23+
static var documentation: String { get }
24+
1925
/// The generic signature to use when describing the type of this macro.
2026
static var genericSignature: GenericParameterClauseSyntax? { get }
2127

@@ -28,3 +34,8 @@ public protocol Macro {
2834
/// semantic one.
2935
static var signature: TypeSyntax { get }
3036
}
37+
38+
extension Macro {
39+
/// Default, empty documentation string for macros.
40+
public static var documentation: String { "" }
41+
}

Sources/_SwiftSyntaxMacros/MacroSystem+Builtin.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import SwiftSyntaxBuilder
1616
struct ColumnMacro: ExpressionMacro {
1717
static var name: String { "column" }
1818

19+
static var documentation: String {
20+
"The column at which this macro is used"
21+
}
22+
1923
static var genericSignature: GenericParameterClauseSyntax? =
2024
"""
2125
<T: ExpressibleByIntegerLiteral>
@@ -36,6 +40,10 @@ struct ColumnMacro: ExpressionMacro {
3640
struct LineMacro: ExpressionMacro {
3741
static var name: String { "line" }
3842

43+
static var documentation: String {
44+
"The line at which this macro is used"
45+
}
46+
3947
static var genericSignature: GenericParameterClauseSyntax? =
4048
"""
4149
<T: ExpressibleByIntegerLiteral>
@@ -68,6 +76,10 @@ extension PatternBindingSyntax {
6876
struct FunctionMacro: ExpressionMacro {
6977
static var name: String { "function" }
7078

79+
static var documentation: String {
80+
"The name of the function in which this macro is used"
81+
}
82+
7183
static var genericSignature: GenericParameterClauseSyntax? =
7284
"""
7385
<T: ExpressibleByStringLiteral>
@@ -180,6 +192,10 @@ struct FunctionMacro: ExpressionMacro {
180192
struct ColorLiteralMacro: ExpressionMacro {
181193
static var name: String { "colorLiteral" }
182194

195+
static var documentation: String {
196+
"A color value expressed in terms of its RGBA components"
197+
}
198+
183199
static var genericSignature: GenericParameterClauseSyntax? =
184200
"""
185201
<T: ExpressibleByColorLiteral>
@@ -206,6 +222,10 @@ struct ColorLiteralMacro: ExpressionMacro {
206222
struct FileLiteralMacro: ExpressionMacro {
207223
static var name: String { "fileLiteral" }
208224

225+
static var documentation: String {
226+
"A file resource in the application bundle"
227+
}
228+
209229
static var genericSignature: GenericParameterClauseSyntax? =
210230
"""
211231
<T: ExpressibleByFileReferenceLiteral>
@@ -228,6 +248,10 @@ struct FileLiteralMacro: ExpressionMacro {
228248
struct ImageLiteralMacro: ExpressionMacro {
229249
static var name: String { "imageLiteral" }
230250

251+
static var documentation: String {
252+
"An image resource in the application bundle"
253+
}
254+
231255
static var genericSignature: GenericParameterClauseSyntax? =
232256
"""
233257
<T: ExpressibleByImageLiteral>
@@ -250,6 +274,10 @@ struct ImageLiteralMacro: ExpressionMacro {
250274
struct FilePathMacro: ExpressionMacro {
251275
static var name: String { "filePath" }
252276

277+
static var documentation: String {
278+
"The full path to the file in which this macro is used"
279+
}
280+
253281
static var genericSignature: GenericParameterClauseSyntax? =
254282
"""
255283
<T: ExpressibleByStringLiteral>
@@ -274,6 +302,10 @@ struct FilePathMacro: ExpressionMacro {
274302
struct FileIDMacro: ExpressionMacro {
275303
static var name: String { "fileID" }
276304

305+
static var documentation: String {
306+
"The file in which this macro is used in the form ModuleName/FileName"
307+
}
308+
277309
static var genericSignature: GenericParameterClauseSyntax? =
278310
"""
279311
<T: ExpressibleByStringLiteral>
@@ -304,6 +336,10 @@ struct FileIDMacro: ExpressionMacro {
304336
struct FileMacro: ExpressionMacro {
305337
static var name: String { "file" }
306338

339+
static var documentation: String {
340+
"The file in which this macro is used"
341+
}
342+
307343
static var genericSignature: GenericParameterClauseSyntax? =
308344
"""
309345
<T: ExpressibleByStringLiteral>

Sources/_SwiftSyntaxMacros/MacroSystem+Examples.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import SwiftSyntaxBuilder
1717
struct StringifyMacro: ExpressionMacro {
1818
static var name: String { "stringify" }
1919

20+
static var documentation: String {
21+
"An example macro that produces the value of an expression and its source text"
22+
}
23+
2024
static var genericSignature: GenericParameterClauseSyntax? = "<T>"
2125

2226
static var signature: TypeSyntax = "(T) -> (T, String)"

Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,9 @@ final class MacroSystemTests: XCTestCase {
140140
"""
141141
)
142142
}
143+
144+
func testDocumentation() {
145+
let macro = MacroSystem.builtinMacroSystem.lookup("line")!
146+
XCTAssertEqual(macro.documentation, "The line at which this macro is used")
147+
}
143148
}

0 commit comments

Comments
 (0)