Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 compiler(>=6)
jsonDecoder.allowsJSON5 = true
#endif
self = try jsonDecoder.decode(Configuration.self, from: data)
}

public init(from decoder: Decoder) throws {
Expand Down
32 changes: 30 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 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 compiler(>=6)
jsonDecoder.allowsJSON5 = true
#endif
let config = try jsonDecoder.decode(Configuration.self, from: jsonData)
XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior)
}
}
Expand All @@ -99,9 +106,30 @@ final class ConfigurationTests: XCTestCase {
}
""".data(using: .utf8)!

let config = try JSONDecoder().decode(Configuration.self, from: jsonData)
let jsonDecoder = JSONDecoder()
#if compiler(>=6)
jsonDecoder.allowsJSON5 = true
#endif
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()
#if compiler(>=6)
jsonDecoder.allowsJSON5 = true
#endif
let config = try jsonDecoder.decode(Configuration.self, from: jsonData)
XCTAssertEqual(config, expected)
}
}