Skip to content
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: 2 additions & 2 deletions lib/ASTGen/Sources/ASTGen/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ extension ASTGenVisitor {
}

// Handle type attributes.
if let attributes = node.attributes {
if !node.attributes.isEmpty {
let typeAttributes = TypeAttributes_create()
for attributeElt in attributes {
for attributeElt in node.attributes {
// FIXME: Ignoring #ifs entirely. We want to provide a filtered view,
// but we don't have that ability right now.
guard case let .attribute(attribute) = attributeElt else {
Expand Down
10 changes: 0 additions & 10 deletions lib/Macros/Sources/ObservationMacros/Availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,3 @@ extension AttributeListSyntax {
return AttributeListSyntax(elements)
}
}

extension DeclGroupSyntax {
var availability: AttributeListSyntax? {
if let attributes {
return attributes.availability
} else {
return nil
}
}
}
21 changes: 8 additions & 13 deletions lib/Macros/Sources/ObservationMacros/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ extension VariableDeclSyntax {
}

var isInstance: Bool {
if let modifiers {
for modifier in modifiers {
for token in modifier.tokens(viewMode: .all) {
if token.tokenKind == .keyword(.static) || token.tokenKind == .keyword(.class) {
return false
}
for modifier in modifiers {
for token in modifier.tokens(viewMode: .all) {
if token.tokenKind == .keyword(.static) || token.tokenKind == .keyword(.class) {
return false
}
}
}
Expand Down Expand Up @@ -103,7 +101,6 @@ extension VariableDeclSyntax {
}

func hasMacroApplication(_ name: String) -> Bool {
guard let attributes else { return false }
for attribute in attributes {
switch attribute {
case .attribute(let attr):
Expand Down Expand Up @@ -179,12 +176,10 @@ extension TypeSyntax {

extension FunctionDeclSyntax {
var isInstance: Bool {
if let modifiers {
for modifier in modifiers {
for token in modifier.tokens(viewMode: .all) {
if token.tokenKind == .keyword(.static) || token.tokenKind == .keyword(.class) {
return false
}
for modifier in modifiers {
for token in modifier.tokens(viewMode: .all) {
if token.tokenKind == .keyword(.static) || token.tokenKind == .keyword(.class) {
return false
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions lib/Macros/Sources/ObservationMacros/ObservableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,11 @@ extension PatternBindingListSyntax {

extension VariableDeclSyntax {
func privatePrefixed(_ prefix: String, addingAttribute attribute: AttributeSyntax) -> VariableDeclSyntax {
let newAttributes = AttributeListSyntax(
(attributes.map(Array.init) ?? []) + [.attribute(attribute)])
let newAttributes = attributes + [.attribute(attribute)]
return VariableDeclSyntax(
leadingTrivia: leadingTrivia,
attributes: newAttributes,
modifiers: modifiers?.privatePrefixed(prefix) ?? DeclModifierListSyntax(keyword: .private),
modifiers: modifiers.privatePrefixed(prefix),
bindingSpecifier: TokenSyntax(bindingSpecifier.tokenKind, leadingTrivia: .space, trailingTrivia: .space, presence: .present),
bindings: bindings.privatePrefixed(prefix),
trailingTrivia: trailingTrivia
Expand Down Expand Up @@ -276,7 +275,7 @@ extension ObservableMacro: ExtensionMacro {
"""
let ext = decl.cast(ExtensionDeclSyntax.self)

if let availability = declaration.availability {
if let availability = declaration.attributes.availability {
return [ext.with(\.attributes, availability)]
} else {
return [ext]
Expand Down
2 changes: 1 addition & 1 deletion lib/Macros/Sources/SwiftMacros/OptionSetMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ extension OptionSetMacro: MemberMacro {
}

// Dig out the access control keyword we need.
let access = decl.modifiers?.first(where: \.isNeededAccessLevelModifier)
let access = decl.modifiers.first(where: \.isNeededAccessLevelModifier)

let staticVars = caseElements.map { (element) -> DeclSyntax in
"""
Expand Down
42 changes: 19 additions & 23 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -900,18 +900,16 @@ public struct AddCompletionHandler: PeerMacro {
"""

// Drop the @addCompletionHandler attribute from the new declaration.
let newAttributeList = AttributeListSyntax(
funcDecl.attributes?.filter {
guard case let .attribute(attribute) = $0,
let attributeType = attribute.attributeName.as(IdentifierTypeSyntax.self),
let nodeType = node.attributeName.as(IdentifierTypeSyntax.self)
else {
return true
}
let newAttributeList = funcDecl.attributes.filter {
guard case let .attribute(attribute) = $0,
let attributeType = attribute.attributeName.as(IdentifierTypeSyntax.self),
let nodeType = node.attributeName.as(IdentifierTypeSyntax.self)
else {
return true
}

return attributeType.name.text != nodeType.name.text
} ?? []
)
return attributeType.name.text != nodeType.name.text
}

var newFunc = funcDecl
newFunc.signature.effectSpecifiers?.asyncSpecifier = nil // drop async
Expand Down Expand Up @@ -1010,18 +1008,16 @@ public struct WrapInType: PeerMacro {
"""

// Drop the peer macro attribute from the new declaration.
let newAttributeList = AttributeListSyntax(
funcDecl.attributes?.filter {
guard case let .attribute(attribute) = $0,
let attributeType = attribute.attributeName.as(IdentifierTypeSyntax.self),
let nodeType = node.attributeName.as(IdentifierTypeSyntax.self)
else {
return true
}
let newAttributeList = funcDecl.attributes.filter {
guard case let .attribute(attribute) = $0,
let attributeType = attribute.attributeName.as(IdentifierTypeSyntax.self),
let nodeType = node.attributeName.as(IdentifierTypeSyntax.self)
else {
return true
}

return attributeType.name.text != nodeType.name.text
} ?? []
)
return attributeType.name.text != nodeType.name.text
}

var method = funcDecl
method.name = "\(context.makeUniqueName(funcDecl.name.text))"
Expand Down Expand Up @@ -1251,7 +1247,7 @@ public struct NewTypeMacro: MemberMacro {
throw CustomError.message("@NewType can only be applied to a struct declarations.")
}

let access = declaration.modifiers?.first(where: \.isNeededAccessLevelModifier)
let access = declaration.modifiers.first(where: \.isNeededAccessLevelModifier)

return [
"\(access)typealias RawValue = \(rawType)",
Expand Down