Skip to content

Add a documentation requirement to the Macro protocol #1017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Sources/_SwiftSyntaxMacros/Macro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public protocol Macro {
/// The name of this macro.
static var name: String { get }

/// Documentation for this macro.
///
/// This documentation should be written in the Markdown format used for
/// commenting Swift declarations.
static var documentation: String { get }

/// The generic signature to use when describing the type of this macro.
static var genericSignature: GenericParameterClauseSyntax? { get }

Expand All @@ -28,3 +34,8 @@ public protocol Macro {
/// semantic one.
static var signature: TypeSyntax { get }
}

extension Macro {
/// Default, empty documentation string for macros.
public static var documentation: String { "" }
}
36 changes: 36 additions & 0 deletions Sources/_SwiftSyntaxMacros/MacroSystem+Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import SwiftSyntaxBuilder
struct ColumnMacro: ExpressionMacro {
static var name: String { "column" }

static var documentation: String {
"The column at which this macro is used"
}

static var genericSignature: GenericParameterClauseSyntax? =
"""
<T: ExpressibleByIntegerLiteral>
Expand All @@ -36,6 +40,10 @@ struct ColumnMacro: ExpressionMacro {
struct LineMacro: ExpressionMacro {
static var name: String { "line" }

static var documentation: String {
"The line at which this macro is used"
}

static var genericSignature: GenericParameterClauseSyntax? =
"""
<T: ExpressibleByIntegerLiteral>
Expand Down Expand Up @@ -68,6 +76,10 @@ extension PatternBindingSyntax {
struct FunctionMacro: ExpressionMacro {
static var name: String { "function" }

static var documentation: String {
"The name of the function in which this macro is used"
}

static var genericSignature: GenericParameterClauseSyntax? =
"""
<T: ExpressibleByStringLiteral>
Expand Down Expand Up @@ -180,6 +192,10 @@ struct FunctionMacro: ExpressionMacro {
struct ColorLiteralMacro: ExpressionMacro {
static var name: String { "colorLiteral" }

static var documentation: String {
"A color value expressed in terms of its RGBA components"
}

static var genericSignature: GenericParameterClauseSyntax? =
"""
<T: ExpressibleByColorLiteral>
Expand All @@ -206,6 +222,10 @@ struct ColorLiteralMacro: ExpressionMacro {
struct FileLiteralMacro: ExpressionMacro {
static var name: String { "fileLiteral" }

static var documentation: String {
"A file resource in the application bundle"
}

static var genericSignature: GenericParameterClauseSyntax? =
"""
<T: ExpressibleByFileReferenceLiteral>
Expand All @@ -228,6 +248,10 @@ struct FileLiteralMacro: ExpressionMacro {
struct ImageLiteralMacro: ExpressionMacro {
static var name: String { "imageLiteral" }

static var documentation: String {
"An image resource in the application bundle"
}

static var genericSignature: GenericParameterClauseSyntax? =
"""
<T: ExpressibleByImageLiteral>
Expand All @@ -250,6 +274,10 @@ struct ImageLiteralMacro: ExpressionMacro {
struct FilePathMacro: ExpressionMacro {
static var name: String { "filePath" }

static var documentation: String {
"The full path to the file in which this macro is used"
}

static var genericSignature: GenericParameterClauseSyntax? =
"""
<T: ExpressibleByStringLiteral>
Expand All @@ -274,6 +302,10 @@ struct FilePathMacro: ExpressionMacro {
struct FileIDMacro: ExpressionMacro {
static var name: String { "fileID" }

static var documentation: String {
"The file in which this macro is used in the form ModuleName/FileName"
}

static var genericSignature: GenericParameterClauseSyntax? =
"""
<T: ExpressibleByStringLiteral>
Expand Down Expand Up @@ -304,6 +336,10 @@ struct FileIDMacro: ExpressionMacro {
struct FileMacro: ExpressionMacro {
static var name: String { "file" }

static var documentation: String {
"The file in which this macro is used"
}

static var genericSignature: GenericParameterClauseSyntax? =
"""
<T: ExpressibleByStringLiteral>
Expand Down
4 changes: 4 additions & 0 deletions Sources/_SwiftSyntaxMacros/MacroSystem+Examples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import SwiftSyntaxBuilder
struct StringifyMacro: ExpressionMacro {
static var name: String { "stringify" }

static var documentation: String {
"An example macro that produces the value of an expression and its source text"
}

static var genericSignature: GenericParameterClauseSyntax? = "<T>"

static var signature: TypeSyntax = "(T) -> (T, String)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//===----------------------------------------------------------------------===//

import SwiftSyntax
import Foundation

extension SyntaxClassification {
var tag: String {
Expand Down
5 changes: 5 additions & 0 deletions Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,9 @@ final class MacroSystemTests: XCTestCase {
"""
)
}

func testDocumentation() {
let macro = MacroSystem.builtinMacroSystem.lookup("line")!
XCTAssertEqual(macro.documentation, "The line at which this macro is used")
}
}