Skip to content

Commit

Permalink
Only skip autocorrect on files with errors (#3933)
Browse files Browse the repository at this point in the history
* Only skip autocorrect on files with errors

* Use other kind of warning so tests succeed on Linux

* Update CHANGELOG.md

Co-authored-by: JP Simard <[email protected]>

Co-authored-by: JP Simard <[email protected]>
  • Loading branch information
marcelofabri and jpsim authored Apr 5, 2022
1 parent cb5fa04 commit 0fbf010
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
[Juozas Valancius](https://github.com/juozasvalancius)
[#3840](https://github.com/realm/SwiftLint/issues/3840)

* Don't skip autocorrect on files that have parser warnings. Only files with
errors reported by the Swift parser will be skipped.
[Marcelo Fabri](https://github.com/marcelofabri)
[#3343](https://github.com/realm/SwiftLint/issues/3343)

#### Bug Fixes

* Fix false positives in `unused_closure_parameter` when using parameters with
Expand Down
16 changes: 11 additions & 5 deletions Source/SwiftLintFramework/Models/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,17 @@ public struct CollectedLinter {
}

if let parserDiagnostics = file.parserDiagnostics {
queuedPrintError(
"Skipping correcting file because it produced Swift parser diagnostics: \(file.path ?? "<nopath>")"
)
queuedPrintError(toJSON(["diagnostics": parserDiagnostics]))
return []
let errorDiagnostics = parserDiagnostics.filter { diagnostic in
diagnostic["key.severity"] as? String == "source.diagnostic.severity.error"
}

if errorDiagnostics.isNotEmpty {
queuedPrintError(
"Skipping correcting file because it produced Swift parser errors: \(file.path ?? "<nopath>")"
)
queuedPrintError(toJSON(["diagnostics": errorDiagnostics]))
return []
}
}

var corrections = [Correction]()
Expand Down
38 changes: 37 additions & 1 deletion Tests/SwiftLintFrameworkTests/ParserDiagnosticsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,47 @@
import XCTest

final class ParserDiagnosticsTests: XCTestCase {
func testFileWithParserDiagnostics() {
func testFileWithParserErrorDiagnostics() {
parserDiagnosticsDisabledForTests = false
XCTAssertNotNil(SwiftLintFile(contents: "importz Foundation").parserDiagnostics)
}

func testFileWithParserErrorDiagnosticsDoesntAutocorrect() throws {
let contents = """
importz Foundation
print(CGPointZero)
"""
XCTAssertNotNil(SwiftLintFile(contents: contents).parserDiagnostics)

let ruleDescription = LegacyConstantRule.description
.with(corrections: [Example(contents): Example(contents)])

let config = try XCTUnwrap(makeConfig(nil, ruleDescription.identifier, skipDisableCommandTests: true))
verifyCorrections(ruleDescription, config: config, disableCommands: [],
testMultiByteOffsets: false, parserDiagnosticsDisabledForTests: false)
}

func testFileWithParserWarningDiagnostics() throws {
parserDiagnosticsDisabledForTests = false
// extraneous duplicate parameter name; 'bar' already has an argument label
let original = """
func foo(bar bar: String) -> Int { 0 }
"""

let corrected = """
func foo(bar bar: String) -> Int { 0 }
"""

XCTAssertNotNil(SwiftLintFile(contents: original).parserDiagnostics)

let ruleDescription = ReturnArrowWhitespaceRule.description
.with(corrections: [Example(original): Example(corrected)])

let config = try XCTUnwrap(makeConfig(nil, ruleDescription.identifier, skipDisableCommandTests: true))
verifyCorrections(ruleDescription, config: config, disableCommands: [],
testMultiByteOffsets: false, parserDiagnosticsDisabledForTests: false)
}

func testFileWithoutParserDiagnostics() {
parserDiagnosticsDisabledForTests = false
XCTAssertNil(SwiftLintFile(contents: "import Foundation").parserDiagnostics)
Expand Down
5 changes: 3 additions & 2 deletions Tests/SwiftLintFrameworkTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,11 @@ extension XCTestCase {
}

func verifyCorrections(_ ruleDescription: RuleDescription, config: Configuration,
disableCommands: [String], testMultiByteOffsets: Bool) {
disableCommands: [String], testMultiByteOffsets: Bool,
parserDiagnosticsDisabledForTests: Bool = true) {
let ruleDescription = ruleDescription.focused()

parserDiagnosticsDisabledForTests = true
SwiftLintFramework.parserDiagnosticsDisabledForTests = parserDiagnosticsDisabledForTests

// corrections
ruleDescription.corrections.forEach {
Expand Down

0 comments on commit 0fbf010

Please sign in to comment.