@@ -78,6 +78,9 @@ public struct ConvertJSONToCodableStruct: EditRefactoringProvider {
78
78
/// We're only going to look at the text of the closure to see if we
79
79
/// have JSON in there.
80
80
text = closure. trimmedDescription
81
+ case let . endingClosure( closure, unexpected) :
82
+ text = closure. trimmedDescription + unexpected. description
83
+
81
84
case . stringLiteral( _, let literalText) :
82
85
/// A string literal that could contain JSON within it.
83
86
text = literalText
@@ -108,6 +111,15 @@ public struct ConvertJSONToCodableStruct: EditRefactoringProvider {
108
111
return [
109
112
SourceEdit ( range: closure. trimmedRange, replacement: decls. description)
110
113
]
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
+ ]
111
123
case . stringLiteral( let literal, _) :
112
124
/// Leave the string literal in place (it might be there for testing
113
125
/// purposes), and put the newly-created structs afterward.
@@ -129,6 +141,10 @@ extension ConvertJSONToCodableStruct {
129
141
/// into Swift.
130
142
case closure( ClosureExprSyntax )
131
143
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
+
132
148
/// A string literal that may contain JSON.
133
149
case stringLiteral( StringLiteralExprSyntax , String )
134
150
}
@@ -141,6 +157,11 @@ extension ConvertJSONToCodableStruct {
141
157
// closure due to the curly braces. The internals might be a syntactic
142
158
// disaster, but we don't actually care.
143
159
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
+ }
144
165
return . closure( closure)
145
166
}
146
167
0 commit comments