diff --git a/Sources/SwiftFormatConfiguration/RuleRegistry+Generated.swift b/Sources/SwiftFormatConfiguration/RuleRegistry+Generated.swift index b7a17de2..f3069a6d 100644 --- a/Sources/SwiftFormatConfiguration/RuleRegistry+Generated.swift +++ b/Sources/SwiftFormatConfiguration/RuleRegistry+Generated.swift @@ -12,8 +12,8 @@ // This file is automatically generated with generate-pipeline. Do Not Edit! -enum RuleRegistry { - static let rules: [String: Bool] = [ +public enum RuleRegistry { + public static let rules: [String: Bool] = [ "AllPublicDeclarationsHaveDocumentation": false, "AlwaysUseLowerCamelCase": true, "AmbiguousTrailingClosureOverload": true, diff --git a/Sources/generate-pipeline/RuleRegistryGenerator.swift b/Sources/generate-pipeline/RuleRegistryGenerator.swift index 7a564420..f6084756 100644 --- a/Sources/generate-pipeline/RuleRegistryGenerator.swift +++ b/Sources/generate-pipeline/RuleRegistryGenerator.swift @@ -40,8 +40,8 @@ final class RuleRegistryGenerator: FileGenerator { // This file is automatically generated with generate-pipeline. Do Not Edit! - enum RuleRegistry { - static let rules: [String: Bool] = [ + publc enum RuleRegistry { + public static let rules: [String: Bool] = [ """ ) diff --git a/Sources/swift-format/Frontend/Frontend.swift b/Sources/swift-format/Frontend/Frontend.swift index 0c8c4a50..ffcc379b 100644 --- a/Sources/swift-format/Frontend/Frontend.swift +++ b/Sources/swift-format/Frontend/Frontend.swift @@ -181,7 +181,9 @@ class Frontend { // loaded. (Do not try to fall back to a path inferred from the source file path.) if let configurationFileURL = configurationFileURL { do { - return try configurationLoader.configuration(at: configurationFileURL) + let configuration = try configurationLoader.configuration(at: configurationFileURL) + self.verifyConfigurationRules(for: configuration) + return configuration } catch { diagnosticsEngine.emitError("Unable to read configuration: \(error.localizedDescription)") return nil @@ -193,6 +195,7 @@ class Frontend { if let swiftFileURL = swiftFileURL { do { if let configuration = try configurationLoader.configuration(forSwiftFileAt: swiftFileURL) { + self.verifyConfigurationRules(for: configuration) return configuration } // Fall through to the default return at the end of the function. @@ -208,4 +211,16 @@ class Frontend { // default configuration. return Configuration() } + + /// Checks if all the rules in the given configuration are supported by the registry. + /// If there are any rules that are not supported, they are emitted as a warning. + private func verifyConfigurationRules(for configuration: Configuration) -> Void { + // If any rules in the decoded configuration are not supported by the registry, + // emit them into the diagnosticsEngine as warnings. + // That way they will be printed out, but we'll continue execution on the valid rules. + let invalidRules = configuration.rules.filter { !RuleRegistry.rules.keys.contains($0.key) } + if !invalidRules.isEmpty { + diagnosticsEngine.emitWarning("Configuration contains an unsupported rule: \(invalidRules.keys.joined(separator: ", "))", location: nil) + } + } } diff --git a/Sources/swift-format/Utilities/DiagnosticsEngine.swift b/Sources/swift-format/Utilities/DiagnosticsEngine.swift index 0eead434..e568ccf0 100644 --- a/Sources/swift-format/Utilities/DiagnosticsEngine.swift +++ b/Sources/swift-format/Utilities/DiagnosticsEngine.swift @@ -65,6 +65,20 @@ final class DiagnosticsEngine { message: message)) } + /// Emits a generic warning message. + /// + /// - Parameters: + /// - message: The message associated with the error. + /// - location: The location in the source code associated with the error, or nil if there is no + /// location associated with the error. + func emitWarning(_ message: String, location: SourceLocation? = nil) { + emit( + Diagnostic( + severity: .warning, + location: location.map(Diagnostic.Location.init), + message: message)) + } + /// Emits a finding from the linter and any of its associated notes as diagnostics. /// /// - Parameter finding: The finding that should be emitted.