Skip to content

Commit 037e55e

Browse files
committed
Handle unexpected nodes following a closure
1 parent e54c99e commit 037e55e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Sources/SourceKitLSP/Swift/CodeActions/ConvertJSONToCodableStruct.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public struct ConvertJSONToCodableStruct: EditRefactoringProvider {
7878
/// We're only going to look at the text of the closure to see if we
7979
/// have JSON in there.
8080
text = closure.trimmedDescription
81+
case let .endingClosure(closure, unexpected):
82+
text = closure.trimmedDescription + unexpected.description
83+
8184
case .stringLiteral(_, let literalText):
8285
/// A string literal that could contain JSON within it.
8386
text = literalText
@@ -108,6 +111,15 @@ public struct ConvertJSONToCodableStruct: EditRefactoringProvider {
108111
return [
109112
SourceEdit(range: closure.trimmedRange, replacement: decls.description)
110113
]
114+
case .endingClosure(let closure, let unexpected):
115+
// Closures are replaced entirely, since they were invalid code to
116+
// start with.
117+
return [
118+
SourceEdit(
119+
range: closure.positionAfterSkippingLeadingTrivia..<unexpected.endPosition,
120+
replacement: decls.description
121+
)
122+
]
111123
case .stringLiteral(let literal, _):
112124
/// Leave the string literal in place (it might be there for testing
113125
/// purposes), and put the newly-created structs afterward.
@@ -129,6 +141,10 @@ extension ConvertJSONToCodableStruct {
129141
/// into Swift.
130142
case closure(ClosureExprSyntax)
131143

144+
/// A closure with a bunch of unexpected nodes following it, which is what
145+
/// a big JSON dictionary looks like when pasted into Swift.
146+
case endingClosure(ClosureExprSyntax, UnexpectedNodesSyntax)
147+
132148
/// A string literal that may contain JSON.
133149
case stringLiteral(StringLiteralExprSyntax, String)
134150
}
@@ -141,6 +157,11 @@ extension ConvertJSONToCodableStruct {
141157
// closure due to the curly braces. The internals might be a syntactic
142158
// disaster, but we don't actually care.
143159
if let closure = syntax.as(ClosureExprSyntax.self) {
160+
if let file = closure.parent?.parent?.parent?.as(SourceFileSyntax.self),
161+
let unexpected = file.unexpectedBetweenStatementsAndEndOfFileToken
162+
{
163+
return .endingClosure(closure, unexpected)
164+
}
144165
return .closure(closure)
145166
}
146167

0 commit comments

Comments
 (0)