Skip to content

Remove try before Parser.parse calls. #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/SwiftFormat/Parsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func parseAndEmitDiagnostics(
parsingDiagnosticHandler: ((Diagnostic, SourceLocation) -> Void)? = nil
) throws -> SourceFileSyntax {
let sourceFile =
try operatorTable.foldAll(Parser.parse(source: source)) { _ in }.as(SourceFileSyntax.self)!
operatorTable.foldAll(Parser.parse(source: source)) { _ in }.as(SourceFileSyntax.self)!

let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: sourceFile)
if let parsingDiagnosticHandler = parsingDiagnosticHandler {
Expand Down
2 changes: 1 addition & 1 deletion Sources/generate-pipeline/RuleCollector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class RuleCollector {

let fileURL = url.appendingPathComponent(baseName)
let fileInput = try String(contentsOf: fileURL)
let sourceFile = try Parser.parse(source: fileInput)
let sourceFile = Parser.parse(source: fileInput)

for statement in sourceFile.statements {
guard let detectedRule = self.detectedRule(at: statement) else { continue }
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftFormatCoreTests/RuleMaskTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class RuleMaskTests: XCTestCase {
private func createMask(sourceText: String) -> RuleMask {
let fileURL = URL(fileURLWithPath: "/tmp/test.swift")
converter = SourceLocationConverter(file: fileURL.path, source: sourceText)
let syntax = try! Parser.parse(source: sourceText)
let syntax = Parser.parse(source: sourceText)
return RuleMask(syntaxNode: Syntax(syntax), sourceLocationConverter: converter)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,7 @@ final class WhitespaceLinterPerformanceTests: DiagnosingTestCase {
/// - input: The user's input text.
/// - expected: The formatted text.
private func performWhitespaceLint(input: String, expected: String) {
let sourceFileSyntax: SourceFileSyntax
do {
sourceFileSyntax = try Parser.parse(source: input)
} catch {
XCTFail("Parsing failed with error: \(error)")
return
}

let sourceFileSyntax = Parser.parse(source: input)
let context = makeContext(sourceFileSyntax: sourceFileSyntax)
let linter = WhitespaceLinter(user: input, formatted: expected, context: context)
linter.lint()
Expand Down
16 changes: 4 additions & 12 deletions Tests/SwiftFormatPrettyPrintTests/PrettyPrintTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,10 @@ class PrettyPrintTestCase: DiagnosingTestCase {
private func prettyPrintedSource(
_ source: String, configuration: Configuration, whitespaceOnly: Bool
) -> String? {
let sourceFileSyntax: SourceFileSyntax
do {
// Ignore folding errors for unrecognized operators so that we fallback to a reasonable
// default instead of throwing an error.
sourceFileSyntax =
try OperatorTable.standardOperators.foldAll(Parser.parse(source: source)) { _ in }
.as(SourceFileSyntax.self)!
} catch {
XCTFail("Parsing failed with error: \(error)")
return nil
}

// Ignore folding errors for unrecognized operators so that we fallback to a reasonable default.
let sourceFileSyntax =
OperatorTable.standardOperators.foldAll(Parser.parse(source: source)) { _ in }
.as(SourceFileSyntax.self)!
let context = makeContext(sourceFileSyntax: sourceFileSyntax, configuration: configuration)
let printer = PrettyPrinter(
context: context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ImportsXCTestVisitorTests: DiagnosingTestCase {
/// Parses the given source, makes a new `Context`, then populates and returns its `XCTest`
/// import state.
private func makeContextAndSetImportsXCTest(source: String) throws -> Context.XCTestImportState {
let sourceFile = try Parser.parse(source: source)
let sourceFile = Parser.parse(source: source)
let context = makeContext(sourceFileSyntax: sourceFile)
setImportsXCTest(context: context, sourceFile: sourceFile)
return context.importsXCTest
Expand Down
16 changes: 2 additions & 14 deletions Tests/SwiftFormatRulesTests/LintOrFormatRuleTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ class LintOrFormatRuleTestCase: DiagnosingTestCase {
file: StaticString = #file,
line: UInt = #line
) {
let sourceFileSyntax: SourceFileSyntax
do {
sourceFileSyntax = try Parser.parse(source: input)
} catch {
XCTFail("\(error)", file: file, line: line)
return
}
let sourceFileSyntax = Parser.parse(source: input)

// Force the rule to be enabled while we test it.
var configuration = Configuration()
Expand Down Expand Up @@ -62,13 +56,7 @@ class LintOrFormatRuleTestCase: DiagnosingTestCase {
file: StaticString = #file,
line: UInt = #line
) {
let sourceFileSyntax: SourceFileSyntax
do {
sourceFileSyntax = try Parser.parse(source: input)
} catch {
XCTFail("\(error)", file: file, line: line)
return
}
let sourceFileSyntax = Parser.parse(source: input)

// Force the rule to be enabled while we test it.
var configuration = configuration ?? Configuration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ class WhitespaceTestCase: DiagnosingTestCase {
/// - expected: The formatted text.
/// - linelength: The maximum allowed line length of the output.
final func performWhitespaceLint(input: String, expected: String, linelength: Int? = nil) {
let sourceFileSyntax: SourceFileSyntax
do {
sourceFileSyntax = try Parser.parse(source: input)
} catch {
XCTFail("Parsing failed with error: \(error)")
return
}

let sourceFileSyntax = Parser.parse(source: input)
var configuration = Configuration()
if let linelength = linelength {
configuration.lineLength = linelength
Expand Down