[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:
Spencer 2018-11-27 16:49:17 -08:00 committed by GitHub
parent aeb5a141ec
commit c122fa96a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 130 additions and 76 deletions

View file

@ -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],
},
],
},

View file

@ -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'),
},
};

View file

@ -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',

View file

@ -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);
}

View file

@ -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';

View file

@ -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 => {

View file

@ -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';

View 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, '')
)
];
}
};

View file

@ -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.
*/

View file

@ -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,

View file

@ -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.
*/