mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
[eslint] use disallow license header rule (#26309)
Fixes #26295 There are several places where we have accidentally added new license headers with linters but failed to remove old license headers manually. This prevents that by applying the an inverted version of the license headers rule that removed invalid license headers when files are moved.
This commit is contained in:
parent
aeb5a141ec
commit
c122fa96a0
11 changed files with 130 additions and 76 deletions
71
.eslintrc.js
71
.eslintrc.js
|
@ -1,9 +1,37 @@
|
|||
const { resolve } = require('path');
|
||||
const { readdirSync } = require('fs');
|
||||
const dedent = require('dedent');
|
||||
|
||||
const restrictedModules = { paths: ['gulp-util'] };
|
||||
|
||||
const APACHE_2_0_LICENSE_HEADER = `
|
||||
/*
|
||||
* Licensed to Elasticsearch B.V. under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch B.V. licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
`;
|
||||
|
||||
const ELASTIC_LICENSE_HEADER = `
|
||||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
`;
|
||||
|
||||
module.exports = {
|
||||
extends: ['@elastic/eslint-config-kibana', '@elastic/eslint-config-kibana/jest'],
|
||||
|
||||
|
@ -215,26 +243,13 @@ module.exports = {
|
|||
'@kbn/license-header/require-license-header': [
|
||||
'error',
|
||||
{
|
||||
license: dedent`
|
||||
/*
|
||||
* Licensed to Elasticsearch B.V. under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch B.V. licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
`,
|
||||
license: APACHE_2_0_LICENSE_HEADER,
|
||||
},
|
||||
],
|
||||
'@kbn/license-header/disallow-license-headers': [
|
||||
'error',
|
||||
{
|
||||
licenses: [ELASTIC_LICENSE_HEADER],
|
||||
},
|
||||
],
|
||||
},
|
||||
|
@ -260,13 +275,13 @@ module.exports = {
|
|||
'@kbn/license-header/require-license-header': [
|
||||
'error',
|
||||
{
|
||||
license: dedent`
|
||||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
`,
|
||||
license: ELASTIC_LICENSE_HEADER,
|
||||
},
|
||||
],
|
||||
'@kbn/license-header/disallow-license-headers': [
|
||||
'error',
|
||||
{
|
||||
licenses: [APACHE_2_0_LICENSE_HEADER],
|
||||
},
|
||||
],
|
||||
},
|
||||
|
|
|
@ -20,6 +20,6 @@
|
|||
module.exports = {
|
||||
rules: {
|
||||
'require-license-header': require('./rules/require_license_header'),
|
||||
'remove-outdated-license-header': require('./rules/remove_outdated_license_header'),
|
||||
'disallow-license-headers': require('./rules/disallow_license_headers'),
|
||||
},
|
||||
};
|
||||
|
|
|
@ -18,10 +18,10 @@
|
|||
*/
|
||||
|
||||
const { RuleTester } = require('eslint');
|
||||
const rule = require('../remove_outdated_license_header');
|
||||
const rule = require('../disallow_license_headers');
|
||||
const dedent = require('dedent');
|
||||
|
||||
const RULE_NAME = '@kbn/license-header/remove-outdated-license-header';
|
||||
const RULE_NAME = '@kbn/license-header/disallow-license-headers';
|
||||
|
||||
const ruleTester = new RuleTester({
|
||||
parser: 'babel-eslint',
|
|
@ -40,7 +40,7 @@ module.exports = {
|
|||
create: context => {
|
||||
return {
|
||||
Program(program) {
|
||||
const nodeValues = init(context, program, () => {
|
||||
const licenses = init(context, program, () => {
|
||||
const options = context.options[0] || {};
|
||||
const licenses = options.licenses;
|
||||
|
||||
|
@ -56,21 +56,17 @@ module.exports = {
|
|||
});
|
||||
});
|
||||
|
||||
if (!nodeValues) return;
|
||||
if (!licenses || !licenses.length) return;
|
||||
|
||||
const sourceCode = context.getSourceCode();
|
||||
|
||||
sourceCode
|
||||
.getAllComments()
|
||||
.filter(node => (
|
||||
nodeValues.find(nodeValue => (
|
||||
normalizeWhitespace(node.value) === nodeValue
|
||||
))
|
||||
))
|
||||
.filter(node => licenses.includes(normalizeWhitespace(node.value)))
|
||||
.forEach(node => {
|
||||
context.report({
|
||||
node,
|
||||
message: 'Remove outdated license header.',
|
||||
message: 'This license header is not allowed in this file.',
|
||||
fix(fixer) {
|
||||
return fixer.remove(node);
|
||||
}
|
|
@ -17,12 +17,6 @@
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { format as formatUrl } from 'url';
|
||||
|
||||
import request from 'request';
|
||||
|
|
|
@ -17,12 +17,6 @@
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { once } from 'lodash';
|
||||
|
||||
const callWithRequest = once(server => {
|
||||
|
|
|
@ -17,12 +17,6 @@
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { callWithRequestFactory } from './call_with_request_factory';
|
||||
import handleEsError from '../../../lib/handle_es_error';
|
||||
|
||||
|
|
51
src/dev/tslint/rules/disallowLicenseHeaderRule.js
Normal file
51
src/dev/tslint/rules/disallowLicenseHeaderRule.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch B.V. under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch B.V. licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
const Lint = require('tslint');
|
||||
|
||||
const FAILURE_STRING = 'This license header is not allowed in this file.';
|
||||
const RULE_NAME = 'disallow-license-header';
|
||||
|
||||
exports.Rule = class extends Lint.Rules.AbstractRule {
|
||||
apply(sourceFile) {
|
||||
const [headerText] = this.getOptions().ruleArguments;
|
||||
|
||||
if (!headerText) {
|
||||
throw new Error(`${RULE_NAME} requires a single argument containing the header text`);
|
||||
}
|
||||
|
||||
if (!sourceFile.text.includes(headerText)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const start = sourceFile.text.indexOf(headerText);
|
||||
const end = start + headerText.length;
|
||||
|
||||
return [
|
||||
new Lint.RuleFailure(
|
||||
sourceFile,
|
||||
start,
|
||||
end,
|
||||
FAILURE_STRING,
|
||||
RULE_NAME,
|
||||
new Lint.Replacement(start, headerText.length, '')
|
||||
)
|
||||
];
|
||||
}
|
||||
};
|
|
@ -32,3 +32,11 @@ rules:
|
|||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
disallow-license-header:
|
||||
- true
|
||||
- |-
|
||||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
|
|
@ -4,25 +4,6 @@
|
|||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Licensed to Elasticsearch B.V. under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch B.V. licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
FeatureCatalogueRegistryProvider,
|
||||
FeatureCatalogueCategory,
|
||||
|
|
|
@ -9,3 +9,24 @@ rules:
|
|||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
disallow-license-header:
|
||||
- true
|
||||
- |-
|
||||
/*
|
||||
* Licensed to Elasticsearch B.V. under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch B.V. licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue