@@ -198,6 +198,7 @@ func printHelp(as type: CLI.OutputType) {
198198 --lenient Suppress errors for unformatted code in --lint mode
199199 --verbose Display detailed formatting output and warnings/errors
200200 --quiet Disables non-critical output messages and warnings
201+ --report The changes report file
201202
202203 SwiftFormat has a number of rules that can be enabled or disabled. By default
203204 most rules are enabled. Use --rules to display all enabled/disabled rules.
@@ -289,6 +290,11 @@ func processArguments(_ args: [String], in directory: String) -> ExitCode {
289290 print ( " warning: \( warning) " , as: . warning)
290291 }
291292
293+ // JSON output
294+ let jsonReporter = args [ " report " ] . map {
295+ JSONReporter ( outputURL: URL ( fileURLWithPath: $0) )
296+ }
297+
292298 // Show help
293299 if args [ " help " ] != nil {
294300 printHelp ( as: . content)
@@ -624,7 +630,7 @@ func processArguments(_ args: [String], in directory: String) -> ExitCode {
624630 }
625631 }
626632 let output = try applyRules ( input, options: options, lineRange: lineRange,
627- verbose: verbose, lint: lint)
633+ verbose: verbose, lint: lint, jsonReporter : jsonReporter )
628634 if let outputURL = outputURL, !useStdout {
629635 if ( try ? String ( contentsOf: outputURL) ) != output, !dryrun {
630636 do {
@@ -692,7 +698,8 @@ func processArguments(_ args: [String], in directory: String) -> ExitCode {
692698 verbose: verbose,
693699 dryrun: dryrun,
694700 lint: lint,
695- cacheURL: cacheURL)
701+ cacheURL: cacheURL,
702+ jsonReporter: jsonReporter)
696703 errors += _errors
697704 } )
698705
@@ -704,6 +711,7 @@ func processArguments(_ args: [String], in directory: String) -> ExitCode {
704711 print ( " warning: No eligible files found at \( inputPaths) . " , as: . warning)
705712 }
706713 print ( " SwiftFormat completed in \( time) . " , as: . success)
714+ try jsonReporter? . write ( )
707715 return printResult ( dryrun, lint, lenient, outputFlags)
708716 } catch {
709717 _ = printWarnings ( errors)
@@ -777,7 +785,7 @@ func computeHash(_ source: String) -> String {
777785}
778786
779787func applyRules( _ source: String , options: Options , lineRange: ClosedRange < Int > ? ,
780- verbose: Bool , lint: Bool ) throws -> String
788+ verbose: Bool , lint: Bool , jsonReporter : JSONReporter ? ) throws -> String
781789{
782790 // Parse source
783791 var tokens = tokenize ( source)
@@ -802,6 +810,7 @@ func applyRules(_ source: String, options: Options, lineRange: ClosedRange<Int>?
802810 let updatedSource = sourceCode ( for: tokens)
803811 if lint, updatedSource != source {
804812 changes. forEach { print ( $0. description, as: . warning) }
813+ jsonReporter? . report ( changes)
805814 }
806815 if verbose {
807816 let rulesApplied = changes. reduce ( into: Set < String > ( ) ) {
@@ -827,7 +836,8 @@ func processInput(_ inputURLs: [URL],
827836 verbose: Bool ,
828837 dryrun: Bool ,
829838 lint: Bool ,
830- cacheURL: URL ? ) -> ( OutputFlags , [ Error ] )
839+ cacheURL: URL ? ,
840+ jsonReporter: JSONReporter ? ) -> ( OutputFlags , [ Error ] )
831841{
832842 // Load cache
833843 let cacheDirectory = cacheURL? . deletingLastPathComponent ( ) . absoluteURL
@@ -914,7 +924,7 @@ func processInput(_ inputURLs: [URL],
914924 }
915925 } else {
916926 output = try applyRules ( input, options: options, lineRange: lineRange,
917- verbose: verbose, lint: lint)
927+ verbose: verbose, lint: lint, jsonReporter : jsonReporter )
918928 if output != input {
919929 sourceHash = nil
920930 }
0 commit comments