forked from hhvm/hhast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoStringInterpolationLinter.hack
151 lines (135 loc) · 3.95 KB
/
NoStringInterpolationLinter.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
/*
* 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 NoStringInterpolationLinter extends AutoFixingASTLinter {
const type TNode = LiteralExpression;
const type TContext = Script;
<<__Override>>
public function getLintErrorForNode(
Script $_context,
LiteralExpression $root_expr,
): ?ASTLintError {
$expr = $root_expr->getExpression();
if (!$expr is NodeList<_>) {
return null;
}
return new ASTLintError(
$this,
'Do not use string interpolation - consider concatenation or '.
'Str\format() instead ',
$root_expr,
() ==> $this->getFixedNode($root_expr),
);
}
<<__Override>>
protected function getTitleForFix(LintError $_): string {
return 'Replace interpolation with concatenation';
}
public function getFixedNode(LiteralExpression $root_expr): ?IExpression {
$expr = $root_expr->getExpression();
invariant($expr is NodeList<_>, 'Expected list, got %s', \get_class($expr));
$children = vec($expr->getChildren());
$child_count = C\count($children);
$new_children = vec[];
for ($i = 0; $i < $child_count; ++$i) {
$child = $children[$i];
if ($child is HeredocStringLiteralHeadToken) {
return null;
}
if ($child is DoubleQuotedStringLiteralHeadToken) {
if ($child->getText() === '"') {
continue;
}
$new_children[] = new DoubleQuotedStringLiteralToken(
$child->getLeading(),
$child->getTrailing(),
$child->getText().'"',
);
continue;
}
if ($child is DoubleQuotedStringLiteralTailToken) {
if ($child->getText() === '"') {
break;
}
$new_children[] = new DoubleQuotedStringLiteralToken(
$child->getLeading(),
$child->getTrailing(),
'"'.$child->getText(),
);
continue;
}
if ($child is StringLiteralBodyToken) {
$new_children[] = new DoubleQuotedStringLiteralToken(
null,
null,
'"'.$child->getText().'"',
);
continue;
}
if ($child is DollarToken) {
/* "${foo}"
*
* (dollar)
* (embedded_braced_expression
* left_brace: Token
* expression: NameToken
* right_brace: Token
* )
*/
invariant(
$i + 1 < $child_count,
"Shouldn't have a dollar token unless there's more tokens after it",
);
$next = $children[$i + 1];
++$i;
invariant(
$next is EmbeddedBracedExpression,
'Dollar token in string should be followed by embedded brace '.
'expression.',
);
$inner = $next->getExpression();
invariant($inner is NameToken, '"${}" should contain a variable name');
$new_children[] = new VariableToken(null, null, '$'.$inner->getText());
continue;
}
if ($child is EmbeddedBracedExpression) {
$new_children[] = $child->getExpression();
continue;
}
$new_children[] = $child;
}
$children = Vec\map(
$new_children,
$child ==> {
if ($child is IExpression) {
return $child;
}
if ($child is DoubleQuotedStringLiteralToken) {
return new LiteralExpression($child);
}
invariant(
$child is VariableToken,
'expected double quoted string or variable, got %s',
\get_class($child),
);
return new VariableExpression($child);
},
);
$expr = C\firstx($children);
for ($i = 1; $i < C\count($children); ++$i) {
$expr = new BinaryExpression(
$expr,
new DotToken(null, null),
$children[$i],
);
}
return $expr;
}
}