Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit 9ba709c

Browse files
committed
Add support for regex strings as values
fixes #104
1 parent f0a609f commit 9ba709c

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?hh // strict
2+
/*
3+
* Copyright (c) 2015-present, Facebook, Inc.
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under the MIT license found in the
7+
* LICENSE file in the root directory of this source tree.
8+
*
9+
*/
10+
11+
namespace Facebook\HackCodegen\_Private;
12+
13+
use type Facebook\HackCodegen\{IHackBuilderValueRenderer, IHackCodegenConfig};
14+
15+
use namespace HH\Lib\{Str, Regex};
16+
17+
final class HackBuilderRegexRenderer<T>
18+
implements IHackBuilderValueRenderer<Regex\Pattern<T>> {
19+
20+
final public function render(
21+
IHackCodegenConfig $_,
22+
Regex\Pattern<T> $value,
23+
): string {
24+
return 're"'.
25+
Str\replace_every(
26+
(string) $value,
27+
dict[
28+
'$' => '\\$',
29+
'"' => '\\"',
30+
],
31+
).
32+
'"';
33+
}
34+
}

src/key-value-render/HackBuilderValues.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public static function export(): IHackBuilderValueRenderer<mixed> {
3232
return new _Private\HackBuilderValueExportRenderer();
3333
}
3434

35+
/**
36+
* Render a regex literal, e.g. `re"/foo/"`.
37+
*/
38+
public static function regex<T>(
39+
): IHackBuilderValueRenderer<\HH\Lib\Regex\Pattern<T>> {
40+
return new _Private\HackBuilderRegexRenderer();
41+
}
42+
3543
/**
3644
* Render a Codegen* value to code, and use it as the value.
3745
*/

tests/HackBuilderTest.codegen

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ if ($do_that) {
3030
);
3131
}
3232

33+
!@#$%codegentest:testAsValue
34+
dict[
35+
'foo' => bar,
36+
]
3337
!@#$%codegentest:testClassnameMap
3438
Map {
3539
\Facebook\HackCodegen\HackBuilderTest::class => \stdClass::class,

tests/HackBuilderTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,18 @@ public function testAsValue(): void {
7474
HackBuilderKeys::export(),
7575
HackBuilderValues::literal(),
7676
),
77-
);
77+
)->getCode();
78+
expect_with_context(static::class, $dict)->toBeUnchanged();
79+
}
80+
81+
public function testRegex(): void {
82+
$make_code = $re ==> $this->getHackBuilder()
83+
->addValue($re, HackBuilderValues::regex())
84+
->getCode();
85+
expect($make_code(re"/foo/"))->toBeSame('re"/foo/"');
86+
expect($make_code(re"/\$foo/"))->toBeSame('re"/\$foo/"');
87+
expect($make_code(re"/a\"b/"))->toBeSame('re"/a\"b/"');
88+
expect($make_code(re"/a?b/"))->toBeSame('re"/a?b/"');
7889
}
7990

8091
public function testShapeWithUniformRendering(): void {

0 commit comments

Comments
 (0)