@mapringg/standard 0.0.0-stage → 1.15.3
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/.claude/skills/adopt-standard/SKILL.md +189 -0
- package/.claude/skills/capture/SKILL.md +27 -0
- package/.claude/skills/pr/LICENSE +21 -0
- package/.claude/skills/pr/SKILL.md +170 -0
- package/.claude/skills/pr/UPSTREAM.md +4 -0
- package/.claude/skills/review-standard/SKILL.md +12 -0
- package/.claude/skills/test-audit/SKILL.md +25 -0
- package/.claude/skills/upgrade-standard/SKILL.md +25 -0
- package/LICENSE +21 -0
- package/README.md +10 -2
- package/bin/check-pr.mjs +11 -0
- package/bin/upload-evidence.mjs +120 -0
- package/knip.json +25 -0
- package/oxfmt.json +8 -0
- package/oxlint.json +464 -0
- package/package.json +63 -4
- package/packages/create-mapringg-repo/LICENSE +21 -0
- package/packages/create-mapringg-repo/cli.mjs +323 -0
- package/packages/create-mapringg-repo/github.mjs +128 -0
- package/packages/create-mapringg-repo/package.json +25 -0
- package/packages/create-mapringg-repo/release.mjs +52 -0
- package/packages/create-mapringg-repo/setup.mjs +173 -0
- package/src/plugin/agent-docs.mjs +240 -0
- package/src/plugin/design-system.mjs +75 -0
- package/src/plugin/feature-imports.mjs +38 -0
- package/src/plugin/hono.mjs +186 -0
- package/src/plugin/index.mjs +42 -0
- package/src/plugin/markdown-lines.mjs +35 -0
- package/src/plugin/next-data.mjs +124 -0
- package/src/plugin/next-routes.mjs +63 -0
- package/src/plugin/no-chained-assertions.mjs +22 -0
- package/src/plugin/no-comments.mjs +23 -0
- package/src/plugin/no-return-types.mjs +48 -0
- package/src/plugin/no-typeof.mjs +27 -0
- package/src/plugin/owners.mjs +428 -0
- package/src/plugin/params.mjs +75 -0
- package/src/plugin/paths.mjs +192 -0
- package/src/plugin/pr-check.mjs +141 -0
- package/src/plugin/query-client.mjs +49 -0
- package/src/plugin/reducer.mjs +84 -0
- package/src/plugin/repo-shape.mjs +351 -0
- package/src/plugin/repository-docs.mjs +119 -0
- package/src/plugin/server-actions.mjs +60 -0
- package/src/plugin/tests.mjs +222 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { locate } from "./paths.mjs";
|
|
2
|
+
|
|
3
|
+
const METHODS = new Set(["get", "post", "patch", "delete"]);
|
|
4
|
+
const LOOSE = new Set(["on", "all"]);
|
|
5
|
+
const CHAIN = new Set([...METHODS, ...LOOSE, "put", "route", "use", "onError", "notFound", "basePath"]);
|
|
6
|
+
const RAW = new Set(["query", "queries", "param", "json", "text", "formData", "parseBody", "arrayBuffer", "blob"]);
|
|
7
|
+
const RAW_BODY = new Set(["text", "arrayBuffer", "blob"]);
|
|
8
|
+
const STATUS = new Set(["json", "body"]);
|
|
9
|
+
const STREAMS = new Set(["streamSSE", "streamText", "stream"]);
|
|
10
|
+
const OPENAPI = new Set(["openAPIRouteHandler", "openAPISpecs", "generateSpecs", "Scalar"]);
|
|
11
|
+
const FUNCTION = new Set(["FunctionExpression", "ArrowFunctionExpression"]);
|
|
12
|
+
const HANDLERS = new Set(["createHandlers", "createMiddleware"]);
|
|
13
|
+
const KEBAB = "[a-z]+(?:-[a-z]+)*";
|
|
14
|
+
const MOUNT = new RegExp(`^/api/${KEBAB}$`);
|
|
15
|
+
const PATH = new RegExp(`^/(?:${KEBAB}|:id)?$`);
|
|
16
|
+
const POST_PATH = new RegExp(`^/(?:${KEBAB}|:id(?:/${KEBAB})?)?$`);
|
|
17
|
+
const OAUTH_PATH = new RegExp(`^/(?:\\.well-known(?:/${KEBAB})+|mcp-oauth/${KEBAB}|authorize|token|register|revoke)$`);
|
|
18
|
+
const OAUTH_MOUNT = /^\/(?:\.well-known|mcp-oauth)?$/;
|
|
19
|
+
const MAX_LINES = 10;
|
|
20
|
+
|
|
21
|
+
function property(node) {
|
|
22
|
+
return node?.type === "MemberExpression" && !node.computed ? node.property.name : null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function calls(node, name) {
|
|
26
|
+
return node?.type === "CallExpression" && node.callee.type === "Identifier" && node.callee.name === name;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function stringValue(node) {
|
|
30
|
+
if (node?.type === "Literal" && typeof node.value === "string") return node.value;
|
|
31
|
+
if (node?.type === "TemplateLiteral" && node.expressions.length === 0) return node.quasis[0].value.cooked;
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isDevelopmentTest(test) {
|
|
36
|
+
if (test?.type !== "BinaryExpression" || !["===", "!=="].includes(test.operator)) return null;
|
|
37
|
+
const sides = [test.left, test.right];
|
|
38
|
+
const env = sides.some((side) => property(side) === "NODE_ENV");
|
|
39
|
+
const value = sides.some((side) => stringValue(side) === "development");
|
|
40
|
+
return env && value ? test.operator : null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function inDevelopmentBranch(node) {
|
|
44
|
+
for (let child = node, current = node.parent; current; child = current, current = current.parent) {
|
|
45
|
+
if (current.type === "IfStatement" || current.type === "ConditionalExpression") {
|
|
46
|
+
const operator = isDevelopmentTest(current.test);
|
|
47
|
+
if (operator === "===" && child === current.consequent) return true;
|
|
48
|
+
if (operator === "!==" && child === current.alternate) return true;
|
|
49
|
+
}
|
|
50
|
+
if (
|
|
51
|
+
current.type === "LogicalExpression" &&
|
|
52
|
+
current.operator === "&&" &&
|
|
53
|
+
child === current.right &&
|
|
54
|
+
isDevelopmentTest(current.left) === "==="
|
|
55
|
+
)
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function inStream(node) {
|
|
62
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
63
|
+
if (current.type === "CallExpression" && current.callee.type === "Identifier" && STREAMS.has(current.callee.name))
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function contextIndex(fn) {
|
|
70
|
+
const call = fn.parent;
|
|
71
|
+
if (call?.type !== "CallExpression" || !call.arguments.includes(fn)) return -1;
|
|
72
|
+
const name = call.callee.type === "Identifier" ? call.callee.name : property(call.callee);
|
|
73
|
+
if (name === "onError") return 1;
|
|
74
|
+
return CHAIN.has(name) || HANDLERS.has(name) ? 0 : -1;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function isContext(node) {
|
|
78
|
+
if (node?.type !== "Identifier") return false;
|
|
79
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
80
|
+
if (!FUNCTION.has(current.type)) continue;
|
|
81
|
+
const index = current.params.findIndex((param) => param.type === "Identifier" && param.name === node.name);
|
|
82
|
+
if (index !== -1) return index === contextIndex(current);
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function chainRoot(node) {
|
|
88
|
+
let current = node;
|
|
89
|
+
while (current?.type === "CallExpression" && current.callee.type === "MemberExpression") {
|
|
90
|
+
if (property(current.callee) === "createApp") return true;
|
|
91
|
+
current = current.callee.object;
|
|
92
|
+
}
|
|
93
|
+
return current?.type === "Identifier" ? current.name : false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isApp(node) {
|
|
97
|
+
if (node?.type === "NewExpression") return node.callee.type === "Identifier" && node.callee.name === "Hono";
|
|
98
|
+
return chainRoot(node) === true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function validates(node, target) {
|
|
102
|
+
return calls(node, "v") && stringValue(node.arguments[0]) === target;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export default {
|
|
106
|
+
meta: {
|
|
107
|
+
type: "problem",
|
|
108
|
+
docs: { description: "Hono routers are one chain of small documented handlers with explicit statuses." },
|
|
109
|
+
},
|
|
110
|
+
create(context) {
|
|
111
|
+
const where = locate(context.filename);
|
|
112
|
+
if (where?.kind !== "hono") return {};
|
|
113
|
+
const routes = /^src\/features\/[^/]+\/routes\.ts$/.test(where.local);
|
|
114
|
+
const app = where.local === "src/app.ts";
|
|
115
|
+
const webhook = where.feature === "webhooks";
|
|
116
|
+
const protocol = where.feature === "oauth";
|
|
117
|
+
const report = (node, message) => context.report({ node, message });
|
|
118
|
+
const routers = new Set();
|
|
119
|
+
const oauthRouters = new Set();
|
|
120
|
+
const isRouter = (object) => {
|
|
121
|
+
const root = chainRoot(object);
|
|
122
|
+
return root === true || routers.has(root);
|
|
123
|
+
};
|
|
124
|
+
const route = (node, method) => {
|
|
125
|
+
if (LOOSE.has(method)) {
|
|
126
|
+
report(node.callee.property, "Register each route with get, post, patch or delete.");
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const path = stringValue(node.arguments[0]);
|
|
130
|
+
if (path === null) report(node.arguments[0] ?? node, "Write the route path as a string literal.");
|
|
131
|
+
else if (!(method === "post" ? POST_PATH : PATH).test(path) && !(protocol && OAUTH_PATH.test(path)))
|
|
132
|
+
report(node.arguments[0], "Use /, /:id, /<kebab-name> or POST /:id/<kebab-verb> as the path.");
|
|
133
|
+
if (!node.arguments.some((arg) => calls(arg, "describeRoute")))
|
|
134
|
+
report(node.callee.property, "Add describeRoute with the response schemas from schema.ts to this route.");
|
|
135
|
+
if (
|
|
136
|
+
node.arguments.some((arg) => validates(arg, "form")) &&
|
|
137
|
+
!node.arguments.some((arg) => calls(arg, "bodyLimit"))
|
|
138
|
+
)
|
|
139
|
+
report(node.callee.property, 'Add bodyLimit({ maxSize }) before v("form", schema) to cap the upload size.');
|
|
140
|
+
if (protocol) return;
|
|
141
|
+
const handler = node.arguments.at(-1);
|
|
142
|
+
if (!FUNCTION.has(handler?.type)) report(handler ?? node, "Write the handler inline as the last argument.");
|
|
143
|
+
else if (handler.loc.end.line - handler.loc.start.line + 1 > MAX_LINES)
|
|
144
|
+
report(handler, `Move the logic into db.ts or service.ts so the handler fits in ${MAX_LINES} lines.`);
|
|
145
|
+
};
|
|
146
|
+
return {
|
|
147
|
+
ImportDeclaration(node) {
|
|
148
|
+
if (node.source.value.startsWith("#/features/oauth/"))
|
|
149
|
+
for (const specifier of node.specifiers) oauthRouters.add(specifier.local.name);
|
|
150
|
+
},
|
|
151
|
+
VariableDeclarator(node) {
|
|
152
|
+
if (node.id.type === "Identifier" && isApp(node.init)) routers.add(node.id.name);
|
|
153
|
+
},
|
|
154
|
+
ExpressionStatement(node) {
|
|
155
|
+
const call = node.expression;
|
|
156
|
+
if (
|
|
157
|
+
(routes || app) &&
|
|
158
|
+
call.type === "CallExpression" &&
|
|
159
|
+
CHAIN.has(property(call.callee)) &&
|
|
160
|
+
call.callee.object.type === "Identifier" &&
|
|
161
|
+
routers.has(call.callee.object.name)
|
|
162
|
+
)
|
|
163
|
+
report(node, "Build each router as one chain: factory.createApp().get(...).post(...).");
|
|
164
|
+
},
|
|
165
|
+
CallExpression(node) {
|
|
166
|
+
const method = property(node.callee);
|
|
167
|
+
const object = node.callee.object;
|
|
168
|
+
const onContext = isContext(object);
|
|
169
|
+
if (STATUS.has(method) && onContext && node.arguments.length < 2 && !inStream(node))
|
|
170
|
+
report(node, `Pass the status explicitly: c.${method}(body, 200).`);
|
|
171
|
+
if (RAW.has(method) && property(object) === "req" && !(webhook && RAW_BODY.has(method)))
|
|
172
|
+
report(node, "Validate with v() from #/lib/validate and read the input with c.req.valid().");
|
|
173
|
+
if (node.callee.type === "Identifier" && OPENAPI.has(node.callee.name) && !inDevelopmentBranch(node))
|
|
174
|
+
report(node, 'Mount the OpenAPI spec and docs only inside env.NODE_ENV === "development".');
|
|
175
|
+
if (method === "route" && app) {
|
|
176
|
+
const path = stringValue(node.arguments[0]);
|
|
177
|
+
const router = node.arguments[1];
|
|
178
|
+
const oauth = router?.type === "Identifier" && oauthRouters.has(router.name);
|
|
179
|
+
if (path === null || !(MOUNT.test(path) || (oauth && OAUTH_MOUNT.test(path))))
|
|
180
|
+
report(node.arguments[0] ?? node, "Mount routers at /api/<plural-kebab>.");
|
|
181
|
+
}
|
|
182
|
+
if (routes && (METHODS.has(method) || LOOSE.has(method)) && isRouter(object)) route(node, method);
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
},
|
|
186
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import agentDocs from "./agent-docs.mjs";
|
|
2
|
+
import designSystem from "./design-system.mjs";
|
|
3
|
+
import featureImports from "./feature-imports.mjs";
|
|
4
|
+
import hono from "./hono.mjs";
|
|
5
|
+
import nextData from "./next-data.mjs";
|
|
6
|
+
import nextRoutes from "./next-routes.mjs";
|
|
7
|
+
import noChainedAssertions from "./no-chained-assertions.mjs";
|
|
8
|
+
import noComments from "./no-comments.mjs";
|
|
9
|
+
import noReturnTypes from "./no-return-types.mjs";
|
|
10
|
+
import noTypeof from "./no-typeof.mjs";
|
|
11
|
+
import owners from "./owners.mjs";
|
|
12
|
+
import params from "./params.mjs";
|
|
13
|
+
import prCheck from "./pr-check.mjs";
|
|
14
|
+
import queryClient from "./query-client.mjs";
|
|
15
|
+
import reducer from "./reducer.mjs";
|
|
16
|
+
import repoShape from "./repo-shape.mjs";
|
|
17
|
+
import serverActions from "./server-actions.mjs";
|
|
18
|
+
import tests from "./tests.mjs";
|
|
19
|
+
|
|
20
|
+
export default {
|
|
21
|
+
meta: { name: "standard" },
|
|
22
|
+
rules: {
|
|
23
|
+
"agent-docs": agentDocs,
|
|
24
|
+
"design-system": designSystem,
|
|
25
|
+
"feature-imports": featureImports,
|
|
26
|
+
hono,
|
|
27
|
+
"next-data": nextData,
|
|
28
|
+
"next-routes": nextRoutes,
|
|
29
|
+
"no-chained-assertions": noChainedAssertions,
|
|
30
|
+
"no-comments": noComments,
|
|
31
|
+
"no-return-types": noReturnTypes,
|
|
32
|
+
"no-typeof": noTypeof,
|
|
33
|
+
owners,
|
|
34
|
+
params,
|
|
35
|
+
"pr-check": prCheck,
|
|
36
|
+
"query-client": queryClient,
|
|
37
|
+
reducer,
|
|
38
|
+
"repo-shape": repoShape,
|
|
39
|
+
"server-actions": serverActions,
|
|
40
|
+
tests,
|
|
41
|
+
},
|
|
42
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const BEGIN = /^<!-- BEGIN:([\w-]+) -->\s*$/;
|
|
2
|
+
const FENCE_OPEN = /^ {0,3}(?:(`{3,})[^`]*|(~{3,}).*)$/;
|
|
3
|
+
|
|
4
|
+
export function classify(text) {
|
|
5
|
+
const lines = text.split(/\r?\n/);
|
|
6
|
+
const kinds = [];
|
|
7
|
+
let fence = null;
|
|
8
|
+
let generated = null;
|
|
9
|
+
for (const [index, line] of lines.entries()) {
|
|
10
|
+
if (fence) {
|
|
11
|
+
kinds.push("fence");
|
|
12
|
+
if (new RegExp(`^ {0,3}${fence[0] === "`" ? "`" : "~"}{${fence.length},}\\s*$`).test(line)) fence = null;
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
const open = FENCE_OPEN.exec(line);
|
|
16
|
+
if (open) {
|
|
17
|
+
fence = open[1] ?? open[2];
|
|
18
|
+
kinds.push("fence");
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (generated) {
|
|
22
|
+
kinds.push("generated");
|
|
23
|
+
if (line.trim() === `<!-- END:${generated} -->`) generated = null;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const begin = BEGIN.exec(line.trim());
|
|
27
|
+
if (begin && lines.slice(index + 1).some((next) => next.trim() === `<!-- END:${begin[1]} -->`)) {
|
|
28
|
+
generated = begin[1];
|
|
29
|
+
kinds.push("generated");
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
kinds.push("prose");
|
|
33
|
+
}
|
|
34
|
+
return lines.map((line, index) => ({ line, kind: kinds[index] }));
|
|
35
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { moduleVisitors, stem } from "./paths.mjs";
|
|
2
|
+
|
|
3
|
+
const FUNCTION = ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"];
|
|
4
|
+
const SERVER_MODULE = /\.server(?:\.[cm]?[jt]sx?)?$/;
|
|
5
|
+
const TAGS = new Set(["cacheTag", "updateTag", "revalidateTag"]);
|
|
6
|
+
|
|
7
|
+
function directiveList(body) {
|
|
8
|
+
const found = [];
|
|
9
|
+
for (const node of Array.isArray(body) ? body : []) {
|
|
10
|
+
if (node.type !== "ExpressionStatement" || typeof node.directive !== "string") break;
|
|
11
|
+
found.push(node.directive);
|
|
12
|
+
}
|
|
13
|
+
return found;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function calleeName(node) {
|
|
17
|
+
if (node.callee.type === "Identifier") return node.callee.name;
|
|
18
|
+
if (node.callee.type === "MemberExpression" && !node.callee.computed) return node.callee.property.name;
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function enclosingFunction(node) {
|
|
23
|
+
for (let current = node.parent; current; current = current.parent)
|
|
24
|
+
if (FUNCTION.includes(current.type)) return current;
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isExported(node) {
|
|
29
|
+
const parent = node.parent;
|
|
30
|
+
if (parent?.type === "ExportNamedDeclaration" || parent?.type === "ExportDefaultDeclaration") return true;
|
|
31
|
+
return parent?.type === "VariableDeclarator" && parent.parent?.parent?.type === "ExportNamedDeclaration";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default {
|
|
35
|
+
meta: {
|
|
36
|
+
type: "problem",
|
|
37
|
+
docs: {
|
|
38
|
+
description:
|
|
39
|
+
"Cached functions tag and time their cache; tags live in <feature>-cache.ts; server files import server-only.",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
create(context) {
|
|
43
|
+
const name = stem(context.filename);
|
|
44
|
+
const cacheFile = name.endsWith("-cache");
|
|
45
|
+
const cached = new Map();
|
|
46
|
+
const routers = new Set();
|
|
47
|
+
let fileCached = false;
|
|
48
|
+
let serverOnly = false;
|
|
49
|
+
let client = false;
|
|
50
|
+
const enter = (node) => {
|
|
51
|
+
const own = directiveList(node.body?.body).some((directive) => directive.startsWith("use cache"));
|
|
52
|
+
if (!own && !(fileCached && !enclosingFunction(node))) return;
|
|
53
|
+
cached.set(node, new Set());
|
|
54
|
+
if (isExported(node) && node.params.some((param) => /^user(?:Id)?$/i.test(param.name ?? ""))) {
|
|
55
|
+
context.report({
|
|
56
|
+
node,
|
|
57
|
+
message:
|
|
58
|
+
"Resolve the user inside the function with 'use cache: private' instead of exporting a cached function that takes a user id.",
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const nearestCached = (node) => {
|
|
63
|
+
for (let current = node.parent; current; current = current.parent) if (cached.has(current)) return current;
|
|
64
|
+
return null;
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
Program(program) {
|
|
68
|
+
const top = directiveList(program.body);
|
|
69
|
+
fileCached = top.some((directive) => directive.startsWith("use cache"));
|
|
70
|
+
client = top.includes("use client");
|
|
71
|
+
},
|
|
72
|
+
...Object.fromEntries(FUNCTION.map((type) => [type, enter])),
|
|
73
|
+
...moduleVisitors((source, node, specifiers) => {
|
|
74
|
+
if (source === "server-only") serverOnly = true;
|
|
75
|
+
const typeOnly =
|
|
76
|
+
node.importKind === "type" ||
|
|
77
|
+
node.exportKind === "type" ||
|
|
78
|
+
(specifiers.length > 0 &&
|
|
79
|
+
specifiers.every((item) => item.importKind === "type" || item.exportKind === "type"));
|
|
80
|
+
if (client && !typeOnly && SERVER_MODULE.test(source))
|
|
81
|
+
context.report({
|
|
82
|
+
node,
|
|
83
|
+
message:
|
|
84
|
+
"Load the data in a Server Component and pass it to this client component as props instead of importing *.server.ts.",
|
|
85
|
+
});
|
|
86
|
+
}),
|
|
87
|
+
VariableDeclarator(node) {
|
|
88
|
+
if (
|
|
89
|
+
node.id.type === "Identifier" &&
|
|
90
|
+
node.init?.type === "CallExpression" &&
|
|
91
|
+
calleeName(node.init) === "useRouter"
|
|
92
|
+
)
|
|
93
|
+
routers.add(node.id.name);
|
|
94
|
+
},
|
|
95
|
+
CallExpression(node) {
|
|
96
|
+
const called = calleeName(node);
|
|
97
|
+
if (called === "refresh" && node.callee.type === "MemberExpression") {
|
|
98
|
+
const object = node.callee.object;
|
|
99
|
+
const named = object.type === "Identifier" && object.name !== "router" && routers.has(object.name);
|
|
100
|
+
if (named || (object.type === "CallExpression" && calleeName(object) === "useRouter")) {
|
|
101
|
+
context.report({
|
|
102
|
+
node,
|
|
103
|
+
message: "Update the cache in the Server Action with updateTag or refresh() instead of router.refresh().",
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const owner = nearestCached(node);
|
|
108
|
+
if (owner && (called === "cacheTag" || called === "cacheLife")) cached.get(owner).add(called);
|
|
109
|
+
const tag = node.arguments[0]?.type;
|
|
110
|
+
if (TAGS.has(called) && !cacheFile && (tag === "Literal" || tag === "TemplateLiteral")) {
|
|
111
|
+
context.report({ node, message: "Import the cache tag from <feature>-cache.ts." });
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
"Program:exit"(program) {
|
|
115
|
+
for (const [node, calls] of cached) {
|
|
116
|
+
if (!calls.has("cacheTag") || !calls.has("cacheLife"))
|
|
117
|
+
context.report({ node, message: "Call cacheTag and cacheLife inside this 'use cache' function." });
|
|
118
|
+
}
|
|
119
|
+
if (name.endsWith(".server") && !serverOnly)
|
|
120
|
+
context.report({ node: program, message: '*.server.ts files import "server-only" first.' });
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { locate, stem } from "./paths.mjs";
|
|
2
|
+
|
|
3
|
+
function defaultFunction(program) {
|
|
4
|
+
const exported = program.body.find((node) => node.type === "ExportDefaultDeclaration")?.declaration;
|
|
5
|
+
if (!exported) return null;
|
|
6
|
+
if (exported.type !== "Identifier") return exported;
|
|
7
|
+
for (const node of program.body) {
|
|
8
|
+
const declaration = node.type === "ExportNamedDeclaration" ? node.declaration : node;
|
|
9
|
+
if (declaration?.type === "FunctionDeclaration" && declaration.id?.name === exported.name) return declaration;
|
|
10
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
11
|
+
const found = declaration.declarations.find((item) => item.id.name === exported.name);
|
|
12
|
+
if (found) return found.init;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function insideFunction(node) {
|
|
19
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
20
|
+
if (["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"].includes(current.type)) return true;
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default {
|
|
26
|
+
meta: {
|
|
27
|
+
type: "problem",
|
|
28
|
+
docs: { description: "Next pages and layouts are synchronous; pages take PageProps." },
|
|
29
|
+
},
|
|
30
|
+
create(context) {
|
|
31
|
+
const where = locate(context.filename);
|
|
32
|
+
if (where?.kind !== "next" || !where.local.startsWith("src/app/")) return {};
|
|
33
|
+
const file = stem(where.name);
|
|
34
|
+
if (file !== "page" && file !== "layout") return {};
|
|
35
|
+
const label = `${file}.tsx`;
|
|
36
|
+
return {
|
|
37
|
+
AwaitExpression(node) {
|
|
38
|
+
if (!insideFunction(node))
|
|
39
|
+
context.report({
|
|
40
|
+
node,
|
|
41
|
+
message: `Move the top-level await out of ${label} into a feature component inside <Section>.`,
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
Program(program) {
|
|
45
|
+
const component = defaultFunction(program);
|
|
46
|
+
if (!component) return;
|
|
47
|
+
if (component.async)
|
|
48
|
+
context.report({
|
|
49
|
+
node: component,
|
|
50
|
+
message: `Make ${label} synchronous and move async work into a feature component inside <Section>.`,
|
|
51
|
+
});
|
|
52
|
+
const props = component.params?.[0];
|
|
53
|
+
if (
|
|
54
|
+
file === "page" &&
|
|
55
|
+
props &&
|
|
56
|
+
!context.sourceCode.getText(props.typeAnnotation ?? props).includes("PageProps")
|
|
57
|
+
) {
|
|
58
|
+
context.report({ node: props, message: 'Type page props as PageProps<"/route">.' });
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const ASSERTIONS = new Set(["TSAsExpression", "TSTypeAssertion"]);
|
|
2
|
+
|
|
3
|
+
function isConst(node) {
|
|
4
|
+
return node.typeAnnotation?.type === "TSTypeReference" && node.typeAnnotation.typeName?.name === "const";
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
meta: {
|
|
9
|
+
type: "problem",
|
|
10
|
+
docs: { description: "No chained type assertions such as x as unknown as Y." },
|
|
11
|
+
},
|
|
12
|
+
create(context) {
|
|
13
|
+
const check = (node) => {
|
|
14
|
+
if (!ASSERTIONS.has(node.expression.type) || isConst(node) || isConst(node.expression)) return;
|
|
15
|
+
context.report({
|
|
16
|
+
node,
|
|
17
|
+
message: "Parse the value with a zod schema at its boundary instead of chaining type assertions.",
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
return { TSAsExpression: check, TSTypeAssertion: check };
|
|
21
|
+
},
|
|
22
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isGenerated } from "./paths.mjs";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
meta: {
|
|
5
|
+
type: "problem",
|
|
6
|
+
docs: { description: "Code carries no comments of any kind, including disable directives." },
|
|
7
|
+
},
|
|
8
|
+
create(context) {
|
|
9
|
+
if (isGenerated(context.filename)) return {};
|
|
10
|
+
return {
|
|
11
|
+
Program() {
|
|
12
|
+
for (const comment of context.sourceCode.getAllComments()) {
|
|
13
|
+
if (comment.type === "Shebang") continue;
|
|
14
|
+
context.report({
|
|
15
|
+
loc: comment.loc,
|
|
16
|
+
message:
|
|
17
|
+
"Delete the comment and name things so the code explains itself, with lasting context in README.md or AGENTS.md.",
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { isGenerated } from "./paths.mjs";
|
|
2
|
+
|
|
3
|
+
function binding(node) {
|
|
4
|
+
if (node.id) return { owner: node, name: node.id.name };
|
|
5
|
+
const parent = node.parent;
|
|
6
|
+
if (parent?.type === "VariableDeclarator" && parent.init === node && parent.id.type === "Identifier")
|
|
7
|
+
return { owner: parent, name: parent.id.name };
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function recursive(context, node) {
|
|
12
|
+
const found = binding(node);
|
|
13
|
+
if (!found) return false;
|
|
14
|
+
const [start, end] = node.body.range;
|
|
15
|
+
return context.sourceCode
|
|
16
|
+
.getDeclaredVariables(found.owner)
|
|
17
|
+
.filter((variable) => variable.name === found.name)
|
|
18
|
+
.some((variable) =>
|
|
19
|
+
variable.references.some(({ identifier }) => {
|
|
20
|
+
const call = identifier.parent;
|
|
21
|
+
return (
|
|
22
|
+
call?.type === "CallExpression" &&
|
|
23
|
+
call.callee === identifier &&
|
|
24
|
+
identifier.range[0] >= start &&
|
|
25
|
+
identifier.range[1] <= end
|
|
26
|
+
);
|
|
27
|
+
}),
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default {
|
|
32
|
+
meta: {
|
|
33
|
+
type: "suggestion",
|
|
34
|
+
docs: { description: "Implementations infer their return types, except functions that call themselves." },
|
|
35
|
+
},
|
|
36
|
+
create(context) {
|
|
37
|
+
if (isGenerated(context.filename)) return {};
|
|
38
|
+
const check = (node) => {
|
|
39
|
+
const annotation = node.returnType?.typeAnnotation;
|
|
40
|
+
if (!annotation || !node.body || annotation.type === "TSTypePredicate" || recursive(context, node)) return;
|
|
41
|
+
context.report({
|
|
42
|
+
node: node.returnType,
|
|
43
|
+
message: "Drop the return type and let TypeScript infer it; use satisfies to pin a contract.",
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
return { FunctionDeclaration: check, FunctionExpression: check, ArrowFunctionExpression: check };
|
|
47
|
+
},
|
|
48
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
function isUndefinedProbe(node) {
|
|
2
|
+
const parent = node.parent;
|
|
3
|
+
if (parent?.type !== "BinaryExpression" || !["===", "!==", "==", "!="].includes(parent.operator)) return false;
|
|
4
|
+
const other = parent.left === node ? parent.right : parent.left;
|
|
5
|
+
return other.type === "Literal" && other.value === "undefined";
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
import { inside, locate } from "./paths.mjs";
|
|
9
|
+
|
|
10
|
+
export default {
|
|
11
|
+
meta: {
|
|
12
|
+
type: "problem",
|
|
13
|
+
docs: { description: "No runtime typeof narrowing outside lib/ serializers; parse at the boundary instead." },
|
|
14
|
+
},
|
|
15
|
+
create(context) {
|
|
16
|
+
if (inside(locate(context.filename)?.local ?? "", "src/lib")) return {};
|
|
17
|
+
return {
|
|
18
|
+
UnaryExpression(node) {
|
|
19
|
+
if (node.operator !== "typeof" || isUndefinedProbe(node)) return;
|
|
20
|
+
context.report({
|
|
21
|
+
node,
|
|
22
|
+
message: "Parse the value with a zod schema at its boundary; typeof belongs only in lib/ serializers.",
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
};
|