forked from hhvm/hhast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplicitPartialModeMigration.hack
More file actions
73 lines (65 loc) · 1.86 KB
/
Copy pathExplicitPartialModeMigration.hack
File metadata and controls
73 lines (65 loc) · 1.86 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
/*
* 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, Str, Vec};
final class ExplicitPartialModeMigration extends BaseMigration {
protected static function addPartialModeIfNoneSpecified(
MarkupSuffix $node,
): MarkupSuffix {
$name = $node->getName()->getText();
if ($name !== 'hh') {
return $node;
}
$trailing = $node->getLastTokenx()->getTrailing();
if (Str\starts_with($trailing->getCode(), ' // ')) {
return $node;
}
if ($trailing->isEmpty()) {
// just `<?hh` then EOF, no trailing newline
$t = $node->getLastTokenx();
return $node->replace(
$t,
$t->withTrailing(
new NodeList(vec[
new WhiteSpace(' '),
new SingleLineComment('// partial'),
]),
),
);
}
$children = $trailing->toVec();
$idx = C\find_key($children, $c ==> $c is EndOfLine);
if ($idx === null) {
return $node->replace($trailing, new NodeList(vec[
new WhiteSpace(' '),
new SingleLineComment('// partial'),
new EndOfLine("\n"),
]));
}
return $node->replace($trailing, new NodeList(Vec\concat(
vec[
new WhiteSpace(' '),
new SingleLineComment('// partial'),
],
Vec\drop($children, $idx),
)));
}
<<__Override>>
public function migrateFile(string $_path, Script $ast): Script {
$markup = C\first($ast->getDeclarationsx()->getChildren());
if (!$markup is MarkupSection) {
return $ast;
}
$suffix = $markup->getSuffix();
if ($suffix is null) {
return $ast;
}
return $ast->replace($suffix, self::addPartialModeIfNoneSpecified($suffix));
}
}