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
42 changes: 34 additions & 8 deletions src/CodegenFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ enum CodegenFileResult: int {
CREATE = 2;
}

enum CodegenFileType: int {
PHP = 0;
HACK_DECL = 1;
HACK_PARTIAL = 2;
HACK_STRICT = 3;
}

/**
* File of generated code. The file is composed by classes.
* The file will be signed, either as autogenerated or partially generated,
* depending on whether there are manual sections.
*/
final class CodegenFile {

private bool $isStrict = false;
private CodegenFileType $fileType = CodegenFileType::HACK_PARTIAL;
private ?string $docBlock;
private string $fileName;
private string $relativeFileName;
Expand Down Expand Up @@ -175,10 +182,20 @@ public function rekeyManualSection(string $old_key, string $new_key): this {
}

/**
* Whether the generated file will be Hack strict mode
* Whether the generated file will be Hack strict mode or partial mode.
* For more flexibility, use setFileType.
*/
public function setIsStrict(bool $value): this {
$this->isStrict = $value;
if ($value) {
$this->setFileType(CodegenFileType::HACK_STRICT);
} else {
$this->setFileType(CodegenFileType::HACK_PARTIAL);
}
return $this;
}

public function setFileType(CodegenFileType $type): this {
$this->fileType = $type;
return $this;
}

Expand All @@ -202,14 +219,23 @@ public function getFormatter(): ?ICodegenFormatter {
}


private function getFileTypeDeclaration(): string {
switch($this->fileType) {
case CodegenFileType::PHP:
return '<?php';
case CodegenFileType::HACK_DECL:
return '<?hh // decl';
case CodegenFileType::HACK_PARTIAL:
return '<?hh';
case CodegenFileType::HACK_STRICT:
return '<?hh // strict';
}
}

public function render(): string {
$builder = hack_builder();

$first_line = '<?hh';
if ($this->isStrict) {
$first_line .= ' // strict';
}
$builder->addLine($first_line);
$builder->addLine($this->getFileTypeDeclaration());
$header = $this->config->getFileHeader();
if ($header) {
foreach ($header as $line) {
Expand Down
24 changes: 24 additions & 0 deletions test/CodegenFileTestCase.codegen
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ class PartiallyGenerated {
class PartiallyGeneratedLoader {
}

!@#$%codegentest:testPhpFile
<?php
// Codegen Tests
/**
* This file is generated. Do not modify it manually!
*
* @-generated SignedSource<<75ccd5361b4a4337b9ded246a44f3b4c>>
*/

class Foo {
}

!@#$%codegentest:testSaveAutogenerated
<?hh
// Codegen Tests
Expand Down Expand Up @@ -122,3 +134,15 @@ class Demo {
}
}

!@#$%codegentest:testStrictFile
<?hh // strict
// Codegen Tests
/**
* This file is generated. Do not modify it manually!
*
* @-generated SignedSource<<5c0182dcd097a22f08d4dfbcc555c52c>>
*/

class Foo {
}

18 changes: 18 additions & 0 deletions test/CodegenFileTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,22 @@ public function testNamespace() {
self::assertUnchanged($code);
}

public function testStrictFile() {
$code = test_codegen_file('no_file')
->setIsStrict(true)
->addClass(codegen_class('Foo'))
->render();

self::assertUnchanged($code);
}

public function testPhpFile() {
$code = test_codegen_file('no_file')
->setFileType(CodegenFileType::PHP)
->addClass(codegen_class('Foo'))
->render();

self::assertUnchanged($code);
}

}