forked from hhvm/hack-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegenWithAttributes.hack
More file actions
70 lines (60 loc) · 1.78 KB
/
CodegenWithAttributes.hack
File metadata and controls
70 lines (60 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* 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\{C, Dict, Str, Vec};
trait CodegenWithAttributes {
protected IHackCodegenConfig $config;
private dict<string, vec<string>> $userAttributes = dict[];
final public function addEmptyUserAttribute(string $name): this {
$this->addUserAttribute($name, vec[], HackBuilderValues::export());
return $this;
}
final public function addUserAttribute<T>(
string $name,
Traversable<T> $values,
IHackBuilderValueRenderer<T> $renderer,
): this {
$this->userAttributes[$name] =
Vec\map($values, $v ==> $renderer->render($this->config, $v));
return $this;
}
final public function getUserAttributes(): dict<string, vec<string>> {
return $this->userAttributes;
}
final public function getAttributes(): dict<string, vec<string>> {
$attributes = $this->getExtraAttributes();
return Dict\merge($attributes, $this->userAttributes);
}
final public function hasAttributes(): bool {
return !C\is_empty($this->getAttributes());
}
final public function renderAttributes(): ?string {
$attributes = $this->getAttributes();
if (C\is_empty($attributes)) {
return null;
}
return '<<'.
Str\join(
Dict\map_with_key(
$attributes,
($name, $params) ==> {
if (C\is_empty($params)) {
return $name;
}
return $name.'('.Str\join($params, ', ').')';
},
),
', '
).
'>>';
}
protected function getExtraAttributes(): dict<string, vec<string>> {
return dict[];
}
}