|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#if swift(>=6) |
| 14 | +public import SwiftSyntax |
| 15 | +#else |
| 16 | +import SwiftSyntax |
| 17 | +#endif |
| 18 | + |
| 19 | +public struct ConvertComputedPropertyToStored: SyntaxRefactoringProvider { |
| 20 | + public static func refactor(syntax: VariableDeclSyntax, in context: ()) -> VariableDeclSyntax? { |
| 21 | + guard syntax.bindings.count == 1, let binding = syntax.bindings.first, |
| 22 | + let accessorBlock = binding.accessorBlock, case let .getter(body) = accessorBlock.accessors, !body.isEmpty |
| 23 | + else { |
| 24 | + return nil |
| 25 | + } |
| 26 | + |
| 27 | + var initializer: InitializerClauseSyntax? |
| 28 | + |
| 29 | + if body.count > 1 { |
| 30 | + let closure = ClosureExprSyntax( |
| 31 | + statements: body.with(\.trailingTrivia, .newline) |
| 32 | + ) |
| 33 | + |
| 34 | + initializer = InitializerClauseSyntax( |
| 35 | + value: FunctionCallExprSyntax( |
| 36 | + leadingTrivia: .space, |
| 37 | + callee: closure |
| 38 | + ) |
| 39 | + ) |
| 40 | + } else if let item = body.first?.item.as(ExprSyntax.self) { |
| 41 | + initializer = InitializerClauseSyntax( |
| 42 | + value: item.with(\.leadingTrivia, .space).with(\.trailingTrivia, Trivia()) |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + guard let initializer else { return nil } |
| 47 | + |
| 48 | + let newBinding = |
| 49 | + binding |
| 50 | + .with(\.initializer, initializer) |
| 51 | + .with(\.accessorBlock, nil) |
| 52 | + |
| 53 | + let bindingSpecifier = syntax.bindingSpecifier |
| 54 | + .with(\.tokenKind, .keyword(.let)) |
| 55 | + |
| 56 | + return |
| 57 | + syntax |
| 58 | + .with(\.bindingSpecifier, bindingSpecifier) |
| 59 | + .with(\.bindings, PatternBindingListSyntax([newBinding])) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +fileprivate extension FunctionCallExprSyntax { |
| 64 | + |
| 65 | + init( |
| 66 | + leadingTrivia: Trivia? = nil, |
| 67 | + callee: some ExprSyntaxProtocol, |
| 68 | + argumentList: () -> LabeledExprListSyntax = { [] }, |
| 69 | + trailingTrivia: Trivia? = nil |
| 70 | + ) { |
| 71 | + let argumentList = argumentList() |
| 72 | + self.init( |
| 73 | + leadingTrivia: leadingTrivia, |
| 74 | + calledExpression: callee, |
| 75 | + leftParen: .leftParenToken(), |
| 76 | + arguments: argumentList, |
| 77 | + rightParen: .rightParenToken(), |
| 78 | + trailingTrivia: trailingTrivia |
| 79 | + ) |
| 80 | + } |
| 81 | +} |
0 commit comments