File tree Expand file tree Collapse file tree 4 files changed +52
-7
lines changed
Sources/SwiftSyntaxBuilder
Tests/SwiftSyntaxBuilderTest Expand file tree Collapse file tree 4 files changed +52
-7
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -2109,21 +2109,29 @@ public extension ExpressibleAsClassRestrictionType {
2109
2109
}
2110
2110
}
2111
2111
2112
- public protocol ExpressibleAsArrayType : ExpressibleAsTypeBuildable {
2112
+ public protocol ExpressibleAsArrayType : ExpressibleAsTypeAnnotation , ExpressibleAsTypeBuildable {
2113
2113
func createArrayType( ) -> ArrayType
2114
2114
}
2115
2115
2116
2116
public extension ExpressibleAsArrayType {
2117
+ /// Conformance to `ExpressibleAsTypeAnnotation`.
2118
+ func createTypeAnnotation( ) -> TypeAnnotation {
2119
+ return TypeAnnotation ( type: self )
2120
+ }
2117
2121
func createTypeBuildable( ) -> TypeBuildable {
2118
2122
return self . createArrayType ( )
2119
2123
}
2120
2124
}
2121
2125
2122
- public protocol ExpressibleAsDictionaryType : ExpressibleAsTypeBuildable {
2126
+ public protocol ExpressibleAsDictionaryType : ExpressibleAsTypeAnnotation , ExpressibleAsTypeBuildable {
2123
2127
func createDictionaryType( ) -> DictionaryType
2124
2128
}
2125
2129
2126
2130
public extension ExpressibleAsDictionaryType {
2131
+ /// Conformance to `ExpressibleAsTypeAnnotation`.
2132
+ func createTypeAnnotation( ) -> TypeAnnotation {
2133
+ return TypeAnnotation ( type: self )
2134
+ }
2127
2135
func createTypeBuildable( ) -> TypeBuildable {
2128
2136
return self . createDictionaryType ( )
2129
2137
}
Original file line number Diff line number Diff line change 2
2
'AccessorList' : [
3
3
'AccessorBlock'
4
4
],
5
+ 'ArrayType' : [
6
+ 'TypeAnnotation'
7
+ ],
5
8
'CodeBlockItemList' : [
6
9
'CodeBlock'
7
10
],
8
11
'DeclBuildable' : [
9
12
'CodeBlockItem' ,
10
13
'MemberDeclListItem'
11
14
],
15
+ 'DictionaryType' : [
16
+ 'TypeAnnotation'
17
+ ],
12
18
'ExprList' : [
13
19
'ConditionElement'
14
20
],
Original file line number Diff line number Diff line change @@ -6,18 +6,19 @@ final class VariableTests: XCTestCase {
6
6
func testVariableDecl( ) {
7
7
let leadingTrivia = Trivia . garbageText ( " ␣ " )
8
8
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 ( ) ) )
11
14
} )
12
15
13
16
let syntax = buildable. buildSyntax ( format: Format ( ) , leadingTrivia: leadingTrivia)
14
17
15
18
var text = " "
16
19
syntax. write ( to: & text)
17
20
18
- XCTAssertEqual ( text, """
19
- ␣let color: UIColor
20
- """ )
21
+ XCTAssertEqual ( text, " ␣let d: [String: Int] = [:] " )
21
22
}
22
23
23
24
func testVariableDeclWithValue( ) {
You can’t perform that action at this time.
0 commit comments