forked from hhvm/hhast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LicenseHeaderLinter.php
155 lines (138 loc) · 3.94 KB
/
LicenseHeaderLinter.php
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
<?hh // strict
/*
* 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\Linters;
use type Facebook\HHAST\{
DelimitedComment,
EditableList,
EditableNode,
EndOfFile,
EndOfLine,
Script,
};
use namespace Facebook\TypeAssert;
use namespace HH\Lib\{C, Str, Vec};
final class LicenseHeaderLinter extends AutoFixingASTLinter<Script> {
<<__Override>>
protected static function getTargetType(): classname<Script> {
return Script::class;
}
<<__Override>>
public function getLintErrorForNode(
Script $script,
vec<EditableNode> $_parents,
): ?FixableASTLintError<Script> {
$first = $script->getDeclarations()->getItems()[1] ?? null;
if ($first === null) {
return null;
}
if ($first instanceof EndOfFile) {
return null;
}
$leading = $first->getFirstToken()?->getLeading();
if ($leading instanceof EditableList) {
$leading = $leading->getItems()[0];
}
if ($leading instanceof DelimitedComment) {
if (
$leading->getText() ===
self::getLicenseHeaderForPath(\dirname($this->getFile()))
) {
return null;
}
}
return new FixableASTLintError(
$this,
'Incorrect or missing license header',
$script,
);
}
<<__Override>>
public function getPrettyTextForNode(
Script $node,
?EditableNode $_context,
): string {
return $node->getDeclarations()->getItems()
|> Vec\take($$, 2)
|> EditableList::fromItems($$)
|> $$->getCode();
}
<<__Override>>
protected function getTitleForFix(FixableASTLintError<Script> $e): string {
if (Str\contains_ci($e->getBlameCode(), 'copyright')) {
return 'Replace license header';
}
return 'Add license header';
}
<<__Override>>
public function getFixedNode(Script $node): Script {
$first = $node->getDeclarations()->getItems()[1]->getFirstTokenx();
$leading = $first->getLeading();
if ($leading instanceof EditableList) {
$leading = $leading->getItems();
} else if ($leading === null) {
$leading = vec[];
} else {
$leading = vec[$leading];
}
$key = C\find_key(
$leading,
$item ==> ($item instanceof DelimitedComment) &&
Str\contains_ci($item->getText(), 'copyright'),
);
if ($key !== null) {
$existing = $leading[$key];
$next = $leading[$key + 1] ?? null;
$next_next = $leading[$key + 2] ?? null;
$new = vec[new DelimitedComment(
TypeAssert\not_null(
self::getLicenseHeaderForPath(\dirname($this->getFile())),
),
)];
if (!($next instanceof EndOfLine && $next_next instanceof EndOfLine)) {
$new[] = new EndOfLine("\n");
}
return $node->replace($existing, EditableList::fromItems($new));
}
$leading = Vec\concat(
vec[
new DelimitedComment(
TypeAssert\not_null(
self::getLicenseHeaderForPath(\dirname($this->getFile())),
),
),
new EndOfLine("\n"),
new EndOfLine("\n"),
],
$leading,
);
return $node->replace(
$first,
$first->withLeading(EditableList::fromItems($leading)),
);
}
<<__Override>>
public static function shouldLintFile(string $path): bool {
return self::getLicenseHeaderForPath($path) !== null;
}
<<__Memoize>>
private static function getLicenseHeaderForPath(string $path): ?string {
if (\file_exists($path.'/.LICENSE_HEADER.hh.txt')) {
$header = \file_get_contents($path.'/.LICENSE_HEADER.hh.txt');
return ($header === '' || $header === "\n") ? null : Str\trim($header);
}
$path = \dirname(\realpath($path));
if (
Str\starts_with($path, \realpath(\Facebook\AutoloadMap\Generated\root()))
) {
return self::getLicenseHeaderForPath($path);
}
return null;
}
}