This repository was archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathMustUseOverrideAttributeLinter.hack
182 lines (162 loc) · 4.82 KB
/
MustUseOverrideAttributeLinter.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* 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 function Facebook\HHAST\resolve_type;
use namespace HH\Lib\{C, Str, Vec};
final class MustUseOverrideAttributeLinter extends AutoFixingASTLinter {
const type TConfig = shape();
const type TNode = MethodishDeclaration;
<<__Override>>
public function getTitleForFix(SingleRuleLintError $_): string {
return 'Add __Override attribute';
}
const type TContext = ClassishDeclaration;
<<__Override>>
public function getLintErrorForNode(
ClassishDeclaration $class,
MethodishDeclaration $node,
): ?ASTLintError {
if ($this->canIgnoreMethod($class, $node)) {
return null;
}
$root = $this->getAST();
$super = self::findSuper($root, $class);
try {
$method = $node->getFunctionDeclHeader()->getName()->getCode()
|> Str\trim($$);
$reflection_method = new \ReflectionMethod($super, $method);
return new ASTLintError(
$this,
Str\format(
'%s::%s() overrides %s::%s() without <<__Override>>',
$class->getNamex()->getCode()
|> Str\trim($$)
|> resolve_type($$, $root, $node)['name'],
$method,
$reflection_method->getDeclaringClass()->getName(),
$method,
),
$node,
() ==> $this->getFixedNode($node),
);
} catch (\ReflectionException $e) {
$method = $node->getFunctionDeclHeader()->getName()->getCode()
|> Str\trim($$);
return null;
}
}
private static function findSuper(
Script $root,
ClassishDeclaration $class,
): string {
$super = C\onlyx($class->getExtendsListx()->getChildren());
$super = $super->getItemx();
if ($super is GenericTypeSpecifier) {
$super = $super->getClassType();
}
return $super->getCode()
|> Str\trim($$)
|> resolve_type($$, $root, $class)['name'];
}
private function canIgnoreMethod(
ClassishDeclaration $class,
MethodishDeclaration $method,
): bool {
if (!$class->hasExtendsKeyword()) {
return true;
}
$name = $method->getFunctionDeclHeader()->getName();
if ($name is ConstructToken) {
return true;
}
$private = $method->getFunctionDeclHeader()
->getModifiersx()
->getFirstDescendantByType<PrivateToken>();
if ($private !== null) {
return true;
}
if (!$class->getKeyword() is ClassToken) {
return true;
}
$attrs = $method->getAttribute();
if ($attrs === null) {
return false;
}
$attrs = $attrs->getAttributes()->getChildrenOfItems()
|> Vec\map($$, $attr ==> ($attr->getType() ?as NameToken)?->getText());
return C\contains($attrs, '__Override');
}
<<__Override>>
public function getPrettyTextForNode(MethodishDeclaration $node): string {
$body = $node->getFunctionBody();
if ($body === null) {
return $node->getCode();
}
return $node->withFunctionBody(
$body
->withStatements(null)
->withLeftBrace($body->getLeftBracex()->withTrailing(null)),
)
->getCode()
|> Str\trim_right($$, "} \n");
}
public function getFixedNode(
MethodishDeclaration $node,
): MethodishDeclaration {
$attrs = $node->getAttribute();
if ($attrs === null) {
$first_token = $node->getFirstTokenx();
return $node->withAttribute(
new OldAttributeSpecification(
new LessThanLessThanToken($first_token->getLeading(), null),
new ConstructorCall(
new NameToken(null, null, '__Override'),
null,
null,
null,
)
|> new NodeList(vec[new ListItem($$, null)]),
new GreaterThanGreaterThanToken(
null,
Str\contains(
C\lastx($first_token->getLeading()->getChildren())->getCode(),
"\n",
)
? null
: new NodeList(vec[new WhiteSpace("\n")]),
),
),
)
->replace(
$first_token,
$first_token->withLeading(
new NodeList(
vec[C\lastx($first_token->getLeading()->getChildren())],
),
),
);
}
$list = $attrs->getAttributes()->toVec();
$last_idx = C\count($list) - 1;
$last = $list[$last_idx];
if (!$last->hasSeparator()) {
$list[$last_idx] = $last->withSeparator(
new CommaToken(null, new NodeList(vec[new WhiteSpace(' ')])),
);
}
$list[] = new ConstructorCall(
new NameToken(null, null, '__Override'),
null,
null,
null,
)
|> new ListItem($$, null);
return $node->withAttribute($attrs->withAttributes(new NodeList($list)));
}
}