Skip to content

Adjustments for merging AttributeSyntax and CustomAttributeSyntax #471

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 1 commit into from
Jan 12, 2023
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
29 changes: 12 additions & 17 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1584,29 +1584,24 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {

override func visit(_ node: AttributeSyntax) -> SyntaxVisitorContinueKind {
before(node.firstToken, tokens: .open)
if node.argument != nil {
switch node.argument {
case .argumentList(let argumentList)?:
if let leftParen = node.leftParen, let rightParen = node.rightParen {
arrangeFunctionCallArgumentList(
argumentList,
leftDelimiter: leftParen,
rightDelimiter: rightParen,
forcesBreakBeforeRightDelimiter: false)
}
case .some:
// Wrap the attribute's arguments in their own group, so arguments stay together with a higher
// affinity than the overall attribute (e.g. allows a break after the opening "(" and then
// having the entire argument list on 1 line). Necessary spaces and breaks are added inside of
// the argument, using type specific visitor methods.
after(node.leftParen, tokens: .break(.open, size: 0), .open(argumentListConsistency()))
before(node.rightParen, tokens: .break(.close, size: 0), .close)
}
after(node.lastToken, tokens: .close)
return .visitChildren
}

override func visit(_ node: CustomAttributeSyntax) -> SyntaxVisitorContinueKind {
// "Custom attributes" are better known to users as "property wrappers".
before(node.firstToken, tokens: .open)
if let argumentList = node.argumentList,
let leftParen = node.leftParen, let rightParen = node.rightParen
{
arrangeFunctionCallArgumentList(
argumentList,
leftDelimiter: leftParen,
rightDelimiter: rightParen,
forcesBreakBeforeRightDelimiter: false)
case nil:
break
}
after(node.lastToken, tokens: .close)
return .visitChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class NeverUseImplicitlyUnwrappedOptionals: SyntaxLintRule {
// Ignores IBOutlet variables
if let attributes = node.attributes {
for attribute in attributes {
if (attribute.as(AttributeSyntax.self))?.attributeName.text == "IBOutlet" {
if (attribute.as(AttributeSyntax.self))?.attributeName.as(SimpleTypeIdentifierSyntax.self)?.name.text == "IBOutlet" {
return .skipChildren
}
}
Expand Down
3 changes: 0 additions & 3 deletions Tests/SwiftFormatPrettyPrintTests/AttributeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,6 @@ final class AttributeTests: PrettyPrintTestCase {
}

func testPropertyWrappers() {
// Property wrappers are `CustomAttributeSyntax` nodes (not `AttributeSyntax`) and their
// arguments are `TupleExprElementListSyntax` (like regular function call argument lists), so
// make sure that those are formatted properly.
let input =
"""
struct X {
Expand Down