Skip to content

Commit e6f3896

Browse files
committed
Make unary builder return Regex type consistently.
Currently, unary regex component builder simply forwards the component's base type. However, this is inconsistent with non-unary builder results. The current behavior may lead to surprising results when the user marks a property with `@RegexComponentBuilder`. This patch makes `RegexComponentBuilder.buildPartialBlock<R>(first: R)` return a `Regex<R.RegexOutput>` rather than `R` itself. --- Before: ```swift // error: cannot convert value of type 'OneOrMore<Substring>' to specified type 'Regex<Substring>' @RegexComponentBuilder var r: Regex<Substring> { OneOrMore("a") // Adding other components below will make the error go away. } struct MyCustomRegex: RegexComponent { // error: cannot convert value of type 'OneOrMore<Substring>' to specified type 'Regex<Substring>' var regex: Regex<Substring> { OneOrMore("a") } } ``` After: No errors.
1 parent fcd0b59 commit e6f3896

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)