Skip to content

Commit

Permalink
Merge pull request #593 from allevato/fewer-modules
Browse files Browse the repository at this point in the history
Collapse almost everything into a single `SwiftFormat` module.
  • Loading branch information
allevato committed Aug 16, 2023
2 parents b896e0f + 69d4084 commit a7f16de
Show file tree
Hide file tree
Showing 195 changed files with 226 additions and 265 deletions.
94 changes: 11 additions & 83 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ let package = Package(
name: "SwiftFormat",
dependencies: [
"SwiftFormatConfiguration",
"SwiftFormatCore",
"SwiftFormatPrettyPrint",
"SwiftFormatRules",
"SwiftFormatWhitespaceLinter",
.product(name: "Markdown", package: "swift-markdown"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftOperators", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
Expand All @@ -64,46 +61,13 @@ let package = Package(
name: "SwiftFormatConfiguration"
),
.target(
name: "SwiftFormatCore",
dependencies: [
"SwiftFormatConfiguration",
.product(name: "Markdown", package: "swift-markdown"),
.product(name: "SwiftOperators", package: "swift-syntax"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
]
),
.target(
name: "SwiftFormatRules",
dependencies: [
"SwiftFormatCore",
"SwiftFormatConfiguration",
.product(name: "Markdown", package: "swift-markdown"),
]
),
.target(
name: "SwiftFormatPrettyPrint",
dependencies: [
"SwiftFormatCore",
"SwiftFormatConfiguration",
.product(name: "SwiftOperators", package: "swift-syntax"),
]
),
.target(
name: "SwiftFormatTestSupport",
name: "_SwiftFormatTestSupport",
dependencies: [
"SwiftFormatCore",
"SwiftFormatRules",
"SwiftFormat",
"SwiftFormatConfiguration",
.product(name: "SwiftOperators", package: "swift-syntax"),
]
),
.target(
name: "SwiftFormatWhitespaceLinter",
dependencies: [
"SwiftFormatCore",
.product(name: "SwiftSyntax", package: "swift-syntax"),
]
),
.plugin(
name: "Format Source Code",
capability: .command(
Expand Down Expand Up @@ -133,8 +97,7 @@ let package = Package(
.executableTarget(
name: "generate-pipeline",
dependencies: [
"SwiftFormatCore",
"SwiftFormatRules",
"SwiftFormat",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
]
Expand All @@ -144,7 +107,6 @@ let package = Package(
dependencies: [
"SwiftFormat",
"SwiftFormatConfiguration",
"SwiftFormatCore",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
Expand All @@ -155,60 +117,26 @@ let package = Package(
name: "SwiftFormatConfigurationTests",
dependencies: ["SwiftFormatConfiguration"]
),
.testTarget(
name: "SwiftFormatCoreTests",
dependencies: [
"SwiftFormatConfiguration",
"SwiftFormatCore",
.product(name: "Markdown", package: "swift-markdown"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
]
),
.testTarget(
name: "SwiftFormatPerformanceTests",
dependencies: [
"SwiftFormatTestSupport",
"SwiftFormatWhitespaceLinter",
"SwiftFormat",
"_SwiftFormatTestSupport",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
]
),
.testTarget(
name: "SwiftFormatPrettyPrintTests",
name: "SwiftFormatTests",
dependencies: [
"SwiftFormat",
"SwiftFormatConfiguration",
"SwiftFormatCore",
"SwiftFormatPrettyPrint",
"SwiftFormatRules",
"SwiftFormatTestSupport",
.product(name: "SwiftSyntax", package: "swift-syntax"),
"_SwiftFormatTestSupport",
.product(name: "Markdown", package: "swift-markdown"),
.product(name: "SwiftOperators", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
]
),
.testTarget(
name: "SwiftFormatRulesTests",
dependencies: [
"SwiftFormatConfiguration",
"SwiftFormatCore",
"SwiftFormatPrettyPrint",
"SwiftFormatRules",
"SwiftFormatTestSupport",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
]
),
.testTarget(
name: "SwiftFormatWhitespaceLinterTests",
dependencies: [
"SwiftFormatConfiguration",
"SwiftFormatCore",
"SwiftFormatTestSupport",
"SwiftFormatWhitespaceLinter",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
]
),
]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
import Foundation
import SwiftDiagnostics
import SwiftFormatConfiguration
import SwiftFormatCore
import SwiftFormatPrettyPrint
import SwiftFormatRules
import SwiftOperators
import SwiftSyntax

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
import Foundation
import SwiftDiagnostics
import SwiftFormatConfiguration
import SwiftFormatCore
import SwiftFormatPrettyPrint
import SwiftFormatRules
import SwiftFormatWhitespaceLinter
import SwiftOperators
import SwiftSyntax

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//
//===----------------------------------------------------------------------===//

import SwiftFormatCore
import SwiftSyntax

fileprivate final class AddModifierRewriter: SyntaxRewriter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import SwiftParser
///
/// Specifically, it is the container for the shared configuration, diagnostic consumer, and URL of
/// the current file.
@_spi(Rules)
public final class Context {

/// Tracks whether `XCTest` has been imported so that certain logic can be modified for files that
Expand All @@ -37,28 +38,28 @@ public final class Context {
}

/// The configuration for this run of the pipeline, provided by a configuration JSON file.
public let configuration: Configuration
let configuration: Configuration

/// Defines the operators and their precedence relationships that were used during parsing.
public let operatorTable: OperatorTable
let operatorTable: OperatorTable

/// Emits findings to the finding consumer.
public let findingEmitter: FindingEmitter
let findingEmitter: FindingEmitter

/// The URL of the file being linted or formatted.
public let fileURL: URL
let fileURL: URL

/// Indicates whether the file is known to import XCTest.
public var importsXCTest: XCTestImportState

/// An object that converts `AbsolutePosition` values to `SourceLocation` values.
public let sourceLocationConverter: SourceLocationConverter
let sourceLocationConverter: SourceLocationConverter

/// Contains the rules have been disabled by comments for certain line numbers.
public let ruleMask: RuleMask
let ruleMask: RuleMask

/// Contains all the available rules' names associated to their types' object identifiers.
public let ruleNameCache: [ObjectIdentifier: String]
let ruleNameCache: [ObjectIdentifier: String]

/// Creates a new Context with the provided configuration, diagnostic engine, and file URL.
public init(
Expand Down Expand Up @@ -87,7 +88,7 @@ public final class Context {

/// Given a rule's name and the node it is examining, determine if the rule is disabled at this
/// location or not.
public func isRuleEnabled<R: Rule>(_ rule: R.Type, node: Syntax) -> Bool {
func isRuleEnabled<R: Rule>(_ rule: R.Type, node: Syntax) -> Bool {
let loc = node.startLocation(converter: self.sourceLocationConverter)

assert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import SwiftSyntax
/// also the nested information that can be provided on a parameter. For example, when a parameter
/// is a function type, it can provide not only a brief summary but also its own parameter and
/// return value descriptions.
@_spi(Testing)
public struct DocumentationComment {
/// A description of a parameter in a documentation comment.
public struct Parameter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import SwiftSyntax
/// This type should be used when only the text of the comment is important, not the Markdown
/// structural organization. It automatically handles trimming leading indentation from comments as
/// well as "ASCII art" in block comments (i.e., leading asterisks on each line).
@_spi(Testing)
public struct DocumentationCommentText {
/// Denotes the kind of punctuation used to introduce the comment.
public enum Introducer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import SwiftSyntax

extension Finding.Location {
/// Creates a new `Finding.Location` by converting the given `SourceLocation` from `SwiftSyntax`.
public init(_ sourceLocation: SourceLocation) {
init(_ sourceLocation: SourceLocation) {
self.init(file: sourceLocation.file, line: sourceLocation.line, column: sourceLocation.column)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/// If the consumer function is nil, then the `emit` function is a no-op. This allows callers, such
/// as lint/format rules and the pretty-printer, to emit findings unconditionally, without wrapping
/// each call in a check about whether the client is interested in receiving those findings or not.
public final class FindingEmitter {
final class FindingEmitter {
/// An optional function that will be called and passed a finding each time one is emitted.
private let consumer: ((Finding) -> Void)?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
//
//===----------------------------------------------------------------------===//

import SwiftFormatCore

/// A type that invokes individual format rules.
///
/// Note that this type is not a `SyntaxVisitor` or `SyntaxRewriter`. That is because, at this time,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//
//===----------------------------------------------------------------------===//

import SwiftFormatCore
import SwiftSyntax

/// A visitor that determines if the target source file imports `XCTest`.
Expand Down Expand Up @@ -53,6 +52,7 @@ private class ImportsXCTestVisitor: SyntaxVisitor {
/// - Parameters:
/// - context: The context information of the target source file.
/// - sourceFile: The file to be visited.
@_spi(Testing)
public func setImportsXCTest(context: Context, sourceFile: SourceFileSyntax) {
guard context.importsXCTest == .notDetermined else { return }
let visitor = ImportsXCTestVisitor(context: context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SwiftSyntax
///
/// Eventually we should get rid of this and update the core formatting code to adjust to the new
/// behavior, but this workaround lets us keep the current implementation without larger changes.
@_spi(Testing)
public func restoringLegacyTriviaBehavior(_ sourceFile: SourceFileSyntax) -> SourceFileSyntax {
return LegacyTriviaBehaviorRewriter().visit(sourceFile)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//
//===----------------------------------------------------------------------===//

import SwiftFormatCore
import SwiftSyntax

/// A syntax visitor that delegates to individual rules for linting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import Foundation
import SwiftDiagnostics
import SwiftFormatCore
import SwiftOperators
import SwiftParser
import SwiftParserDiagnostics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

// This file is automatically generated with generate-pipeline. Do Not Edit!

import SwiftFormatCore
import SwiftFormatRules
import SwiftSyntax

/// A syntax visitor that delegates to individual rules for linting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Foundation
import SwiftSyntax

/// A Rule is a linting or formatting pass that executes in a given context.
@_spi(Rules)
public protocol Rule {
/// The context in which the rule is executed.
var context: Context { get }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import SwiftSyntax
///
/// The rules themselves reference RuleMask to see if it is disabled for the line it is currently
/// examining.
@_spi(Testing)
public class RuleMask {
/// Stores the source ranges in which all rules are ignored.
private var allRulesIgnoredRanges: [SourceRange] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// This file is automatically generated with generate-pipeline. Do Not Edit!

/// By default, the `Rule.ruleName` should be the name of the implementing rule type.
@_spi(Testing)
public let ruleNameCache: [ObjectIdentifier: String] = [
ObjectIdentifier(AllPublicDeclarationsHaveDocumentation.self): "AllPublicDeclarationsHaveDocumentation",
ObjectIdentifier(AlwaysUseLowerCamelCase.self): "AlwaysUseLowerCamelCase",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/// The enablement of a lint/format rule based on the presence or absence of comment directives in
/// the source file.
@_spi(Testing)
public enum RuleState {

/// There is no explicit information in the source file about whether the rule should be enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
import SwiftSyntax

/// A rule that both formats and lints a given file.
open class SyntaxFormatRule: SyntaxRewriter, Rule {
@_spi(Rules)
public class SyntaxFormatRule: SyntaxRewriter, Rule {
/// Whether this rule is opt-in, meaning it's disabled by default. Rules are opt-out unless they
/// override this property.
open class var isOptIn: Bool {
public class var isOptIn: Bool {
return false
}

Expand All @@ -28,7 +29,7 @@ open class SyntaxFormatRule: SyntaxRewriter, Rule {
self.context = context
}

open override func visitAny(_ node: Syntax) -> Syntax? {
public override func visitAny(_ node: Syntax) -> Syntax? {
// If the rule is not enabled, then return the node unmodified; otherwise, returning nil tells
// SwiftSyntax to continue with the standard dispatch.
guard context.isRuleEnabled(type(of: self), node: node) else { return node }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import Foundation
import SwiftSyntax

/// A rule that lints a given file.
open class SyntaxLintRule: SyntaxVisitor, Rule {
@_spi(Rules)
public class SyntaxLintRule: SyntaxVisitor, Rule {
/// Whether this rule is opt-in, meaning it's disabled by default. Rules are opt-out unless they
/// override this property.
open class var isOptIn: Bool {
public class var isOptIn: Bool {
return false
}

Expand Down
Loading

0 comments on commit a7f16de

Please sign in to comment.