@@ -109,7 +109,7 @@ class Frontend {
109
109
/// Processes source content from standard input.
110
110
private func processStandardInput( ) {
111
111
guard let configuration = configuration (
112
- at : lintFormatOptions. configurationPath . map ( URL . init ( fileURLWithPath : ) ) ,
112
+ fromPathOrString : lintFormatOptions. configuration ,
113
113
orInferredFromSwiftFileAt: nil )
114
114
else {
115
115
// Already diagnosed in the called method.
@@ -150,7 +150,7 @@ class Frontend {
150
150
151
151
guard
152
152
let configuration = configuration (
153
- at : lintFormatOptions. configurationPath . map ( URL . init ( fileURLWithPath : ) ) ,
153
+ fromPathOrString : lintFormatOptions. configuration ,
154
154
orInferredFromSwiftFileAt: url)
155
155
else {
156
156
// Already diagnosed in the called method.
@@ -161,31 +161,44 @@ 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. Checks for unrecognized rules within the configuration.
164
+ /// configuration path is also perhaps provided.
165
+ ///
166
+ /// This method also checks for unrecognized rules within the configuration.
165
167
///
166
168
/// - Parameters:
167
- /// - configurationFilePath: The path to a configuration file that will be loaded, or `nil` to
168
- /// try to infer it from `swiftFilePath`.
169
+ /// - pathOrString: A string containing either the path to a configuration file that will be
170
+ /// loaded, JSON configuration data directly, or `nil` to try to infer it from
171
+ /// `swiftFilePath`.
169
172
/// - swiftFilePath: The path to a `.swift` file, which will be used to infer the path to the
170
173
/// configuration file if `configurationFilePath` is nil.
171
174
///
172
- /// - Returns: If successful, the returned configuration is the one loaded from
173
- /// `configurationFilePath` if it was provided, or by searching in paths inferred by
174
- /// `swiftFilePath` if one exists, or the default configuration otherwise. If an error occurred
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
+ /// - Returns: If successful, the returned configuration is the one loaded from `pathOrString` if
176
+ /// it was provided, or by searching in paths inferred by `swiftFilePath` if one exists, or the
177
+ /// default configuration otherwise. If an error occurred when reading the configuration, a
178
+ /// diagnostic is emitted and `nil` is returned. If neither `pathOrString` nor `swiftFilePath`
179
+ /// were provided, a default `Configuration()` will be returned.
177
180
private func configuration(
178
- at configurationFileURL : URL ? ,
181
+ fromPathOrString pathOrString : String ? ,
179
182
orInferredFromSwiftFileAt swiftFileURL: URL ?
180
183
) -> Configuration ? {
181
- // If an explicit configuration file path was given, try to load it and fail if it cannot be
182
- // loaded. (Do not try to fall back to a path inferred from the source file path.)
183
- if let configurationFileURL = configurationFileURL {
184
+ if let pathOrString = pathOrString {
185
+ // If an explicit configuration file path was given, try to load it and fail if it cannot be
186
+ // loaded. (Do not try to fall back to a path inferred from the source file path.)
187
+ let configurationFileURL = URL ( fileURLWithPath: pathOrString)
184
188
do {
185
189
let configuration = try configurationLoader. configuration ( at: configurationFileURL)
186
190
self . checkForUnrecognizedRules ( in: configuration)
187
191
return configuration
188
192
} catch {
193
+ // If we failed to load this from the path, try interpreting the string as configuration
194
+ // data itself because the user might have written something like `--configuration '{...}'`,
195
+ let data = pathOrString. data ( using: . utf8) !
196
+ if let configuration = try ? Configuration ( data: data) {
197
+ return configuration
198
+ }
199
+
200
+ // Fail if the configuration flag was neither a valid file path nor valid configuration
201
+ // data.
189
202
diagnosticsEngine. emitError ( " Unable to read configuration: \( error. localizedDescription) " )
190
203
return nil
191
204
}
0 commit comments