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

Commit 4a6fd11

Browse files
committed
Support file-level constants
1 parent 595b169 commit 4a6fd11

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/CodegenFile.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ final class CodegenFile {
4444
private vec<CodegenFunction> $functions = vec[];
4545
private vec<CodegenType> $beforeTypes = vec[];
4646
private vec<CodegenType> $afterTypes = vec[];
47+
private vec<(string, string, ?string)> $consts = vec[];
4748
private bool $doClobber = false;
4849
protected ?CodegenGeneratedFrom $generatedFrom;
4950
private bool $isSignedFile = true;
@@ -95,6 +96,17 @@ public function addClass(CodegenClassBase $class): this {
9596
return $this;
9697
}
9798

99+
public function addConst<T>(
100+
string $name,
101+
T $value,
102+
?string $comment = null,
103+
IHackBuilderValueRenderer<T> $values_config = HackBuilderValues::export(),
104+
): this {
105+
$rendered_value = $values_config->render($this->config, $value);
106+
$this->consts[] = tuple($name, $rendered_value, $comment);
107+
return $this;
108+
}
109+
98110
public function getClasses(): vec<CodegenClassBase> {
99111
return $this->classes;
100112
}
@@ -396,6 +408,18 @@ private function getContent(): string {
396408
$builder->ensureNewLine()->newLine();
397409
$builder->add($type->render());
398410
}
411+
foreach ($this->consts as list($name, $value, $comment)) {
412+
$builder->ensureEmptyLine();
413+
if ($comment !== null) {
414+
$builder->addDocBlock($comment);
415+
}
416+
$builder->addWithSuggestedLineBreaksf(
417+
"const %s =\t%s;",
418+
$name,
419+
$value,
420+
);
421+
$builder->newLine();
422+
}
399423
foreach ($this->functions as $function) {
400424
$builder->ensureNewLine()->newLine();
401425
$builder->add($function->render());

test/CodegenFileTest.codegen

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ class AllAutogenerated {
1717
}
1818
}
1919

20+
!@#$%codegentest:testConstants
21+
<?hh // strict
22+
// Codegen Tests
23+
/**
24+
* This file is generated. Do not modify it manually!
25+
*
26+
* @-generated SignedSource<<3081d5c20598b6bd1aa52a8035ffd207>>
27+
*/
28+
namespace Foo\Bar;
29+
use namespace Herp\Derp;
30+
31+
const string FOO = 'bar';
32+
33+
/**
34+
* doc comment
35+
*/
36+
const string HERP = 'derp';
37+
2038
!@#$%codegentest:testExecutable
2139
#!/usr/bin/env hhvm
2240
<?hh

test/CodegenFileTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,4 +460,16 @@ public function testFormattingPartiallyGeneratedFile(): void {
460460
'bad signed source',
461461
);
462462
}
463+
464+
public function testConstants(): void {
465+
$cgf = $this->getCodegenFactory();
466+
$code = $cgf
467+
->codegenFile('no_file')
468+
->setNamespace('Foo\\Bar')
469+
->useNamespace('Herp\\Derp')
470+
->addConst('string FOO', 'bar')
471+
->addConst('string HERP', 'derp', 'doc comment')
472+
->render();
473+
$this->assertUnchanged($code);
474+
}
463475
}

0 commit comments

Comments
 (0)