Skip to content

Commit

Permalink
[eslint] use disallow license header rule (#26309)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Spencer authored Nov 28, 2018
1 parent aeb5a14 commit c122fa9
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 76 deletions.
71 changes: 43 additions & 28 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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'],

Expand Down Expand Up @@ -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],
},
],
},
Expand All @@ -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],
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-eslint-plugin-license-header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
6 changes: 0 additions & 6 deletions packages/kbn-test/src/functional_tests/lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
51 changes: 51 additions & 0 deletions src/dev/tslint/rules/disallowLicenseHeaderRule.js
Original file line number Diff line number Diff line change
@@ -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, '')
)
];
}
};
8 changes: 8 additions & 0 deletions tslint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
19 changes: 0 additions & 19 deletions x-pack/plugins/canvas/public/register_feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions x-pack/tslint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

0 comments on commit c122fa9

Please sign in to comment.