Skip to content

Commit e70ab31

Browse files
authored
Allow comments in .swift-format (#1014)
* Allow comments in `.swift-format`
1 parent ae522f2 commit e70ab31

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

Sources/SwiftFormat/API/Configuration.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,11 @@ public struct Configuration: Codable, Equatable {
292292

293293
/// Creates a new `Configuration` by decoding it from the UTF-8 representation in the given data.
294294
public init(data: Data) throws {
295-
self = try JSONDecoder().decode(Configuration.self, from: data)
295+
let jsonDecoder = JSONDecoder()
296+
#if canImport(Darwin) || compiler(>=6)
297+
jsonDecoder.allowsJSON5 = true
298+
#endif
299+
self = try jsonDecoder.decode(Configuration.self, from: data)
296300
}
297301

298302
public init(from decoder: Decoder) throws {

Tests/SwiftFormatTests/API/ConfigurationTests.swift

+33-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ final class ConfigurationTests: XCTestCase {
2323

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

@@ -79,7 +82,11 @@ final class ConfigurationTests: XCTestCase {
7982
}
8083
""".data(using: .utf8)!
8184

82-
let config = try JSONDecoder().decode(Configuration.self, from: jsonData)
85+
let jsonDecoder = JSONDecoder()
86+
#if canImport(Darwin) || compiler(>=6)
87+
jsonDecoder.allowsJSON5 = true
88+
#endif
89+
let config = try jsonDecoder.decode(Configuration.self, from: jsonData)
8390
XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior)
8491
}
8592
}
@@ -99,9 +106,33 @@ final class ConfigurationTests: XCTestCase {
99106
}
100107
""".data(using: .utf8)!
101108

102-
let config = try JSONDecoder().decode(Configuration.self, from: jsonData)
109+
let jsonDecoder = JSONDecoder()
110+
#if canImport(Darwin) || compiler(>=6)
111+
jsonDecoder.allowsJSON5 = true
112+
#endif
113+
let config = try jsonDecoder.decode(Configuration.self, from: jsonData)
103114
XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior)
104115
}
105116
}
106117

118+
func testConfigurationWithComments() throws {
119+
#if !canImport(Darwin) && compiler(<6)
120+
try XCTSkipIf(true, "JSONDecoder does not support JSON5")
121+
#else
122+
let expected = Configuration()
123+
124+
let jsonData = """
125+
{
126+
// Indicates the configuration schema version.
127+
"version": 1,
128+
}
129+
""".data(using: .utf8)!
130+
131+
let jsonDecoder = JSONDecoder()
132+
133+
jsonDecoder.allowsJSON5 = true
134+
let config = try jsonDecoder.decode(Configuration.self, from: jsonData)
135+
XCTAssertEqual(config, expected)
136+
#endif
137+
}
107138
}

0 commit comments

Comments
 (0)