Skip to content

Commit e8a62dc

Browse files
authored
Merge pull request swiftlang#226 from dabelknap/whitespace-linter
Use the pretty-printer output for whitespace linting
2 parents dcfadbe + 329190d commit e8a62dc

File tree

5 files changed

+595
-1
lines changed

5 files changed

+595
-1
lines changed

Package.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ let package = Package(
4444
"SwiftFormatCore",
4545
"SwiftFormatPrettyPrint",
4646
"SwiftFormatRules",
47+
"SwiftFormatWhitespaceLinter",
4748
"SwiftSyntax",
4849
]
4950
),
@@ -57,6 +58,13 @@ let package = Package(
5758
name: "SwiftFormatPrettyPrint",
5859
dependencies: ["SwiftFormatCore", "SwiftFormatConfiguration"]
5960
),
61+
.target(
62+
name: "SwiftFormatWhitespaceLinter",
63+
dependencies: [
64+
"SwiftFormatCore",
65+
"SwiftSyntax",
66+
]
67+
),
6068
.target(name: "generate-pipeline", dependencies: ["SwiftSyntax"]),
6169
.target(
6270
name: "swift-format",
@@ -89,5 +97,14 @@ let package = Package(
8997
"SwiftSyntax",
9098
]
9199
),
100+
.testTarget(
101+
name: "SwiftFormatWhitespaceLinterTests",
102+
dependencies: [
103+
"SwiftFormatConfiguration",
104+
"SwiftFormatCore",
105+
"SwiftFormatWhitespaceLinter",
106+
"SwiftSyntax",
107+
]
108+
),
92109
]
93110
)

Sources/SwiftFormat/SwiftLinter.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import Foundation
1414
import SwiftFormatConfiguration
1515
import SwiftFormatCore
16+
import SwiftFormatPrettyPrint
17+
import SwiftFormatWhitespaceLinter
1618
import SwiftSyntax
1719

1820
/// Diagnoses and reports problems in Swift source code or syntax trees according to the Swift style
@@ -64,7 +66,15 @@ public final class SwiftLinter {
6466

6567
pipeline.visit(syntax as Syntax)
6668

67-
// TODO: Extend the pretty printer to make it possible to lint spacing and breaking issues.
69+
// Perform whitespace linting by comparing the input source text with the output of the
70+
// pretty-printer.
71+
let printer = PrettyPrinter(
72+
context: context,
73+
node: syntax,
74+
printTokenStream: debugOptions.contains(.dumpTokenStream))
75+
let formatted = printer.prettyPrint()
76+
let ws = WhitespaceLinter(user: syntax.description, formatted: formatted, context: context)
77+
ws.lint()
6878
}
6979

7080
// TODO: Add an overload of `lint` that takes the source text directly.

0 commit comments

Comments
 (0)