11
11
//===----------------------------------------------------------------------===//
12
12
13
13
import Foundation
14
- import SwiftFormat
14
+ @ _spi ( Internal ) import SwiftFormat
15
15
import SwiftSyntax
16
16
import SwiftParser
17
17
@@ -161,17 +161,19 @@ class Frontend {
161
161
}
162
162
163
163
/// Returns the configuration that applies to the given `.swift` source file, when an explicit
164
- /// configuration path is also perhaps provided.
164
+ /// configuration path is also perhaps provided. Checks for unrecognized rules within the configuration.
165
165
///
166
166
/// - Parameters:
167
167
/// - configurationFilePath: The path to a configuration file that will be loaded, or `nil` to
168
168
/// try to infer it from `swiftFilePath`.
169
169
/// - swiftFilePath: The path to a `.swift` file, which will be used to infer the path to the
170
170
/// configuration file if `configurationFilePath` is nil.
171
+ ///
171
172
/// - Returns: If successful, the returned configuration is the one loaded from
172
173
/// `configurationFilePath` if it was provided, or by searching in paths inferred by
173
174
/// `swiftFilePath` if one exists, or the default configuration otherwise. If an error occurred
174
175
/// when reading the configuration, a diagnostic is emitted and `nil` is returned.
176
+ /// if neither `configurationFilePath` nor `swiftFilePath` were provided, a default `Configuration()` will be returned.
175
177
private func configuration(
176
178
at configurationFileURL: URL ? ,
177
179
orInferredFromSwiftFileAt swiftFileURL: URL ?
@@ -180,7 +182,9 @@ class Frontend {
180
182
// loaded. (Do not try to fall back to a path inferred from the source file path.)
181
183
if let configurationFileURL = configurationFileURL {
182
184
do {
183
- return try configurationLoader. configuration ( at: configurationFileURL)
185
+ let configuration = try configurationLoader. configuration ( at: configurationFileURL)
186
+ self . checkForUnrecognizedRules ( in: configuration)
187
+ return configuration
184
188
} catch {
185
189
diagnosticsEngine. emitError ( " Unable to read configuration: \( error. localizedDescription) " )
186
190
return nil
@@ -192,6 +196,7 @@ class Frontend {
192
196
if let swiftFileURL = swiftFileURL {
193
197
do {
194
198
if let configuration = try configurationLoader. configuration ( forSwiftFileAt: swiftFileURL) {
199
+ self . checkForUnrecognizedRules ( in: configuration)
195
200
return configuration
196
201
}
197
202
// Fall through to the default return at the end of the function.
@@ -207,4 +212,16 @@ class Frontend {
207
212
// default configuration.
208
213
return Configuration ( )
209
214
}
215
+
216
+ /// Checks if all the rules in the given configuration are supported by the registry.
217
+ /// If there are any rules that are not supported, they are emitted as a warning.
218
+ private func checkForUnrecognizedRules( in configuration: Configuration ) {
219
+ // If any rules in the decoded configuration are not supported by the registry,
220
+ // emit them into the diagnosticsEngine as warnings.
221
+ // That way they will be printed out, but we'll continue execution on the valid rules.
222
+ let invalidRules = configuration. rules. filter { !RuleRegistry. rules. keys. contains ( $0. key) }
223
+ for rule in invalidRules {
224
+ diagnosticsEngine. emitWarning ( " Configuration contains an unrecognized rule: \( rule. key) " , location: nil )
225
+ }
226
+ }
210
227
}
0 commit comments