Skip to content

Commit 11df2cc

Browse files
committed
Allow comments in .swift-format
1 parent ae522f2 commit 11df2cc

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Sources/SwiftFormat/API/Configuration.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@ 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+
jsonDecoder.allowsJSON5 = true
297+
self = try jsonDecoder.decode(Configuration.self, from: data)
296298
}
297299

298300
public init(from decoder: Decoder) throws {

Tests/SwiftFormatTests/API/ConfigurationTests.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ final class ConfigurationTests: XCTestCase {
2323

2424
let emptyDictionaryData = "{}\n".data(using: .utf8)!
2525
let jsonDecoder = JSONDecoder()
26+
jsonDecoder.allowsJSON5 = true
2627
let emptyJSONConfig =
2728
try! jsonDecoder.decode(Configuration.self, from: emptyDictionaryData)
2829

@@ -79,7 +80,9 @@ final class ConfigurationTests: XCTestCase {
7980
}
8081
""".data(using: .utf8)!
8182

82-
let config = try JSONDecoder().decode(Configuration.self, from: jsonData)
83+
let jsonDecoder = JSONDecoder()
84+
jsonDecoder.allowsJSON5 = true
85+
let config = try jsonDecoder.decode(Configuration.self, from: jsonData)
8386
XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior)
8487
}
8588
}
@@ -99,9 +102,26 @@ final class ConfigurationTests: XCTestCase {
99102
}
100103
""".data(using: .utf8)!
101104

102-
let config = try JSONDecoder().decode(Configuration.self, from: jsonData)
105+
let jsonDecoder = JSONDecoder()
106+
jsonDecoder.allowsJSON5 = true
107+
let config = try jsonDecoder.decode(Configuration.self, from: jsonData)
103108
XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior)
104109
}
105110
}
106111

112+
func testConfigurationWithComments() throws {
113+
let expected = Configuration()
114+
115+
let jsonData = """
116+
{
117+
// Indicates the configuration schema version.
118+
"version": 1,
119+
}
120+
""".data(using: .utf8)!
121+
122+
let jsonDecoder = JSONDecoder()
123+
jsonDecoder.allowsJSON5 = true
124+
let config = try jsonDecoder.decode(Configuration.self, from: jsonData)
125+
XCTAssertEqual(config, expected)
126+
}
107127
}

0 commit comments

Comments
 (0)