2018-05-29 05:06:30 +02:00
|
|
|
/*
|
2021-01-20 02:52:56 +01:00
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
|
|
* and the Server Side Public License, v 1; you may not use this file except in
|
|
|
|
* compliance with, at your election, the Elastic License or the Server Side
|
|
|
|
* Public License, v 1.
|
2018-05-29 05:06:30 +02:00
|
|
|
*/
|
|
|
|
|
2018-04-20 21:13:37 +02:00
|
|
|
const babelEslint = require('babel-eslint');
|
|
|
|
|
|
|
|
const { assert, normalizeWhitespace, init } = require('../lib');
|
|
|
|
|
2018-05-29 05:06:30 +02:00
|
|
|
function isHashbang(text) {
|
|
|
|
return text.trim().startsWith('#!') && !text.trim().includes('\n');
|
|
|
|
}
|
|
|
|
|
2018-04-20 21:13:37 +02:00
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
fixable: 'code',
|
2019-03-23 01:12:14 +01:00
|
|
|
schema: [
|
|
|
|
{
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
license: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
2018-04-20 21:13:37 +02:00
|
|
|
},
|
2019-03-23 01:12:14 +01:00
|
|
|
additionalProperties: false,
|
2018-04-20 21:13:37 +02:00
|
|
|
},
|
2019-03-23 01:12:14 +01:00
|
|
|
],
|
2018-04-20 21:13:37 +02:00
|
|
|
},
|
2020-05-22 09:08:58 +02:00
|
|
|
create: (context) => {
|
2018-04-20 21:13:37 +02:00
|
|
|
return {
|
|
|
|
Program(program) {
|
2020-05-22 09:08:58 +02:00
|
|
|
const license = init(context, program, function () {
|
2018-04-20 21:13:37 +02:00
|
|
|
const options = context.options[0] || {};
|
|
|
|
const license = options.license;
|
|
|
|
|
|
|
|
assert(!!license, '"license" option is required');
|
|
|
|
|
|
|
|
const parsed = babelEslint.parse(license);
|
|
|
|
assert(!parsed.body.length, '"license" option must only include a single comment');
|
2019-03-23 01:12:14 +01:00
|
|
|
assert(
|
|
|
|
parsed.comments.length === 1,
|
|
|
|
'"license" option must only include a single comment'
|
|
|
|
);
|
2018-04-20 21:13:37 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
source: license,
|
2019-03-23 01:12:14 +01:00
|
|
|
nodeValue: normalizeWhitespace(parsed.comments[0].value),
|
2018-04-20 21:13:37 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!license) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sourceCode = context.getSourceCode();
|
2019-03-23 01:12:14 +01:00
|
|
|
const comment = sourceCode
|
|
|
|
.getAllComments()
|
2020-05-22 09:08:58 +02:00
|
|
|
.find((node) => normalizeWhitespace(node.value) === license.nodeValue);
|
2018-04-20 21:13:37 +02:00
|
|
|
|
|
|
|
// no licence comment
|
|
|
|
if (!comment) {
|
|
|
|
context.report({
|
|
|
|
message: 'File must start with a license header',
|
|
|
|
loc: {
|
|
|
|
start: { line: 1, column: 0 },
|
2019-03-23 01:12:14 +01:00
|
|
|
end: { line: 1, column: sourceCode.lines[0].length - 1 },
|
2018-04-20 21:13:37 +02:00
|
|
|
},
|
|
|
|
fix(fixer) {
|
2018-05-29 05:06:30 +02:00
|
|
|
if (isHashbang(sourceCode.lines[0])) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2018-04-20 21:13:37 +02:00
|
|
|
return fixer.replaceTextRange([0, 0], license.source + '\n\n');
|
2019-03-23 01:12:14 +01:00
|
|
|
},
|
2018-04-20 21:13:37 +02:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure there is nothing before the comment
|
2019-03-23 01:12:14 +01:00
|
|
|
const sourceBeforeNode = sourceCode
|
|
|
|
.getText()
|
|
|
|
.slice(0, sourceCode.getIndexFromLoc(comment.loc.start));
|
2018-05-29 05:06:30 +02:00
|
|
|
if (sourceBeforeNode.length && !isHashbang(sourceBeforeNode)) {
|
2018-04-20 21:13:37 +02:00
|
|
|
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'),
|
|
|
|
];
|
2019-03-23 01:12:14 +01:00
|
|
|
},
|
2018-04-20 21:13:37 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2019-03-23 01:12:14 +01:00
|
|
|
},
|
2018-04-20 21:13:37 +02:00
|
|
|
};
|