@whil/guardrails 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +208 -2
- package/catalog-assets/claude-hooks/guardrails-check.sh +37 -0
- package/catalog-assets/eslint-guardrails/config.mjs +21 -0
- package/catalog-assets/eslint-rules/_rule-tester.mjs +21 -0
- package/catalog-assets/eslint-rules/cache-policy-constant.mjs +35 -0
- package/catalog-assets/eslint-rules/client-imports-server.mjs +26 -0
- package/catalog-assets/eslint-rules/effects-in-hooks.mjs +46 -0
- package/catalog-assets/eslint-rules/env-through-config.mjs +26 -0
- package/catalog-assets/eslint-rules/hook-body-order.mjs +169 -0
- package/catalog-assets/eslint-rules/import-boundaries.mjs +97 -0
- package/catalog-assets/eslint-rules/no-comments.mjs +35 -0
- package/catalog-assets/eslint-rules/no-default-export.mjs +20 -0
- package/catalog-assets/eslint-rules/no-delete.mjs +16 -0
- package/catalog-assets/eslint-rules/no-enum.mjs +14 -0
- package/catalog-assets/eslint-rules/no-heavy-inline-styles.mjs +20 -0
- package/catalog-assets/eslint-rules/no-let.mjs +14 -0
- package/catalog-assets/eslint-rules/no-logic-in-jsx.mjs +23 -0
- package/catalog-assets/eslint-rules/no-manual-memoization.mjs +45 -0
- package/catalog-assets/eslint-rules/no-member-assignment.mjs +28 -0
- package/catalog-assets/eslint-rules/no-module-cache.mjs +40 -0
- package/catalog-assets/eslint-rules/no-mutating-array-methods.mjs +85 -0
- package/catalog-assets/eslint-rules/no-process-exit.mjs +15 -0
- package/catalog-assets/eslint-rules/no-state-mutation.mjs +62 -0
- package/catalog-assets/eslint-rules/no-unnecessary-use-client.mjs +40 -0
- package/catalog-assets/eslint-rules/pure-components.mjs +48 -0
- package/catalog-assets/eslint-rules/query-key-constant.mjs +54 -0
- package/catalog-assets/eslint-rules/require-disable-reason.mjs +29 -0
- package/catalog-assets/eslint-rules/restrict-timers.mjs +35 -0
- package/catalog-assets/eslint-rules/rules-of-hooks.mjs +51 -0
- package/catalog-assets/eslint-rules/thin-api-routes.mjs +18 -0
- package/catalog-assets/eslint-rules/thin-routes.mjs +18 -0
- package/catalog-assets/eslint-rules/virtualized-lists.mjs +32 -0
- package/catalog-assets/githooks/commit-msg +13 -0
- package/catalog-assets/githooks/pre-commit +26 -0
- package/catalog-assets/scripts/check-exact-deps.mjs +26 -0
- package/dist/cli.js +54 -0
- package/dist/core/app/analysis.js +6 -0
- package/dist/core/app/args.js +44 -0
- package/dist/core/app/catalog.js +20 -0
- package/dist/core/app/ci-plan.js +14 -0
- package/dist/core/app/claude.js +37 -0
- package/dist/core/app/commands/add-rule.js +32 -0
- package/dist/core/app/commands/agents-md.js +13 -0
- package/dist/core/app/commands/apply.js +74 -0
- package/dist/core/app/commands/audit.js +38 -0
- package/dist/core/app/commands/check.js +33 -0
- package/dist/core/app/commands/detect.js +16 -0
- package/dist/core/app/commands/init.js +124 -0
- package/dist/core/app/commands/learn.js +48 -0
- package/dist/core/app/commands/plan.js +40 -0
- package/dist/core/app/commands/pr-status.js +66 -0
- package/dist/core/app/commands/self-check.js +17 -0
- package/dist/core/app/git.js +75 -0
- package/dist/core/app/github.js +146 -0
- package/dist/core/app/hooks-script.js +27 -0
- package/dist/core/app/output.js +17 -0
- package/dist/core/app/run.js +32 -0
- package/dist/core/app/session.js +18 -0
- package/dist/core/app/timing.js +11 -0
- package/dist/core/domain/agents-md.js +92 -0
- package/dist/core/domain/audit-report.js +7 -0
- package/dist/core/domain/changes.js +8 -0
- package/dist/core/domain/check-report.js +23 -0
- package/dist/core/domain/claude-settings.js +42 -0
- package/dist/core/domain/constraint.js +7 -0
- package/dist/core/domain/diagnostics.js +3 -0
- package/dist/core/domain/errors.js +20 -0
- package/dist/core/domain/facts.js +18 -0
- package/dist/core/domain/init-report.js +28 -0
- package/dist/core/domain/json.js +43 -0
- package/dist/core/domain/learn.js +78 -0
- package/dist/core/domain/log-parse.js +99 -0
- package/dist/core/domain/pack-defaults.js +17 -0
- package/dist/core/domain/pack.js +1 -0
- package/dist/core/domain/pr-comment.js +45 -0
- package/dist/core/domain/pr-loop.js +85 -0
- package/dist/core/domain/profile.js +25 -0
- package/dist/core/domain/protection.js +43 -0
- package/dist/core/domain/ratchet.js +40 -0
- package/dist/core/domain/registry.js +32 -0
- package/dist/core/domain/remote.js +4 -0
- package/dist/core/domain/result.js +25 -0
- package/dist/core/domain/thresholds.js +9 -0
- package/dist/core/domain/types.js +1 -0
- package/dist/core/domain/workflows.js +252 -0
- package/dist/core/infra/assets.js +7 -0
- package/dist/core/infra/backup.js +31 -0
- package/dist/core/infra/baseline.js +8 -0
- package/dist/core/infra/changes.js +17 -0
- package/dist/core/infra/exec.js +14 -0
- package/dist/core/infra/facts.js +61 -0
- package/dist/core/infra/fs.js +87 -0
- package/dist/core/infra/gh.js +32 -0
- package/dist/core/infra/hooks.js +31 -0
- package/dist/core/infra/learn-sources.js +40 -0
- package/dist/core/infra/pr.js +48 -0
- package/dist/packs/index.js +6 -0
- package/dist/packs/js-ts/app/add-rule.js +116 -0
- package/dist/packs/js-ts/app/analysis.js +104 -0
- package/dist/packs/js-ts/app/bootstrap.js +16 -0
- package/dist/packs/js-ts/app/eslint-constraints.js +155 -0
- package/dist/packs/js-ts/app/eslint-files.js +56 -0
- package/dist/packs/js-ts/app/fixes.js +3 -0
- package/dist/packs/js-ts/app/pr-rules.js +30 -0
- package/dist/packs/js-ts/app/project.js +87 -0
- package/dist/packs/js-ts/app/tests.js +63 -0
- package/dist/packs/js-ts/domain/catalog-specs.js +147 -0
- package/dist/packs/js-ts/domain/ci.js +47 -0
- package/dist/packs/js-ts/domain/data.js +34 -0
- package/dist/packs/js-ts/domain/diagnostics-js.js +17 -0
- package/dist/packs/js-ts/domain/disables.js +11 -0
- package/dist/packs/js-ts/domain/eslint-output.js +46 -0
- package/dist/packs/js-ts/domain/eslint-state.js +80 -0
- package/dist/packs/js-ts/domain/hooks.js +49 -0
- package/dist/packs/js-ts/domain/pr-diagnostics.js +33 -0
- package/dist/packs/js-ts/domain/pr-tests.js +27 -0
- package/dist/packs/js-ts/domain/profile-pure.js +51 -0
- package/dist/packs/js-ts/domain/profile.js +112 -0
- package/dist/packs/js-ts/domain/report.js +74 -0
- package/dist/packs/js-ts/domain/rule-templates.js +44 -0
- package/dist/packs/js-ts/domain/signals.js +14 -0
- package/dist/packs/js-ts/domain/testing.js +33 -0
- package/dist/packs/js-ts/domain/ts-options.js +13 -0
- package/dist/packs/js-ts/index.js +50 -0
- package/dist/packs/js-ts/infra/eslint.js +50 -0
- package/dist/packs/js-ts/infra/scan.js +25 -0
- package/dist/packs/js-ts/infra/tsc.js +21 -0
- package/dist/packs/nextjs/domain/data.js +9 -0
- package/dist/packs/nextjs/domain/profile.js +21 -0
- package/dist/packs/nextjs/domain/specs.js +33 -0
- package/dist/packs/nextjs/index.js +23 -0
- package/dist/packs/node/domain/specs.js +23 -0
- package/dist/packs/node/index.js +26 -0
- package/dist/packs/react/domain/data.js +9 -0
- package/dist/packs/react/domain/specs.js +63 -0
- package/dist/packs/react/index.js +30 -0
- package/dist/packs/react-native/domain/specs.js +29 -0
- package/dist/packs/react-native/domain/testing.js +20 -0
- package/dist/packs/react-native/index.js +30 -0
- package/package.json +60 -4
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { dirname, relative, resolve, sep } from "node:path";
|
|
2
|
+
|
|
3
|
+
function normalizeSource(source, filename, cwd) {
|
|
4
|
+
if (source.startsWith("@/")) return `src/${source.slice(2)}`;
|
|
5
|
+
if (!source.startsWith(".")) return source;
|
|
6
|
+
return relative(cwd, resolve(dirname(filename), source))
|
|
7
|
+
.split(sep)
|
|
8
|
+
.join("/");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function isTypeOnlyImport(node) {
|
|
12
|
+
if (node.importKind === "type" || node.exportKind === "type") return true;
|
|
13
|
+
const specifiers = node.specifiers ?? [];
|
|
14
|
+
return (
|
|
15
|
+
specifiers.length > 0 &&
|
|
16
|
+
specifiers.every(
|
|
17
|
+
(specifier) =>
|
|
18
|
+
specifier.importKind === "type" || specifier.exportKind === "type",
|
|
19
|
+
)
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const importBoundaries = {
|
|
24
|
+
meta: {
|
|
25
|
+
type: "problem",
|
|
26
|
+
schema: [
|
|
27
|
+
{
|
|
28
|
+
type: "array",
|
|
29
|
+
items: {
|
|
30
|
+
type: "object",
|
|
31
|
+
properties: {
|
|
32
|
+
files: { type: "string" },
|
|
33
|
+
forbid: {
|
|
34
|
+
type: "array",
|
|
35
|
+
items: {
|
|
36
|
+
type: "object",
|
|
37
|
+
properties: {
|
|
38
|
+
source: { type: "string" },
|
|
39
|
+
allowTypeImports: { type: "boolean" },
|
|
40
|
+
message: { type: "string" },
|
|
41
|
+
},
|
|
42
|
+
required: ["source", "allowTypeImports", "message"],
|
|
43
|
+
additionalProperties: false,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
required: ["files", "forbid"],
|
|
48
|
+
additionalProperties: false,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
messages: { forbidden: "{{message}}" },
|
|
53
|
+
},
|
|
54
|
+
create(context) {
|
|
55
|
+
const filePath = relative(context.cwd, context.filename)
|
|
56
|
+
.split(sep)
|
|
57
|
+
.join("/");
|
|
58
|
+
const forbidden = (context.options[0] ?? [])
|
|
59
|
+
.filter((entry) => new RegExp(entry.files).test(filePath))
|
|
60
|
+
.flatMap((entry) =>
|
|
61
|
+
entry.forbid.map((rule) => ({
|
|
62
|
+
...rule,
|
|
63
|
+
pattern: new RegExp(rule.source),
|
|
64
|
+
})),
|
|
65
|
+
);
|
|
66
|
+
if (forbidden.length === 0) return {};
|
|
67
|
+
|
|
68
|
+
const check = (node, isTypeOnly) => {
|
|
69
|
+
const source = normalizeSource(
|
|
70
|
+
node.source.value,
|
|
71
|
+
context.filename,
|
|
72
|
+
context.cwd,
|
|
73
|
+
);
|
|
74
|
+
forbidden
|
|
75
|
+
.filter(
|
|
76
|
+
(rule) =>
|
|
77
|
+
rule.pattern.test(source) && !(rule.allowTypeImports && isTypeOnly),
|
|
78
|
+
)
|
|
79
|
+
.forEach((rule) =>
|
|
80
|
+
context.report({
|
|
81
|
+
node: node.source,
|
|
82
|
+
messageId: "forbidden",
|
|
83
|
+
data: { message: rule.message },
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
ImportDeclaration: (node) => check(node, isTypeOnlyImport(node)),
|
|
90
|
+
ExportNamedDeclaration: (node) =>
|
|
91
|
+
node.source && check(node, isTypeOnlyImport(node)),
|
|
92
|
+
ExportAllDeclaration: (node) => check(node, node.exportKind === "type"),
|
|
93
|
+
ImportExpression: (node) =>
|
|
94
|
+
node.source.type === "Literal" && check({ source: node.source }, false),
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const ESLINT_DIRECTIVE =
|
|
2
|
+
/^\s*eslint-(disable|disable-next-line|disable-line|enable)\b/;
|
|
3
|
+
const REFERENCE_DIRECTIVE = /^\/\s*<reference\s/;
|
|
4
|
+
const TS_EXPECT_ERROR_WITH_REASON = /^\s*@ts-expect-error\s+\S.{2,}/;
|
|
5
|
+
|
|
6
|
+
function isAllowedComment(comment) {
|
|
7
|
+
if (comment.type === "Shebang") return true;
|
|
8
|
+
if (ESLINT_DIRECTIVE.test(comment.value)) return true;
|
|
9
|
+
if (comment.type === "Line" && REFERENCE_DIRECTIVE.test(comment.value))
|
|
10
|
+
return true;
|
|
11
|
+
return TS_EXPECT_ERROR_WITH_REASON.test(comment.value);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const noComments = {
|
|
15
|
+
meta: {
|
|
16
|
+
type: "problem",
|
|
17
|
+
schema: [],
|
|
18
|
+
messages: {
|
|
19
|
+
forbidden:
|
|
20
|
+
"Comments are forbidden: a comment means the name is wrong or the code is doing too much. Rename the variable or function until it says the same thing, or extract a named function. Only eslint directives with `-- reason`, `/// <reference` and `@ts-expect-error <reason>` are allowed.",
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
create(context) {
|
|
24
|
+
return {
|
|
25
|
+
Program() {
|
|
26
|
+
context.sourceCode
|
|
27
|
+
.getAllComments()
|
|
28
|
+
.filter((comment) => !isAllowedComment(comment))
|
|
29
|
+
.forEach((comment) =>
|
|
30
|
+
context.report({ loc: comment.loc, messageId: "forbidden" }),
|
|
31
|
+
);
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const noDefaultExport = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: "problem",
|
|
4
|
+
schema: [],
|
|
5
|
+
messages: {
|
|
6
|
+
defaultExport:
|
|
7
|
+
"`export default` is only for route files and root config files. Elsewhere use a named export (`export function ProjectCard() {}`) so imports are greppable and renames are safe.",
|
|
8
|
+
},
|
|
9
|
+
},
|
|
10
|
+
create(context) {
|
|
11
|
+
return {
|
|
12
|
+
ExportDefaultDeclaration: (node) =>
|
|
13
|
+
context.report({ node, messageId: "defaultExport" }),
|
|
14
|
+
ExportSpecifier(node) {
|
|
15
|
+
if (node.exported.name === "default")
|
|
16
|
+
context.report({ node, messageId: "defaultExport" });
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const noDelete = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: "problem",
|
|
4
|
+
schema: [],
|
|
5
|
+
messages: {
|
|
6
|
+
delete:
|
|
7
|
+
"`delete` mutates the object in place. Build a new object without the key: `const { [key]: _removed, ...rest } = value;` or `Object.fromEntries(Object.entries(value).filter(([name]) => name !== key))`.",
|
|
8
|
+
},
|
|
9
|
+
},
|
|
10
|
+
create(context) {
|
|
11
|
+
return {
|
|
12
|
+
"UnaryExpression[operator='delete']": (node) =>
|
|
13
|
+
context.report({ node, messageId: "delete" }),
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const noEnum = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: "problem",
|
|
4
|
+
schema: [],
|
|
5
|
+
messages: {
|
|
6
|
+
enum: 'An `enum` emits runtime code and breaks tree shaking. Replace it with an `as const` array or object and derive the union: `export const STATUSES = ["idle", "done"] as const; export type Status = (typeof STATUSES)[number];`.',
|
|
7
|
+
},
|
|
8
|
+
},
|
|
9
|
+
create(context) {
|
|
10
|
+
return {
|
|
11
|
+
TSEnumDeclaration: (node) => context.report({ node, messageId: "enum" }),
|
|
12
|
+
};
|
|
13
|
+
},
|
|
14
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const DEFAULT_MAX = 2;
|
|
2
|
+
|
|
3
|
+
export const noHeavyInlineStyles = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: "problem",
|
|
6
|
+
schema: [{ type: "object", properties: { max: { type: "integer" } }, additionalProperties: false }],
|
|
7
|
+
messages: {
|
|
8
|
+
heavy: "This inline `style` object has {{count}} entries (the limit is {{max}}). It is rebuilt on every render and cannot be reused. Move it to `StyleSheet.create({ name: { ... } })` and use `style={styles.name}`.",
|
|
9
|
+
},
|
|
10
|
+
},
|
|
11
|
+
create(context) {
|
|
12
|
+
const max = context.options[0]?.max ?? DEFAULT_MAX;
|
|
13
|
+
return {
|
|
14
|
+
"JSXAttribute[name.name='style'] > JSXExpressionContainer > ObjectExpression": (node) => {
|
|
15
|
+
const count = node.properties.length;
|
|
16
|
+
if (count > max) context.report({ node, messageId: "heavy", data: { count: String(count), max: String(max) } });
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const noLet = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: "problem",
|
|
4
|
+
schema: [],
|
|
5
|
+
messages: {
|
|
6
|
+
let: "`let` invites reassignment. Build the value with an expression (`map`, `filter`, `reduce`, `Object.fromEntries`) or a small function with early returns, and bind it with `const`.",
|
|
7
|
+
},
|
|
8
|
+
},
|
|
9
|
+
create(context) {
|
|
10
|
+
return {
|
|
11
|
+
"VariableDeclaration[kind='let']": (node) => context.report({ node, messageId: "let" }),
|
|
12
|
+
};
|
|
13
|
+
},
|
|
14
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const HEAVY_METHODS = ["filter", "reduce", "sort", "flatMap", "toSorted"];
|
|
2
|
+
|
|
3
|
+
export const noLogicInJsx = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: "problem",
|
|
6
|
+
schema: [],
|
|
7
|
+
messages: {
|
|
8
|
+
method: "`{{name}}` runs inside JSX on every render. Compute the value above the return (a derived const or a hook) and pass the result in.",
|
|
9
|
+
handler: "This handler has a multi-statement body inside JSX. Declare a named handler before the return and pass it: `onPress={handlePress}`.",
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
create(context) {
|
|
13
|
+
return {
|
|
14
|
+
"JSXExpressionContainer CallExpression[callee.type='MemberExpression']": (node) => {
|
|
15
|
+
const name = node.callee.property.type === "Identifier" ? node.callee.property.name : null;
|
|
16
|
+
if (name !== null && HEAVY_METHODS.includes(name)) context.report({ node, messageId: "method", data: { name } });
|
|
17
|
+
},
|
|
18
|
+
"JSXAttribute > JSXExpressionContainer > ArrowFunctionExpression[body.type='BlockStatement']": (node) => {
|
|
19
|
+
if (node.body.body.length > 1) context.report({ node, messageId: "handler" });
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const MEMO_APIS = ["useMemo", "useCallback", "memo"];
|
|
2
|
+
|
|
3
|
+
export const noManualMemoization = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: "problem",
|
|
6
|
+
schema: [],
|
|
7
|
+
messages: {
|
|
8
|
+
memo: "`{{name}}` is redundant: the React Compiler is on and memoizes handlers and values by itself; manual wrappers only hide rule violations the Compiler lint would report. Write the plain function or value.",
|
|
9
|
+
},
|
|
10
|
+
},
|
|
11
|
+
create(context) {
|
|
12
|
+
return {
|
|
13
|
+
ImportDeclaration(node) {
|
|
14
|
+
if (node.source.value !== "react") return;
|
|
15
|
+
node.specifiers
|
|
16
|
+
.filter(
|
|
17
|
+
(specifier) =>
|
|
18
|
+
specifier.type === "ImportSpecifier" &&
|
|
19
|
+
MEMO_APIS.includes(specifier.imported.name),
|
|
20
|
+
)
|
|
21
|
+
.forEach((specifier) =>
|
|
22
|
+
context.report({
|
|
23
|
+
node: specifier,
|
|
24
|
+
messageId: "memo",
|
|
25
|
+
data: { name: specifier.imported.name },
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
},
|
|
29
|
+
MemberExpression(node) {
|
|
30
|
+
if (node.object.type !== "Identifier" || node.object.name !== "React")
|
|
31
|
+
return;
|
|
32
|
+
if (
|
|
33
|
+
node.property.type !== "Identifier" ||
|
|
34
|
+
!MEMO_APIS.includes(node.property.name)
|
|
35
|
+
)
|
|
36
|
+
return;
|
|
37
|
+
context.report({
|
|
38
|
+
node: node.property,
|
|
39
|
+
messageId: "memo",
|
|
40
|
+
data: { name: node.property.name },
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const ALLOWED_OBJECTS = ["exports"];
|
|
2
|
+
|
|
3
|
+
function isAllowedTarget(target) {
|
|
4
|
+
if (target.type !== "MemberExpression") return true;
|
|
5
|
+
if (!target.computed && target.property.name === "current") return true;
|
|
6
|
+
if (target.object.type !== "Identifier") return false;
|
|
7
|
+
if (ALLOWED_OBJECTS.includes(target.object.name)) return true;
|
|
8
|
+
return target.object.name === "module" && target.property.name === "exports";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const noMemberAssignment = {
|
|
12
|
+
meta: {
|
|
13
|
+
type: "problem",
|
|
14
|
+
schema: [],
|
|
15
|
+
messages: {
|
|
16
|
+
assign:
|
|
17
|
+
"Assigning to a property mutates state, props, arguments or a shared object. Return a new value instead: `{ ...value, field: next }` or `value.with(index, next)`. Only `ref.current` and `module.exports` may be assigned.",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
const check = (node, target) =>
|
|
22
|
+
!isAllowedTarget(target) && context.report({ node, messageId: "assign" });
|
|
23
|
+
return {
|
|
24
|
+
AssignmentExpression: (node) => check(node, node.left),
|
|
25
|
+
UpdateExpression: (node) => check(node, node.argument),
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const COLLECTIONS = ["Map", "WeakMap", "Set", "WeakSet"];
|
|
2
|
+
const READONLY_TYPES = ["ReadonlyMap", "ReadonlySet"];
|
|
3
|
+
const FUNCTION_TYPES = ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"];
|
|
4
|
+
|
|
5
|
+
function isModuleLevel(ancestors) {
|
|
6
|
+
return !ancestors.some((ancestor) => FUNCTION_TYPES.includes(ancestor.type));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function isReadonlyDeclaration(node) {
|
|
10
|
+
const parent = node.parent;
|
|
11
|
+
if (parent.type !== "VariableDeclarator" || parent.init !== node) return false;
|
|
12
|
+
const annotation = parent.id.typeAnnotation?.typeAnnotation;
|
|
13
|
+
return (
|
|
14
|
+
annotation !== undefined &&
|
|
15
|
+
annotation.type === "TSTypeReference" &&
|
|
16
|
+
annotation.typeName.type === "Identifier" &&
|
|
17
|
+
READONLY_TYPES.includes(annotation.typeName.name)
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const noModuleCache = {
|
|
22
|
+
meta: {
|
|
23
|
+
type: "problem",
|
|
24
|
+
schema: [],
|
|
25
|
+
messages: {
|
|
26
|
+
moduleCache:
|
|
27
|
+
"A mutable Map/Set at module level is state shared by every request and session, and it survives resets. Create it inside a factory (`createXCache()`) held by its owner, or type it `ReadonlyMap`/`ReadonlySet` if it never changes.",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
create(context) {
|
|
31
|
+
return {
|
|
32
|
+
NewExpression(node) {
|
|
33
|
+
if (node.callee.type !== "Identifier" || !COLLECTIONS.includes(node.callee.name)) return;
|
|
34
|
+
if (!isModuleLevel(context.sourceCode.getAncestors(node))) return;
|
|
35
|
+
if (isReadonlyDeclaration(node)) return;
|
|
36
|
+
context.report({ node, messageId: "moduleCache" });
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const MUTATING_METHODS = [
|
|
2
|
+
"push",
|
|
3
|
+
"pop",
|
|
4
|
+
"shift",
|
|
5
|
+
"unshift",
|
|
6
|
+
"splice",
|
|
7
|
+
"sort",
|
|
8
|
+
"reverse",
|
|
9
|
+
"fill",
|
|
10
|
+
"copyWithin",
|
|
11
|
+
];
|
|
12
|
+
const COPYING_METHODS = [
|
|
13
|
+
"slice",
|
|
14
|
+
"map",
|
|
15
|
+
"filter",
|
|
16
|
+
"concat",
|
|
17
|
+
"flat",
|
|
18
|
+
"flatMap",
|
|
19
|
+
"toSorted",
|
|
20
|
+
"toReversed",
|
|
21
|
+
"with",
|
|
22
|
+
"toSpliced",
|
|
23
|
+
];
|
|
24
|
+
const ARRAY_SYMBOL_NAMES = ["Array", "ReadonlyArray"];
|
|
25
|
+
|
|
26
|
+
function isArrayType(type) {
|
|
27
|
+
if (type.isUnion()) return type.types.every(isArrayType);
|
|
28
|
+
return ARRAY_SYMBOL_NAMES.includes(type.getSymbol()?.getName() ?? "");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isFreshCopy(node) {
|
|
32
|
+
if (node.type === "ArrayExpression") return true;
|
|
33
|
+
if (node.type !== "CallExpression") return false;
|
|
34
|
+
if (
|
|
35
|
+
node.callee.type !== "MemberExpression" ||
|
|
36
|
+
node.callee.property.type !== "Identifier"
|
|
37
|
+
)
|
|
38
|
+
return false;
|
|
39
|
+
const isArrayFrom =
|
|
40
|
+
node.callee.object.type === "Identifier" &&
|
|
41
|
+
node.callee.object.name === "Array" &&
|
|
42
|
+
node.callee.property.name === "from";
|
|
43
|
+
return isArrayFrom || COPYING_METHODS.includes(node.callee.property.name);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const noMutatingArrayMethods = {
|
|
47
|
+
meta: {
|
|
48
|
+
type: "problem",
|
|
49
|
+
schema: [],
|
|
50
|
+
messages: {
|
|
51
|
+
mutating:
|
|
52
|
+
"`{{method}}` mutates the array in place, so React, the Compiler and the query cache see the same reference and render stale data. Use `toSorted`/`toReversed`/`with`, spread (`[...items, item]`), `filter`/`map`, or `reduce` to build a new value. `[...items].sort()` on a fresh copy is fine. A local scratch buffer that never escapes may use `// eslint-disable-next-line kit/no-mutating-array-methods -- <why it is local>`.",
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
create(context) {
|
|
56
|
+
const services = context.sourceCode.parserServices;
|
|
57
|
+
if (!services?.program) return {};
|
|
58
|
+
const checker = services.program.getTypeChecker();
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
CallExpression(node) {
|
|
62
|
+
const { callee } = node;
|
|
63
|
+
if (
|
|
64
|
+
callee.type !== "MemberExpression" ||
|
|
65
|
+
callee.computed ||
|
|
66
|
+
callee.property.type !== "Identifier"
|
|
67
|
+
)
|
|
68
|
+
return;
|
|
69
|
+
if (!MUTATING_METHODS.includes(callee.property.name)) return;
|
|
70
|
+
if (isFreshCopy(callee.object)) return;
|
|
71
|
+
const type = checker.getApparentType(
|
|
72
|
+
checker.getTypeAtLocation(
|
|
73
|
+
services.esTreeNodeToTSNodeMap.get(callee.object),
|
|
74
|
+
),
|
|
75
|
+
);
|
|
76
|
+
if (!isArrayType(type)) return;
|
|
77
|
+
context.report({
|
|
78
|
+
node: callee.property,
|
|
79
|
+
messageId: "mutating",
|
|
80
|
+
data: { method: callee.property.name },
|
|
81
|
+
});
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const noProcessExit = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: "problem",
|
|
4
|
+
schema: [],
|
|
5
|
+
messages: {
|
|
6
|
+
exit: "`process.exit` is called outside the entry point. It skips cleanup and makes this function impossible to test. Return a Result or throw to the entry point (cli, main, server) and exit there once.",
|
|
7
|
+
},
|
|
8
|
+
},
|
|
9
|
+
create(context) {
|
|
10
|
+
return {
|
|
11
|
+
"CallExpression[callee.type='MemberExpression'][callee.object.name='process'][callee.property.name='exit']": (node) =>
|
|
12
|
+
context.report({ node, messageId: "exit" }),
|
|
13
|
+
};
|
|
14
|
+
},
|
|
15
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const STATE_HOOKS = ["useState", "useReducer"];
|
|
2
|
+
const MUTATING = ["push", "pop", "shift", "unshift", "splice", "sort", "reverse", "fill", "copyWithin", "set", "add", "delete", "clear"];
|
|
3
|
+
|
|
4
|
+
function stateNames(program) {
|
|
5
|
+
const names = new Set();
|
|
6
|
+
const visit = (node) => {
|
|
7
|
+
if (node === null || typeof node !== "object") return;
|
|
8
|
+
if (
|
|
9
|
+
node.type === "VariableDeclarator" &&
|
|
10
|
+
node.id.type === "ArrayPattern" &&
|
|
11
|
+
node.id.elements[0]?.type === "Identifier" &&
|
|
12
|
+
node.init?.type === "CallExpression" &&
|
|
13
|
+
node.init.callee.type === "Identifier" &&
|
|
14
|
+
STATE_HOOKS.includes(node.init.callee.name)
|
|
15
|
+
) {
|
|
16
|
+
names.add(node.id.elements[0].name);
|
|
17
|
+
}
|
|
18
|
+
Object.entries(node)
|
|
19
|
+
.filter(([key]) => key !== "parent")
|
|
20
|
+
.forEach(([, child]) => (Array.isArray(child) ? child.forEach(visit) : visit(child)));
|
|
21
|
+
};
|
|
22
|
+
visit(program);
|
|
23
|
+
return names;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function rootName(node) {
|
|
27
|
+
if (node.type === "Identifier") return node.name;
|
|
28
|
+
return node.type === "MemberExpression" ? rootName(node.object) : null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const noStateMutation = {
|
|
32
|
+
meta: {
|
|
33
|
+
type: "problem",
|
|
34
|
+
schema: [],
|
|
35
|
+
messages: {
|
|
36
|
+
mutation: "`{{name}}` is React state and is mutated in place. React compares state by reference, so the component does not re-render and memoization breaks. Build a new value and pass it to the setter: `setItems([...items, item])`, `setUser({ ...user, name })`.",
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
create(context) {
|
|
40
|
+
const names = new Set();
|
|
41
|
+
const report = (node, name) => names.has(name) && context.report({ node, messageId: "mutation", data: { name } });
|
|
42
|
+
return {
|
|
43
|
+
Program(node) {
|
|
44
|
+
stateNames(node).forEach((name) => names.add(name));
|
|
45
|
+
},
|
|
46
|
+
CallExpression(node) {
|
|
47
|
+
const { callee } = node;
|
|
48
|
+
if (callee.type !== "MemberExpression" || callee.computed || callee.property.type !== "Identifier") return;
|
|
49
|
+
if (MUTATING.includes(callee.property.name)) report(node, rootName(callee.object));
|
|
50
|
+
},
|
|
51
|
+
AssignmentExpression(node) {
|
|
52
|
+
if (node.left.type === "MemberExpression") report(node, rootName(node.left));
|
|
53
|
+
},
|
|
54
|
+
UpdateExpression(node) {
|
|
55
|
+
if (node.argument.type === "MemberExpression") report(node, rootName(node.argument));
|
|
56
|
+
},
|
|
57
|
+
UnaryExpression(node) {
|
|
58
|
+
if (node.operator === "delete" && node.argument.type === "MemberExpression") report(node, rootName(node.argument));
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const BROWSER_GLOBALS = ["window", "document", "localStorage", "sessionStorage", "navigator", "location"];
|
|
2
|
+
const HOOK_NAME = /^use[A-Z0-9]/;
|
|
3
|
+
const HANDLER_PROP = /^on[A-Z]/;
|
|
4
|
+
|
|
5
|
+
function directive(program) {
|
|
6
|
+
return program.body.find((statement) => statement.type === "ExpressionStatement" && statement.directive === "use client");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const noUnnecessaryUseClient = {
|
|
10
|
+
meta: {
|
|
11
|
+
type: "suggestion",
|
|
12
|
+
schema: [],
|
|
13
|
+
messages: {
|
|
14
|
+
unnecessary: "This file is marked 'use client' but uses no hooks, event handlers or browser APIs. The directive ships this file and its imports to the browser. Delete it, or move the interactive part into a small client component.",
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
create(context) {
|
|
18
|
+
const uses = { client: false };
|
|
19
|
+
const markClient = () => {
|
|
20
|
+
uses.client = true;
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
CallExpression(node) {
|
|
24
|
+
if (node.callee.type === "Identifier" && HOOK_NAME.test(node.callee.name)) markClient();
|
|
25
|
+
if (node.callee.type === "MemberExpression" && node.callee.property.type === "Identifier" && HOOK_NAME.test(node.callee.property.name)) markClient();
|
|
26
|
+
},
|
|
27
|
+
JSXAttribute(node) {
|
|
28
|
+
if (node.name.type === "JSXIdentifier" && HANDLER_PROP.test(node.name.name)) markClient();
|
|
29
|
+
},
|
|
30
|
+
Identifier(node) {
|
|
31
|
+
if (BROWSER_GLOBALS.includes(node.name) && node.parent.type !== "Property" && node.parent.type !== "MemberExpression") markClient();
|
|
32
|
+
if (BROWSER_GLOBALS.includes(node.name) && node.parent.type === "MemberExpression" && node.parent.object === node) markClient();
|
|
33
|
+
},
|
|
34
|
+
"Program:exit": (program) => {
|
|
35
|
+
const found = directive(program);
|
|
36
|
+
if (found !== undefined && !uses.client) context.report({ node: found, messageId: "unnecessary" });
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const IMPURE_CALLS = ["Math.random", "Date.now", "performance.now", "crypto.randomUUID", "fetch"];
|
|
2
|
+
const BROWSER_GLOBALS = ["window", "document", "localStorage", "sessionStorage", "navigator"];
|
|
3
|
+
const COMPONENT_NAME = /^[A-Z]/;
|
|
4
|
+
|
|
5
|
+
function calleeText(callee) {
|
|
6
|
+
if (callee.type === "Identifier") return callee.name;
|
|
7
|
+
if (callee.type === "MemberExpression" && !callee.computed && callee.object.type === "Identifier" && callee.property.type === "Identifier") return `${callee.object.name}.${callee.property.name}`;
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function isFunction(node) {
|
|
12
|
+
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function functionName(fn) {
|
|
16
|
+
if (fn.id) return fn.id.name;
|
|
17
|
+
return fn.parent.type === "VariableDeclarator" && fn.parent.id.type === "Identifier" ? fn.parent.id.name : null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const pureComponents = {
|
|
21
|
+
meta: {
|
|
22
|
+
type: "problem",
|
|
23
|
+
schema: [],
|
|
24
|
+
messages: {
|
|
25
|
+
impure: "`{{what}}` runs during render of a component. Render must return the same output for the same props and state, so clocks, randomness, network and browser globals do not belong here. Move it into a hook, an effect inside a hook, or an event handler, or pass the value in as a prop or state.",
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
create(context) {
|
|
29
|
+
const inRender = (node) => {
|
|
30
|
+
const owner = context.sourceCode.getAncestors(node).findLast(isFunction);
|
|
31
|
+
const name = owner === undefined ? null : functionName(owner);
|
|
32
|
+
return name !== null && COMPONENT_NAME.test(name);
|
|
33
|
+
};
|
|
34
|
+
const report = (node, what) => inRender(node) && context.report({ node, messageId: "impure", data: { what } });
|
|
35
|
+
return {
|
|
36
|
+
CallExpression(node) {
|
|
37
|
+
const text = calleeText(node.callee);
|
|
38
|
+
if (text !== null && IMPURE_CALLS.includes(text)) report(node, text);
|
|
39
|
+
},
|
|
40
|
+
NewExpression(node) {
|
|
41
|
+
if (node.callee.type === "Identifier" && node.callee.name === "Date" && node.arguments.length === 0) report(node, "new Date()");
|
|
42
|
+
},
|
|
43
|
+
MemberExpression(node) {
|
|
44
|
+
if (node.object.type === "Identifier" && BROWSER_GLOBALS.includes(node.object.name)) report(node, node.object.name);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
},
|
|
48
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const KEY_NAME = /_QUERY_KEY$/;
|
|
2
|
+
const CACHE_READERS = ["getQueryData", "setQueryData", "getQueryState"];
|
|
3
|
+
|
|
4
|
+
function isKeyConstant(node) {
|
|
5
|
+
return node.type === "Identifier" && KEY_NAME.test(node.name);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function isValidQueryKey(node) {
|
|
9
|
+
if (isKeyConstant(node)) return true;
|
|
10
|
+
return (
|
|
11
|
+
node.type === "ArrayExpression" &&
|
|
12
|
+
node.elements.length > 0 &&
|
|
13
|
+
node.elements[0] !== null &&
|
|
14
|
+
isKeyConstant(node.elements[0])
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const queryKeyConstant = {
|
|
19
|
+
meta: {
|
|
20
|
+
type: "problem",
|
|
21
|
+
schema: [],
|
|
22
|
+
messages: {
|
|
23
|
+
literal:
|
|
24
|
+
"A query key starts with a `<NAME>_QUERY_KEY` constant exported from the hook that owns the query, followed by every id the data depends on (`[PROJECT_QUERY_KEY, projectId]`). A string or a hand-written array in the call site drifts from the hook and breaks invalidation.",
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
create(context) {
|
|
28
|
+
const check = (node) =>
|
|
29
|
+
!isValidQueryKey(node) && context.report({ node, messageId: "literal" });
|
|
30
|
+
return {
|
|
31
|
+
Property(node) {
|
|
32
|
+
if (
|
|
33
|
+
node.key.type === "Identifier" &&
|
|
34
|
+
node.key.name === "queryKey" &&
|
|
35
|
+
node.parent.type === "ObjectExpression"
|
|
36
|
+
)
|
|
37
|
+
check(node.value);
|
|
38
|
+
},
|
|
39
|
+
CallExpression(node) {
|
|
40
|
+
const { callee } = node;
|
|
41
|
+
if (
|
|
42
|
+
callee.type !== "MemberExpression" ||
|
|
43
|
+
callee.property.type !== "Identifier"
|
|
44
|
+
)
|
|
45
|
+
return;
|
|
46
|
+
if (
|
|
47
|
+
CACHE_READERS.includes(callee.property.name) &&
|
|
48
|
+
node.arguments.length > 0
|
|
49
|
+
)
|
|
50
|
+
check(node.arguments[0]);
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
};
|