forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcheck-license-header.js
More file actions
255 lines (230 loc) · 8.15 KB
/
Copy pathcheck-license-header.js
File metadata and controls
255 lines (230 loc) · 8.15 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// 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.
/**
* @fileoverview Rule to check license headers
* @author Tim van der Lippe
*/
'use strict';
const path = require('path');
const FRONT_END_FOLDER = path.join(
__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 META_LINE_LICENSE_HEADER = ['Copyright \\(c\\) Meta Platforms, Inc. and affiliates.', ...LINE_LICENSE_HEADER];
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 META_LINE_REGEXES = [new RegExp('[ ]?' + META_LINE_LICENSE_HEADER[0]), ...LINE_REGEXES];
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 META_LICENSE_HEADER_ADDITION = META_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 META_CODE_PATHS = [
'core/host/RNPerfMetrics.ts',
'core/rn_experiments',
'core/sdk/ReactNativeApplicationModel.ts',
'entrypoints/rn_fusebox',
'entrypoints/rn_inspector',
'entrypoints/shell/browser_compatibility_guard.ts',
'global_typings/react_native.d.ts',
'models/react_native',
'panels/react_devtools',
'panels/rn_welcome',
'panels/timeline/ReactNativeTimelineLandingPage.ts',
];
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',
];
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
/**
* Check each linecomment that should (combined) result in the LINE_LICENSE_HEADER.
*/
function isMissingLineCommentLicense(comments, licenseType) {
const regexes = licenseType === 'meta' ? META_LINE_REGEXES : LINE_REGEXES;
for (let i = 0; i < regexes.length; i++) {
if (!comments[i] || !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) {
return !BLOCK_REGEX.test(licenseText);
}
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'check license headers',
category: 'Possible Errors',
},
fixable: 'code',
schema: [], // no options
},
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, '/');
let licenseType = 'google';
if (META_CODE_PATHS.some(pathMatch => relativePath.startsWith(pathMatch))) {
licenseType = 'meta';
}
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,
message: 'Missing license header',
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, licenseType)) {
context.report({
node,
message: 'Incorrect line license header',
fix(fixer) {
return fixer.insertTextBefore(
firstCommentToCheck,
licenseType === 'meta' ? META_LICENSE_HEADER_ADDITION : LICENSE_HEADER_ADDITION,
);
},
});
}
} else if (
isMissingBlockLineCommentLicense(firstCommentToCheck.value)
) {
context.report({
node,
message: 'Incorrect block license header',
fix(fixer) {
return fixer.insertTextBefore(
firstCommentToCheck,
LICENSE_HEADER_ADDITION,
);
},
});
}
},
};
},
};