Skip to content
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
6 changes: 5 additions & 1 deletion Sources/SwiftFormat/API/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ 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()
#if canImport(Darwin) || compiler(>=6)
jsonDecoder.allowsJSON5 = true
#endif
self = try jsonDecoder.decode(Configuration.self, from: data)
}

public init(from decoder: Decoder) throws {
Expand Down
35 changes: 33 additions & 2 deletions Tests/SwiftFormatTests/API/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ final class ConfigurationTests: XCTestCase {

let emptyDictionaryData = "{}\n".data(using: .utf8)!
let jsonDecoder = JSONDecoder()
#if canImport(Darwin) || compiler(>=6)
jsonDecoder.allowsJSON5 = true
#endif
let emptyJSONConfig =
try! jsonDecoder.decode(Configuration.self, from: emptyDictionaryData)

Expand Down Expand Up @@ -79,7 +82,11 @@ final class ConfigurationTests: XCTestCase {
}
""".data(using: .utf8)!

let config = try JSONDecoder().decode(Configuration.self, from: jsonData)
let jsonDecoder = JSONDecoder()
#if canImport(Darwin) || compiler(>=6)
jsonDecoder.allowsJSON5 = true
#endif
let config = try jsonDecoder.decode(Configuration.self, from: jsonData)
XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior)
}
}
Expand All @@ -99,9 +106,33 @@ final class ConfigurationTests: XCTestCase {
}
""".data(using: .utf8)!

let config = try JSONDecoder().decode(Configuration.self, from: jsonData)
let jsonDecoder = JSONDecoder()
#if canImport(Darwin) || compiler(>=6)
jsonDecoder.allowsJSON5 = true
#endif
let config = try jsonDecoder.decode(Configuration.self, from: jsonData)
XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior)
}
}

func testConfigurationWithComments() throws {
#if !canImport(Darwin) && compiler(<6)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to wrap the rest of the body in an #else here, too, otherwise it will still try to compile the code below in the compiler(<6) case (since it's just unguarded code right now).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Fixed

try XCTSkipIf(true, "JSONDecoder does not support JSON5")
#else
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)
#endif
}
}