@servrox/code-quality 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +287 -2
- package/check/cli.mjs +66 -0
- package/check/git-files.mjs +163 -0
- package/check/run-tools.mjs +46 -0
- package/check/tool-process.mjs +13 -0
- package/dependency-cruiser/core.cjs +20 -0
- package/dependency-cruiser/core.d.cts +8 -0
- package/dependency-cruiser/create-runtime-config.cjs +54 -0
- package/dependency-cruiser/create-runtime-config.d.cts +8 -0
- package/dependency-cruiser/runtime.cjs +9 -0
- package/jscpd/cli.mjs +204 -0
- package/jscpd/core.json +24 -0
- package/oxfmt/core.d.mts +4 -0
- package/oxfmt/core.mjs +3 -0
- package/oxlint/anti-slop.d.mts +1 -0
- package/oxlint/anti-slop.mjs +1 -0
- package/oxlint/core.d.mts +1 -0
- package/oxlint/core.mjs +1 -0
- package/oxlint/modularity.d.mts +6 -0
- package/oxlint/modularity.mjs +106 -0
- package/oxlint/playwright.d.mts +1 -0
- package/oxlint/playwright.mjs +1 -0
- package/oxlint/react.d.mts +1 -0
- package/oxlint/react.mjs +1 -0
- package/oxlint/strict.d.mts +4 -0
- package/oxlint/strict.mjs +25 -0
- package/package.json +122 -4
- package/src/baseline.d.mts +23 -0
- package/src/baseline.mjs +168 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
|
|
3
|
+
const functionTypes = "/^(ArrowFunctionExpression|FunctionExpression)$/";
|
|
4
|
+
|
|
5
|
+
const functionBinding =
|
|
6
|
+
`:matches(` +
|
|
7
|
+
`VariableDeclarator[init.type=${functionTypes}],` +
|
|
8
|
+
`VariableDeclarator[init.type=/^(TSAsExpression|TSSatisfiesExpression|TSNonNullExpression)$/]` +
|
|
9
|
+
`[init.expression.type=${functionTypes}])`;
|
|
10
|
+
|
|
11
|
+
const functions =
|
|
12
|
+
`:matches(` +
|
|
13
|
+
`FunctionDeclaration[body],` +
|
|
14
|
+
`ExportNamedDeclaration[declaration.type="FunctionDeclaration"][declaration.body],` +
|
|
15
|
+
`ExportDefaultDeclaration[declaration.type=/^(FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/],` +
|
|
16
|
+
`ExportDefaultDeclaration[declaration.type=/^(TSAsExpression|TSSatisfiesExpression|TSNonNullExpression)$/][declaration.expression.type=${functionTypes}],` +
|
|
17
|
+
`TSExportAssignment[expression.type=${functionTypes}],` +
|
|
18
|
+
`ExpressionStatement[expression.type="AssignmentExpression"][expression.left.object.name="module"][expression.right.type=${functionTypes}]:matches([expression.left.computed=false][expression.left.property.name="exports"],[expression.left.computed=true][expression.left.property.value="exports"]),` +
|
|
19
|
+
`VariableDeclaration:has(> ${functionBinding}),` +
|
|
20
|
+
`ExportNamedDeclaration:has(> VariableDeclaration:has(> ${functionBinding})))`;
|
|
21
|
+
|
|
22
|
+
const exports =
|
|
23
|
+
":matches(ExportNamedDeclaration[declaration]," +
|
|
24
|
+
"ExportNamedDeclaration[specifiers.length>0],ExportDefaultDeclaration,ExportAllDeclaration[exported])";
|
|
25
|
+
|
|
26
|
+
const moduleAssignment =
|
|
27
|
+
'AssignmentExpression[left.object.name="module"]:matches([left.computed=false][left.property.name="exports"],[left.computed=true][left.property.value="exports"])';
|
|
28
|
+
|
|
29
|
+
const commonJsExport = `:matches(${moduleAssignment},TSExportAssignment)`;
|
|
30
|
+
|
|
31
|
+
const leafBindings =
|
|
32
|
+
':matches(Property[value.type="Identifier"],Property[value.type="AssignmentPattern"][value.left.type="Identifier"],ArrayPattern > Identifier,ArrayPattern > AssignmentPattern[left.type="Identifier"],RestElement[argument.type="Identifier"])';
|
|
33
|
+
|
|
34
|
+
export const functionRestrictions = [
|
|
35
|
+
{
|
|
36
|
+
selector: Array.from({ length: 9 }, () => `${functions}[parent.type="Program"]`).join(" ~ "),
|
|
37
|
+
message: "Maximum 8 top-level functions per file.",
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
selector:
|
|
41
|
+
`Program > VariableDeclaration[declarations.length>1]:has(> ${functionBinding}) > VariableDeclarator,` +
|
|
42
|
+
`Program > ExportNamedDeclaration > VariableDeclaration[declarations.length>1]:has(> ${functionBinding}) > VariableDeclarator`,
|
|
43
|
+
message: "Declare module-level function bindings separately.",
|
|
44
|
+
},
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
export const exportRestrictions = [
|
|
48
|
+
{
|
|
49
|
+
selector: Array.from({ length: 6 }, () => `${exports}[parent.type="Program"]`).join(" ~ "),
|
|
50
|
+
message: "Maximum 5 explicit ESM exports per file.",
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
selector:
|
|
54
|
+
"Program > ExportNamedDeclaration[specifiers.length>1] > ExportSpecifier," +
|
|
55
|
+
"Program > ExportNamedDeclaration > VariableDeclaration[declarations.length>1] > VariableDeclarator",
|
|
56
|
+
message: "Export one symbol per statement outside explicit package entrypoints.",
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
selector: `Program > ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > :matches(ObjectPattern,ArrayPattern) ${leafBindings}`,
|
|
60
|
+
message: "Export named bindings individually.",
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
selector: "Program > ExportAllDeclaration:not([exported])",
|
|
64
|
+
message: "Wildcard exports are restricted to explicit package entrypoints.",
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
selector: `${commonJsExport} > ObjectExpression[properties.length>5] > :matches(Property,SpreadElement)`,
|
|
68
|
+
message: "Maximum 5 properties in a CommonJS export object.",
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
selector: `${commonJsExport} > ObjectExpression > SpreadElement`,
|
|
72
|
+
message: "Export CommonJS properties explicitly so the export budget is checkable.",
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
selector:
|
|
76
|
+
'AssignmentExpression[left.object.name="exports"],AssignmentExpression[left.object.object.name="module"]:matches([left.object.computed=false][left.object.property.name="exports"],[left.object.computed=true][left.object.property.value="exports"])',
|
|
77
|
+
message: "Declare CommonJS exports in one explicit export object.",
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
selector:
|
|
81
|
+
`${moduleAssignment}:not([right.type=/^(ObjectExpression|FunctionExpression|ArrowFunctionExpression)$/]),` +
|
|
82
|
+
"TSExportAssignment:not([expression.type=/^(ObjectExpression|FunctionExpression|ArrowFunctionExpression)$/])",
|
|
83
|
+
message: "Export an inline CommonJS object or function so its public surface is checkable.",
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
selector:
|
|
87
|
+
':matches(CallExpression[callee.object.name="Object"]:matches([callee.computed=false][callee.property.name=/^(assign|defineProperty|defineProperties)$/],[callee.computed=true][callee.property.value=/^(assign|defineProperty|defineProperties)$/]),CallExpression[callee.object.name="Reflect"]:matches([callee.computed=false][callee.property.name=/^(set|defineProperty)$/],[callee.computed=true][callee.property.value=/^(set|defineProperty)$/])):matches([arguments.0.name="exports"],[arguments.0.object.name="module"][arguments.0.computed=false][arguments.0.property.name="exports"],[arguments.0.object.name="module"][arguments.0.computed=true][arguments.0.property.value="exports"])',
|
|
88
|
+
message: "Declare CommonJS exports explicitly instead of using mutation helpers.",
|
|
89
|
+
},
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
export default {
|
|
93
|
+
jsPlugins: [
|
|
94
|
+
{ name: "eslint-js", specifier: fileURLToPath(import.meta.resolve("oxlint-plugin-eslint")) },
|
|
95
|
+
],
|
|
96
|
+
rules: {
|
|
97
|
+
"max-lines": ["error", { max: 300, skipBlankLines: false, skipComments: false }],
|
|
98
|
+
"eslint-js/no-restricted-syntax": ["error", ...functionRestrictions, ...exportRestrictions],
|
|
99
|
+
},
|
|
100
|
+
overrides: [
|
|
101
|
+
{
|
|
102
|
+
files: ["**/*.{test,spec}.{ts,tsx,js,jsx,mts,cts,mjs,cjs}"],
|
|
103
|
+
rules: { "max-lines": ["error", { max: 500, skipBlankLines: false, skipComments: false }] },
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "ultracite/oxlint/playwright";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "ultracite/oxlint/playwright";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "ultracite/oxlint/react";
|
package/oxlint/react.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "ultracite/oxlint/react";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
jsPlugins: [
|
|
5
|
+
{ name: "unicorn-js", specifier: fileURLToPath(import.meta.resolve("eslint-plugin-unicorn")) },
|
|
6
|
+
{ name: "sonarjs", specifier: fileURLToPath(import.meta.resolve("eslint-plugin-sonarjs")) },
|
|
7
|
+
],
|
|
8
|
+
rules: {
|
|
9
|
+
"unicorn-js/prefer-minimal-ternary": "error",
|
|
10
|
+
"unicorn-js/consistent-conditional-object-spread": ["error", "logical"],
|
|
11
|
+
"unicorn-js/prefer-simplified-conditions": "error",
|
|
12
|
+
"unicorn-js/no-useless-re-export": "error",
|
|
13
|
+
"sonarjs/prefer-immediate-return": "error",
|
|
14
|
+
"sonarjs/prefer-single-boolean-return": "error",
|
|
15
|
+
"sonarjs/no-identical-functions": "error",
|
|
16
|
+
"sonarjs/cognitive-complexity": ["error", 10],
|
|
17
|
+
"max-lines-per-function": ["error", { max: 50, skipBlankLines: true, skipComments: true }],
|
|
18
|
+
complexity: ["error", 8],
|
|
19
|
+
"max-depth": ["error", 3],
|
|
20
|
+
"max-params": ["error", 4],
|
|
21
|
+
"max-statements": ["error", 20],
|
|
22
|
+
"max-nested-callbacks": ["error", 3],
|
|
23
|
+
"max-classes-per-file": ["error", 1],
|
|
24
|
+
},
|
|
25
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,124 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@servrox/code-quality",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Reusable Ultracite/Oxc presets and quality policy for servrox projects.",
|
|
5
|
+
"homepage": "https://github.com/servrox/code-quality#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/servrox/code-quality/issues"
|
|
8
|
+
},
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/servrox/code-quality.git"
|
|
13
|
+
},
|
|
14
|
+
"bin": {
|
|
15
|
+
"code-quality-check": "./check/cli.mjs",
|
|
16
|
+
"code-quality-duplicates": "./jscpd/cli.mjs"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"oxlint",
|
|
20
|
+
"oxfmt",
|
|
21
|
+
"jscpd",
|
|
22
|
+
"dependency-cruiser",
|
|
23
|
+
"src",
|
|
24
|
+
"check"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
"./oxlint/core": {
|
|
29
|
+
"types": "./oxlint/core.d.mts",
|
|
30
|
+
"default": "./oxlint/core.mjs"
|
|
31
|
+
},
|
|
32
|
+
"./oxlint/strict": {
|
|
33
|
+
"types": "./oxlint/strict.d.mts",
|
|
34
|
+
"default": "./oxlint/strict.mjs"
|
|
35
|
+
},
|
|
36
|
+
"./oxlint/modularity": {
|
|
37
|
+
"types": "./oxlint/modularity.d.mts",
|
|
38
|
+
"default": "./oxlint/modularity.mjs"
|
|
39
|
+
},
|
|
40
|
+
"./oxlint/anti-slop": {
|
|
41
|
+
"types": "./oxlint/anti-slop.d.mts",
|
|
42
|
+
"default": "./oxlint/anti-slop.mjs"
|
|
43
|
+
},
|
|
44
|
+
"./oxlint/react": {
|
|
45
|
+
"types": "./oxlint/react.d.mts",
|
|
46
|
+
"default": "./oxlint/react.mjs"
|
|
47
|
+
},
|
|
48
|
+
"./oxlint/playwright": {
|
|
49
|
+
"types": "./oxlint/playwright.d.mts",
|
|
50
|
+
"default": "./oxlint/playwright.mjs"
|
|
51
|
+
},
|
|
52
|
+
"./oxfmt": {
|
|
53
|
+
"types": "./oxfmt/core.d.mts",
|
|
54
|
+
"default": "./oxfmt/core.mjs"
|
|
55
|
+
},
|
|
56
|
+
"./jscpd": "./jscpd/core.json",
|
|
57
|
+
"./dependency-cruiser": {
|
|
58
|
+
"types": "./dependency-cruiser/core.d.cts",
|
|
59
|
+
"default": "./dependency-cruiser/core.cjs"
|
|
60
|
+
},
|
|
61
|
+
"./dependency-cruiser/runtime": {
|
|
62
|
+
"types": "./dependency-cruiser/core.d.cts",
|
|
63
|
+
"default": "./dependency-cruiser/runtime.cjs"
|
|
64
|
+
},
|
|
65
|
+
"./dependency-cruiser/create-runtime-config": {
|
|
66
|
+
"types": "./dependency-cruiser/create-runtime-config.d.cts",
|
|
67
|
+
"default": "./dependency-cruiser/create-runtime-config.cjs"
|
|
68
|
+
},
|
|
69
|
+
"./baseline": {
|
|
70
|
+
"types": "./src/baseline.d.mts",
|
|
71
|
+
"default": "./src/baseline.mjs"
|
|
72
|
+
},
|
|
73
|
+
"./package.json": "./package.json"
|
|
74
|
+
},
|
|
75
|
+
"publishConfig": {
|
|
76
|
+
"access": "public",
|
|
77
|
+
"registry": "https://registry.npmjs.org/"
|
|
78
|
+
},
|
|
79
|
+
"scripts": {
|
|
80
|
+
"lint": "ultracite check",
|
|
81
|
+
"lint:policy": "oxlint --config oxlint.policy.config.mjs oxlint oxfmt src dependency-cruiser jscpd scripts check",
|
|
82
|
+
"fix": "ultracite fix",
|
|
83
|
+
"check": "pnpm lint && pnpm lint:policy && tsc --noEmit && node scripts/check.mjs",
|
|
84
|
+
"test": "node --test tests/*.test.mjs",
|
|
85
|
+
"test:package": "node scripts/check-package.mjs",
|
|
86
|
+
"verify": "pnpm check && pnpm test && pnpm test:package"
|
|
87
|
+
},
|
|
88
|
+
"dependencies": {
|
|
89
|
+
"eslint": "10.15.0",
|
|
90
|
+
"eslint-plugin-sonarjs": "4.2.3",
|
|
91
|
+
"eslint-plugin-unicorn": "79.0.0",
|
|
92
|
+
"oxlint-plugin-eslint": "1.93.0",
|
|
93
|
+
"zod": "4.6.7"
|
|
94
|
+
},
|
|
95
|
+
"devDependencies": {
|
|
96
|
+
"@types/node": "26.6.6",
|
|
97
|
+
"dependency-cruiser": "18.9.0",
|
|
98
|
+
"jscpd": "5.4.3",
|
|
99
|
+
"oxfmt": "0.78.0",
|
|
100
|
+
"oxlint": "1.93.0",
|
|
101
|
+
"oxlint-tsgolint": "7.0.2007",
|
|
102
|
+
"typescript": "6.0.3",
|
|
103
|
+
"ultracite": "8.2.1"
|
|
104
|
+
},
|
|
105
|
+
"peerDependencies": {
|
|
106
|
+
"dependency-cruiser": "18.9.0",
|
|
107
|
+
"jscpd": "5.4.3",
|
|
108
|
+
"oxfmt": "0.78.0",
|
|
109
|
+
"oxlint": "1.93.0",
|
|
110
|
+
"ultracite": "8.2.1"
|
|
111
|
+
},
|
|
112
|
+
"peerDependenciesMeta": {
|
|
113
|
+
"dependency-cruiser": {
|
|
114
|
+
"optional": true
|
|
115
|
+
},
|
|
116
|
+
"jscpd": {
|
|
117
|
+
"optional": true
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
"engines": {
|
|
121
|
+
"node": ">=24.15.0"
|
|
122
|
+
},
|
|
123
|
+
"packageManager": "pnpm@12.16.1"
|
|
124
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface Finding {
|
|
2
|
+
file: string;
|
|
3
|
+
rule: string;
|
|
4
|
+
message: string;
|
|
5
|
+
value: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface Baseline {
|
|
9
|
+
version: 1;
|
|
10
|
+
entries: Record<string, number[]>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface Regression extends Finding {
|
|
14
|
+
allowed: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export declare const normalizeLint: (input: unknown) => Finding[];
|
|
18
|
+
export declare const normalizeKnip: (input: unknown) => Finding[];
|
|
19
|
+
export declare const createBaseline: (findings: readonly Finding[]) => Baseline;
|
|
20
|
+
export declare const findRegressions: (
|
|
21
|
+
findings: readonly Finding[],
|
|
22
|
+
input: unknown
|
|
23
|
+
) => Regression[];
|
package/src/baseline.mjs
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/** @typedef {{ file: string, rule: string, message: string, value: number }} Finding */
|
|
4
|
+
|
|
5
|
+
const diagnosticSchema = z.object({
|
|
6
|
+
filename: z.string().min(1),
|
|
7
|
+
code: z.string().regex(/^[\w-]+\([\w-]+\)$/u),
|
|
8
|
+
message: z.string(),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const lintReportSchema = z.object({
|
|
12
|
+
diagnostics: z.array(diagnosticSchema),
|
|
13
|
+
number_of_files: z.number().int().positive(),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const knipEntrySchema = z.object({
|
|
17
|
+
name: z.string().min(1),
|
|
18
|
+
namespace: z.string().min(1).optional(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// Export namespaces are consumer-local aliases; member/catalog namespaces are declared owners.
|
|
22
|
+
const knipExportRules = new Set(["exports", "types", "nsExports", "nsTypes"]);
|
|
23
|
+
|
|
24
|
+
/** @param {z.infer<typeof knipEntrySchema>} entry - Knip symbol and its optional namespace. @param {string} rule - Reported issue category. */
|
|
25
|
+
const knipSymbolName = (entry, rule) =>
|
|
26
|
+
entry.namespace === undefined || knipExportRules.has(rule)
|
|
27
|
+
? entry.name
|
|
28
|
+
: `${entry.namespace}[${JSON.stringify(entry.name)}]`;
|
|
29
|
+
|
|
30
|
+
const knipReportSchema = z.object({
|
|
31
|
+
issues: z.array(
|
|
32
|
+
z
|
|
33
|
+
.object({ file: z.string().min(1) })
|
|
34
|
+
.catchall(z.array(z.union([knipEntrySchema, z.array(knipEntrySchema)])))
|
|
35
|
+
),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const baselineSchema = z.object({
|
|
39
|
+
version: z.literal(1),
|
|
40
|
+
entries: z.record(z.string(), z.array(z.number().nonnegative())),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const identitySchema = z.tuple([z.string(), z.string(), z.string()]);
|
|
44
|
+
|
|
45
|
+
// Uncountable syntax must be fixed or explicitly scoped in config, never grandfathered.
|
|
46
|
+
const countableSyntaxMessages = new Set([
|
|
47
|
+
"Maximum 8 top-level functions per file.",
|
|
48
|
+
"Maximum 5 explicit ESM exports per file.",
|
|
49
|
+
"Maximum 5 properties in a CommonJS export object.",
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
const numericRules = new Map([
|
|
53
|
+
["eslint(max-lines)", /too many lines \((?<metric>\d+)\)/u],
|
|
54
|
+
["eslint(max-lines-per-function)", /too many lines \((?<metric>\d+)\)/u],
|
|
55
|
+
["eslint(complexity)", /complexity of (?<metric>\d+)/u],
|
|
56
|
+
["sonarjs(cognitive-complexity)", /Complexity from (?<metric>\d+)/u],
|
|
57
|
+
["eslint(max-depth)", /nested too deeply \((?<metric>\d+)\)/u],
|
|
58
|
+
["eslint(max-params)", /too many parameters \((?<metric>\d+)\)/u],
|
|
59
|
+
["eslint(max-statements)", /too many statements \((?<metric>\d+)\)/u],
|
|
60
|
+
["eslint(max-nested-callbacks)", /nested callbacks \((?<metric>\d+)\)/u],
|
|
61
|
+
["eslint(max-classes-per-file)", /too many classes \((?<metric>\d+)\)/u],
|
|
62
|
+
]);
|
|
63
|
+
|
|
64
|
+
/** @param {unknown} input - Input to validate or compare. @returns {Finding[]} */
|
|
65
|
+
export const normalizeLint = (input) => {
|
|
66
|
+
const report = lintReportSchema.parse(input);
|
|
67
|
+
|
|
68
|
+
return report.diagnostics.map(({ filename, code, message }) => {
|
|
69
|
+
const match = numericRules.get(code)?.exec(message);
|
|
70
|
+
|
|
71
|
+
let normalized = match
|
|
72
|
+
? message.replace(match[0], match[0].replace(match[1], "<value>"))
|
|
73
|
+
: message;
|
|
74
|
+
|
|
75
|
+
if (code.startsWith("sonarjs(")) {
|
|
76
|
+
normalized = normalized.replaceAll(/\bon line \d+\b/gu, "on line <location>");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
file: filename.replaceAll("\\", "/"),
|
|
81
|
+
rule: code,
|
|
82
|
+
message: normalized,
|
|
83
|
+
value: match ? Number(match[1]) : 1,
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/** @param {unknown} input - Input to validate or compare. @returns {Finding[]} */
|
|
89
|
+
export const normalizeKnip = (input) => {
|
|
90
|
+
const report = knipReportSchema.parse(input);
|
|
91
|
+
/** @type {Finding[]} */
|
|
92
|
+
const findings = [];
|
|
93
|
+
|
|
94
|
+
for (const issue of report.issues) {
|
|
95
|
+
const { file, ...categories } = issue;
|
|
96
|
+
|
|
97
|
+
for (const [rule, entries] of Object.entries(categories)) {
|
|
98
|
+
if (rule === "owners") {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
for (const entry of entries.flat()) {
|
|
103
|
+
findings.push({
|
|
104
|
+
file,
|
|
105
|
+
rule: `knip/${rule}`,
|
|
106
|
+
message: knipSymbolName(entry, rule),
|
|
107
|
+
value: 1,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return findings;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/** @param {Finding} finding - Diagnostic to classify for baseline eligibility. */
|
|
117
|
+
const isUncountableSyntax = (finding) =>
|
|
118
|
+
finding.rule === "eslint-js(no-restricted-syntax)" &&
|
|
119
|
+
!countableSyntaxMessages.has(finding.message);
|
|
120
|
+
|
|
121
|
+
/** @param {readonly Finding[]} findings - Input to validate or compare. */
|
|
122
|
+
export const createBaseline = (findings) => {
|
|
123
|
+
/** @type {Record<string, number[]>} */
|
|
124
|
+
const entries = {};
|
|
125
|
+
|
|
126
|
+
for (const finding of findings) {
|
|
127
|
+
if (isUncountableSyntax(finding)) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
`Resolve uncountable module syntax before baselining: ${finding.file}: ${finding.message}`
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const key = JSON.stringify([finding.file, finding.rule, finding.message]);
|
|
134
|
+
(entries[key] ??= []).push(finding.value);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
version: 1,
|
|
139
|
+
entries: Object.fromEntries(
|
|
140
|
+
Object.entries(entries)
|
|
141
|
+
.toSorted(([a], [b]) => a.localeCompare(b, "en"))
|
|
142
|
+
.map(([key, values]) => [key, values.toSorted((a, b) => b - a)])
|
|
143
|
+
),
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/** @param {readonly Finding[]} findings - Input to validate or compare. @param {unknown} input - Input to validate or compare. */
|
|
148
|
+
export const findRegressions = (findings, input) => {
|
|
149
|
+
const baseline = baselineSchema.parse(input);
|
|
150
|
+
const actual = createBaseline(findings.filter((finding) => !isUncountableSyntax(finding)));
|
|
151
|
+
|
|
152
|
+
const regressions = findings
|
|
153
|
+
.filter(isUncountableSyntax)
|
|
154
|
+
.map((finding) => ({ ...finding, allowed: 0 }));
|
|
155
|
+
|
|
156
|
+
for (const [key, values] of Object.entries(actual.entries)) {
|
|
157
|
+
const budget = (baseline.entries[key] ?? []).toSorted((a, b) => b - a);
|
|
158
|
+
const [file, rule, message] = identitySchema.parse(JSON.parse(key));
|
|
159
|
+
|
|
160
|
+
for (const [index, value] of values.entries()) {
|
|
161
|
+
if (index >= budget.length || value > budget[index]) {
|
|
162
|
+
regressions.push({ file, rule, message, value, allowed: budget[index] ?? 0 });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return regressions;
|
|
168
|
+
};
|