Skip to content

Commit

Permalink
Merge pull request #503 from rxwei/unary-buildpartialblock
Browse files Browse the repository at this point in the history
Make unary builder return `Regex` type consistently
  • Loading branch information
rxwei authored Jun 20, 2022
2 parents fcd0b59 + e6f3896 commit cc9efb9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Sources/RegexBuilder/Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public enum RegexComponentBuilder {
.init(node: .empty)
}

public static func buildPartialBlock<R: RegexComponent>(first: R ) -> R {
first
public static func buildPartialBlock<R: RegexComponent>(
first component: R
) -> Regex<R.RegexOutput> {
component.regex
}

public static func buildExpression<R: RegexComponent>(_ regex: R) -> R {
Expand Down
22 changes: 21 additions & 1 deletion Tests/RegexBuilderTests/RegexDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ class RegexDSLTests: XCTestCase {
XCTAssertEqual(str.wholeMatch(of: parser)?.1, version)
}
}

func testZeroWidthConsumer() throws {
struct Trace: CustomConsumingRegexComponent {
typealias RegexOutput = Void
Expand Down Expand Up @@ -1051,6 +1051,26 @@ class RegexDSLTests: XCTestCase {
""")
}

func testRegexComponentBuilderResultType() {
// Test that the user can declare a closure or computed property marked with
// `@RegexComponentBuilder` with `Regex` as the result type.
@RegexComponentBuilder
var unaryWithSingleNonRegex: Regex<Substring> {
OneOrMore("a")
}
@RegexComponentBuilder
var multiComponent: Regex<Substring> {
OneOrMore("a")
"b"
}
struct MyCustomRegex: RegexComponent {
@RegexComponentBuilder
var regex: Regex<Substring> {
OneOrMore("a")
}
}
}
}

extension Unicode.Scalar {
Expand Down

0 comments on commit cc9efb9

Please sign in to comment.