Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/CodegenFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ final class CodegenFile {
private ?Map<string, Vector<string>> $rekey = null;
private bool $createOnly = false;
private ?ICodegenFormatter $formatter;
private ?string $fileNamespace;
private Map<string, ?string> $useNamespaces = Map {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map is actually better.


public function __construct(
private IHackCodegenConfig $config,
Expand Down Expand Up @@ -270,6 +272,15 @@ public function setDoClobber(bool $do_force): this {

private function getContent(): string {
$builder = hack_builder();
$builder->addLineIf(
$this->fileNamespace !== null,
'namespace %s;',
$this->fileNamespace,
);
foreach ($this->useNamespaces as $ns => $as) {
$builder->addLine($as === null ? "use $ns;" : "use $ns as $as;");
}

foreach ($this->beforeTypes as $type) {
$builder->ensureNewLine()->newLine();
$builder->add($type->render());
Expand Down Expand Up @@ -330,6 +341,33 @@ public function setGeneratedFrom(
return $this;
}

public function setNamespace(string $file_namespace): this {
invariant($this->fileNamespace === null, 'namespace has already been set');
$this->fileNamespace = $file_namespace;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check that it's not already set

return $this;
}

public function useNamespace(string $ns, ?string $as = null): this {
invariant(
!$this->useNamespaces->contains($ns),
$ns.' is already being used',
);
$this->useNamespaces[$ns] = $as;
return $this;
}

public function useClass(string $ns, ?string $as = null): this {
return $this->useNamespace($ns, $as);
}

public function useFunction(string $ns, ?string $as = null): this {
return $this->useNamespace('function '.$ns, $as);
}

public function useConst(string $ns, ?string $as = null): this {
return $this->useNamespace('const '.$ns, $as);
}

/**
* If called, save() will only write the file if it doesn't exist
*/
Expand Down
17 changes: 17 additions & 0 deletions test/CodegenFileTestCase.codegen
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ function fun(): int {
return 0;
}

!@#$%codegentest:testNamespace
<?hh
// Codegen Tests
/**
* This file is generated. Do not modify it manually!
*
* @-generated SignedSource<<1b0ad98452e2fc568c0eda46ed43952e>>
*/
namespace MyNamespace;
use Another\Space;
use My\Space\Bar as bar;
use function My\Space\my_function as f;
use const My\Space\MAX_RETRIES;

class Foo {
}

!@#$%codegentest:testNoSignature
<?hh
// Codegen Tests
Expand Down
15 changes: 15 additions & 0 deletions test/CodegenFileTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,19 @@ public function testNoSignature() {
self::assertUnchanged($code);
}

public function testNamespace() {
$code = test_codegen_file('no_file')
->setNamespace('MyNamespace')
->useNamespace('Another\Space')
->useClass('My\Space\Bar', 'bar')
->useFunction('My\Space\my_function', 'f')
->useConst('My\Space\MAX_RETRIES')
->addClass(
codegen_class('Foo')
)
->render();

self::assertUnchanged($code);
}

}