-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathConvertToTernaryExpression.swift
More file actions
306 lines (267 loc) · 10.4 KB
/
ConvertToTernaryExpression.swift
File metadata and controls
306 lines (267 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if swift(>=6)
public import SwiftSyntax
#else
import SwiftSyntax
#endif
/// Converts an if-else statement that assigns to the same variable in both branches
/// into a ternary expression assignment.
///
/// ## Before
///
/// ```swift
/// let result: Type
/// if condition {
/// result = trueValue
/// } else {
/// result = falseValue
/// }
/// ```
///
/// ## After
///
/// ```swift
/// let result: Type = condition ? trueValue : falseValue
/// ```
///
/// Also supports:
/// - Tuple assignments: `(a, b) = condition ? (1, "a") : (2, "b")`
/// - Assignment without declaration: `existingVar = condition ? value1 : value2`
///
/// This refactoring is applicable when:
/// - There is an if-else statement (no else-if chains)
/// - Both branches contain a single assignment expression
/// - Both assignments target the same variable / same tuple pattern
/// - Optionally, the variable is declared immediately before the if statement
public struct ConvertToTernaryExpression: EditRefactoringProvider {
public static func textRefactor(syntax: IfExprSyntax, in context: Void) throws -> [SourceEdit] {
guard let convertible = try ConvertibleIfElse(ifExpr: syntax) else {
throw RefactoringNotApplicableError(
"Cannot convert: if-else branches must each contain a single assignment to the same variable"
)
}
// Walk up to find the CodeBlockItemSyntax containing this if expression
let ifItem: CodeBlockItemSyntax
if let exprStmt = syntax.parent?.as(ExpressionStmtSyntax.self),
let item = exprStmt.parent?.as(CodeBlockItemSyntax.self)
{
ifItem = item
} else if let item = syntax.parent?.as(CodeBlockItemSyntax.self) {
ifItem = item
} else {
throw RefactoringNotApplicableError("if expression is not in a code block")
}
guard let codeBlockList = ifItem.parent?.as(CodeBlockItemListSyntax.self) else {
throw RefactoringNotApplicableError("if expression is not in a code block list")
}
// Check for a preceding compatible variable declaration
let items = Array(codeBlockList)
guard let ifIdx = items.firstIndex(where: { $0.id == ifItem.id }) else {
throw RefactoringNotApplicableError("cannot find if expression in code block")
}
let precedingItem = ifIdx > 0 ? items[ifIdx - 1] : nil
if let precedingItem,
let varDecl = precedingItem.item.as(VariableDeclSyntax.self),
varDecl.isVariableDeclarationWithoutInitializer,
let binding = varDecl.bindings.only,
let identifierPattern = binding.pattern.as(IdentifierPatternSyntax.self),
identifierPattern.identifier.text == convertible.assignmentTargetName
{
// Case: merge preceding decl + if → single declaration with ternary initializer.
// Replace the range from the start of the declaration to the end of the if expression
// (excluding surrounding trivia), preserving whatever whitespace is on either side.
let newDecl = createTernaryDeclaration(from: convertible, variableDecl: varDecl)
let range = precedingItem.positionAfterSkippingLeadingTrivia..<ifItem.endPositionBeforeTrailingTrivia
return [SourceEdit(range: range, replacement: newDecl.trimmed.description)]
} else {
// Case: standalone if → assignment expression with ternary.
// Replace just the if expression's content, preserving surrounding trivia.
let assignment = createTernaryAssignment(from: convertible)
let assignmentStmt = ExpressionStmtSyntax(expression: assignment)
let range = ifItem.positionAfterSkippingLeadingTrivia..<ifItem.endPositionBeforeTrailingTrivia
return [SourceEdit(range: range, replacement: assignmentStmt.trimmed.description)]
}
}
// MARK: - Extracting Assignments
fileprivate static func extractSingleAssignment(
from codeBlock: CodeBlockSyntax
) throws -> AssignmentInfo? {
guard let statement = codeBlock.statements.only else {
return nil
}
let expr: ExprSyntax
if let exprStmt = statement.item.as(ExpressionStmtSyntax.self) {
expr = exprStmt.expression
} else if let directExpr = statement.item.as(ExprSyntax.self) {
expr = directExpr
} else {
return nil
}
guard let sequenceExpr = expr.as(SequenceExprSyntax.self) else {
return nil
}
return try extractFromSequenceAssignment(sequenceExpr)
}
private static func extractFromSequenceAssignment(
_ sequenceExpr: SequenceExprSyntax
) throws -> AssignmentInfo? {
let elements = Array(sequenceExpr.elements)
guard elements.count >= 3, elements[1].as(AssignmentExprSyntax.self) != nil else {
return nil
}
let lhs = ExprSyntax(elements[0])
let rhs: ExprSyntax
if elements.count == 3 {
rhs = ExprSyntax(elements[2])
} else {
rhs = ExprSyntax(SequenceExprSyntax(elements: ExprListSyntax(Array(elements[2...]))))
}
return AssignmentInfo(
targetExpr: lhs,
targetName: lhs.as(DeclReferenceExprSyntax.self)?.baseName.text,
valueExpr: rhs
)
}
// MARK: - Builders
private static func makeTernaryExpr(from convertible: ConvertibleIfElse) -> TernaryExprSyntax {
TernaryExprSyntax(
condition: convertible.condition.trimmed.with(\.trailingTrivia, .space),
questionMark: .infixQuestionMarkToken(trailingTrivia: .space),
thenExpression: convertible.trueExpr.trimmed.with(\.trailingTrivia, .space),
colon: .colonToken(trailingTrivia: .space),
elseExpression: convertible.falseExpr.trimmed
)
}
/// Creates the new variable declaration with ternary initializer.
/// Preserves the original type annotation for named tuples only.
private static func createTernaryDeclaration(
from convertible: ConvertibleIfElse,
variableDecl: VariableDeclSyntax
) -> VariableDeclSyntax {
guard let originalBinding = variableDecl.bindings.only else {
fatalError("Invalid state: binding should exist")
}
let ternaryExpr = makeTernaryExpr(from: convertible)
let preserveAnnotation = originalBinding.typeAnnotation.map { isNamedTupleType($0.type) } ?? false
let typeAnnotation: TypeAnnotationSyntax? = preserveAnnotation ? originalBinding.typeAnnotation?.trimmed : nil
let newBinding =
originalBinding
.with(\.typeAnnotation, typeAnnotation)
.with(
\.initializer,
InitializerClauseSyntax(
equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space),
value: ExprSyntax(ternaryExpr)
)
)
return variableDecl.with(\.bindings, PatternBindingListSyntax([newBinding]))
}
/// Creates a ternary assignment expression (when no declaration exists).
private static func createTernaryAssignment(from convertible: ConvertibleIfElse) -> ExprSyntax {
let ternaryExpr = makeTernaryExpr(from: convertible)
let assignmentSeq = SequenceExprSyntax(
elements: ExprListSyntax([
convertible.assignmentTargetExpr.with(\.trailingTrivia, .space),
ExprSyntax(AssignmentExprSyntax()).with(\.trailingTrivia, .space),
ExprSyntax(ternaryExpr),
])
)
return ExprSyntax(assignmentSeq)
}
// MARK: - Validation Helpers
private static func isNamedTupleType(_ type: TypeSyntax) -> Bool {
guard let tupleType = type.as(TupleTypeSyntax.self) else { return false }
return tupleType.elements.contains { $0.firstName != nil }
}
}
// MARK: - Models
/// Holds the extracted components of a convertible if-else pattern.
///
/// Example: given
/// ```swift
/// if condition {
/// result = trueValue
/// } else {
/// result = falseValue
/// }
/// ```
/// - `assignmentTargetExpr` is `result`
/// - `assignmentTargetName` is `"result"` (`nil` for tuple targets like `(x, y)`)
/// - `condition` is `condition`
/// - `trueExpr` is `trueValue` (from the then-branch)
/// - `falseExpr` is `falseValue` (from the else-branch)
private struct ConvertibleIfElse {
/// LHS of the assignment (`result` or `(x, y)`).
let assignmentTargetExpr: ExprSyntax
/// Only present when LHS is a simple identifier (e.g. `result`).
let assignmentTargetName: String?
let condition: ExprSyntax
let trueExpr: ExprSyntax
let falseExpr: ExprSyntax
init?(ifExpr: IfExprSyntax) throws {
guard let conditionElement = ifExpr.conditions.only,
case .expression(let condition) = conditionElement.condition
else {
return nil
}
guard let elseBody = ifExpr.elseBody, case .codeBlock(let elseBlock) = elseBody else {
return nil
}
guard let thenAssignment = try ConvertToTernaryExpression.extractSingleAssignment(from: ifExpr.body) else {
return nil
}
guard let elseAssignment = try ConvertToTernaryExpression.extractSingleAssignment(from: elseBlock) else {
return nil
}
guard thenAssignment.targetExpr.trimmed.description == elseAssignment.targetExpr.trimmed.description else {
return nil
}
self.assignmentTargetExpr = thenAssignment.targetExpr
self.assignmentTargetName = thenAssignment.targetName
self.condition = condition
self.trueExpr = thenAssignment.valueExpr
self.falseExpr = elseAssignment.valueExpr
}
}
/// Represents the extracted components of a single assignment expression.
///
/// Example: for `result = trueValue`:
/// - `targetExpr` is `result`
/// - `targetName` is `"result"` (nil for tuple targets like `(x, y)`)
/// - `valueExpr` is `trueValue`
///
/// Example: for `(x, y) = (1, 2)`:
/// - `targetExpr` is `(x, y)`
/// - `targetName` is `nil`
/// - `valueExpr` is `(1, 2)`
private struct AssignmentInfo {
let targetExpr: ExprSyntax
let targetName: String?
let valueExpr: ExprSyntax
}
extension VariableDeclSyntax {
/// Returns `true` if this is a `let` or `var` declaration with a type annotation
/// but no initializer and no attributes.
///
/// Example: `let result: Int` → `true`; `let result = 0` → `false`
fileprivate var isVariableDeclarationWithoutInitializer: Bool {
guard let binding = bindings.only,
binding.typeAnnotation?.type != nil,
binding.initializer == nil,
attributes.isEmpty
else {
return false
}
return true
}
}