forked from hhvm/hhast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXHPClassModifierMigration.hack
More file actions
56 lines (48 loc) · 1.37 KB
/
XHPClassModifierMigration.hack
File metadata and controls
56 lines (48 loc) · 1.37 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
/*
* 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\Str;
final class XHPClassModifierMigration extends StepBasedMigration {
private static function replaceXHPClassWithModifier(
ClassishDeclaration $in,
): ClassishDeclaration {
if ($in->hasXhp()) {
return $in;
}
$name = $in->getName();
if (!$name is XHPClassNameToken) {
return $in;
}
$kw = $in->getKeyword();
$class = Str\strip_prefix($name->getText(), ':')
|> Str\replace($$, '-', '_');
return $in->withXhp(
new XHPToken($kw->getLeading(), new NodeList(vec[new WhiteSpace(' ')])),
)
->withKeyword($kw->withLeading(null))
->withName(
Str\contains($class, ':')
? new XHPClassNameToken(
$name->getLeading(),
$name->getTrailing(),
$class,
)
: $name->withText($class),
);
}
<<__Override>>
public function getSteps(): Traversable<IMigrationStep> {
return vec[
new NodeTypeMigrationStep<ClassishDeclaration, _>(
'Replace `class :foo:bar` with `xhp class foo:bar`',
$node ==> self::replaceXHPClassWithModifier($node),
),
];
}
}