diff --git a/Source/SwiftLintCore/Extensions/SwiftLintFile+Cache.swift b/Source/SwiftLintCore/Extensions/SwiftLintFile+Cache.swift index 66a535b6c..c48b0a417 100644 --- a/Source/SwiftLintCore/Extensions/SwiftLintFile+Cache.swift +++ b/Source/SwiftLintCore/Extensions/SwiftLintFile+Cache.swift @@ -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) diff --git a/Source/SwiftLintFramework/Models/Linter.swift b/Source/SwiftLintFramework/Models/Linter.swift index f0a3c12fe..1af72e043 100644 --- a/Source/SwiftLintFramework/Models/Linter.swift +++ b/Source/SwiftLintFramework/Models/Linter.swift @@ -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 ?? "")" ) - queuedPrintError(toJSON(["diagnostics": parserDiagnostics])) + queuedPrintError(toJSON(["diagnostics": file.parserDiagnostics])) return [:] } diff --git a/Tests/FrameworkTests/ParserDiagnosticsTests.swift b/Tests/FrameworkTests/ParserDiagnosticsTests.swift index 8d1d4bec0..fad92d20d 100644 --- a/Tests/FrameworkTests/ParserDiagnosticsTests.swift +++ b/Tests/FrameworkTests/ParserDiagnosticsTests.swift @@ -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, []) + } } } diff --git a/Tests/TestHelpers/TestHelpers.swift b/Tests/TestHelpers/TestHelpers.swift index 10cdc8b23..d532dc777 100644 --- a/Tests/TestHelpers/TestHelpers.swift +++ b/Tests/TestHelpers/TestHelpers.swift @@ -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) + } } } }