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

Commit b39cb55

Browse files
Merge pull request #8 from facebook/namespace
Support setting and using namespaces
2 parents 20e1d4d + 94a0403 commit b39cb55

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/CodegenFile.php

Lines changed: 38 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 Map<string, ?string> $useNamespaces = Map {};
4244

4345
public function __construct(
4446
private IHackCodegenConfig $config,
@@ -270,6 +272,15 @@ 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+
foreach ($this->useNamespaces as $ns => $as) {
281+
$builder->addLine($as === null ? "use $ns;" : "use $ns as $as;");
282+
}
283+
273284
foreach ($this->beforeTypes as $type) {
274285
$builder->ensureNewLine()->newLine();
275286
$builder->add($type->render());
@@ -330,6 +341,33 @@ public function setGeneratedFrom(
330341
return $this;
331342
}
332343

344+
public function setNamespace(string $file_namespace): this {
345+
invariant($this->fileNamespace === null, 'namespace has already been set');
346+
$this->fileNamespace = $file_namespace;
347+
return $this;
348+
}
349+
350+
public function useNamespace(string $ns, ?string $as = null): this {
351+
invariant(
352+
!$this->useNamespaces->contains($ns),
353+
$ns.' is already being used',
354+
);
355+
$this->useNamespaces[$ns] = $as;
356+
return $this;
357+
}
358+
359+
public function useClass(string $ns, ?string $as = null): this {
360+
return $this->useNamespace($ns, $as);
361+
}
362+
363+
public function useFunction(string $ns, ?string $as = null): this {
364+
return $this->useNamespace('function '.$ns, $as);
365+
}
366+
367+
public function useConst(string $ns, ?string $as = null): this {
368+
return $this->useNamespace('const '.$ns, $as);
369+
}
370+
333371
/**
334372
* If called, save() will only write the file if it doesn't exist
335373
*/

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)