-
Notifications
You must be signed in to change notification settings - Fork 0
/
LicenseHeaderLinter.hack
152 lines (133 loc) · 4.02 KB
/
LicenseHeaderLinter.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
/*
* 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 Facebook\TypeAssert;
use namespace HH\Lib\{C, Str, Vec};
final class LicenseHeaderLinter extends AutoFixingASTLinter {
const type TConfig = shape();
const type TNode = Script;
public static ?string $forcedHeader = null;
const type TContext = Script;
<<__Override>>
public function getLintErrorForNode(
Script $_context,
Script $script,
): ?ASTLintError {
$decls = $script->getDeclarations()->getChildren();
$first = C\first($decls);
if ($first is MarkupSection) {
// <?hh or <?php ; not present in .hack files
$first = $decls[1] ?? null;
}
if ($first === null) {
return null;
}
if ($first is EndOfFile) {
return null;
}
$leading = C\first(
$first->getFirstToken()?->getLeading()?->toVec() ?? vec[],
);
if ($leading is DelimitedComment) {
if (
$leading->getText() ===
self::getLicenseHeaderForPath(\dirname($this->getFile()->getPath()))
) {
return null;
}
}
return new ASTLintError(
$this,
'Incorrect or missing license header',
$script,
() ==> $this->getFixedNode($script),
);
}
<<__Override>>
public function getPrettyTextForNode(Script $node): string {
return $node->getDeclarations()->getChildren()
|> Vec\take($$, 2)
|> NodeList::createMaybeEmptyList($$)
|> $$->getCode();
}
<<__Override>>
protected function getTitleForFix(ASTLintError $e): string {
if (Str\contains_ci($e->getBlameCode(), 'copyright')) {
return 'Replace license header';
}
return 'Add license header';
}
public function getFixedNode(Script $node): Script {
$children = $node->getDeclarations()->getChildren();
$first = (($children[0] is MarkupSection) ? $children[1] : $children[0])
->getFirstTokenx();
$leading = $first->getLeading()->toVec();
$key = C\find_key(
$leading,
$item ==> ($item is 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()->getPath())),
),
)];
if (!($next is EndOfLine && $next_next is EndOfLine)) {
$new[] = new EndOfLine("\n");
}
return $node->replace($existing, NodeList::createMaybeEmptyList($new));
}
$leading = Vec\concat(
vec[
new DelimitedComment(
TypeAssert\not_null(
self::getLicenseHeaderForPath(
\dirname($this->getFile()->getPath()),
),
),
),
new EndOfLine("\n"),
new EndOfLine("\n"),
],
$leading,
);
return $node->replace(
$first,
$first->withLeading(NodeList::createMaybeEmptyList($leading)),
);
}
<<__Override>>
public static function shouldLintFile(File $file): bool {
return self::getLicenseHeaderForPath($file->getPath()) !== null;
}
public static function __setExpectedHeaderForTesting(string $header): void {
self::$forcedHeader = $header;
}
<<__Memoize>>
private static function getLicenseHeaderForPath(string $path): ?string {
if (self::$forcedHeader !== null) {
return Str\trim(self::$forcedHeader);
}
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;
}
}