Skip to content

Commit

Permalink
Modify
Browse files Browse the repository at this point in the history
  • Loading branch information
laisspportugal committed Aug 19, 2024
1 parent 92586bc commit f5df9b7
Showing 1 changed file with 12 additions and 43 deletions.
55 changes: 12 additions & 43 deletions rules/license-header.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
const ALLOWED_PREFIX_LINES = ["/** @jest-environment jsdom */"];
const NEW_FILE_LICENSE_HEADER = `
/* SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
* SPDX-FileCopyrightText: Copyright Foxglove Technologies Inc.
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/
*/
`.trim();
// A custom eslint rule checking for the existence of an MPL license header,
// while allowing certain prefixes that cannot be moved below the license header.

const MODIFIED_FILE_LICENSE_HEADER = `
const ALLOWED_PREFIX_LINES = ["/** @jest-environment jsdom */"];
const LICENSE_HEADER = `
/* SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
* SPDX-License-Identifier: MPL-2.0
*
Expand All @@ -31,41 +23,18 @@ module.exports = {
return {
Program: () => {
const source = context.getSourceCode().getText();
const isNewFileHeaderPresent =
source.indexOf(NEW_FILE_LICENSE_HEADER) !== -1;
const isModifiedFileHeaderPresent =
source.indexOf(MODIFIED_FILE_LICENSE_HEADER) !== -1;
const headerIndex = source.indexOf(MODIFIED_FILE_LICENSE_HEADER);
const prefixLines = source
.substring(0, Math.max(0, source.indexOf("\n")))
.trim()
.split("\n");
const headerIndex = source.indexOf(LICENSE_HEADER);
const prefixLines = source.substring(0, headerIndex).trim().split("\n");
const prefixLinesAreValid = prefixLines.every(
(line) => line === "" || ALLOWED_PREFIX_LINES.includes(line)
);

if (!isNewFileHeaderPresent && !isModifiedFileHeaderPresent) {
context.report({
message: "Missing license header for a new file",
loc: { start: 0, end: 0 },
fix: (fixer) =>
fixer.insertTextBeforeRange(
[0, 0],
NEW_FILE_LICENSE_HEADER + "\n\n"
),
});
} else if (
!prefixLinesAreValid ||
(isModifiedFileHeaderPresent && headerIndex === -1)
) {
if (headerIndex === -1 || !prefixLinesAreValid) {
context.report({
message: "Missing or incorrect license header for a modified file",
loc: { start: 0, end: 0 },
fix: (fixer) =>
fixer.insertTextBeforeRange(
[0, 0],
MODIFIED_FILE_LICENSE_HEADER + "\n\n"
),
message: "Missing license header",
loc: { start: 0, end: +source.indexOf("\n") + 1 },
fix: () => {
return { range: [0, 0], text: LICENSE_HEADER + "\n\n" };
},
});
}
},
Expand Down

0 comments on commit f5df9b7

Please sign in to comment.