Skip to content

Commit 2ce1c25

Browse files
authored
Merge pull request swiftlang#11 from kitasuke/SR-11115/type-annotation
[SR-11115] Missing type annotation on multiple declarations
2 parents 97674b5 + e4a7589 commit 2ce1c25

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Sources/SwiftFormatRules/OneVariableDeclarationPerLine.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,14 @@ public final class OneVariableDeclarationPerLine: SyntaxFormatRule {
5454
// The first binding corresponds to the original `var`/`let`
5555
// declaration, so it should not have its trivia replaced.
5656
var isFirst = true
57+
// The last binding only has type annotation when multiple
58+
// declarations in one line, so make a new binding with its type
59+
let typeAnnotation = varDecl.bindings.last?.typeAnnotation
5760
for binding in varDecl.bindings {
58-
let newBinding = binding.withTrailingComma(nil)
61+
var newBinding = binding.withTrailingComma(nil)
62+
if typeAnnotation != nil && binding.typeAnnotation == nil {
63+
newBinding = newBinding.withTypeAnnotation(typeAnnotation)
64+
}
5965
let newDecl = varDecl.withBindings(
6066
SyntaxFactory.makePatternBindingList([newBinding]))
6167
var finalDecl: Syntax = newDecl

Tests/SwiftFormatRulesTests/OneVariableDeclarationPerLineTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class OneVariableDeclarationPerLineTests: DiagnosingTestCase {
1212
var a = 0, b = 2, (c, d) = (0, "h")
1313
let e = 0, f = 2, (g, h) = (0, "h")
1414
var x: Int { return 3 }
15+
let a, b, c: Int
1516
""",
1617
expected: """
1718
var a = 0
@@ -21,6 +22,9 @@ public class OneVariableDeclarationPerLineTests: DiagnosingTestCase {
2122
let f = 2
2223
let (g, h) = (0, "h")
2324
var x: Int { return 3 }
25+
let a: Int
26+
let b: Int
27+
let c: Int
2428
""")
2529
}
2630

0 commit comments

Comments
 (0)