kibana/packages/kbn-eslint-plugin-eslint/rules/disallow_license_headers.js
Patryk Kopyciński 9ef198ad0d
Bump eslint@7 (#94347) (#114256)
# Conflicts:
#	packages/elastic-eslint-config-kibana/javascript.js
#	packages/kbn-eslint-plugin-eslint/rules/disallow_license_headers.test.js
#	packages/kbn-eslint-plugin-eslint/rules/no_restricted_paths.test.js
#	packages/kbn-eslint-plugin-eslint/rules/require_license_header.test.js
#	src/dev/eslint/lint_files.ts
#	x-pack/plugins/security_solution/public/common/components/markdown_editor/plugins/timeline/processor.tsx
#	yarn.lock

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2021-10-07 13:35:23 -04:00

75 lines
2.1 KiB
JavaScript

/*
* 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);
},
});
});
},
};
},
};