Skip to content

Commit 07c7442

Browse files
authored
Merge pull request #425 from dylansturg/unparseable
Fix ignore-unparsable-files flag so that it works.
2 parents aeac83e + 49f8ee3 commit 07c7442

File tree

9 files changed

+29
-133
lines changed

9 files changed

+29
-133
lines changed

Package.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,6 @@ let package = Package(
112112
]
113113
),
114114

115-
.testTarget(
116-
name: "SwiftFormatTests",
117-
dependencies: [
118-
"SwiftFormat",
119-
.product(name: "SwiftSyntax", package: "swift-syntax"),
120-
.product(name: "SwiftParser", package: "swift-syntax"),
121-
]
122-
),
123115
.testTarget(
124116
name: "SwiftFormatConfigurationTests",
125117
dependencies: ["SwiftFormatConfiguration"]

Sources/SwiftFormat/Parsing.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,19 @@ func parseAndEmitDiagnostics(
4040
let sourceFile =
4141
try operatorTable.foldAll(Parser.parse(source: source)) { _ in }.as(SourceFileSyntax.self)!
4242

43+
let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: sourceFile)
4344
if let parsingDiagnosticHandler = parsingDiagnosticHandler {
4445
let expectedConverter =
4546
SourceLocationConverter(file: url?.path ?? "<unknown>", tree: sourceFile)
46-
let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: sourceFile)
4747
for diagnostic in diagnostics {
4848
let location = diagnostic.location(converter: expectedConverter)
4949
parsingDiagnosticHandler(diagnostic, location)
5050
}
5151
}
5252

53+
guard diagnostics.isEmpty else {
54+
throw SwiftFormatError.fileContainsInvalidSyntax
55+
}
56+
5357
return sourceFile
5458
}

Sources/SwiftFormat/SwiftFormatError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ public enum SwiftFormatError: Error {
2222
case isDirectory
2323

2424
/// The file contains invalid or unrecognized Swift syntax and cannot be handled safely.
25-
case fileContainsInvalidSyntax(position: AbsolutePosition)
25+
case fileContainsInvalidSyntax
2626
}

