Skip to content

Commit e3e0ae0

Browse files
authored
Merge pull request swiftlang#214 from google/cleanup
Remove some dead rules; refactor format/lint logic into an API.
2 parents b5e68e0 + f4fd524 commit e3e0ae0

17 files changed

+357
-363
lines changed

Package.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ let package = Package(
1919
name: "swift-format",
2020
products: [
2121
.executable(name: "swift-format", targets: ["swift-format"]),
22+
.library(name: "SwiftFormat", targets: ["SwiftFormat"]),
2223
],
2324
dependencies: [
2425
.package(url: "https://github.com/apple/swift-package-manager.git", from: "0.1.0"),
@@ -35,6 +36,16 @@ let package = Package(
3536
]
3637
),
3738
.target(name: "CommonMark", dependencies: ["CCommonMark"]),
39+
.target(
40+
name: "SwiftFormat",
41+
dependencies: [
42+
"SwiftFormatConfiguration",
43+
"SwiftFormatCore",
44+
"SwiftFormatPrettyPrint",
45+
"SwiftFormatRules",
46+
"SwiftSyntax",
47+
]
48+
),
3849
.target(name: "SwiftFormatConfiguration"),
3950
.target(name: "SwiftFormatCore", dependencies: ["SwiftFormatConfiguration", "SwiftSyntax"]),
4051
.target(
@@ -49,10 +60,9 @@ let package = Package(
4960
.target(
5061
name: "swift-format",
5162
dependencies: [
63+
"SwiftFormat",
5264
"SwiftFormatConfiguration",
5365
"SwiftFormatCore",
54-
"SwiftFormatPrettyPrint",
55-
"SwiftFormatRules",
5666
"SwiftSyntax",
5767
"Utility",
5868
]

Sources/swift-format/DebugOptions.swift renamed to Sources/SwiftFormat/DebugOptions.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212

1313
/// Advanced options that are useful when debugging and developing the formatter, but are otherwise
1414
/// not meant for general use.
15-
struct DebugOptions: OptionSet {
15+
public struct DebugOptions: OptionSet {
1616

1717
/// Disables the pretty-printer pass entirely, executing only the syntax-transforming rules in the
1818
/// pipeline.
19-
static let disablePrettyPrint = DebugOptions(rawValue: 1 << 0)
19+
public static let disablePrettyPrint = DebugOptions(rawValue: 1 << 0)
2020

2121
/// Dumps a verbose representation of the raw pretty-printer token stream.
22-
static let dumpTokenStream = DebugOptions(rawValue: 1 << 1)
22+
public static let dumpTokenStream = DebugOptions(rawValue: 1 << 1)
2323

24-
let rawValue: Int
24+
public let rawValue: Int
2525

26-
init(rawValue: Int) { self.rawValue = rawValue }
26+
public init(rawValue: Int) { self.rawValue = rawValue }
2727

2828
/// Inserts or removes the given element from the option set, based on the value of `enabled`.
29-
mutating func set(_ element: Element, enabled: Bool) {
29+
public mutating func set(_ element: Element, enabled: Bool) {
3030
if enabled { insert(element) } else { remove(element) }
3131
}
3232
}

Sources/swift-format/PopulatePipeline.swift renamed to Sources/SwiftFormat/PopulatePipeline.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import SwiftSyntax
2020
/// - Parameter pipeline: The pipeline to populate with passes.
2121
func populate(_ pipeline: Pipeline) {
2222
/// MARK: File Passes
23-
pipeline.addFileRule(UseOnlySpaces.self)
2423
pipeline.addFileRule(UseOnlyUTF8.self)
2524
pipeline.addFileRule(UseSpecialEscapeSequences.self)
26-
pipeline.addFileRule(ValidFilename.self)
2725

2826
/// MARK: Formatting Passes
2927

@@ -263,6 +261,7 @@ func populate(_ pipeline: Pipeline) {
263261
pipeline.addLinter(
264262
BeginDocumentationCommentWithOneLineSummary.self,
265263
for:
264+
AssociatedtypeDeclSyntax.self,
266265
ClassDeclSyntax.self,
267266
DeinitializerDeclSyntax.self,
268267
EnumDeclSyntax.self,
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
14+
import SwiftFormatConfiguration
15+
import SwiftFormatCore
16+
import SwiftFormatPrettyPrint
17+
import SwiftSyntax
18+
19+
/// Formats Swift source code or syntax trees according to the Swift style guidelines.
20+
public final class SwiftFormatter {
21+
22+
/// The configuration settings that control the formatter's behavior.
23+
public let configuration: Configuration
24+
25+
/// A diagnostic engine to which non-fatal errors will be reported.
26+
public let diagnosticEngine: DiagnosticEngine?
27+
28+
/// Advanced options that are useful when debugging the formatter's behavior but are not meant for
29+
/// general use.
30+
public var debugOptions: DebugOptions = []
31+
32+
/// Creates a new Swift code formatter with the given configuration.
33+
///
34+
/// - Parameters:
35+
/// - configuration: The configuration settings that control the formatter's behavior.
36+
/// - diagnosticEngine: The diagnostic engine to which non-fatal errors will be reported.
37+
/// Defaults to nil.
38+
public init(configuration: Configuration, diagnosticEngine: DiagnosticEngine? = nil) {
39+
self.configuration = configuration
40+
self.diagnosticEngine = diagnosticEngine
41+
}
42+
43+
/// Formats the Swift code at the given file URL and writes the result to an output stream.
44+
///
45+
/// - Parameters:
46+
/// - url: The URL of the file containing the code to format.
47+
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
48+
/// be written.
49+
/// - Throws: If an unrecoverable error occurs when formatting the code.
50+
public func format<Output: TextOutputStream>(
51+
contentsOf url: URL, to outputStream: inout Output
52+
) throws {
53+
let sourceFile = try SyntaxTreeParser.parse(url)
54+
try format(syntax: sourceFile, assumingFileURL: url, to: &outputStream)
55+
}
56+
57+
/// Formats the given Swift syntax tree and writes the result to an output stream.
58+
///
59+
/// - Parameters:
60+
/// - syntax: The Swift syntax tree to be converted to source code and formatted.
61+
/// - url: A file URL denoting the filename/path that should be assumed for this syntax tree.
62+
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
63+
/// be written.
64+
/// - Throws: If an unrecoverable error occurs when formatting the code.
65+
public func format<Output: TextOutputStream>(
66+
syntax: Syntax, assumingFileURL url: URL, to outputStream: inout Output
67+
) throws {
68+
let context
69+
= Context(configuration: configuration, diagnosticEngine: diagnosticEngine, fileURL: url)
70+
let pipeline = FormatPipeline(context: context)
71+
populate(pipeline)
72+
73+
let transformedSyntax = pipeline.visit(syntax)
74+
75+
if debugOptions.contains(.disablePrettyPrint) {
76+
outputStream.write(transformedSyntax.description)
77+
return
78+
}
79+
80+
let printer = PrettyPrinter(
81+
context: context,
82+
node: transformedSyntax,
83+
printTokenStream: debugOptions.contains(.dumpTokenStream))
84+
outputStream.write(printer.prettyPrint())
85+
}
86+
87+
// TODO: Add an overload of `format` that takes the source text directly.
88+
}

Sources/SwiftFormat/SwiftLinter.swift

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
14+
import SwiftFormatConfiguration
15+
import SwiftFormatCore
16+
import SwiftSyntax
17+
18+
/// Diagnoses and reports problems in Swift source code or syntax trees according to the Swift style
19+
/// guidelines.
20+
public final class SwiftLinter {
21+
22+
/// The configuration settings that control the linter's behavior.
23+
public let configuration: Configuration
24+
25+
/// A diagnostic engine to which lint findings will be reported.
26+
public let diagnosticEngine: DiagnosticEngine
27+
28+
/// Advanced options that are useful when debugging the linter's behavior but are not meant for
29+
/// general use.
30+
public var debugOptions: DebugOptions = []
31+
32+
/// Creates a new Swift code linter with the given configuration.
33+
///
34+
/// - Parameters:
35+
/// - configuration: The configuration settings that control the linter's behavior.
36+
/// - diagnosticEngine: The diagnostic engine to which lint findings will be reported.
37+
public init(configuration: Configuration, diagnosticEngine: DiagnosticEngine) {
38+
self.configuration = configuration
39+
self.diagnosticEngine = diagnosticEngine
40+
}
41+
42+
/// Lints the Swift code at the given file URL.
43+
///
44+
/// - Parameters url: The URL of the file containing the code to format.
45+
/// - Throws: If an unrecoverable error occurs when formatting the code.
46+
public func lint(contentsOf url: URL) throws {
47+
let sourceFile = try SyntaxTreeParser.parse(url)
48+
try lint(syntax: sourceFile, assumingFileURL: url)
49+
}
50+
51+
/// Lints the given Swift syntax tree.
52+
///
53+
/// - Parameters:
54+
/// - syntax: The Swift syntax tree to be converted to be linted.
55+
/// - url: A file URL denoting the filename/path that should be assumed for this syntax tree.
56+
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
57+
/// be written.
58+
/// - Throws: If an unrecoverable error occurs when formatting the code.
59+
public func lint(syntax: Syntax, assumingFileURL url: URL) throws {
60+
let context
61+
= Context(configuration: configuration, diagnosticEngine: diagnosticEngine, fileURL: url)
62+
let pipeline = LintPipeline(context: context)
63+
populate(pipeline)
64+
65+
pipeline.visit(syntax as Syntax)
66+
67+
// TODO: Extend the pretty printer to make it possible to lint spacing and breaking issues.
68+
}
69+
70+
// TODO: Add an overload of `lint` that takes the source text directly.
71+
}

Sources/SwiftFormatConfiguration/Configuration.swift

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ public class Configuration: Codable {
6565
/// Rules for limiting blank lines between members.
6666
public var blankLineBetweenMembers = BlankLineBetweenMembersConfiguration()
6767

68-
/// Rules for adding backticks around special symbols in documentation comments.
69-
public var surroundSymbolsWithBackticks = SurroundSymbolsWithBackticksConfiguration()
70-
7168
/// Constructs a Configuration with all default values.
7269
public init() {
7370
self.version = highestSupportedConfigurationVersion
@@ -104,9 +101,6 @@ public class Configuration: Codable {
104101
self.blankLineBetweenMembers = try container.decodeIfPresent(
105102
BlankLineBetweenMembersConfiguration.self, forKey: .blankLineBetweenMembers)
106103
?? BlankLineBetweenMembersConfiguration()
107-
self.surroundSymbolsWithBackticks = try container.decodeIfPresent(
108-
SurroundSymbolsWithBackticksConfiguration.self, forKey: .surroundSymbolsWithBackticks)
109-
?? SurroundSymbolsWithBackticksConfiguration()
110104
}
111105

112106
public func encode(to encoder: Encoder) throws {
@@ -119,7 +113,6 @@ public class Configuration: Codable {
119113
try container.encode(indentation, forKey: .indentation)
120114
try container.encode(respectsExistingLineBreaks, forKey: .respectsExistingLineBreaks)
121115
try container.encode(blankLineBetweenMembers, forKey: .blankLineBetweenMembers)
122-
try container.encode(surroundSymbolsWithBackticks, forKey: .surroundSymbolsWithBackticks)
123116
}
124117
}
125118

@@ -129,18 +122,6 @@ public struct BlankLineBetweenMembersConfiguration: Codable {
129122
public let ignoreSingleLineProperties = true
130123
}
131124

132-
// TODO(abl): Expand the whitelist and blacklist.
133-
/// Configuration for the SurroundSymbolsWithBackticks rule.
134-
public struct SurroundSymbolsWithBackticksConfiguration: Codable {
135-
/// List of global symbols; added to the list of file-local symbols. Case-sensitive.
136-
public let symbolWhitelist = ["String"]
137-
138-
/// List of symbols to ignore. Case-sensitive.
139-
public let symbolBlacklist = [
140-
"URL", // symbol name and capitalization is the same as the term.
141-
]
142-
}
143-
144125
/// Configuration for the NoPlaygroundLiterals rule.
145126
public struct NoPlaygroundLiteralsConfiguration: Codable {
146127
public enum ResolveBehavior: String, Codable {

Sources/SwiftFormatRules/LineLengthLimit.swift

Lines changed: 0 additions & 24 deletions
This file was deleted.

Sources/SwiftFormatRules/OneStatementPerLine.swift

Lines changed: 0 additions & 29 deletions
This file was deleted.

Sources/SwiftFormatRules/OverloadsAreGrouped.swift

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)