Skip to content

Commit cc9efb9

Browse files
authored
Merge pull request #503 from rxwei/unary-buildpartialblock
Make unary builder return `Regex` type consistently
2 parents fcd0b59 + e6f3896 commit cc9efb9

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Diff for: Sources/RegexBuilder/Builder.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ public enum RegexComponentBuilder {
1818
.init(node: .empty)
1919
}
2020

21-
public static func buildPartialBlock<R: RegexComponent>(first: R ) -> R {
22-
first
21+
public static func buildPartialBlock<R: RegexComponent>(
22+
first component: R
23+
) -> Regex<R.RegexOutput> {
24+
component.regex
2325
}
2426

2527
public static func buildExpression<R: RegexComponent>(_ regex: R) -> R {

Diff for: Tests/RegexBuilderTests/RegexDSLTests.swift

+21-1
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ class RegexDSLTests: XCTestCase {
10151015
XCTAssertEqual(str.wholeMatch(of: parser)?.1, version)
10161016
}
10171017
}
1018-
1018+
10191019
func testZeroWidthConsumer() throws {
10201020
struct Trace: CustomConsumingRegexComponent {
10211021
typealias RegexOutput = Void
@@ -1051,6 +1051,26 @@ class RegexDSLTests: XCTestCase {
10511051
10521052
""")
10531053
}
1054+
1055+
func testRegexComponentBuilderResultType() {
1056+
// Test that the user can declare a closure or computed property marked with
1057+
// `@RegexComponentBuilder` with `Regex` as the result type.
1058+
@RegexComponentBuilder
1059+
var unaryWithSingleNonRegex: Regex<Substring> {
1060+
OneOrMore("a")
1061+
}
1062+
@RegexComponentBuilder
1063+
var multiComponent: Regex<Substring> {
1064+
OneOrMore("a")
1065+
"b"
1066+
}
1067+
struct MyCustomRegex: RegexComponent {
1068+
@RegexComponentBuilder
1069+
var regex: Regex<Substring> {
1070+
OneOrMore("a")
1071+
}
1072+
}
1073+
}
10541074
}
10551075

10561076
extension Unicode.Scalar {

0 commit comments

Comments
 (0)