forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisallow_license_headers.js
75 lines (65 loc) · 2.08 KB
/
disallow_license_headers.js
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
const babelEslint = require('@babel/eslint-parser');
const { assert, normalizeWhitespace, init } = require('../lib');
module.exports = {
meta: {
fixable: 'code',
schema: [
{
type: 'object',
properties: {
licenses: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
],
},
create: (context) => {
return {
Program(program) {
const licenses = init(context, program, () => {
const options = context.options[0] || {};
const licenses = options.licenses;
assert(!!licenses, '"licenses" option is required');
return licenses.map((license, i) => {
const parsed = babelEslint.parse(license, { requireConfigFile: false });
assert(
!parsed.body.length,
`"licenses[${i}]" option must only include a single comment`
);
assert(
parsed.comments.length === 1,
`"licenses[${i}]" option must only include a single comment`
);
return normalizeWhitespace(parsed.comments[0].value);
});
});
if (!licenses || !licenses.length) return;
const sourceCode = context.getSourceCode();
sourceCode
.getAllComments()
.filter((node) => licenses.includes(normalizeWhitespace(node.value)))
.forEach((node) => {
context.report({
node,
message: 'This license header is not allowed in this file.',
fix(fixer) {
return fixer.remove(node);
},
});
});
},
};
},
};