Skip to content
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

Rewrite vertical_whitespace rule using SwiftSyntax #5309

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Foundation
import SourceKittenFramework
import SwiftIDEUtils
import SwiftSyntax

private let defaultDescriptionReason = "Limit vertical whitespace to a single empty line"

struct VerticalWhitespaceRule: CorrectableRule {
struct VerticalWhitespaceRule: CorrectableRule, SourceKitFreeRule {
var configuration = VerticalWhitespaceConfiguration()

static let description = RuleDescription(
Expand All @@ -15,12 +17,46 @@ struct VerticalWhitespaceRule: CorrectableRule {
Example("let abc = 0\n"),
Example("let abc = 0\n\n"),
Example("/* bcs \n\n\n\n*/"),
Example("// bca \n\n")
Example("// bca \n\n"),
Example(#"""
"""
This is a




very long



string with newlines






"""
"""#),
Example(#"""
example(value: """
Something


""")
"""#)
],
triggeringExamples: [
Example("let aaaa = 0\n\n\n"),
Example("struct AAAA {}\n\n\n\n"),
Example("class BBBB {}\n\n\n")
Example("class BBBB {}\n\n\n"),
Example("""
func foo() {}


// MARK: - Something

""")
],
corrections: [
Example("let b = 0\n\n\nclass AAA {}\n"): Example("let b = 0\n\nclass AAA {}\n"),
Expand Down Expand Up @@ -67,14 +103,13 @@ struct VerticalWhitespaceRule: CorrectableRule {
let blankLinesSections = extractSections(from: filteredLines)

// filtering out violations in comments and strings
let stringAndComments = SyntaxKind.commentAndStringKinds
let syntaxMap = file.syntaxMap
let result = blankLinesSections.compactMap { eachSection -> (lastLine: Line, linesToRemove: Int)? in
guard let lastLine = eachSection.last else {
return nil
}
let kindInSection = syntaxMap.kinds(inByteRange: lastLine.byteRange)
if stringAndComments.isDisjoint(with: kindInSection) {

guard let classificationsInRange = file.syntaxTree.classification(at: lastLine.byteRange.location.value),
classificationsInRange.kind.isStringOrComment else {
return (lastLine, eachSection.count)
}

Expand Down Expand Up @@ -148,3 +183,15 @@ struct VerticalWhitespaceRule: CorrectableRule {
return []
}
}

private extension SyntaxClassification {
var isStringOrComment: Bool {
switch self {
case .docBlockComment, .docLineComment, .blockComment, .lineComment, .stringLiteral:
return true
case .attribute, .dollarIdentifier, .editorPlaceholder, .floatLiteral, .identifier,
.ifConfigDirective, .integerLiteral, .keyword, .none, .operator, .regexLiteral, .type:
return false
}
}
}