-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathcheck-license-header.ts
222 lines (202 loc) · 7.3 KB
/
check-license-header.ts
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import type {TSESTree} from '@typescript-eslint/utils';
import * as path from 'path';
import {createRule} from './utils/ruleCreator.ts';
const FRONT_END_FOLDER = path.join(
// If we want to fix this we need to change to .mts or use a package.json
// with type : module
// @ts-expect-error we don't build the file to CJS
import.meta.filename,
'..',
'..',
'..',
'..',
'front_end',
);
const CURRENT_YEAR = `${new Date().getFullYear()}`;
const LINE_LICENSE_HEADER = [
`Copyright ${CURRENT_YEAR} The Chromium Authors. All rights reserved.`,
'Use of this source code is governed by a BSD-style license that can be',
'found in the LICENSE file.',
];
const BLOCK_LICENSE_HEADER = [
'Copyright \\(C\\) \\d{4} Google Inc. All rights reserved.',
'',
'Redistribution and use in source and binary forms, with or without',
'modification, are permitted provided that the following conditions are',
'met:',
'',
' \\* Redistributions of source code must retain the above copyright',
'notice, this list of conditions and the following disclaimer.',
' \\* Redistributions in binary form must reproduce the above',
'copyright notice, this list of conditions and the following disclaimer',
'in the documentation and/or other materials provided with the',
'distribution.',
' \\* Neither the name of Google Inc. nor the names of its',
'contributors may be used to endorse or promote products derived from',
'this software without specific prior written permission.',
'',
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS',
'"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT',
'LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR',
'A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT',
'OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,',
'SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \\(INCLUDING, BUT NOT',
'LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,',
'DATA, OR PROFITS; OR BUSINESS INTERRUPTION\\) HOWEVER CAUSED AND ON ANY',
'THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT',
'\\(INCLUDING NEGLIGENCE OR OTHERWISE\\) ARISING IN ANY WAY OUT OF THE USE',
'OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.',
];
const LINE_REGEXES = LINE_LICENSE_HEADER.map(
line => new RegExp('[ ]?' + line.replace(CURRENT_YEAR, '(\\(c\\) )?\\d{4}')),
);
const BLOCK_REGEX = new RegExp(
'[\\s\\\\n\\*]*' + BLOCK_LICENSE_HEADER.join('[\\s\\\\n\\*]*'),
'm',
);
const LICENSE_HEADER_ADDITION = LINE_LICENSE_HEADER.map(line => `// ${line}`).join('\n') + '\n\n';
const EXCLUDED_FILES = [
// FIXME: Diff bundles must be moved to third_party
'diff/diff_match_patch.js',
];
const OTHER_LICENSE_HEADERS = [
// Apple
'common/Color.js',
'common/Object.js',
'common/ResourceType.js',
'data_grid/DataGrid.js',
'dom_extension/DOMExtension.js',
'elements/MetricsSidebarPane.js',
'sdk/Resource.js',
'sdk/Script.js',
'sources/CallStackSidebarPane.js',
'ui/Panel.js',
'ui/Treeoutline.js',
// Research In Motion Limited
'network/ResourceWebSocketFrameView.js',
// IBM Corp
'sources/WatchExpressionsSidebarPane.js',
// Multiple authors
'elements/ComputedStyleWidget.js',
'elements/ElementsPanel.js',
'elements/ElementsTreeElement.js',
'elements/ElementsTreeOutline.js',
'elements/EventListenersWidget.js',
'elements/PropertiesWidget.js',
'elements/StylesSidebarPane.js',
'main/MainImpl.ts',
'platform/UIString.ts',
'sdk/DOMModel.js',
'sources/ScopeChainSidebarPane.js',
'sources/SourcesPanel.js',
'theme_support/ThemeSupport.js',
'timeline/TimelinePanel.js',
'timeline/TimelineUIUtils.js',
'ui/KeyboardShortcut.js',
'ui/SearchableView.js',
'ui/TextPrompt.js',
'ui/UIUtils.js',
'ui/Widget.js',
];
/**
* Check each line comment that should (combined) result in the LINE_LICENSE_HEADER.
*/
function isMissingLineCommentLicense(comments: TSESTree.Comment[]) {
for (let i = 0; i < LINE_REGEXES.length; i++) {
if (!comments[i] || !LINE_REGEXES[i].test(comments[i].value)) {
return true;
}
}
return false;
}
/**
* We match the whole block comment, including potential leading asterisks of the jsdoc.
*/
function isMissingBlockLineCommentLicense(licenseText: string) {
return !BLOCK_REGEX.test(licenseText);
}
export default createRule({
name: 'check-license-header',
meta: {
type: 'problem',
docs: {
description: 'check license headers',
category: 'Possible Errors',
},
fixable: 'code',
schema: [], // no options
messages: {
missingLicense: 'Missing license header',
incorrectLineLicense: 'Incorrect line license header',
incorrectBlockLicense: 'Incorrect block license header',
},
},
defaultOptions: [],
create: function(context) {
const sourceCode = context.sourceCode ?? context.getSourceCode();
const filename = context.filename ?? context.getFilename();
const fileName = filename;
// Fix windows paths for exemptions
const relativePath = path.relative(FRONT_END_FOLDER, fileName).replace(/\\/g, '/');
if (relativePath.startsWith('third_party') || fileName.endsWith('TestRunner.js') ||
EXCLUDED_FILES.includes(relativePath) || OTHER_LICENSE_HEADERS.includes(relativePath)) {
return {};
}
return {
Program(node) {
if (node.body.length === 0) {
return;
}
const comments = sourceCode.getCommentsBefore(node.body[0]);
if (!comments || comments.length === 0 ||
// @ts-expect-error Shebang is not on the types
(comments.length === 1 && comments[0].type === 'Shebang')) {
context.report({
node,
messageId: 'missingLicense',
fix(fixer) {
return fixer.insertTextBefore(node, LICENSE_HEADER_ADDITION);
},
});
return;
}
// If a file has a Shebang comment, it has to be the very first line, so we need to check the license exists _after_ that comment;
let commentsToCheck = comments;
let firstCommentToCheck = comments[0];
// @ts-expect-error Shebang is not on the types
if (comments[0].type === 'Shebang') {
commentsToCheck = comments.slice(1);
firstCommentToCheck = commentsToCheck[0];
}
if (firstCommentToCheck.type === 'Line') {
if (isMissingLineCommentLicense(commentsToCheck)) {
context.report({
node,
messageId: 'incorrectLineLicense',
fix(fixer) {
return fixer.insertTextBefore(
firstCommentToCheck,
LICENSE_HEADER_ADDITION,
);
},
});
}
} else if (isMissingBlockLineCommentLicense(firstCommentToCheck.value)) {
context.report({
node,
messageId: 'incorrectBlockLicense',
fix(fixer) {
return fixer.insertTextBefore(
firstCommentToCheck,
LICENSE_HEADER_ADDITION,
);
},
});
}
},
};
},
});