forked from hhvm/hhast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDollarBraceEmbeddedVariableMigration.hack
More file actions
76 lines (72 loc) · 2.17 KB
/
Copy pathDollarBraceEmbeddedVariableMigration.hack
File metadata and controls
76 lines (72 loc) · 2.17 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
/*
* 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;
final class DollarBraceEmbeddedVariableMigration extends StepBasedMigration {
private static function migrateLiteralExpression(
LiteralExpression $node,
): LiteralExpression {
$children = $node->getChildren();
$child = C\first($children);
if (!($child is NodeList<_>)) {
return $node;
}
$literal_parts = $child->getChildren();
$first_part = C\first($literal_parts);
if (
!(
$first_part is DoubleQuotedStringLiteralHeadToken ||
$first_part is HeredocStringLiteralHeadToken
)
) {
return $node;
}
$new_literal_parts = vec[];
$made_change = false;
for ($i = 0; $i < C\count($literal_parts); $i++) {
$current_part = $literal_parts[$i];
if ($current_part is DollarToken) {
$next_part = idx($literal_parts, $i + 1);
if ($next_part is EmbeddedBracedExpression) {
$braced_expression_inner = $next_part->getExpression();
if ($braced_expression_inner is NameToken) {
$new_literal_parts[] = $next_part->replace(
$braced_expression_inner,
new VariableExpression(
new VariableToken(
$braced_expression_inner->getLeading(),
$braced_expression_inner->getTrailing(),
'$'.$braced_expression_inner->getCode(),
),
),
);
$i++;
$made_change = true;
continue;
}
}
}
$new_literal_parts[] = $current_part;
}
if ($made_change) {
return $node->replace($child, new NodeList($new_literal_parts));
} else {
return $node;
}
}
<<__Override>>
public function getSteps(): Traversable<IMigrationStep> {
return vec[
new NodeTypeMigrationStep<LiteralExpression, _>(
'convert "${foo}" to "{$foo}"',
$node ==> self::migrateLiteralExpression($node),
),
];
}
}