Skip to content

Commit 2ee9dff

Browse files
authored
Merge pull request #2023 from ahoppen/dont-escape-slashes
Don't escape slashes in various `JSONEncoder`s
2 parents 49d0338 + 2305dfa commit 2ee9dff

File tree

8 files changed

+32
-37
lines changed

8 files changed

+32
-37
lines changed

SourceKitLSPDevUtils/Sources/ConfigSchemaGen/ConfigSchemaGen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ package struct ConfigSchemaGen {
128128
jsonSchema.title = "SourceKit-LSP Configuration"
129129
jsonSchema.comment = "DO NOT EDIT THIS FILE. This file is generated by \(#fileID)."
130130
let encoder = JSONEncoder()
131-
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
131+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
132132
return try encoder.encode(jsonSchema)
133133
}
134134

Sources/Diagnose/RequestInfo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ package struct RequestInfo: Sendable {
4040

4141
package func request(for file: URL) throws -> String {
4242
let encoder = JSONEncoder()
43-
encoder.outputFormatting = .prettyPrinted
43+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
4444
guard var compilerArgs = String(data: try encoder.encode(compilerArgs), encoding: .utf8) else {
4545
throw GenericError("Failed to encode compiler arguments")
4646
}

Sources/SKLogging/CustomLogStringConvertible.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ extension Optional where Wrapped: CustomLogStringConvertible {
101101
extension Encodable {
102102
package var prettyPrintedJSON: String {
103103
let encoder = JSONEncoder()
104-
encoder.outputFormatting.insert(.prettyPrinted)
105-
encoder.outputFormatting.insert(.sortedKeys)
104+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
106105
guard let data = try? encoder.encode(self) else {
107106
return "\(self)"
108107
}
@@ -137,7 +136,7 @@ extension Encodable {
137136
let jsonObject = try? JSONSerialization.jsonObject(with: encoded),
138137
let data = try? JSONSerialization.data(
139138
withJSONObject: redact(subject: jsonObject),
140-
options: [.prettyPrinted, .sortedKeys]
139+
options: [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
141140
),
142141
let string = String(data: data, encoding: .utf8)
143142
else {

Sources/SKTestSupport/CheckCoding.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ package func checkCoding<T: Codable & Equatable>(
2424
line: UInt = #line
2525
) {
2626
let encoder = JSONEncoder()
27-
encoder.outputFormatting.insert(.prettyPrinted)
28-
encoder.outputFormatting.insert(.sortedKeys)
27+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
2928

3029
let data = try! encoder.encode(WrapFragment(value: value))
3130
let wrappedStr = String(data: data, encoding: .utf8)!
@@ -64,8 +63,7 @@ package func checkEncoding<T: Encodable & Equatable>(
6463
line: UInt = #line
6564
) {
6665
let encoder = JSONEncoder()
67-
encoder.outputFormatting.insert(.prettyPrinted)
68-
encoder.outputFormatting.insert(.sortedKeys)
66+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
6967
let data = try! encoder.encode(value)
7068
let str = String(data: data, encoding: .utf8)!
7169
// Remove trailing whitespace to normalize between corelibs and Apple Foundation.
@@ -101,8 +99,7 @@ package func checkCoding<T: Codable>(
10199
body: (T) -> Void
102100
) {
103101
let encoder = JSONEncoder()
104-
encoder.outputFormatting.insert(.prettyPrinted)
105-
encoder.outputFormatting.insert(.sortedKeys)
102+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
106103
let data = try! encoder.encode(value)
107104
let str = String(data: data, encoding: .utf8)!
108105
// Remove trailing whitespace to normalize between corelibs and Apple Foundation.

Sources/SKTestSupport/IndexedSingleSwiftFileTestProject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ package struct IndexedSingleSwiftFileTestProject {
130130
]
131131
)
132132
let encoder = JSONEncoder()
133-
encoder.outputFormatting = .prettyPrinted
133+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
134134
try encoder.encode(compilationDatabase).write(
135135
to: testWorkspaceDirectory.appendingPathComponent(JSONCompilationDatabaseBuildSystem.dbName)
136136
)

Tests/LanguageServerProtocolJSONRPCTests/CodingTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class CodingTests: XCTestCase {
4646
},
4747
"locale" : "en-US",
4848
"processId" : 1,
49-
"rootPath" : "\\/foo",
49+
"rootPath" : "/foo",
5050
"trace" : "off"
5151
}
5252
}
@@ -74,7 +74,7 @@ final class CodingTests: XCTestCase {
7474
7575
},
7676
"processId" : 1,
77-
"rootPath" : "\\/foo",
77+
"rootPath" : "/foo",
7878
"trace" : "off"
7979
}
8080
}
@@ -86,7 +86,7 @@ final class CodingTests: XCTestCase {
8686
json: """
8787
{
8888
"jsonrpc" : "2.0",
89-
"method" : "$\\/cancelRequest",
89+
"method" : "$/cancelRequest",
9090
"params" : {
9191
"id" : 1
9292
}

Tests/LanguageServerProtocolTests/CodingTests.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ final class CodingTests: XCTestCase {
1919
func testValueCoding() throws {
2020
let url = URL(fileURLWithPath: "/foo.swift")
2121
let uri = DocumentURI(url)
22-
// The \\/\\/\\/ is escaping file:// + /foo.swift, which is silly but allowed by json.
23-
let urljson = "file:\\/\\/\\/foo.swift"
22+
let urljson = "file:///foo.swift"
2423

2524
let range = Position(line: 5, utf16index: 23)..<Position(line: 6, utf16index: 0)
2625
// Range.lowerBound -> start, Range.upperBound -> end
@@ -256,7 +255,7 @@ final class CodingTests: XCTestCase {
256255
CodeDescription(href: try DocumentURI(string: "file:///some/path")),
257256
json: """
258257
{
259-
"href" : "file:\\/\\/\\/some\\/path"
258+
"href" : "file:///some/path"
260259
}
261260
"""
262261
)
@@ -697,7 +696,7 @@ final class CodingTests: XCTestCase {
697696
"line" : 1
698697
}
699698
},
700-
"uri" : "file:\\/\\/\\/foo.swift"
699+
"uri" : "file:///foo.swift"
701700
},
702701
"fromRanges" : [
703702
{
@@ -862,7 +861,7 @@ final class CodingTests: XCTestCase {
862861
],
863862
"kind" : "full",
864863
"relatedDocuments" : [
865-
"file:\/\/\/some\/path",
864+
"file:///some/path",
866865
{
867866
"kind" : "unchanged",
868867
"resultId" : "myOtherResults"
@@ -898,7 +897,7 @@ final class CodingTests: XCTestCase {
898897
{
899898
"kind" : "unchanged",
900899
"relatedDocuments" : [
901-
"file:\/\/\/some\/path",
900+
"file:///some/path",
902901
{
903902
"kind" : "unchanged",
904903
"resultId" : "myOtherResults"
@@ -1039,7 +1038,7 @@ final class CodingTests: XCTestCase {
10391038
json: #"""
10401039
{
10411040
"textDocument" : {
1042-
"uri" : "file:\/\/\/some\/path"
1041+
"uri" : "file:///some/path"
10431042
}
10441043
}
10451044
"""#,
@@ -1056,7 +1055,7 @@ final class CodingTests: XCTestCase {
10561055
"line" : 4
10571056
},
10581057
"textDocument" : {
1059-
"uri" : "file:\/\/\/some\/path"
1058+
"uri" : "file:///some/path"
10601059
}
10611060
}
10621061
"""#,
@@ -1091,7 +1090,7 @@ final class CodingTests: XCTestCase {
10911090
10921091
],
10931092
"kind" : "full",
1094-
"uri" : "file:\/\/\/some\/path"
1093+
"uri" : "file:///some/path"
10951094
}
10961095
"""#
10971096
)
@@ -1107,7 +1106,7 @@ final class CodingTests: XCTestCase {
11071106
{
11081107
"kind" : "unchanged",
11091108
"resultId" : "myResults",
1110-
"uri" : "file:\/\/\/some\/path"
1109+
"uri" : "file:///some/path"
11111110
}
11121111
"""#
11131112
)
@@ -1139,7 +1138,7 @@ final class CodingTests: XCTestCase {
11391138
"line" : 3
11401139
}
11411140
},
1142-
"uri" : "file:\/\/\/some\/path"
1141+
"uri" : "file:///some/path"
11431142
},
11441143
"name" : "mySym"
11451144
}
@@ -1160,7 +1159,7 @@ final class CodingTests: XCTestCase {
11601159
{
11611160
"kind" : 17,
11621161
"location" : {
1163-
"uri" : "file:\/\/\/some\/path"
1162+
"uri" : "file:///some/path"
11641163
},
11651164
"name" : "mySym"
11661165
}
@@ -1173,7 +1172,7 @@ final class CodingTests: XCTestCase {
11731172
WorkspaceSymbol.WorkspaceSymbolLocation.uri(.init(uri: try DocumentURI(string: "file:///some/path"))),
11741173
json: #"""
11751174
{
1176-
"uri" : "file:\/\/\/some\/path"
1175+
"uri" : "file:///some/path"
11771176
}
11781177
"""#
11791178
)
@@ -1197,7 +1196,7 @@ final class CodingTests: XCTestCase {
11971196
"line" : 3
11981197
}
11991198
},
1200-
"uri" : "file:\/\/\/some\/path"
1199+
"uri" : "file:///some/path"
12011200
}
12021201
"""#
12031202
)

config.schema.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"$comment" : "DO NOT EDIT THIS FILE. This file is generated by ConfigSchemaGen\/ConfigSchemaGen.swift.",
3-
"$schema" : "http:\/\/json-schema.org\/draft-07\/schema#",
2+
"$comment" : "DO NOT EDIT THIS FILE. This file is generated by ConfigSchemaGen/ConfigSchemaGen.swift.",
3+
"$schema" : "http://json-schema.org/draft-07/schema#",
44
"properties" : {
55
"backgroundIndexing" : {
66
"description" : "Whether background indexing is enabled.",
@@ -28,8 +28,8 @@
2828
"type" : "integer"
2929
},
3030
"cancelTextDocumentRequestsOnEditAndClose" : {
31-
"description" : "Whether sending a `textDocument\/didChange` or `textDocument\/didClose` notification for a document should cancel all pending requests for that document.",
32-
"markdownDescription" : "Whether sending a `textDocument\/didChange` or `textDocument\/didClose` notification for a document should cancel all pending requests for that document.",
31+
"description" : "Whether sending a `textDocument/didChange` or `textDocument/didClose` notification for a document should cancel all pending requests for that document.",
32+
"markdownDescription" : "Whether sending a `textDocument/didChange` or `textDocument/didClose` notification for a document should cancel all pending requests for that document.",
3333
"type" : "boolean"
3434
},
3535
"clangdOptions" : {
@@ -75,10 +75,10 @@
7575
"is-indexing-request"
7676
],
7777
"markdownEnumDescriptions" : [
78-
"Enable support for the `textDocument\/onTypeFormatting` request.",
79-
"Enable support for the `workspace\/_setOptions` request.",
80-
"Enable the `workspace\/_sourceKitOptions` request.",
81-
"Enable the `sourceKit\/_isIndexing` request."
78+
"Enable support for the `textDocument/onTypeFormatting` request.",
79+
"Enable support for the `workspace/_setOptions` request.",
80+
"Enable the `workspace/_sourceKitOptions` request.",
81+
"Enable the `sourceKit/_isIndexing` request."
8282
],
8383
"type" : "string"
8484
},

0 commit comments

Comments
 (0)