Skip to content

Commit

Permalink
Support generating .hack files
Browse files Browse the repository at this point in the history
fixes #111
refs #112
  • Loading branch information
fredemmott committed Feb 20, 2019
1 parent 46b37e1 commit a5b17b2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/CodegenFile.hack
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum CodegenFileType: int {
HACK_DECL = 1;
HACK_PARTIAL = 2;
HACK_STRICT = 3;
DOT_HACK = 4;
}

/**
Expand Down Expand Up @@ -223,6 +224,8 @@ final class CodegenFile {
return '<?hh // partial';
case CodegenFileType::HACK_STRICT:
return '<?hh // strict';
case CodegenFileType::DOT_HACK:
return '';
}
}

Expand Down Expand Up @@ -298,7 +301,10 @@ final class CodegenFile {
$builder->addLine($shebang);
}

$builder->addLine($this->getFileTypeDeclaration());
$decl = $this->getFileTypeDeclaration();
if ($decl !== '') {
$builder->addLine($decl);
}
$header = $this->config->getFileHeader();
if ($header) {
foreach ($header as $line) {
Expand Down
14 changes: 14 additions & 0 deletions tests/CodegenFileTest.codegen
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ const string FOO = 'bar';
*/
const string HERP = 'derp';

!@#$%codegentest:testDotHackExecutable
#!/usr/bin/env hhvm
// Codegen Tests
/**
* This file is generated. Do not modify it manually!
*
* @-generated SignedSource<<2e680a68425d6302b789a6588a8218e4>>
*/

<<__EntryPoint>>
function main(): noreturn {
exit(0);
}

!@#$%codegentest:testExecutable
#!/usr/bin/env hhvm
<?hh // partial
Expand Down
16 changes: 16 additions & 0 deletions tests/CodegenFileTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,22 @@ final class CodegenFileTest extends CodegenBaseTest {
);
}

public function testDotHackExecutable(): void {
$cgf = $this->getCodegenFactory();
$code = $cgf
->codegenFile('no_file')
->setFileType(CodegenFileType::DOT_HACK)
->setShebangLine('#!/usr/bin/env hhvm')
->addFunction(
$cgf->codegenFunction('main')
->setReturnType('noreturn')
->addEmptyUserAttribute('__EntryPoint')
->setBody('exit(0);')
)
->render();
expect_with_context(static::class, $code)->toBeUnchanged();
}

public function testNoPseudoMainHeaderInStrict(): void {
expect(() ==> {
$cgf = $this->getCodegenFactory();
Expand Down

0 comments on commit a5b17b2

Please sign in to comment.