forked from hhvm/hhast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRewriteBehaviorTest.hack
200 lines (172 loc) · 4.77 KB
/
RewriteBehaviorTest.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* 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\{Str, Vec};
final class RewriteBehaviorTest extends TestCase {
public function testTokenIdentity(): void {
$node = new VariableToken(null, null, '$var');
expect($node->rewrite(($x, $_) ==> $x))
->toBeSame($node);
}
public function testTriviaIdentity(): void {
$node = new DelimitedComment('foo');
expect($node->rewrite(($x, $_) ==> $x))
->toBeSame($node);
}
public function testSyntaxIdentity(): void {
$node = new Script(new NodeList(vec[], null));
expect($node->rewrite(($x, $_) ==> $x))
->toBeSame($node);
}
public function testListIdentity(): void {
$node = new NodeList(
vec[
new SingleLineComment('foo'),
new DelimitedComment('bar'),
],
);
expect($node->rewrite(($x, $_) ==> $x))
->toBeSame($node);
}
public function testRewritingListChildren(): void {
$orig = new NodeList(
vec[
new SingleLineComment('foo'),
],
);
$new = $orig->rewrite(
($x, $_) ==> {
if (!$x is SingleLineComment) {
return $x;
}
return new DelimitedComment('bar');
},
);
expect($orig)->toNotBeSame($new);
expect($new->getCode())->toBeSame('bar');
}
public function testEditingCommentInList(): void {
$orig = new NodeList(
vec[
new SingleLineComment('foo'),
],
);
$new = $orig->rewrite(
($x, $_) ==> {
if (!$x is SingleLineComment) {
return $x;
}
return $x->withText('bar');
},
);
expect($orig)->toNotBeSame($new);
expect($new->getCode())->toBeSame('bar');
}
public function testRewriteLeadingComment(): void {
$orig = new VariableToken(
new NodeList(
vec[
new DelimitedComment('/* foo */'),
new WhiteSpace(' '),
],
),
null,
'$var',
);
$new = $orig->rewrite(
($node, $_) ==> {
if (!$node is DelimitedComment) {
return $node;
}
return $node->withText('/* bar */');
},
);
expect($orig)->toNotBeSame($new);
expect($new->getCode())->toBeSame('/* bar */ $var');
}
public function testEmptyingList(): void {
$orig = new VariableToken(
new NodeList(
vec[
new DelimitedComment('/* foo */'),
new WhiteSpace(' '),
],
),
null,
'$var',
);
$new = $orig->rewrite(
($node, $_parents) ==> {
if (!$node is Trivia) {
return $node;
}
return null;
},
)
|> expect($$)->toBeInstanceOf(VariableToken::class);
expect($new->getLeading()->isEmpty())->toBeTrue(
'Should have empty leading',
);
}
public async function testRewritePreservesCaseOfFixedTextTokens(
): Awaitable<void> {
$code = "<?hh \$x = NuLl;";
$ast = await from_file_async(File::fromPathAndContents('/dev/null', $code));
expect($ast->getCode())->toBeSame($code);
}
public async function testRewritesNewChildren(): Awaitable<void> {
$code = "<?hh type T = shape('subshape' => shape('subfield' => string));";
$orig = await from_file_async(
File::fromPathAndContents('/dev/null', $code),
);
expect($orig->getCode())->toBeSame($code);
$new = $orig->rewrite(
($shape, $_) ==> {
if (!$shape is ShapeTypeSpecifier) {
return $shape;
}
return $shape->withFields(
new NodeList(
Vec\map(
$shape->getFieldsx()->getChildren(),
$item ==> {
$field = $item->getItem();
$name = $field->getName();
$name_t = $name->getFirstDescendantByType<
SingleQuotedStringLiteralToken,
>();
if ($name_t === null) {
return $item;
}
return $field->withName(
$name->replace(
$name_t,
$name_t->withText(
Str\slice(
$name_t->getText(),
1,
Str\length($name_t->getText()) - 2,
)
|> Str\format("'%s_new'", $$),
),
),
)
|> $item->replace($field, $$);
},
),
),
);
},
);
expect($new->getCode())->toBeSame(
"<?hh type T = shape('subshape_new' => shape('subfield_new' => string));",
);
}
}