forked from hhvm/hhast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeTypesTest.hack
182 lines (165 loc) · 7.43 KB
/
NodeTypesTest.hack
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (c) 2017-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\HHAST;
use function Facebook\FBExpect\expect;
use namespace HH\Lib\{C, Str, Vec};
final class NodeTypesTest extends TestCase {
public function testListVariance(): void {
$_typecheck = (
(?NodeList<ListItem<IParameter>> $_) ==> null
)((null ?as FunctionDeclarationHeader)?->getParameterListx());
$_typecheck = (
(?NodeList<ListItem<Node>> $_) ==> null
)((null ?as FunctionDeclarationHeader)?->getParameterListx());
$_typecheck = (
(?NodeList<Node> $_) ==> null
)((null ?as FunctionDeclarationHeader)?->getParameterListx());
}
public function testBinaryExpressionOperandsAreExpressions(): void {
$typecheck = (?IExpression $_) ==> null;
$be = null ?as BinaryExpression;
$typecheck($be?->getLeftOperand());
$typecheck($be?->getRightOperand());
}
public function testFunctionTypeIsITypeSpecifier(): void {
$typecheck = (?ITypeSpecifier $_) ==> null;
$func = null ?as FunctionDeclarationHeader;
$typecheck($func?->getType());
}
public async function testNoreturnFunctionTypeIsWrapped(): Awaitable<void> {
$code = '<?hh function foo(): noreturn {}';
$ast = await from_file_async(File::fromPathAndContents('/dev/null', $code));
list($_markup, $func) = $ast->getDeclarations()->getChildren();
$func = expect($func)->toBeInstanceOf(FunctionDeclaration::class);
$ret = $func->getDeclarationHeader()->getType();
$ret = expect($ret)->toBeInstanceOf(SimpleTypeSpecifier::class);
expect($ret->getSpecifier())->toBeInstanceOf(NoreturnToken::class);
}
public async function testTypeConstantLHSIsWrapped(): Awaitable<void> {
$code = "<?hh function foo(this::TFoo \$_): void {}";
$ast = await from_file_async(File::fromPathAndContents('/dev/null', $code));
list($_markup, $func) = $ast->getDeclarations()->getChildren();
$func = expect($func)->toBeInstanceOf(FunctionDeclaration::class);
$p = $func->getDeclarationHeader()
->getParameterListx()
->getChildrenOfItems()
|> C\firstx($$);
$t = expect($p->getType())->toBeInstanceOf(TypeConstant::class);
$lhs = expect($t->getLeftType())->toBeInstanceOf(
SimpleTypeSpecifier::class,
);
expect($lhs->getSpecifier())->toBeInstanceOf(ThisToken::class);
}
public async function testNameTokenWrappedAsExpression(): Awaitable<void> {
$code = "<?hh \$x = SOME_CONST; \$y = SOME\\NAMESPACED\\CONST;";
$ast = await from_file_async(File::fromPathAndContents('/dev/null', $code));
list($_markup, $x, $y) = $ast->getDeclarations()->getChildren();
$x = expect($x)->toBeInstanceOf(ExpressionStatement::class);
$x_expr = $x->getExpressionx() as BinaryExpression;
$some_const = expect($x_expr->getRightOperand())->toBeInstanceOf(
NameExpression::class,
);
$name_token = expect($some_const->getWrappedNode())->toBeInstanceOf(
NameToken::class,
);
expect($name_token->getText())->toBeSame('SOME_CONST');
$y = expect($y)->toBeInstanceOf(ExpressionStatement::class);
$y_expr = $y->getExpressionx() as BinaryExpression;
$some_namespaced_const = expect($y_expr->getRightOperand())->toBeInstanceOf(
NameExpression::class,
);
$qualified_name = expect($some_namespaced_const->getWrappedNode())
->toBeInstanceOf(QualifiedName::class);
expect(
$qualified_name->getDescendantsByType<Token>()
|> Vec\map($$, $t ==> $t->getText())
|> Str\join($$, ''),
)->toBeSame('SOME\\NAMESPACED\\CONST');
}
public async function testNameTokenWrappedInListOfItemsOfExpressions(
): Awaitable<void> {
// Make sure that tokens in List<ListItem<IExpression>> get wrapped
$code = "<?hh \$x = tuple(SOME_CONST, SOME\\NAMESPACED\\CONST);";
$ast = await from_file_async(File::fromPathAndContents('/dev/null', $code));
list($_markup, $x) = $ast->getDeclarations()->getChildren();
$x = expect($x)->toBeInstanceOf(ExpressionStatement::class);
$x_expr = $x->getExpressionx() as BinaryExpression;
$rhs = expect($x_expr->getRightOperand())->toBeInstanceOf(
TupleExpression::class,
);
list($c1, $c2) = $rhs->getItemsx()->getChildrenOfItems();
$c1 = expect($c1)->toBeInstanceOf(
NameExpression::class,
'constant in tuple is not wrapped',
);
$c2 = expect($c2)->toBeInstanceOf(
NameExpression::class,
'namespaced constant in tuple is not wrapped',
);
$c1 = expect($c1->getWrappedNode())->toBeInstanceOf(NameToken::class);
expect($c1->getText())->toBeSame('SOME_CONST');
$c2 = expect($c2->getWrappedNode())->toBeInstanceOf(QualifiedName::class);
expect(
$c2->getDescendantsByType<Token>()
|> Vec\map($$, $t ==> $t->getText())
|> Str\join($$, ''),
)->toBeSame('SOME\\NAMESPACED\\CONST');
}
public async function testXHPClassNameAsNameExpression(): Awaitable<void> {
$code = '<?hh class :foo { children (pcdata | :bar+); }';
$ast = await from_file_async(File::fromPathAndContents('/dev/null', $code));
list($_markup, $x) = $ast->getDeclarations()->getChildren();
$class = expect($x)->toBeInstanceOf(ClassishDeclaration::class);
$decl = $class->getBody()
->getElementsx()
->getChildrenByType<XHPChildrenDeclaration>()
|> C\onlyx($$);
// :bar+
$bin_expr = $decl->getDescendantsByType<PostfixUnaryExpression>()
|> C\onlyx($$);
$lhs = $bin_expr->getOperand();
$lhs = expect($lhs)->toBeInstanceOf(NameExpression::class);
expect($lhs->getCode())->toBeSame(':bar');
expect($lhs->getWrappedNode())->toBeInstanceOf(XHPClassNameToken::class);
$rhs = $bin_expr->getOperator();
expect($rhs)->toBeInstanceOf(PlusToken::class);
}
public async function testXHPChildListAsExpression(): Awaitable<void> {
$code = '<?hh class :foo { children (pcdata)*; }';
$ast = await from_file_async(File::fromPathAndContents('/dev/null', $code));
list($_markup, $x) = $ast->getDeclarations()->getChildren();
$class = expect($x)->toBeInstanceOf(ClassishDeclaration::class);
$decl = $class->getBody()
->getElementsx()
->getChildrenByType<XHPChildrenDeclaration>()
|> C\onlyx($$);
$bin_expr = $decl->getDescendantsByType<PostfixUnaryExpression>()
|> C\onlyx($$);
$lhs = $bin_expr->getOperand();
$lhs = expect($lhs)->toBeInstanceOf(XHPChildrenParenthesizedList::class);
expect($lhs->getCode())->toBeSame('(pcdata)');
$rhs = $bin_expr->getOperator();
expect($rhs)->toBeInstanceOf(StarToken::class);
}
public async function testOldStyleXHPClassNodes(): Awaitable<void> {
$code = '<?hh class :foo { children (pcdata)*; }';
$ast = await from_file_async(File::fromPathAndContents('/dev/null', $code));
list($_markup, $x) = $ast->getDeclarations()->getChildren();
$class = expect($x)->toBeInstanceOf(ClassishDeclaration::class);
expect($class->getName()->getText())->toEqual(':foo');
}
public async function testNewStyleXHPClassNodes(): Awaitable<void> {
$code = '<?hh xhp class foo { };';
$ast = await from_file_async(File::fromPathAndContents('/dev/null', $code));
list($_markup, $x) = $ast->getDeclarations()->getChildren();
$class = expect($x)->toBeInstanceOf(ClassishDeclaration::class);
expect($class->getName()->getText())->toEqual('foo');
expect($class->getXhp())->toBeInstanceOf(XHPToken::class);
}
}