Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapting header rule to check for license type #6

Merged
merged 8 commits into from
Oct 17, 2024
4 changes: 2 additions & 2 deletions .github/workflows/bump-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Auto Bump Version
on:
push:
branches:
- main # Trigger on push to main branch
- main # Trigger on push to main branch

jobs:
bump:
Expand All @@ -17,7 +17,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
node-version: "18"

- run: corepack enable

Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ module.exports = {

You can add `"plugin:@lichtblick/typescript"` to the top level `extends` instead of using `overrides` if your project contains no `.js` files.

## License Header Rule Configuration

To use the license-header rule, your project must specify the license type in the ESLint configuration file (`.eslintrc`). This is done by defining an object with a `licenseType` attribute that matches your project's license.

Here’s an example of how to configure it:

```js
rules:
"@lichtblick/license-header": ["error", {licenseType: "MPL-2.0"}]
```

In this example, the `licenseType` is set to "MPL-2.0", but you should replace it with the appropriate license type for your project (e.g., "MIT", "Apache-2.0").


## License

@lichtblick/eslint-plugin is released under the [MIT License](/LICENSE.md).
Expand All @@ -73,4 +87,3 @@ You can add `"plugin:@lichtblick/typescript"` to the top level `extends` instead
tag=$(npm version minor) && echo "$tag"
git push && git push origin "$tag"
```

45 changes: 23 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"prettier": "^3"
},
"devDependencies": {
"@foxglove/tsconfig": "2.0.0",
"@lichtblick/tsconfig": "1.0.0",
"@lichtblick/eslint-plugin": "file:.",
"@types/jest": "29.5.3",
"@typescript-eslint/eslint-plugin": "6.1.0",
Expand Down
5 changes: 4 additions & 1 deletion rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ This rule accepts a single object option with the following default configuratio

```json
{
"@lichtblick/no-boolean-parameters": ["error", { "allowLoneParameter": false }]
"@lichtblick/no-boolean-parameters": [
"error",
{ "allowLoneParameter": false }
]
}
```

Expand Down
39 changes: 32 additions & 7 deletions rules/license-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,47 @@
// while allowing certain prefixes that cannot be moved below the 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
`.trim();

module.exports = {
meta: {
type: "suggestion",
fixable: "code",
schema: [],
schema: [
{
type: "object",
properties: {
licenseType: {
type: "string",
descritpion:
"Type of license that should be displayed on the header.",
},
},
additionalProperties: false,
},
],
messages: {
missingLicenseError: "Missing license error",
wrongHeaderError:
"There is an error with the file header. Please check if the header exists or if there is a mistake in it.",
missingTypeOfLicenseError:
"Please specify the license type in the .eslintrc configuration. For more information, refer to our documentation: https://github.com/Lichtblick-Suite/eslint-plugin?tab=readme-ov-file#lichtblickeslint-plugin",
},
},

create: (context) => {
const options = context.options[0];
if (!options || !options.licenseType) {
context.report({
loc: { line: 0, column: 0 },
messageId: "missingTypeOfLicenseError",
});
return {};
}
const licenseType = options.licenseType;
const LICENSE_HEADER = `
// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: ${licenseType}
`.trim();

return {
Program: () => {
const source = context.getSourceCode().getText();
Expand All @@ -28,7 +53,7 @@ module.exports = {
);
if (headerIndex === -1 || !prefixLinesAreValid) {
context.report({
messageId: "missingLicenseError",
messageId: "wrongHeaderError",
loc: { start: 0, end: +source.indexOf("\n") + 1 },
fix: () => {
return { range: [0, 0], text: LICENSE_HEADER + "\n\n" };
Expand Down
92 changes: 70 additions & 22 deletions rules/license-header.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import { RuleTester } from "@typescript-eslint/rule-tester";
import { TSESLint } from "@typescript-eslint/utils";

// should be the same of LICENSE_HEADER defined on license-header.js file.
const LICENSE_HEADER = `
const mplLicense = "MPL-2.0";
const mitLicense = "MIT";
const noLicense = "";
ctw-joao-luis marked this conversation as resolved.
Show resolved Hide resolved

function createHeader(license: string) {
return `
// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: MPL-2.0
// SPDX-License-Identifier: ${license}
`.trim();
}

const rule =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require("./license-header") as TSESLint.RuleModule<"missingLicenseError">;
require("./license-header") as TSESLint.RuleModule<
"wrongHeaderError" | "missingTypeOfLicenseError",
Array<Record<string, string>>
>;

const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
Expand All @@ -23,8 +32,7 @@ const ruleTester = new RuleTester({
});

const validLichtblickHeader = `
// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: MPL-2.0
${createHeader(mitLicense)}

// Rest of file
`;
Expand All @@ -33,8 +41,7 @@ const validLichtblickHeaderWithSpaces = `



// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: MPL-2.0
${createHeader(mplLicense)}



Expand All @@ -43,8 +50,7 @@ const validLichtblickHeaderWithSpaces = `
const validLichtblickHeaderWithSpacesWithJsdom = `
/** @jest-environment jsdom */

// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: MPL-2.0
${createHeader(mplLicense)}
`;

const invalidLichtblickHeaderEmpty = `
Expand All @@ -63,23 +69,65 @@ var b = 2
console.log(1 + 2)
`;

const invalidLichtblickHeaderCases = [
invalidLichtblickHeaderEmpty,
invalidLichtblickHeaderOlder,
invalidLichtblickHeaderRandom,
];
const invalidLichtblickHeaderWithMissingTypeOfLicense = `
${createHeader(noLicense)}

`;

const invalidLichtblickHeaderWithWrongTypeOfLicense = `
${createHeader(mplLicense)}

`;

ruleTester.run("check-license-header", rule, {
valid: [
validLichtblickHeader,
validLichtblickHeaderWithSpaces,
validLichtblickHeaderWithSpacesWithJsdom,
{
code: validLichtblickHeader,
options: [{ licenseType: "MIT" }],
},
{
code: validLichtblickHeaderWithSpaces,
options: [{ licenseType: "MPL-2.0" }],
},
{
code: validLichtblickHeaderWithSpacesWithJsdom,
options: [{ licenseType: "MPL-2.0" }],
},
],

// Test if the lint fix were successfull, adding the LICENSE_HEADER followed by two empty lines
invalid: invalidLichtblickHeaderCases.map((invalidHeader) => ({
code: invalidHeader,
errors: [{ messageId: "missingLicenseError" }],
output: LICENSE_HEADER + "\n\n" + invalidHeader,
})),
invalid: [
{
code: invalidLichtblickHeaderEmpty,
options: [{ licenseType: "MPL-2.0" }],
errors: [{ messageId: "wrongHeaderError" }],
output: createHeader(mplLicense) + "\n\n" + invalidLichtblickHeaderEmpty,
},
{
code: invalidLichtblickHeaderOlder,
options: [{ licenseType: "MPL-2.0" }],
errors: [{ messageId: "wrongHeaderError" }],
output: createHeader(mplLicense) + "\n\n" + invalidLichtblickHeaderOlder,
},
{
code: invalidLichtblickHeaderRandom,
options: [{ licenseType: "MPL-2.0" }],
errors: [{ messageId: "wrongHeaderError" }],
output: createHeader(mplLicense) + "\n\n" + invalidLichtblickHeaderRandom,
},
{
code: invalidLichtblickHeaderWithMissingTypeOfLicense,
options: [],
errors: [{ messageId: "missingTypeOfLicenseError" }],
},
{
code: invalidLichtblickHeaderWithWrongTypeOfLicense,
options: [{ licenseType: "MIT" }],
errors: [{ messageId: "wrongHeaderError" }],
output:
createHeader(mitLicense) +
"\n\n" +
invalidLichtblickHeaderWithWrongTypeOfLicense,
},
],
});
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// -*- jsonc -*-
{
"extends": "@foxglove/tsconfig/base",
"extends": "@lichtblick/tsconfig/base",
"compilerOptions": {
"lib": ["es2020", "dom"],
"jsx": "preserve",
"noEmit": true,
"moduleResolution": "Node16"
"moduleResolution": "Node16",
"module": "Node16"
}
}
2 changes: 1 addition & 1 deletion tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// -*- jsonc -*-
{
"extends": "@foxglove/tsconfig/base",
"extends": "@lichtblick/tsconfig/base",
"compilerOptions": {
"lib": ["es2020", "dom"],
"noEmit": true
Expand Down
Loading
Loading