From e7a19f3a10c3ea98844abd98ec402c8b02b4db9b Mon Sep 17 00:00:00 2001 From: Lais Portugal Date: Wed, 14 Aug 2024 14:37:48 +0100 Subject: [PATCH 1/6] Edit license header --- rules/license-header.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rules/license-header.js b/rules/license-header.js index bb9c559..45ab47b 100644 --- a/rules/license-header.js +++ b/rules/license-header.js @@ -3,9 +3,14 @@ const ALLOWED_PREFIX_LINES = ["/** @jest-environment jsdom */"]; const LICENSE_HEADER = ` -// 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/ +/* 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(); module.exports = { From 587d1c9f7949e9fe0e45b250adedceb0f7a94ec4 Mon Sep 17 00:00:00 2001 From: Lais Portugal Date: Mon, 19 Aug 2024 14:49:05 +0100 Subject: [PATCH 2/6] change header file --- rules/license-header.js | 43 +++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/rules/license-header.js b/rules/license-header.js index 45ab47b..525773d 100644 --- a/rules/license-header.js +++ b/rules/license-header.js @@ -1,8 +1,5 @@ -// 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 ALLOWED_PREFIX_LINES = ["/** @jest-environment jsdom */"]; -const LICENSE_HEADER = ` +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 @@ -13,6 +10,16 @@ const LICENSE_HEADER = ` */ `.trim(); +const MODIFIED_FILE_LICENSE_HEADER = ` +/* SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) +* 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(); + module.exports = { meta: { type: "suggestion", @@ -24,18 +31,30 @@ module.exports = { return { Program: () => { const source = context.getSourceCode().getText(); - const headerIndex = source.indexOf(LICENSE_HEADER); - const prefixLines = source.substring(0, headerIndex).trim().split("\n"); + + // Check if either header is present + const isNewFileHeaderPresent = source.indexOf(NEW_FILE_LICENSE_HEADER) !== -1; + const isModifiedFileHeaderPresent = source.indexOf(MODIFIED_FILE_LICENSE_HEADER) !== -1; + + const prefixLines = source.substring(0, Math.max(0, source.indexOf('\n'))).trim().split("\n"); const prefixLinesAreValid = prefixLines.every( (line) => line === "" || ALLOWED_PREFIX_LINES.includes(line) ); - if (headerIndex === -1 || !prefixLinesAreValid) { + + // If neither header is present, treat it as a new file and add the full header + 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"), + }); + } + // If a header is missing or invalid, treat it as a modified file and add the modified header + else if (!prefixLinesAreValid || (isModifiedFileHeaderPresent && headerIndex === -1)) { context.report({ - message: "Missing license header", - loc: { start: 0, end: +source.indexOf("\n") + 1 }, - fix: () => { - return { range: [0, 0], text: LICENSE_HEADER + "\n\n" }; - }, + 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"), }); } }, From 05bf3615e4c651720dcdce0cd8fadb0b0e3b29ca Mon Sep 17 00:00:00 2001 From: Lais Portugal Date: Mon, 19 Aug 2024 14:50:58 +0100 Subject: [PATCH 3/6] Removing redundant comments --- rules/license-header.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/rules/license-header.js b/rules/license-header.js index 525773d..ffe613b 100644 --- a/rules/license-header.js +++ b/rules/license-header.js @@ -31,17 +31,13 @@ module.exports = { return { Program: () => { const source = context.getSourceCode().getText(); - - // Check if either header is present const isNewFileHeaderPresent = source.indexOf(NEW_FILE_LICENSE_HEADER) !== -1; const isModifiedFileHeaderPresent = source.indexOf(MODIFIED_FILE_LICENSE_HEADER) !== -1; - const prefixLines = source.substring(0, Math.max(0, source.indexOf('\n'))).trim().split("\n"); const prefixLinesAreValid = prefixLines.every( (line) => line === "" || ALLOWED_PREFIX_LINES.includes(line) ); - // If neither header is present, treat it as a new file and add the full header if (!isNewFileHeaderPresent && !isModifiedFileHeaderPresent) { context.report({ message: "Missing license header for a new file", @@ -49,7 +45,6 @@ module.exports = { fix: (fixer) => fixer.insertTextBeforeRange([0, 0], NEW_FILE_LICENSE_HEADER + "\n\n"), }); } - // If a header is missing or invalid, treat it as a modified file and add the modified header else if (!prefixLinesAreValid || (isModifiedFileHeaderPresent && headerIndex === -1)) { context.report({ message: "Missing or incorrect license header for a modified file", From 92586bcaf4f2b59630ed8f8947c894dfe77745a5 Mon Sep 17 00:00:00 2001 From: Lais Portugal Date: Mon, 19 Aug 2024 15:01:05 +0100 Subject: [PATCH 4/6] Lint fixes --- rules/license-header.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/rules/license-header.js b/rules/license-header.js index ffe613b..65f9da8 100644 --- a/rules/license-header.js +++ b/rules/license-header.js @@ -31,9 +31,15 @@ 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 prefixLines = source.substring(0, Math.max(0, source.indexOf('\n'))).trim().split("\n"); + 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 prefixLinesAreValid = prefixLines.every( (line) => line === "" || ALLOWED_PREFIX_LINES.includes(line) ); @@ -42,14 +48,24 @@ module.exports = { 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"), + fix: (fixer) => + fixer.insertTextBeforeRange( + [0, 0], + NEW_FILE_LICENSE_HEADER + "\n\n" + ), }); - } - else if (!prefixLinesAreValid || (isModifiedFileHeaderPresent && headerIndex === -1)) { + } else if ( + !prefixLinesAreValid || + (isModifiedFileHeaderPresent && headerIndex === -1) + ) { 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"), + fix: (fixer) => + fixer.insertTextBeforeRange( + [0, 0], + MODIFIED_FILE_LICENSE_HEADER + "\n\n" + ), }); } }, From f5df9b75e15c5a73b41c99d5dc768e050be2f025 Mon Sep 17 00:00:00 2001 From: Lais Portugal Date: Mon, 19 Aug 2024 15:23:52 +0100 Subject: [PATCH 5/6] 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" }; + }, }); } }, From fac92065340168488cda772a00cd85b9ee718d7a Mon Sep 17 00:00:00 2001 From: Lais Portugal Date: Mon, 19 Aug 2024 15:27:32 +0100 Subject: [PATCH 6/6] Removing block comments --- rules/license-header.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/rules/license-header.js b/rules/license-header.js index eaa8537..40051f1 100644 --- a/rules/license-header.js +++ b/rules/license-header.js @@ -3,13 +3,8 @@ 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 -* -* 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/ -*/ +// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) +// SPDX-License-Identifier: MPL-2.0 `.trim(); module.exports = {