Skip to content

Commit f153a48

Browse files
committed
chore: better linting
1 parent 91ca583 commit f153a48

File tree

8 files changed

+73
-119
lines changed

8 files changed

+73
-119
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
.idea/
22
.vscode/
33

4-
build/
5-
coverage/
64
node_modules/

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

eslint.config.mjs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
// @ts-check
2-
3-
import eslint from "@eslint/js";
1+
import { defineConfig } from "eslint/config";
2+
import js from "@eslint/js";
3+
import globals from "globals";
44
import tseslint from "typescript-eslint";
5-
import prettierConfig from "eslint-plugin-prettier/recommended";
5+
import eslintConfigPrettier from "eslint-config-prettier/flat";
66

7-
export default tseslint.config(
8-
eslint.configs.recommended,
9-
tseslint.configs.strict,
10-
tseslint.configs.stylistic,
11-
prettierConfig,
7+
export default defineConfig([
8+
{
9+
files: ["**/*.{js,mjs,cjs,ts,mts,cts}"],
10+
plugins: { js },
11+
extends: ["js/recommended"],
12+
},
13+
{
14+
files: ["**/*.{js,mjs,cjs,ts,mts,cts}"],
15+
languageOptions: { globals: globals.browser },
16+
},
17+
tseslint.configs.recommended,
18+
eslintConfigPrettier,
1219
{
13-
ignores: ["dist/**", "node_modules/**"],
20+
ignores: ["dist"],
1421
},
15-
);
22+
]);

package-lock.json

Lines changed: 17 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@types/node": "^24.0.15",
4444
"eslint": "^9.31.0",
4545
"eslint-config-prettier": "^10.1.8",
46-
"eslint-plugin-prettier": "^5.5.3",
46+
"globals": "^16.3.0",
4747
"prettier": "^3.6.2",
4848
"rollup": "^4.45.1",
4949
"tslib": "^2.8.1",

src/lint.test.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
getConventionalCommitTypes,
3-
lintPullRequest,
3+
isConventionalCommitTitleValid,
44
isBotIgnored,
55
} from "./lint.js";
66
import { context } from "@actions/github";
@@ -27,7 +27,7 @@ describe("getConvetionalCommitTypes tests", () => {
2727
});
2828
});
2929

30-
describe("lintPullRequest tests", () => {
30+
describe("isConventionalCommitTitleValid tests", () => {
3131
const tests = [
3232
{ args: "feat: test", expected: true },
3333
{ args: "feat(test): test", expected: true },
@@ -42,7 +42,7 @@ describe("lintPullRequest tests", () => {
4242

4343
tests.forEach(({ args, expected }) => {
4444
it(`should pass or fail linting ['${args}', '${expected}']`, async () => {
45-
expect(await lintPullRequest(args)).toBe(expected);
45+
expect(await isConventionalCommitTitleValid(args)).toBe(expected);
4646
});
4747
});
4848

@@ -54,31 +54,43 @@ describe("lintPullRequest tests", () => {
5454
it("should pass if subject matches pattern", async () => {
5555
vi.spyOn(core, "getInput").mockReturnValue("matching");
5656

57-
expect(await lintPullRequest(`feat: matching`)).toBe(true);
58-
expect(await lintPullRequest(`feat(test): matching`)).toBe(true);
57+
expect(await isConventionalCommitTitleValid(`feat: matching`)).toBe(true);
58+
expect(await isConventionalCommitTitleValid(`feat(test): matching`)).toBe(
59+
true,
60+
);
5961
});
6062

6163
it("should fail if subject does not match pattern", async () => {
6264
vi.spyOn(core, "getInput").mockReturnValue("not-matching");
6365

64-
expect(await lintPullRequest(`feat: matching`)).toBe(false);
65-
expect(await lintPullRequest(`feat(test): matching`)).toBe(false);
66+
expect(await isConventionalCommitTitleValid(`feat: matching`)).toBe(
67+
false,
68+
);
69+
expect(await isConventionalCommitTitleValid(`feat(test): matching`)).toBe(
70+
false,
71+
);
6672
});
6773

6874
it("should handle empty subject pattern", async () => {
6975
vi.spyOn(core, "getInput").mockReturnValue("");
7076

71-
expect(await lintPullRequest("feat: test")).toBe(true);
72-
expect(await lintPullRequest("feat(test): test")).toBe(true);
77+
expect(await isConventionalCommitTitleValid("feat: test")).toBe(true);
78+
expect(await isConventionalCommitTitleValid("feat(test): test")).toBe(
79+
true,
80+
);
7381
});
7482

7583
it("should handle complex regex", async () => {
7684
vi.spyOn(core, "getInput").mockReturnValue(
7785
"[a-z]{1,5}[0-9]{1,3}[!@#]HELLO",
7886
);
7987

80-
expect(await lintPullRequest("feat: ab11@HELLO")).toBe(true);
81-
expect(await lintPullRequest("feat(test): ddd999!HELLO")).toBe(true);
88+
expect(await isConventionalCommitTitleValid("feat: ab11@HELLO")).toBe(
89+
true,
90+
);
91+
expect(
92+
await isConventionalCommitTitleValid("feat(test): ddd999!HELLO"),
93+
).toBe(true);
8294
});
8395
});
8496
});

src/lint.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,26 @@ export function getConventionalCommitTypes(): string {
2121
.join("\n");
2222
}
2323

24-
export async function lintPullRequest(title: string) {
24+
export async function isConventionalCommitTitleValid(
25+
title: string,
26+
): Promise<boolean> {
2527
const subjectPatternInput = getInput("subject_pattern");
2628
const subjectPattern = subjectPatternInput
2729
? new RegExp(subjectPatternInput)
2830
: null;
2931

30-
const matches = Object.keys(types).map((type) => {
31-
return new RegExp(`^${type}(\\(.+\\))?!?:.+$`);
32-
});
32+
const typeRegexes = Object.keys(types).map(
33+
(type) => new RegExp(`^${type}(\\(.+\\))?!?:\\s?.+$`),
34+
);
3335

34-
const matchedType = matches.find((regex) => regex.test(title));
35-
if (!matchedType) return false;
36+
const hasValidType = typeRegexes.some((regex) => regex.test(title));
37+
if (!hasValidType) return false;
3638

3739
if (subjectPattern) {
38-
const match = title.match(/^.+?:\s*(.+)$/);
39-
if (!match || !match[1]) return false;
40+
const subjectMatch = title.match(/^.+?:\s*(.+)$/);
41+
if (!subjectMatch || !subjectMatch[1]) return false;
4042

41-
const subject = match[1];
43+
const subject = subjectMatch[1];
4244
return subjectPattern.test(subject);
4345
}
4446

@@ -52,7 +54,7 @@ export async function lint() {
5254
}
5355

5456
const pr = await getPullRequest();
55-
const isPrTitleOk = await lintPullRequest(pr.title);
57+
const isPrTitleOk = await isConventionalCommitTitleValid(pr.title);
5658

5759
if (isPrTitleOk) {
5860
await deletePrComment();

0 commit comments

Comments
 (0)