From f5df9b75e15c5a73b41c99d5dc768e050be2f025 Mon Sep 17 00:00:00 2001 From: Lais Portugal Date: Mon, 19 Aug 2024 15:23:52 +0100 Subject: [PATCH] Modify --- rules/license-header.js | 55 +++++++++-------------------------------- 1 file changed, 12 insertions(+), 43 deletions(-) diff --git a/rules/license-header.js b/rules/license-header.js index 65f9da8..eaa8537 100644 --- a/rules/license-header.js +++ b/rules/license-header.js @@ -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) -* 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) * SPDX-License-Identifier: MPL-2.0 * @@ -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" }; + }, }); } },