From 11df2cc8b001e29166b4b5146187468649f35182 Mon Sep 17 00:00:00 2001 From: Vladimir Gusev Date: Wed, 7 May 2025 16:50:14 +0300 Subject: [PATCH 1/4] Allow comments in `.swift-format` --- Sources/SwiftFormat/API/Configuration.swift | 4 +++- .../API/ConfigurationTests.swift | 24 +++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Sources/SwiftFormat/API/Configuration.swift b/Sources/SwiftFormat/API/Configuration.swift index cb5dfb025..11073e52b 100644 --- a/Sources/SwiftFormat/API/Configuration.swift +++ b/Sources/SwiftFormat/API/Configuration.swift @@ -292,7 +292,9 @@ public struct Configuration: Codable, Equatable { /// Creates a new `Configuration` by decoding it from the UTF-8 representation in the given data. public init(data: Data) throws { - self = try JSONDecoder().decode(Configuration.self, from: data) + let jsonDecoder = JSONDecoder() + jsonDecoder.allowsJSON5 = true + self = try jsonDecoder.decode(Configuration.self, from: data) } public init(from decoder: Decoder) throws { diff --git a/Tests/SwiftFormatTests/API/ConfigurationTests.swift b/Tests/SwiftFormatTests/API/ConfigurationTests.swift index 9c6977db8..e39a5a7b6 100644 --- a/Tests/SwiftFormatTests/API/ConfigurationTests.swift +++ b/Tests/SwiftFormatTests/API/ConfigurationTests.swift @@ -23,6 +23,7 @@ final class ConfigurationTests: XCTestCase { let emptyDictionaryData = "{}\n".data(using: .utf8)! let jsonDecoder = JSONDecoder() + jsonDecoder.allowsJSON5 = true let emptyJSONConfig = try! jsonDecoder.decode(Configuration.self, from: emptyDictionaryData) @@ -79,7 +80,9 @@ final class ConfigurationTests: XCTestCase { } """.data(using: .utf8)! - let config = try JSONDecoder().decode(Configuration.self, from: jsonData) + let jsonDecoder = JSONDecoder() + jsonDecoder.allowsJSON5 = true + let config = try jsonDecoder.decode(Configuration.self, from: jsonData) XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior) } } @@ -99,9 +102,26 @@ final class ConfigurationTests: XCTestCase { } """.data(using: .utf8)! - let config = try JSONDecoder().decode(Configuration.self, from: jsonData) + let jsonDecoder = JSONDecoder() + jsonDecoder.allowsJSON5 = true + let config = try jsonDecoder.decode(Configuration.self, from: jsonData) XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior) } } + func testConfigurationWithComments() throws { + let expected = Configuration() + + let jsonData = """ + { + // Indicates the configuration schema version. + "version": 1, + } + """.data(using: .utf8)! + + let jsonDecoder = JSONDecoder() + jsonDecoder.allowsJSON5 = true + let config = try jsonDecoder.decode(Configuration.self, from: jsonData) + XCTAssertEqual(config, expected) + } } From 4f5d5d60fc5c1726bb26c1e59091781d4820c7ea Mon Sep 17 00:00:00 2001 From: Vladimir Gusev Date: Fri, 9 May 2025 19:35:55 +0300 Subject: [PATCH 2/4] Add check for compiler version --- Sources/SwiftFormat/API/Configuration.swift | 2 ++ Tests/SwiftFormatTests/API/ConfigurationTests.swift | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/Sources/SwiftFormat/API/Configuration.swift b/Sources/SwiftFormat/API/Configuration.swift index 11073e52b..bb2edf45c 100644 --- a/Sources/SwiftFormat/API/Configuration.swift +++ b/Sources/SwiftFormat/API/Configuration.swift @@ -293,7 +293,9 @@ public struct Configuration: Codable, Equatable { /// Creates a new `Configuration` by decoding it from the UTF-8 representation in the given data. public init(data: Data) throws { let jsonDecoder = JSONDecoder() + #if compiler(>=6) jsonDecoder.allowsJSON5 = true + #endif self = try jsonDecoder.decode(Configuration.self, from: data) } diff --git a/Tests/SwiftFormatTests/API/ConfigurationTests.swift b/Tests/SwiftFormatTests/API/ConfigurationTests.swift index e39a5a7b6..3f600d697 100644 --- a/Tests/SwiftFormatTests/API/ConfigurationTests.swift +++ b/Tests/SwiftFormatTests/API/ConfigurationTests.swift @@ -23,7 +23,9 @@ final class ConfigurationTests: XCTestCase { let emptyDictionaryData = "{}\n".data(using: .utf8)! let jsonDecoder = JSONDecoder() + #if compiler(>=6) jsonDecoder.allowsJSON5 = true + #endif let emptyJSONConfig = try! jsonDecoder.decode(Configuration.self, from: emptyDictionaryData) @@ -81,7 +83,9 @@ final class ConfigurationTests: XCTestCase { """.data(using: .utf8)! let jsonDecoder = JSONDecoder() + #if compiler(>=6) jsonDecoder.allowsJSON5 = true + #endif let config = try jsonDecoder.decode(Configuration.self, from: jsonData) XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior) } @@ -103,7 +107,9 @@ final class ConfigurationTests: XCTestCase { """.data(using: .utf8)! let jsonDecoder = JSONDecoder() + #if compiler(>=6) jsonDecoder.allowsJSON5 = true + #endif let config = try jsonDecoder.decode(Configuration.self, from: jsonData) XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior) } @@ -120,7 +126,9 @@ final class ConfigurationTests: XCTestCase { """.data(using: .utf8)! let jsonDecoder = JSONDecoder() + #if compiler(>=6) jsonDecoder.allowsJSON5 = true + #endif let config = try jsonDecoder.decode(Configuration.self, from: jsonData) XCTAssertEqual(config, expected) } From 1f8976bb12eb0c7a5a2c363fea71f72dce19a42b Mon Sep 17 00:00:00 2001 From: Vladimir Gusev Date: Fri, 9 May 2025 19:50:28 +0300 Subject: [PATCH 3/4] Fix checking for availability --- Sources/SwiftFormat/API/Configuration.swift | 2 +- Tests/SwiftFormatTests/API/ConfigurationTests.swift | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Sources/SwiftFormat/API/Configuration.swift b/Sources/SwiftFormat/API/Configuration.swift index bb2edf45c..98a1b9b70 100644 --- a/Sources/SwiftFormat/API/Configuration.swift +++ b/Sources/SwiftFormat/API/Configuration.swift @@ -293,7 +293,7 @@ public struct Configuration: Codable, Equatable { /// Creates a new `Configuration` by decoding it from the UTF-8 representation in the given data. public init(data: Data) throws { let jsonDecoder = JSONDecoder() - #if compiler(>=6) + #if canImport(Darwin) || compiler(>=6) jsonDecoder.allowsJSON5 = true #endif self = try jsonDecoder.decode(Configuration.self, from: data) diff --git a/Tests/SwiftFormatTests/API/ConfigurationTests.swift b/Tests/SwiftFormatTests/API/ConfigurationTests.swift index 3f600d697..831e530ac 100644 --- a/Tests/SwiftFormatTests/API/ConfigurationTests.swift +++ b/Tests/SwiftFormatTests/API/ConfigurationTests.swift @@ -23,7 +23,7 @@ final class ConfigurationTests: XCTestCase { let emptyDictionaryData = "{}\n".data(using: .utf8)! let jsonDecoder = JSONDecoder() - #if compiler(>=6) + #if canImport(Darwin) || compiler(>=6) jsonDecoder.allowsJSON5 = true #endif let emptyJSONConfig = @@ -83,7 +83,7 @@ final class ConfigurationTests: XCTestCase { """.data(using: .utf8)! let jsonDecoder = JSONDecoder() - #if compiler(>=6) + #if canImport(Darwin) || compiler(>=6) jsonDecoder.allowsJSON5 = true #endif let config = try jsonDecoder.decode(Configuration.self, from: jsonData) @@ -107,7 +107,7 @@ final class ConfigurationTests: XCTestCase { """.data(using: .utf8)! let jsonDecoder = JSONDecoder() - #if compiler(>=6) + #if canImport(Darwin) || compiler(>=6) jsonDecoder.allowsJSON5 = true #endif let config = try jsonDecoder.decode(Configuration.self, from: jsonData) @@ -116,6 +116,9 @@ final class ConfigurationTests: XCTestCase { } func testConfigurationWithComments() throws { + #if !canImport(Darwin) && compiler(<6) + try XCTSkipIf(true, "JSONDecoder does not support JSON5") + #endif let expected = Configuration() let jsonData = """ @@ -126,9 +129,8 @@ final class ConfigurationTests: XCTestCase { """.data(using: .utf8)! let jsonDecoder = JSONDecoder() - #if compiler(>=6) + jsonDecoder.allowsJSON5 = true - #endif let config = try jsonDecoder.decode(Configuration.self, from: jsonData) XCTAssertEqual(config, expected) } From d5c471afb3c11a86fc91e82db319321aa7097b94 Mon Sep 17 00:00:00 2001 From: Vladimir Gusev Date: Fri, 9 May 2025 20:23:18 +0300 Subject: [PATCH 4/4] Wrap the rest of the body --- Tests/SwiftFormatTests/API/ConfigurationTests.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/SwiftFormatTests/API/ConfigurationTests.swift b/Tests/SwiftFormatTests/API/ConfigurationTests.swift index 831e530ac..d983e9cb9 100644 --- a/Tests/SwiftFormatTests/API/ConfigurationTests.swift +++ b/Tests/SwiftFormatTests/API/ConfigurationTests.swift @@ -118,7 +118,7 @@ final class ConfigurationTests: XCTestCase { func testConfigurationWithComments() throws { #if !canImport(Darwin) && compiler(<6) try XCTSkipIf(true, "JSONDecoder does not support JSON5") - #endif + #else let expected = Configuration() let jsonData = """ @@ -133,5 +133,6 @@ final class ConfigurationTests: XCTestCase { jsonDecoder.allowsJSON5 = true let config = try jsonDecoder.decode(Configuration.self, from: jsonData) XCTAssertEqual(config, expected) + #endif } }