Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,20 @@ public struct ConvertStoredPropertyToComputed: SyntaxRefactoringProvider {
if let existingType = binding.typeAnnotation {
typeAnnotation = existingType
} else if let providedType = context.type {
typeAnnotation = TypeAnnotationSyntax(type: providedType)
typeAnnotation = TypeAnnotationSyntax(
colon: .colonToken(leadingTrivia: [], trailingTrivia: .space),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leading trivia is empty by default so we don’t need to specify it.

type: providedType
)
} else {
typeAnnotation = TypeAnnotationSyntax(
colon: .colonToken(leadingTrivia: [], trailingTrivia: .space),
type: TypeSyntax(stringLiteral: "<#Type#>")
)
}

let newBinding =
binding
.with(\.pattern, binding.pattern.with(\.trailingTrivia, []))
.with(\.initializer, nil)
.with(\.typeAnnotation, typeAnnotation)
.with(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,14 @@ final class ConvertStoredPropertyToComputedTest: XCTestCase {

func testRefactoringStoredPropertyMissingTypeAnnotation() throws {
let baseline: DeclSyntax = "var foo = \"abc\""
let expected: DeclSyntax = "var foo :<#Type#>{ \"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 expected: DeclSyntax = "var foo: String{ \"abc\" }"

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