forked from foxglove/eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92586bc
commit f5df9b7
Showing
1 changed file
with
12 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* | ||
|
@@ -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" }; | ||
}, | ||
}); | ||
} | ||
}, | ||
|