forked from hhvm/hhast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveXHPChildDeclarationsMigration.hack
More file actions
86 lines (79 loc) · 2.57 KB
/
Copy pathRemoveXHPChildDeclarationsMigration.hack
File metadata and controls
86 lines (79 loc) · 2.57 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* 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 namespace HH\Lib\{C, Vec};
final class RemoveXHPChildDeclarationsMigration extends StepBasedMigration {
private static function replaceTrait(ClassishBody $in): ClassishBody {
$uses = $in->getElements()?->getChildrenByType<TraitUse>() ?? vec[]
|> Vec\map($$, $use ==> $use->getNames()->getChildrenOfItems())
|> Vec\flatten($$);
foreach ($uses as $use) {
$name = $use ?as SimpleTypeSpecifier
|> $$?->getSpecifier() ?as NameToken
|> $$?->getText();
if ($name === 'XHPChildDeclarationConsistencyValidation') {
return $in->replace(
$use,
($use as SimpleTypeSpecifier)->withSpecifier(
new NameToken(null, null, 'XHPChildValidation'),
),
);
}
}
return $in;
}
private static function removeChildrenDeclaration(
ClassishBody $in,
): ClassishBody {
$decls = $in->getElements()
?->getChildrenByType<XHPChildrenDeclaration>() ??
vec[];
if (C\is_empty($decls)) {
return $in;
}
$decls = vec($decls);
$elems = $in->getElementsx()->toVec();
foreach ($decls as $decl) {
$idx = C\find_key($elems, $elem ==> $elem === $decl) as nonnull;
$next = $elems[$idx + 1] ?? null;
if ($next !== null) {
$trivia = $decl->getFirstTokenx()->getLeading()->toVec();
$to_trim = C\find_key(
Vec\reverse($trivia),
$node ==> !($node is WhiteSpace || $node is EndOfLine),
);
$keep = $to_trim === null
? vec[]
: Vec\slice($trivia, 0, C\count($trivia) - $to_trim);
$t = $next->getFirstTokenx();
$elems[$idx + 1] = $next->replace(
$t,
$t->withLeading(
new NodeList(Vec\concat($keep, $t->getLeading()->toVec())),
),
);
}
$elems = Vec\concat(Vec\take($elems, $idx), Vec\drop($elems, $idx + 1));
}
return $in->withElements(new NodeList($elems));
}
<<__Override>>
public function getSteps(): Traversable<IMigrationStep> {
return vec[
new NodeTypeMigrationStep<ClassishBody, _>(
'Replace consistency trait with validation trait',
$node ==> self::replaceTrait($node),
),
new NodeTypeMigrationStep<ClassishBody, _>(
'Remove `children` declarations',
$node ==> self::removeChildrenDeclaration($node),
),
];
}
}