Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Sources/SwiftRefactor/ConvertStoredPropertyToComputed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ import SwiftSyntax
#endif

public struct ConvertStoredPropertyToComputed: SyntaxRefactoringProvider {
public static func refactor(syntax: VariableDeclSyntax, in context: ()) throws -> VariableDeclSyntax {
public struct Context {
public let type: TypeSyntax?

public init(type: TypeSyntax? = nil) {
self.type = type
}
}
public static func refactor(syntax: VariableDeclSyntax, in context: Context) throws -> VariableDeclSyntax {
guard syntax.bindings.count == 1, let binding = syntax.bindings.first, let initializer = binding.initializer else {
throw RefactoringNotApplicableError("unsupported variable declaration")
}
Expand Down Expand Up @@ -58,10 +65,21 @@ public struct ConvertStoredPropertyToComputed: SyntaxRefactoringProvider {
body.trailingTrivia += .space
codeBlockSyntax = body
}
let typeAnnotation: TypeAnnotationSyntax?
if let existingType = binding.typeAnnotation {
typeAnnotation = existingType
} else if let providedType = context.type {
typeAnnotation = TypeAnnotationSyntax(type: providedType)
} else {
typeAnnotation = TypeAnnotationSyntax(
type: TypeSyntax(stringLiteral: "<#Type#>")
)
}

let newBinding =
binding
.with(\.initializer, nil)
.with(\.typeAnnotation, typeAnnotation)
.with(
\.accessorBlock,
AccessorBlockSyntax(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,41 @@ final class ConvertStoredPropertyToComputedTest: XCTestCase {

try assertRefactorStructConvert(baseline, expected: expected)
}

func testRefactoringStoredPropertyMissingTypeAnnotation() throws {
let baseline: DeclSyntax = """
var foo = "abc"
"""
let expected: DeclSyntax = """
var foo: <#Type#> {"abc"}
"""

try assertRefactorConvert(baseline, expected: expected)
}

func testRefactoringStoredPropertyWithTypeAnnotation() throws {
let baseline: DeclSyntax = """
var foo = "abc"
"""
let expected: DeclSyntax = """
var foo: String {"abc"}
"""

let context = ConvertStoredPropertyToComputed.Context(type: TypeSyntax(stringLiteral: "String"))
try assertRefactorConvert(baseline, expected: expected, context: context)
}
}

private func assertRefactorConvert(
_ callDecl: DeclSyntax,
expected: DeclSyntax?,
context: ConvertStoredPropertyToComputed.Context = .init(),
file: StaticString = #filePath,
line: UInt = #line
) throws {
try assertRefactor(
callDecl,
context: (),
context: context,
provider: ConvertStoredPropertyToComputed.self,
expected: expected,
file: file,
Expand All @@ -321,7 +345,10 @@ private func assertRefactorStructConvert(

let structCallDecl = try XCTUnwrap(callDecl.as(StructDeclSyntax.self))
let variable = try XCTUnwrap(structCallDecl.memberBlock.members.first?.decl.as(VariableDeclSyntax.self))
let refactored = try ConvertStoredPropertyToComputed.refactor(syntax: variable, in: ())
let refactored = try ConvertStoredPropertyToComputed.refactor(
syntax: variable,
in: ConvertStoredPropertyToComputed.Context()
)

let members = MemberBlockItemListSyntax {
MemberBlockItemSyntax(decl: DeclSyntax(refactored))
Expand Down
Loading