Skip to content

Commit bc6c9a0

Browse files
committed
Builder based initializer for DictionaryExpr
Allow to use `ArrayType` and `DictionaryType` as `PatternBinding.typeAnnotation` argument
1 parent 9efbb3f commit bc6c9a0

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 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 DictionaryExpr {
16+
/// A convenience initializer that allows passing in members using a result builder
17+
/// instead of having to wrap them in a `DictionaryElementList`.
18+
public init(
19+
leftSquare: TokenSyntax = .`leftSquareBracket`,
20+
rightSquare: TokenSyntax = .`rightSquareBracket`,
21+
@DictionaryElementListBuilder contentBuilder: () -> ExpressibleAsDictionaryElementList = { DictionaryElementList([]) }
22+
) {
23+
let elementList = contentBuilder().createDictionaryElementList()
24+
self.init(
25+
leftSquare: leftSquare,
26+
content: elementList.elements.isEmpty ? SyntaxFactory.makeColonToken() : elementList,
27+
rightSquare: rightSquare
28+
)
29+
}
30+
}

Sources/SwiftSyntaxBuilder/gyb_generated/ExpressibleAsProtocols.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,21 +2109,29 @@ public extension ExpressibleAsClassRestrictionType {
21092109
}
21102110
}
21112111

2112-
public protocol ExpressibleAsArrayType: ExpressibleAsTypeBuildable {
2112+
public protocol ExpressibleAsArrayType: ExpressibleAsTypeAnnotation, ExpressibleAsTypeBuildable {
21132113
func createArrayType() -> ArrayType
21142114
}
21152115

21162116
public extension ExpressibleAsArrayType {
2117+
/// Conformance to `ExpressibleAsTypeAnnotation`.
2118+
func createTypeAnnotation() -> TypeAnnotation {
2119+
return TypeAnnotation(type: self)
2120+
}
21172121
func createTypeBuildable() -> TypeBuildable {
21182122
return self.createArrayType()
21192123
}
21202124
}
21212125

2122-
public protocol ExpressibleAsDictionaryType: ExpressibleAsTypeBuildable {
2126+
public protocol ExpressibleAsDictionaryType: ExpressibleAsTypeAnnotation, ExpressibleAsTypeBuildable {
21232127
func createDictionaryType() -> DictionaryType
21242128
}
21252129

21262130
public extension ExpressibleAsDictionaryType {
2131+
/// Conformance to `ExpressibleAsTypeAnnotation`.
2132+
func createTypeAnnotation() -> TypeAnnotation {
2133+
return TypeAnnotation(type: self)
2134+
}
21272135
func createTypeBuildable() -> TypeBuildable {
21282136
return self.createDictionaryType()
21292137
}

Sources/SwiftSyntaxBuilder/gyb_helpers/ExpressibleAsConformances.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
'AccessorList': [
33
'AccessorBlock'
44
],
5+
'ArrayType': [
6+
'TypeAnnotation'
7+
],
58
'CodeBlockItemList': [
69
'CodeBlock'
710
],
811
'DeclBuildable': [
912
'CodeBlockItem',
1013
'MemberDeclListItem'
1114
],
15+
'DictionaryType': [
16+
'TypeAnnotation'
17+
],
1218
'ExprList': [
1319
'ConditionElement'
1420
],

Tests/SwiftSyntaxBuilderTest/VariableTests.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ final class VariableTests: XCTestCase {
66
func testVariableDecl() {
77
let leadingTrivia = Trivia.garbageText("")
88

9-
let buildable = VariableDecl(letOrVarKeyword: .let, bindingsBuilder: {
10-
PatternBinding(pattern: "color", typeAnnotation: "UIColor")
9+
let buildable = VariableDecl(letOrVarKeyword: .let, bindingsBuilder: {
10+
PatternBinding(
11+
pattern: "d",
12+
typeAnnotation: DictionaryType(keyType: "String", valueType: "Int"),
13+
initializer: InitializerClause(value: DictionaryExpr()))
1114
})
1215

1316
let syntax = buildable.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
1417

1518
var text = ""
1619
syntax.write(to: &text)
1720

18-
XCTAssertEqual(text, """
19-
␣let color: UIColor
20-
""")
21+
XCTAssertEqual(text, "␣let d: [String: Int] = [:]")
2122
}
2223

2324
func testVariableDeclWithValue() {

0 commit comments

Comments
 (0)