Skip to content

Fix swift-format In Response to SwiftSyntax Changes #384

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
Aug 2, 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
37 changes: 28 additions & 9 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,21 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
Syntax(node),
attributes: node.attributes,
modifiers: node.modifiers,
typeKeyword: node.classOrActorKeyword,
typeKeyword: node.classKeyword,
identifier: node.identifier,
genericParameterClause: node.genericParameterClause,
inheritanceClause: node.inheritanceClause,
genericWhereClause: node.genericWhereClause,
members: node.members)
return .visitChildren
}

override func visit(_ node: ActorDeclSyntax) -> SyntaxVisitorContinueKind {
arrangeTypeDeclBlock(
Syntax(node),
attributes: node.attributes,
modifiers: node.modifiers,
typeKeyword: node.actorKeyword,
identifier: node.identifier,
genericParameterClause: node.genericParameterClause,
inheritanceClause: node.inheritanceClause,
Expand Down Expand Up @@ -305,22 +319,27 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
let hasArguments = !node.parameters.parameterList.isEmpty
let hasArguments = !node.signature.input.parameterList.isEmpty

arrangeParameterClause(node.parameters, forcesBreakBeforeRightParen: node.body != nil)
// Prioritize keeping ") throws" together. We can only do this if the function
// has arguments.
if hasArguments && config.prioritizeKeepingFunctionOutputTogether {
// Due to visitation order, the matching .open break is added in ParameterClauseSyntax.
after(node.signature.lastToken, tokens: .close)
}

arrangeParameterClause(node.signature.input, forcesBreakBeforeRightParen: node.body != nil)

// Prioritize keeping "<modifiers> init<punctuation>" together.
let firstTokenAfterAttributes = node.modifiers?.firstToken ?? node.initKeyword
before(firstTokenAfterAttributes, tokens: .open)

if hasArguments || node.genericParameterClause != nil {
after(node.parameters.leftParen, tokens: .close)
after(node.signature.input.leftParen, tokens: .close)
} else {
after(node.parameters.rightParen, tokens: .close)
after(node.signature.input.rightParen, tokens: .close)
}

before(node.throwsOrRethrowsKeyword, tokens: .break)

arrangeFunctionLikeDecl(
Syntax(node),
attributes: node.attributes,
Expand Down Expand Up @@ -2486,7 +2505,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
var verbatimText = ""
for piece in trailingTrivia[...lastGarbageIndex] {
switch piece {
case .garbageText, .spaces, .tabs, .formfeeds, .verticalTabs:
case .shebang, .garbageText, .spaces, .tabs, .formfeeds, .verticalTabs:
piece.write(to: &verbatimText)
default:
// The implementation of the lexer today ensures that newlines, carriage returns, and
Expand Down Expand Up @@ -2937,7 +2956,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}
}

case .garbageText(let text):
case .shebang(let text), .garbageText(let text):
// Garbage text in leading trivia might be something meaningful that would be disruptive to
// throw away when formatting the file, like a hashbang line or Unicode byte-order marker at
// the beginning of a file, or source control conflict markers. Keep it as verbatim text so
Expand Down
3 changes: 1 addition & 2 deletions Sources/SwiftFormatRules/FullyIndirectEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public final class FullyIndirectEnum: SyntaxFormatRule {

let newModifier = SyntaxFactory.makeDeclModifier(
name: SyntaxFactory.makeIdentifier(
"indirect", leadingTrivia: leadingTrivia, trailingTrivia: .spaces(1)), detailLeftParen: nil,
detail: nil, detailRightParen: nil)
"indirect", leadingTrivia: leadingTrivia, trailingTrivia: .spaces(1)), detail: nil)

let newMemberBlock = node.members.withMembers(SyntaxFactory.makeMemberDeclList(newMembers))
return DeclSyntax(newEnumDecl.addModifier(newModifier).withMembers(newMemberBlock))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension ModifierListSyntax {
func createModifierToken(name: String) -> DeclModifierSyntax {
let id = SyntaxFactory.makeIdentifier(name, trailingTrivia: .spaces(1))
let newModifier = SyntaxFactory.makeDeclModifier(
name: id, detailLeftParen: nil, detail: nil, detailRightParen: nil)
name: id, detail: nil)
return newModifier
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
private func hasNonWhitespaceLeadingTrivia(_ token: TokenSyntax) -> Bool {
for piece in token.leadingTrivia {
switch piece {
case .blockComment, .docBlockComment, .docLineComment, .garbageText, .lineComment:
case .blockComment, .docBlockComment, .docLineComment, .garbageText, .lineComment, .shebang:
return true
default:
break
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftFormatRules/UseSynthesizedInitializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
// Collect any possible redundant initializers into a list
} else if let initDecl = member.as(InitializerDeclSyntax.self) {
guard initDecl.optionalMark == nil else { continue }
guard initDecl.throwsOrRethrowsKeyword == nil else { continue }
guard initDecl.signature.throwsOrRethrowsKeyword == nil else { continue }
initializers.append(initDecl)
}
}
Expand All @@ -51,7 +51,7 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
for initializer in initializers {
guard
matchesPropertyList(
parameters: initializer.parameters.parameterList,
parameters: initializer.signature.input.parameterList,
properties: storedProperties)
else { continue }
guard
Expand Down
11 changes: 5 additions & 6 deletions Sources/SwiftFormatRules/ValidateDocumentationComments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,19 @@ public final class ValidateDocumentationComments: SyntaxLintRule {

public override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
return checkFunctionLikeDocumentation(
DeclSyntax(node), name: "init", parameters: node.parameters.parameterList, throwsOrRethrowsKeyword: node.throwsOrRethrowsKeyword)
DeclSyntax(node), name: "init", signature: node.signature)
}

public override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
return checkFunctionLikeDocumentation(
DeclSyntax(node), name: node.identifier.text, parameters: node.signature.input.parameterList, throwsOrRethrowsKeyword: node.signature.throwsOrRethrowsKeyword,
DeclSyntax(node), name: node.identifier.text, signature: node.signature,
returnClause: node.signature.output)
}

private func checkFunctionLikeDocumentation(
_ node: DeclSyntax,
name: String,
parameters: FunctionParameterListSyntax,
throwsOrRethrowsKeyword: TokenSyntax?,
signature: FunctionSignatureSyntax,
returnClause: ReturnClauseSyntax? = nil
) -> SyntaxVisitorContinueKind {
guard let declComment = node.docComment else { return .skipChildren }
Expand All @@ -64,10 +63,10 @@ public final class ValidateDocumentationComments: SyntaxLintRule {
}

validateThrows(
throwsOrRethrowsKeyword, name: name, throwsDesc: commentInfo.throwsDescription, node: node)
signature.throwsOrRethrowsKeyword, name: name, throwsDesc: commentInfo.throwsDescription, node: node)
validateReturn(
returnClause, name: name, returnDesc: commentInfo.returnsDescription, node: node)
let funcParameters = funcParametersIdentifiers(in: parameters)
let funcParameters = funcParametersIdentifiers(in: signature.input.parameterList)

// If the documentation of the parameters is wrong 'docCommentInfo' won't
// parse the parameters correctly. First the documentation has to be fix
Expand Down