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

Commit a169f3b

Browse files
committed
Support setting and using namespaces
1 parent 20e1d4d commit a169f3b

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/CodegenFile.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ final class CodegenFile {
3939
private ?Map<string, Vector<string>> $rekey = null;
4040
private bool $createOnly = false;
4141
private ?ICodegenFormatter $formatter;
42+
private ?string $fileNamespace;
43+
private Vector<string> $useNamespaces = Vector {};
4244

4345
public function __construct(
4446
private IHackCodegenConfig $config,
@@ -270,6 +272,13 @@ public function setDoClobber(bool $do_force): this {
270272

271273
private function getContent(): string {
272274
$builder = hack_builder();
275+
$builder->addLineIf(
276+
$this->fileNamespace !== null,
277+
'namespace %s;',
278+
$this->fileNamespace,
279+
);
280+
$builder->addLines($this->useNamespaces);
281+
273282
foreach ($this->beforeTypes as $type) {
274283
$builder->ensureNewLine()->newLine();
275284
$builder->add($type->render());
@@ -330,6 +339,30 @@ public function setGeneratedFrom(
330339
return $this;
331340
}
332341

342+
public function setNamespace(string $file_namespace): this {
343+
$this->fileNamespace = $file_namespace;
344+
return $this;
345+
}
346+
347+
public function useNamespace(string $ns, ?string $as = null): this {
348+
$this->useNamespaces[] = $as === null
349+
? "use $ns;"
350+
: "use $ns as $as;";
351+
return $this;
352+
}
353+
354+
public function useClass(string $ns, ?string $as = null): this {
355+
return $this->useNamespace($ns, $as);
356+
}
357+
358+
public function useFunction(string $ns, ?string $as = null): this {
359+
return $this->useNamespace('function '.$ns, $as);
360+
}
361+
362+
public function useConst(string $ns, ?string $as = null): this {
363+
return $this->useNamespace('const '.$ns, $as);
364+
}
365+
333366
/**
334367
* If called, save() will only write the file if it doesn't exist
335368
*/

test/CodegenFileTestCase.codegen

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ function fun(): int {
3030
return 0;
3131
}
3232

33+
!@#$%codegentest:testNamespace
34+
<?hh
35+
// Codegen Tests
36+
/**
37+
* This file is generated. Do not modify it manually!
38+
*
39+
* @-generated SignedSource<<1b0ad98452e2fc568c0eda46ed43952e>>
40+
*/
41+
namespace MyNamespace;
42+
use Another\Space;
43+
use My\Space\Bar as bar;
44+
use function My\Space\my_function as f;
45+
use const My\Space\MAX_RETRIES;
46+
47+
class Foo {
48+
}
49+
3350
!@#$%codegentest:testNoSignature
3451
<?hh
3552
// Codegen Tests

test/CodegenFileTestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,19 @@ public function testNoSignature() {
230230
self::assertUnchanged($code);
231231
}
232232

233+
public function testNamespace() {
234+
$code = test_codegen_file('no_file')
235+
->setNamespace('MyNamespace')
236+
->useNamespace('Another\Space')
237+
->useClass('My\Space\Bar', 'bar')
238+
->useFunction('My\Space\my_function', 'f')
239+
->useConst('My\Space\MAX_RETRIES')
240+
->addClass(
241+
codegen_class('Foo')
242+
)
243+
->render();
244+
245+
self::assertUnchanged($code);
246+
}
247+
233248
}

0 commit comments

Comments
 (0)