Skip to content

Commit 1a6a880

Browse files
feat: added special webpack rule to check license comments (#66)
1 parent dd3d648 commit 1a6a880

File tree

7 files changed

+142
-0
lines changed

7 files changed

+142
-0
lines changed

configs/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import nodeConfig from "./node.js";
66
import packageJSON from "./package-json.js";
77
import stylisticConfig from "./stylistic.js";
88
import typescriptConfig from "./typescript.js";
9+
import webpackSpecial from "./webpack-special.js";
910

1011
const configs = {
1112
...browserConfig,
@@ -16,6 +17,7 @@ const configs = {
1617
...stylisticConfig,
1718
...typescriptConfig,
1819
...packageJSON,
20+
...webpackSpecial,
1921
};
2022

2123
export default configs;

configs/webpack-special.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { configs } from "../plugins/webpack/index.js";
2+
3+
const recommendedWebpackSpecialConfig = {
4+
...configs.recommended,
5+
};
6+
7+
export default {
8+
"webpack/special": recommendedWebpackSpecialConfig,
9+
};

eslint.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ export default defineConfig([
3939
"n/hashbang": "off",
4040
},
4141
},
42+
// For test purposes
43+
{
44+
files: ["./validation/webpack/**/*"],
45+
extends: [configs["recommended-commonjs"], configs["webpack/special"]],
46+
},
4247
]);

plugins/webpack/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { createRequire } from "node:module";
2+
import { allExtensions } from "../../configs/utils/extensions.js";
3+
import { rule as requireLicenseComment } from "./rules/require-license-comment.js";
4+
5+
const require = createRequire(import.meta.url);
6+
7+
const { version } = require("../../package.json");
8+
9+
const rules = {
10+
"require-license-comment": requireLicenseComment,
11+
};
12+
13+
const recommendedRules = {
14+
...Object.fromEntries(
15+
Object.entries(rules)
16+
.filter(([, rule]) => rule.meta.docs?.recommended)
17+
.map(([name]) => [`webpack/${name}`, "error"]),
18+
),
19+
};
20+
21+
const configs = {
22+
recommended: {
23+
name: "webpack/recommended",
24+
files: [`**/*.{${allExtensions.map((item) => item.slice(1)).join(",")}}`],
25+
plugins: {
26+
get webpack() {
27+
// eslint-disable-next-line no-use-before-define
28+
return plugin;
29+
},
30+
},
31+
rules: recommendedRules,
32+
},
33+
};
34+
35+
const plugin = {
36+
configs,
37+
meta: {
38+
version,
39+
},
40+
rules,
41+
};
42+
43+
export { configs, rules };
44+
45+
export default plugin;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @type {import("eslint").Rule} rule
3+
*/
4+
export const rule = {
5+
create(context) {
6+
const sourceCode = context.getSourceCode();
7+
8+
return {
9+
"Program:exit"(program) {
10+
const comments = sourceCode.getAllComments();
11+
const licenseComment = comments.find(
12+
(comment) =>
13+
comment.type === "Block" &&
14+
/\n\s*MIT License http:\/\/www\.opensource\.org\/licenses\/mit-license\.php\n\s*(?:(Authors? .+)\n)?\s*/g.test(
15+
comment.value,
16+
),
17+
);
18+
19+
if (!licenseComment) {
20+
context.report({
21+
loc: program.loc,
22+
message: "Expected license comment.",
23+
});
24+
25+
return;
26+
}
27+
28+
const afterComment = sourceCode.text[licenseComment.end];
29+
30+
if (afterComment !== "\n") {
31+
context.report({
32+
loc: licenseComment.loc,
33+
message: "Expected newline after license comment.",
34+
});
35+
36+
return;
37+
}
38+
39+
const afterAfterComment = sourceCode.text[licenseComment.end + 1];
40+
41+
if (afterAfterComment !== "\n") {
42+
context.report({
43+
loc: licenseComment.loc,
44+
message: "Expected newline after license comment.",
45+
});
46+
}
47+
},
48+
};
49+
},
50+
meta: {
51+
docs: {
52+
category: "Best Practices",
53+
description: "Require license comment",
54+
recommended: true,
55+
},
56+
fixable: "code",
57+
type: "layout",
58+
},
59+
};

validation/webpack/file.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Alexander Akait @alexander-akait
4+
*/
5+
6+
"use strict";
7+
8+
/**
9+
* @param {number} a a
10+
* @param {number} b b
11+
* @returns {number} result
12+
*/
13+
function sum(a, b) {
14+
return a + b;
15+
}
16+
17+
sum(1, 2);

validation/webpack/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "webpack-package",
3+
"version": "1.0.0",
4+
"description": "Test"
5+
}

0 commit comments

Comments
 (0)