mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
Use task-local parser diagnostic toggle (#6273)
This commit is contained in:
@@ -55,7 +55,7 @@ private let swiftSyntaxTokensCache = Cache { file -> [SwiftLintSyntaxToken]? in
|
||||
package typealias AssertHandler = () -> Void
|
||||
// Re-enable once all parser diagnostics in tests have been addressed.
|
||||
// https://github.com/realm/SwiftLint/issues/3348
|
||||
package nonisolated(unsafe) var parserDiagnosticsDisabledForTests = false
|
||||
@TaskLocal package var parserDiagnosticsDisabledForTests = false
|
||||
|
||||
private let assertHandlerCache = Cache { (_: SwiftLintFile) -> AssertHandler? in nil }
|
||||
|
||||
@@ -124,9 +124,9 @@ extension SwiftLintFile {
|
||||
}
|
||||
}
|
||||
|
||||
public var parserDiagnostics: [String]? {
|
||||
public var parserDiagnostics: [String] {
|
||||
if parserDiagnosticsDisabledForTests {
|
||||
return nil
|
||||
return []
|
||||
}
|
||||
|
||||
return ParseDiagnosticsGenerator.diagnostics(for: syntaxTree)
|
||||
|
||||
@@ -408,11 +408,11 @@ public struct CollectedLinter {
|
||||
return [:]
|
||||
}
|
||||
|
||||
if let parserDiagnostics = file.parserDiagnostics, parserDiagnostics.isNotEmpty {
|
||||
if file.parserDiagnostics.isNotEmpty {
|
||||
queuedPrintError(
|
||||
"warning: Skipping correcting file because it produced Swift parser errors: \(file.path ?? "<nopath>")"
|
||||
)
|
||||
queuedPrintError(toJSON(["diagnostics": parserDiagnostics]))
|
||||
queuedPrintError(toJSON(["diagnostics": file.parserDiagnostics]))
|
||||
return [:]
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,9 @@ import XCTest
|
||||
|
||||
final class ParserDiagnosticsTests: SwiftLintTestCase {
|
||||
func testFileWithParserErrorDiagnostics() {
|
||||
parserDiagnosticsDisabledForTests = false
|
||||
XCTAssertNotNil(SwiftLintFile(contents: "importz Foundation").parserDiagnostics)
|
||||
$parserDiagnosticsDisabledForTests.withValue(false) {
|
||||
XCTAssertNotNil(SwiftLintFile(contents: "importz Foundation").parserDiagnostics)
|
||||
}
|
||||
}
|
||||
|
||||
func testFileWithParserErrorDiagnosticsDoesntAutocorrect() throws {
|
||||
@@ -24,7 +25,6 @@ final class ParserDiagnosticsTests: SwiftLintTestCase {
|
||||
}
|
||||
|
||||
func testFileWithParserWarningDiagnostics() throws {
|
||||
parserDiagnosticsDisabledForTests = false
|
||||
// extraneous duplicate parameter name; 'bar' already has an argument label
|
||||
let original = """
|
||||
func foo(bar bar: String) -> Int { 0 }
|
||||
@@ -34,7 +34,9 @@ final class ParserDiagnosticsTests: SwiftLintTestCase {
|
||||
func foo(bar bar: String) -> Int { 0 }
|
||||
"""
|
||||
|
||||
XCTAssertEqual(SwiftLintFile(contents: original).parserDiagnostics, [])
|
||||
$parserDiagnosticsDisabledForTests.withValue(false) {
|
||||
XCTAssertEqual(SwiftLintFile(contents: original).parserDiagnostics, [])
|
||||
}
|
||||
|
||||
let ruleDescription = ReturnArrowWhitespaceRule.description
|
||||
.with(corrections: [Example(original): Example(corrected)])
|
||||
@@ -45,7 +47,8 @@ final class ParserDiagnosticsTests: SwiftLintTestCase {
|
||||
}
|
||||
|
||||
func testFileWithoutParserDiagnostics() {
|
||||
parserDiagnosticsDisabledForTests = false
|
||||
XCTAssertEqual(SwiftLintFile(contents: "import Foundation").parserDiagnostics, [])
|
||||
$parserDiagnosticsDisabledForTests.withValue(false) {
|
||||
XCTAssertEqual(SwiftLintFile(contents: "import Foundation").parserDiagnostics, [])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -439,23 +439,23 @@ public extension XCTestCase {
|
||||
parserDiagnosticsDisabledForTests: Bool = true) {
|
||||
let ruleDescription = ruleDescription.focused()
|
||||
|
||||
SwiftLintCore.parserDiagnosticsDisabledForTests = parserDiagnosticsDisabledForTests
|
||||
SwiftLintCore.$parserDiagnosticsDisabledForTests.withValue(parserDiagnosticsDisabledForTests) {
|
||||
// corrections
|
||||
ruleDescription.corrections.forEach {
|
||||
testCorrection($0, configuration: config, testMultiByteOffsets: testMultiByteOffsets)
|
||||
}
|
||||
// make sure strings that don't trigger aren't corrected
|
||||
ruleDescription.nonTriggeringExamples.forEach {
|
||||
testCorrection(($0, $0), configuration: config, testMultiByteOffsets: testMultiByteOffsets)
|
||||
}
|
||||
|
||||
// corrections
|
||||
ruleDescription.corrections.forEach {
|
||||
testCorrection($0, configuration: config, testMultiByteOffsets: testMultiByteOffsets)
|
||||
}
|
||||
// make sure strings that don't trigger aren't corrected
|
||||
ruleDescription.nonTriggeringExamples.forEach {
|
||||
testCorrection(($0, $0), configuration: config, testMultiByteOffsets: testMultiByteOffsets)
|
||||
}
|
||||
|
||||
// "disable" commands do not correct
|
||||
ruleDescription.corrections.forEach { before, _ in
|
||||
for command in disableCommands {
|
||||
let beforeDisabled = command + before.code
|
||||
let expectedCleaned = before.with(code: cleanedContentsAndMarkerOffsets(from: beforeDisabled).0)
|
||||
config.assertCorrection(expectedCleaned, expected: expectedCleaned)
|
||||
// "disable" commands do not correct
|
||||
ruleDescription.corrections.forEach { before, _ in
|
||||
for command in disableCommands {
|
||||
let beforeDisabled = command + before.code
|
||||
let expectedCleaned = before.with(code: cleanedContentsAndMarkerOffsets(from: beforeDisabled).0)
|
||||
config.assertCorrection(expectedCleaned, expected: expectedCleaned)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user