-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for codegen xhp classes (#138)
* Add support for codegen xhp classes Adds xhp attribute codegen to traits and classes. No error is thrown when attributes are added to non-xhp classes. This syntax is valid, but not useful in the current class. Inheritance maybe? * Fix broken copy paste * Compatibility with older hsl versions * Whitespace lint * Update xhp attribute doc I copy pasted property and did not touch up the doc well enough.
- Loading branch information
Showing
10 changed files
with
291 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HackCodegen; | ||
|
||
use namespace HH\Lib\Str; | ||
|
||
/** | ||
* Generate code for an xhp attribute. Please don't use this class directly; | ||
* instead use the function ICodegenFactory->codegenAttribute. E.g.: | ||
* | ||
* ICodegenFactory->codegenAttribute('src') | ||
* ->setType('string') | ||
* ->setInlineComment('A script src must be a valid URI') | ||
* ->render(); | ||
*/ | ||
final class CodegenXHPAttribute implements ICodeBuilderRenderer { | ||
|
||
use HackBuilderRenderer; | ||
|
||
private ?string $comment; | ||
private ?string $type; | ||
private ?string $value; | ||
private ?XHPAttributeDecorator $decorator; | ||
|
||
public function __construct( | ||
protected IHackCodegenConfig $config, | ||
private string $name, | ||
) {} | ||
|
||
public function getName(): string { | ||
return $this->name; | ||
} | ||
|
||
public function getType(): ?string { | ||
return $this->type; | ||
} | ||
|
||
public function getValue(): mixed { | ||
return $this->value; | ||
} | ||
|
||
public function setDecorator(?XHPAttributeDecorator $decorator): this { | ||
invariant( | ||
$decorator is null || $this->value is null, | ||
'XHP attributes with a default value can not have an %s decorator', | ||
xhp_attribute_decorator_to_string($decorator), | ||
); | ||
$this->decorator = $decorator; | ||
return $this; | ||
} | ||
|
||
public function setInlineComment(string $comment): this { | ||
$this->comment = $comment; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Set the type of the member var. In Hack, if it's nullable | ||
* you should prepend the question mark, e.g. "?string". | ||
* XHP enums should be avoided, but you can specify "enum { 'foo' }" | ||
* as a literal string if you need it. | ||
*/ | ||
public function setType(string $type): this { | ||
$this->type = $type; | ||
return $this; | ||
} | ||
|
||
public function setTypef( | ||
Str\SprintfFormatString $format, | ||
mixed ...$args | ||
): this { | ||
return $this->setType(\vsprintf($format, $args)); | ||
} | ||
|
||
/** | ||
* Set the initial value for the variable. You can pass numbers, strings, | ||
* arrays, etc, and it will generate the code to render those values. | ||
*/ | ||
public function setValue<T>( | ||
T $value, | ||
IHackBuilderValueRenderer<T> $renderer, | ||
): this { | ||
invariant( | ||
$this->decorator is null, | ||
'XHP attributes with an %s decorator can not have a default value', | ||
xhp_attribute_decorator_to_string($this->decorator), | ||
); | ||
$this->value = $renderer->render($this->config, $value); | ||
return $this; | ||
} | ||
|
||
public function appendToBuilder(HackBuilder $builder): HackBuilder { | ||
$value = $this->value; | ||
|
||
return $builder | ||
->addDocBlock($this->comment) | ||
->addIf($this->type is nonnull, $this->type.' ') | ||
->add($this->name) | ||
->addIf($this->value is nonnull, ' = '.$value) | ||
->addIf( | ||
$this->decorator is nonnull, | ||
' '. | ||
xhp_attribute_decorator_to_string( | ||
$this->decorator ?? XHPAttributeDecorator::REQUIRED, | ||
), | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HackCodegen; | ||
|
||
enum XHPAttributeDecorator: int { | ||
REQUIRED = 0; | ||
LATE_INIT = 1; | ||
} | ||
|
||
function xhp_attribute_decorator_to_string( | ||
XHPAttributeDecorator $decorator, | ||
): string { | ||
switch ($decorator) { | ||
case XHPAttributeDecorator::REQUIRED: | ||
return '@required'; | ||
case XHPAttributeDecorator::LATE_INIT: | ||
return '@lateinit'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.