-
Notifications
You must be signed in to change notification settings - Fork 247
Add NoEmptyLinesOpeningClosingBraces rule #797
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
allevato
merged 3 commits into
swiftlang:main
from
mateusrodriguesxyz:no-empty-lines-opening-closing-braces
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
Sources/SwiftFormat/Rules/NoEmptyLineOpeningClosingBraces.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftSyntax | ||
|
||
/// Empty lines are forbidden after opening braces and before closing braces. | ||
/// | ||
/// Lint: Empty lines after opening braces and before closing braces yield a lint error. | ||
/// | ||
/// Format: Empty lines after opening braces and before closing braces will be removed. | ||
@_spi(Rules) | ||
public final class NoEmptyLinesOpeningClosingBraces: SyntaxFormatRule { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add a doc comment for this rule using a similar format to the other rules? |
||
public override class var isOptIn: Bool { return true } | ||
|
||
public override func visit(_ node: AccessorBlockSyntax) -> AccessorBlockSyntax { | ||
var result = node | ||
switch node.accessors { | ||
case .accessors(let accessors): | ||
result.accessors = .init(rewritten(accessors)) | ||
case .getter(let getter): | ||
result.accessors = .init(rewritten(getter)) | ||
} | ||
result.rightBrace = rewritten(node.rightBrace) | ||
return result | ||
} | ||
|
||
public override func visit(_ node: CodeBlockSyntax) -> CodeBlockSyntax { | ||
var result = node | ||
result.statements = rewritten(node.statements) | ||
result.rightBrace = rewritten(node.rightBrace) | ||
return result | ||
} | ||
|
||
public override func visit(_ node: MemberBlockSyntax) -> MemberBlockSyntax { | ||
var result = node | ||
result.members = rewritten(node.members) | ||
result.rightBrace = rewritten(node.rightBrace) | ||
return result | ||
} | ||
|
||
public override func visit(_ node: ClosureExprSyntax) -> ExprSyntax { | ||
var result = node | ||
result.statements = rewritten(node.statements) | ||
result.rightBrace = rewritten(node.rightBrace) | ||
return ExprSyntax(result) | ||
} | ||
|
||
public override func visit(_ node: SwitchExprSyntax) -> ExprSyntax { | ||
var result = node | ||
result.cases = rewritten(node.cases) | ||
result.rightBrace = rewritten(node.rightBrace) | ||
return ExprSyntax(result) | ||
} | ||
|
||
public override func visit(_ node: PrecedenceGroupDeclSyntax) -> DeclSyntax { | ||
var result = node | ||
result.attributes = rewritten(node.attributes) | ||
result.rightBrace = rewritten(node.rightBrace) | ||
return DeclSyntax(result) | ||
} | ||
|
||
func rewritten(_ token: TokenSyntax) -> TokenSyntax { | ||
let (trimmedLeadingTrivia, count) = token.leadingTrivia.trimmingSuperfluousNewlines() | ||
if trimmedLeadingTrivia.sourceLength != token.leadingTriviaLength { | ||
diagnose(.removeEmptyLinesBefore(count), on: token, anchor: .start) | ||
return token.with(\.leadingTrivia, trimmedLeadingTrivia) | ||
} else { | ||
return token | ||
} | ||
} | ||
|
||
func rewritten<C: SyntaxCollection>(_ collection: C) -> C { | ||
var result = collection | ||
if let first = collection.first, first.leadingTrivia.containsNewlines, | ||
let index = collection.index(of: first) | ||
{ | ||
let (trimmedLeadingTrivia, count) = first.leadingTrivia.trimmingSuperfluousNewlines() | ||
if trimmedLeadingTrivia.sourceLength != first.leadingTriviaLength { | ||
diagnose(.removeEmptyLinesAfter(count), on: first, anchor: .leadingTrivia(0)) | ||
result[index] = first.with(\.leadingTrivia, trimmedLeadingTrivia) | ||
} | ||
} | ||
return rewrite(result).as(C.self)! | ||
} | ||
} | ||
|
||
extension Trivia { | ||
func trimmingSuperfluousNewlines() -> (Trivia, Int) { | ||
var trimmmed = 0 | ||
let pieces = self.indices.reduce([TriviaPiece]()) { (partialResult, index) in | ||
let piece = self[index] | ||
// Collapse consecutive newlines into a single one | ||
if case .newlines(let count) = piece { | ||
if let last = partialResult.last, last.isNewline { | ||
trimmmed += count | ||
return partialResult | ||
} else { | ||
trimmmed += count - 1 | ||
return partialResult + [.newlines(1)] | ||
} | ||
} | ||
// Remove spaces/tabs surrounded by newlines | ||
if piece.isSpaceOrTab, index > 0, index < self.count - 1, self[index - 1].isNewline, self[index + 1].isNewline { | ||
return partialResult | ||
} | ||
// Retain other trivia pieces | ||
return partialResult + [piece] | ||
} | ||
|
||
return (Trivia(pieces: pieces), trimmmed) | ||
} | ||
} | ||
|
||
extension Finding.Message { | ||
fileprivate static func removeEmptyLinesAfter(_ count: Int) -> Finding.Message { | ||
"remove empty \(count > 1 ? "lines" : "line") after '{'" | ||
} | ||
|
||
fileprivate static func removeEmptyLinesBefore(_ count: Int) -> Finding.Message { | ||
"remove empty \(count > 1 ? "lines" : "line") before '}'" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's too bad we don't have a convenient way to just visit anything that conforms to
BracedSyntax
here.Can you add
PrecedenceGroupDeclSyntax
andSwitchExprSyntax
, which look like the only two otherBracedSyntax
node types that are missing here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To visit anything conforming to
BracedSyntax
you can do the followingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general that would work for a regular visitor, but
generate-swift-format
scans the rules to figure out which concrete nodes they visit so that it can generate the pipeline visitor.It might not be difficult to add
visitAny
support to the pipeline, but it would be better to do it separately from this PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏽 Just wanted to put this pattern out there because it’s not terribly obvious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've wished more than once that
override func f(value: some T)
in a class would override any concrete method that matches.