Skip to content

[5.9][Macros] Ability to opt-out from auto-propagation of attrs/modifiers #1739

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
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
4 changes: 3 additions & 1 deletion Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public func expandFreestandingMacro(
var rewritten = try declMacroDef.expansion(of: node, in: context)
// Copy attributes and modifiers to the generated decls.
if let expansionDecl = node.as(MacroExpansionDeclSyntax.self) {
let attributes = declMacroDef.propagateFreestandingMacroAttributes ? expansionDecl.attributes : nil
let modifiers = declMacroDef.propagateFreestandingMacroModifiers ? expansionDecl.modifiers : nil
rewritten = rewritten.map {
$0.applying(attributes: expansionDecl.attributes, modifiers: expansionDecl.modifiers)
$0.applying(attributes: attributes, modifiers: modifiers)
}
}
expandedSyntax = Syntax(
Expand Down
12 changes: 12 additions & 0 deletions Sources/SwiftSyntaxMacros/MacroProtocols/DeclarationMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ public protocol DeclarationMacro: FreestandingMacro {
of node: Node,
in context: Context
) throws -> [DeclSyntax]

/// Whether to copy attributes on the expansion syntax to expanded declarations,
/// 'true' by default.
static var propagateFreestandingMacroAttributes: Bool { get }
/// Whether to copy modifiers on the expansion syntax to expanded declarations,
/// 'true' by default.
static var propagateFreestandingMacroModifiers: Bool { get }
}

public extension DeclarationMacro {
static var propagateFreestandingMacroAttributes: Bool { true }
static var propagateFreestandingMacroModifiers: Bool { true }
}
8 changes: 6 additions & 2 deletions Sources/SwiftSyntaxMacros/MacroSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
in: context
)
if let declExpansion = expansion.as(MacroExpansionDeclSyntax.self) {
let attributes = macro.propagateFreestandingMacroAttributes ? declExpansion.attributes : nil
let modifiers = macro.propagateFreestandingMacroModifiers ? declExpansion.modifiers : nil
expandedItemList = expandedItemList.map {
$0.applying(attributes: declExpansion.attributes, modifiers: declExpansion.modifiers)
$0.applying(attributes: attributes, modifiers: modifiers)
}
}
newItems.append(
Expand Down Expand Up @@ -198,8 +200,10 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
of: declExpansion,
in: context
)
let attributes = freestandingMacro.propagateFreestandingMacroAttributes ? declExpansion.attributes : nil
let modifiers = freestandingMacro.propagateFreestandingMacroModifiers ? declExpansion.modifiers : nil
expandedList = expandedList.map {
$0.applying(attributes: declExpansion.attributes, modifiers: declExpansion.modifiers)
$0.applying(attributes: attributes, modifiers: modifiers)
}

newItems.append(
Expand Down
41 changes: 41 additions & 0 deletions Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,30 @@ public struct DeclsFromStringsMacro: DeclarationMacro {
}
}

public struct DeclsFromStringsMacroNoAttrs: DeclarationMacro {
public static var propagateFreestandingMacroAttributes: Bool { false }
public static var propagateFreestandingMacroModifiers: Bool { false }

public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
var strings: [String] = []
for arg in node.argumentList {
guard
let value = arg.expression.as(StringLiteralExprSyntax.self)?.representedLiteralValue
else {
continue
}
strings.append(value)
}

return strings.map {
"\(raw: $0)"
}
}
}

// MARK: Tests

/// The set of test macros we use here.
Expand Down Expand Up @@ -1114,5 +1138,22 @@ final class MacroSystemTests: XCTestCase {
macros: ["decls": DeclsFromStringsMacro.self],
indentationWidth: indentationWidth
)

assertMacroExpansion(
#"""
@attribute
@otherAttribute(x: 1)
public #decls("@moreAttibute var global = 42",
"private func foo() {}")
"""#,
expandedSource: #"""
@moreAttibute var global = 42
private func foo() {
}
"""#,
macros: ["decls": DeclsFromStringsMacroNoAttrs.self],
indentationWidth: indentationWidth
)

}
}