-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlint-license-rule.mjs
More file actions
129 lines (115 loc) · 3.48 KB
/
lint-license-rule.mjs
File metadata and controls
129 lines (115 loc) · 3.48 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
import babelEslint from '@babel/eslint-parser';
const licenseHeader = `/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/`;
export const requireLicenseHeader = {
meta: {
fixable: 'code',
schema: [
{
type: 'object',
properties: {
license: {
type: 'string',
},
},
additionalProperties: false,
},
],
},
create: (context) => {
return {
Program(program) {
const license = init(context, program, function () {
const parsed = babelEslint.parse(licenseHeader, { requireConfigFile: false });
assert(!parsed.body.length, '"license" option must only include a single comment');
assert(
parsed.comments.length === 1,
'"license" option must only include a single comment'
);
return {
source: licenseHeader,
nodeValue: normalizeWhitespace(parsed.comments[0].value),
};
});
if (!license) {
return;
}
const sourceCode = context.sourceCode;
const comment = sourceCode
.getAllComments()
.find((node) => normalizeWhitespace(node.value) === license.nodeValue);
// no license comment
if (!comment) {
context.report({
message: 'File must start with a license header',
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: sourceCode.lines[0].length - 1 },
},
fix(fixer) {
if (isHashbang(sourceCode.lines[0])) {
return undefined;
}
return fixer.replaceTextRange([0, 0], licenseHeader + '\n\n');
},
});
return;
}
// ensure there is nothing before the comment
const sourceBeforeNode = sourceCode
.getText()
.slice(0, sourceCode.getIndexFromLoc(comment.loc.start));
if (sourceBeforeNode.length && !isHashbang(sourceBeforeNode)) {
context.report({
node: comment,
message: 'License header must be at the very beginning of the file',
fix(fixer) {
// replace leading whitespace if possible
if (sourceBeforeNode.trim() === '') {
return fixer.replaceTextRange([0, sourceBeforeNode.length], '');
}
// inject content at top and remove node from current location
// if removing whitespace is not possible
return [
fixer.remove(comment),
fixer.replaceTextRange([0, 0], license.source + '\n\n'),
];
},
});
}
},
};
},
};
function isHashbang(text) {
return text.trim().startsWith('#!') && !text.trim().includes('\n');
}
function assert(truth, message) {
if (truth) {
return;
}
const error = new Error(message);
error.failedAssertion = true;
throw error;
}
function normalizeWhitespace(string) {
return string.replace(/\s+/g, ' ');
}
function init(context, program, initStep) {
try {
return initStep();
} catch (error) {
if (error.failedAssertion) {
context.report({
node: program,
message: error.message,
});
} else {
throw error;
}
}
}