@@ -24,12 +24,13 @@ import TSCBasic
24
24
/// - configuration: The `Configuration` that contains user-specific settings.
25
25
/// - sourceFile: A file handle from which to read the source code to be linted.
26
26
/// - assumingFilename: The filename of the source file, used in diagnostic output.
27
+ /// - ignoreUnparsableFiles: Whether or not to ignore files that contain syntax errors.
27
28
/// - debugOptions: The set containing any debug options that were supplied on the command line.
28
29
/// - diagnosticEngine: A diagnostic collector that handles diagnostic messages.
29
30
/// - Returns: Zero if there were no lint errors, otherwise a non-zero number.
30
31
func lintMain(
31
32
configuration: Configuration , sourceFile: FileHandle , assumingFilename: String ? ,
32
- debugOptions: DebugOptions , diagnosticEngine: DiagnosticEngine
33
+ ignoreUnparsableFiles : Bool , debugOptions: DebugOptions , diagnosticEngine: DiagnosticEngine
33
34
) {
34
35
let linter = SwiftLinter ( configuration: configuration, diagnosticEngine: diagnosticEngine)
35
36
linter. debugOptions = debugOptions
@@ -49,6 +50,9 @@ func lintMain(
49
50
Diagnostic . Message ( . error, " Unable to lint \( path) : file is not readable or does not exist. " ) )
50
51
return
51
52
} catch SwiftFormatError . fileContainsInvalidSyntax( let position) {
53
+ guard !ignoreUnparsableFiles else {
54
+ return
55
+ }
52
56
let path = assumingFileURL. path
53
57
let location = SourceLocationConverter ( file: path, source: source) . location ( for: position)
54
58
diagnosticEngine. diagnose (
@@ -69,12 +73,13 @@ func lintMain(
69
73
/// - sourceFile: A file handle from which to read the source code to be linted.
70
74
/// - assumingFilename: The filename of the source file, used in diagnostic output.
71
75
/// - inPlace: Whether or not to overwrite the current file when formatting.
76
+ /// - ignoreUnparsableFiles: Whether or not to ignore files that contain syntax errors.
72
77
/// - debugOptions: The set containing any debug options that were supplied on the command line.
73
78
/// - diagnosticEngine: A diagnostic collector that handles diagnostic messages.
74
79
/// - Returns: Zero if there were no format errors, otherwise a non-zero number.
75
80
func formatMain(
76
81
configuration: Configuration , sourceFile: FileHandle , assumingFilename: String ? , inPlace: Bool ,
77
- debugOptions: DebugOptions , diagnosticEngine: DiagnosticEngine
82
+ ignoreUnparsableFiles : Bool , debugOptions: DebugOptions , diagnosticEngine: DiagnosticEngine
78
83
) {
79
84
// Even though `diagnosticEngine` is defined, it's use is reserved for fatal messages. Pass nil
80
85
// to the formatter to suppress other messages since they will be fixed or can't be automatically
@@ -111,6 +116,15 @@ func formatMain(
111
116
. error, " Unable to format \( path) : file is not readable or does not exist. " ) )
112
117
return
113
118
} catch SwiftFormatError . fileContainsInvalidSyntax( let position) {
119
+ guard !ignoreUnparsableFiles else {
120
+ guard !inPlace else {
121
+ // For in-place mode, nothing is expected to stdout and the file shouldn't be modified.
122
+ return
123
+ }
124
+ stdoutStream. write ( source)
125
+ stdoutStream. flush ( )
126
+ return
127
+ }
114
128
let path = assumingFileURL. path
115
129
let location = SourceLocationConverter ( file: path, source: source) . location ( for: position)
116
130
diagnosticEngine. diagnose (
0 commit comments