Sources/SwiftFormat/SwiftFormatter.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,6 @@ public final class SwiftFormatter {
146146
syntax: SourceFileSyntax, operatorTable: OperatorTable,
147147
assumingFileURL url: URL?, source: String?, to outputStream: inout Output
148148
) throws {
149-
if let position = _firstInvalidSyntaxPosition(in: Syntax(syntax)) {
150-
throw SwiftFormatError.fileContainsInvalidSyntax(position: position)
151-
}
152-
153149
let assumedURL = url ?? URL(fileURLWithPath: "source")
154150
let context = Context(
155151
configuration: configuration, operatorTable: operatorTable, findingConsumer: findingConsumer,

Sources/SwiftFormat/SwiftLinter.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,6 @@ public final class SwiftLinter {
134134
assumingFileURL url: URL,
135135
source: String?
136136
) throws {
137-
if let position = _firstInvalidSyntaxPosition(in: Syntax(syntax)) {
138-
throw SwiftFormatError.fileContainsInvalidSyntax(position: position)
139-
}
140-
141137
let context = Context(
142138
configuration: configuration, operatorTable: operatorTable, findingConsumer: findingConsumer,
143139
fileURL: url, sourceFileSyntax: syntax, source: source, ruleNameCache: ruleNameCache)

Sources/SwiftFormat/SyntaxValidatingVisitor.swift

Lines changed: 0 additions & 75 deletions
This file was deleted.

Sources/swift-format/Frontend/FormatFrontend.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Foundation
14+
import SwiftDiagnostics
1415
import SwiftFormat
1516
import SwiftFormatConfiguration
1617
import SwiftSyntax
@@ -39,6 +40,13 @@ class FormatFrontend: Frontend {
3940
return
4041
}
4142

43+
let diagnosticHandler: (Diagnostic, SourceLocation) -> () = { (diagnostic, location) in
44+
guard !self.lintFormatOptions.ignoreUnparsableFiles else {
45+
// No diagnostics should be emitted in this mode.
46+
return
47+
}
48+
self.diagnosticsEngine.consumeParserDiagnostic(diagnostic, location)
49+
}
4250
var stdoutStream = FileHandleTextOutputStream(FileHandle.standardOutput)
4351
do {
4452
if inPlace {
@@ -47,7 +55,7 @@ class FormatFrontend: Frontend {
4755
source: source,
4856
assumingFileURL: url,
4957
to: &buffer,
50-
parsingDiagnosticHandler: diagnosticsEngine.consumeParserDiagnostic)
58+
parsingDiagnosticHandler: diagnosticHandler)
5159

5260
if buffer != source {
5361
let bufferData = buffer.data(using: .utf8)! // Conversion to UTF-8 cannot fail
@@ -58,13 +66,13 @@ class FormatFrontend: Frontend {
5866
source: source,
5967
assumingFileURL: url,
6068
to: &stdoutStream,
61-
parsingDiagnosticHandler: diagnosticsEngine.consumeParserDiagnostic)
69+
parsingDiagnosticHandler: diagnosticHandler)
6270
}
6371
} catch SwiftFormatError.fileNotReadable {
6472
diagnosticsEngine.emitError(
6573
"Unable to format \(url.relativePath): file is not readable or does not exist.")
6674
return
67-
} catch SwiftFormatError.fileContainsInvalidSyntax(let position) {
75+
} catch SwiftFormatError.fileContainsInvalidSyntax {
6876
guard !lintFormatOptions.ignoreUnparsableFiles else {
6977
guard !inPlace else {
7078
// For in-place mode, nothing is expected to stdout and the file shouldn't be modified.
@@ -73,9 +81,7 @@ class FormatFrontend: Frontend {
7381
stdoutStream.write(source)
7482
return
7583
}
76-
let location = SourceLocationConverter(file: url.path, source: source).location(for: position)
77-
diagnosticsEngine.emitError(
78-
"file contains invalid or unrecognized Swift syntax.", location: location)
84+
// Otherwise, relevant diagnostics about the problematic nodes have been emitted.
7985
return
8086
} catch {
8187
diagnosticsEngine.emitError("Unable to format \(url.relativePath): \(error)")

Sources/swift-format/Frontend/LintFrontend.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Foundation
14+
import SwiftDiagnostics
1415
import SwiftFormat
1516
import SwiftFormatConfiguration
1617
import SwiftSyntax
@@ -32,20 +33,24 @@ class LintFrontend: Frontend {
3233
do {
3334
try linter.lint(
3435
source: source,
35-
assumingFileURL: url,
36-
parsingDiagnosticHandler: diagnosticsEngine.consumeParserDiagnostic)
36+
assumingFileURL: url) { (diagnostic, location) in
37+
guard !self.lintFormatOptions.ignoreUnparsableFiles else {
38+
// No diagnostics should be emitted in this mode.
39+
return
40+
}
41+
self.diagnosticsEngine.consumeParserDiagnostic(diagnostic, location)
42+
}
43+
3744
} catch SwiftFormatError.fileNotReadable {
3845
diagnosticsEngine.emitError(
3946
"Unable to lint \(url.relativePath): file is not readable or does not exist.")
4047
return
41-
} catch SwiftFormatError.fileContainsInvalidSyntax(let position) {
48+
} catch SwiftFormatError.fileContainsInvalidSyntax {
4249
guard !lintFormatOptions.ignoreUnparsableFiles else {
4350
// The caller wants to silently ignore this error.
4451
return
4552
}
46-
let location = SourceLocationConverter(file: url.path, source: source).location(for: position)
47-
diagnosticsEngine.emitError(
48-
"file contains invalid or unrecognized Swift syntax.", location: location)
53+
// Otherwise, relevant diagnostics about the problematic nodes have been emitted.
4954
return
5055
} catch {
5156
diagnosticsEngine.emitError("Unable to lint \(url.relativePath): \(error)")

Tests/SwiftFormatTests/SyntaxValidatingVisitorTests.swift

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)