Skip to content

Commit 8738d98

Browse files
authored
Merge pull request #308 from kimdv/kimdv/add-convenience-initializers-to-variable-decl
Add convenience initializers to `VariableDecl`
2 parents ad768ea + 150dbfa commit 8738d98

7 files changed

+91
-4
lines changed

Sources/SwiftSyntaxBuilder/BooleanLiteralExprConvenienceInitializers.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import SwiftSyntax
14-
1513
extension BooleanLiteralExpr {
1614
public init(_ value: Bool) {
1715
self.init(booleanLiteral: value ? .true : .false)

Sources/SwiftSyntaxBuilder/FloatLiteralExprConvenienceInitializers.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import SwiftSyntax
14-
1513
extension FloatLiteralExpr {
1614
public init(_ value: Float) {
1715
self.init(floatingDigits: String(value))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 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+
extension IdentifierPattern {
14+
init(_ identifier: String) {
15+
self.init(identifier: .identifier(identifier))
16+
}
17+
}
18+
19+
extension IdentifierPattern: ExpressibleByStringLiteral {
20+
public init(stringLiteral value: String) {
21+
self.init(value)
22+
}
23+
}

Sources/SwiftSyntaxBuilder/TypeBuildables.swift renamed to Sources/SwiftSyntaxBuilder/SimpleTypeIdentifierConvenienceInitializers.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ extension SimpleTypeIdentifier {
1818
genericArgumentClause: nil)
1919
}
2020
}
21+
22+
extension SimpleTypeIdentifier: ExpressibleByStringLiteral {
23+
public init(stringLiteral value: String) {
24+
self.init(value)
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 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+
extension TypeAnnotation {
14+
public init(_ type: String) {
15+
self.init(type: SimpleTypeIdentifier(type))
16+
}
17+
}
18+
19+
extension TypeAnnotation: ExpressibleByStringLiteral {
20+
public init(stringLiteral value: String) {
21+
self.init(value)
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 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+
import SwiftSyntax
14+
15+
extension VariableDecl {
16+
public init(_ letOrVarKeyword: TokenSyntax, name: String, type: String) {
17+
self.init(letOrVarKeyword: letOrVarKeyword, bindingsBuilder: {
18+
PatternBinding(pattern: IdentifierPattern(name),
19+
typeAnnotation: TypeAnnotation(type))
20+
})
21+
}
22+
}

Tests/SwiftSyntaxBuilderTest/VariableTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,21 @@ final class VariableTests: XCTestCase {
4040
␣var number: Int = 123
4141
""")
4242
}
43+
44+
func testConvenienceInitializer() {
45+
let leadingTrivia = Trivia.garbageText("")
46+
47+
let testCases: [UInt: (TokenSyntax, String, String, String)] = [
48+
#line: (TokenSyntax.let, "foo", "Int", "␣let foo: Int"),
49+
#line: (TokenSyntax.var, "bar", "Baz", "␣var bar: Baz")
50+
]
51+
52+
for (line, testCase) in testCases {
53+
let (keyword, name, type, expected) = testCase
54+
let builder = VariableDecl(keyword, name: name, type: type)
55+
let syntax = builder.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
56+
57+
XCTAssertEqual(syntax.description, expected, line: line)
58+
}
59+
}
4360
}

0 commit comments

Comments
 (0)