@thestraylight/so-hot-right-now 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 +164 -2
- package/bin/so-hot-right-now.mjs +9 -0
- package/docs/plugins.md +77 -0
- package/package.json +61 -4
- package/plugins/so-hot-right-now/.claude-plugin/plugin.json +9 -0
- package/plugins/so-hot-right-now/skills/hot-paths/SKILL.md +91 -0
- package/plugins/so-hot-right-now/skills/hot-paths/rules.md +56 -0
- package/plugins/so-hot-right-now/skills/hot-paths-setup/SKILL.md +103 -0
- package/src/baseline.mjs +41 -0
- package/src/checker.mjs +388 -0
- package/src/cli.mjs +171 -0
- package/src/config.mjs +68 -0
- package/src/helpers.d.ts +32 -0
- package/src/helpers.mjs +23 -0
- package/src/index.d.ts +151 -0
- package/src/index.mjs +11 -0
- package/src/options.mjs +85 -0
- package/src/rules/browser.mjs +108 -0
- package/src/rules/core.mjs +247 -0
- package/src/rules/frameworks.mjs +18 -0
- package/src/rules/webgpu.mjs +47 -0
- package/src/syntax.mjs +237 -0
- package/src/types.mjs +87 -0
package/src/checker.mjs
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { resolveOptions } from "./options.mjs";
|
|
4
|
+
import {
|
|
5
|
+
READ,
|
|
6
|
+
WRITE,
|
|
7
|
+
accessKind,
|
|
8
|
+
classMember,
|
|
9
|
+
createSyntax,
|
|
10
|
+
heritageNames,
|
|
11
|
+
isFunctionLike,
|
|
12
|
+
isNested,
|
|
13
|
+
memberName,
|
|
14
|
+
normalize,
|
|
15
|
+
symbolAt,
|
|
16
|
+
tagText,
|
|
17
|
+
withoutParentheses,
|
|
18
|
+
} from "./syntax.mjs";
|
|
19
|
+
import { isLibraryDeclaration, resolvedDeclaration } from "./types.mjs";
|
|
20
|
+
|
|
21
|
+
// The walk owns these messages. The plugins own the rest.
|
|
22
|
+
export const walkerMessages = {
|
|
23
|
+
closure: "Do not create a closure in a hot path. Use a loop or a method on a long-lived object.",
|
|
24
|
+
reason:
|
|
25
|
+
"Give a reason after `@hotIgnore`: say when the function runs or why it may break the rules.",
|
|
26
|
+
untagged:
|
|
27
|
+
"This function runs in a hot path. Read it, then tag it `@hot` if it is fast, or `@hotIgnore <reason>` if it is not.",
|
|
28
|
+
staleHot:
|
|
29
|
+
"No hot path calls this function. Remove `@hot`, or name its caller after `@hot` if it is a root.",
|
|
30
|
+
staleIgnore: "No hot path calls this function. Remove `@hotIgnore`.",
|
|
31
|
+
misplaced:
|
|
32
|
+
"Put `@hot` and `@hotIgnore` on a function, or on the declaration that holds a function.",
|
|
33
|
+
allow: "Give a reason after `hot-path: allow`: name the API that must allocate.",
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// `// hot-path: allow <reason>` on the line above a problem accepts it. The reason is necessary.
|
|
37
|
+
function allowance(sourceFile, node) {
|
|
38
|
+
const { line } = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
39
|
+
if (line === 0) return null;
|
|
40
|
+
const previous = sourceFile.text
|
|
41
|
+
.slice(
|
|
42
|
+
sourceFile.getPositionOfLineAndCharacter(line - 1, 0),
|
|
43
|
+
sourceFile.getPositionOfLineAndCharacter(line, 0),
|
|
44
|
+
)
|
|
45
|
+
.trim();
|
|
46
|
+
const match = /^\/\/ hot-path: allow\b(.*)$/.exec(previous);
|
|
47
|
+
return match ? match[1].trim() : null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function ownerMatches(classNode, owner) {
|
|
51
|
+
if (owner.implements !== undefined)
|
|
52
|
+
return heritageNames(classNode, ts.SyntaxKind.ImplementsKeyword).includes(owner.implements);
|
|
53
|
+
if (owner.extends !== undefined)
|
|
54
|
+
return heritageNames(classNode, ts.SyntaxKind.ExtendsKeyword).includes(owner.extends);
|
|
55
|
+
const name = classNode.name?.text ?? "";
|
|
56
|
+
return owner.name instanceof RegExp ? owner.name.test(name) : owner.name === name;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function memberOf(declaration) {
|
|
60
|
+
return ts.isFunctionTypeNode(declaration) && ts.isPropertySignature(declaration.parent)
|
|
61
|
+
? declaration.parent
|
|
62
|
+
: declaration;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function checkHotPaths(program, options) {
|
|
66
|
+
const settings = resolveOptions(options, walkerMessages);
|
|
67
|
+
const checker = program.getTypeChecker();
|
|
68
|
+
const syntax = createSyntax(checker, settings.functionHooks);
|
|
69
|
+
const hooks = settings.plugins.map((plugin) => plugin.create?.(program) ?? {});
|
|
70
|
+
const problems = [];
|
|
71
|
+
|
|
72
|
+
const projectFiles = new Map();
|
|
73
|
+
const isProjectSource = (sourceFile) => {
|
|
74
|
+
let result = projectFiles.get(sourceFile);
|
|
75
|
+
if (result === undefined) {
|
|
76
|
+
result =
|
|
77
|
+
!sourceFile.isDeclarationFile &&
|
|
78
|
+
!program.isSourceFileFromExternalLibrary(sourceFile) &&
|
|
79
|
+
settings.isProjectFile(settings.relative(sourceFile.fileName));
|
|
80
|
+
projectFiles.set(sourceFile, result);
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
};
|
|
84
|
+
const isStrict = (sourceFile) => settings.isStrictFile(settings.relative(sourceFile.fileName));
|
|
85
|
+
|
|
86
|
+
const report = (sourceFile, node, messageId, chain) => {
|
|
87
|
+
const message = settings.messages[messageId];
|
|
88
|
+
if (message === undefined) throw new Error(`No plugin defines the message "${messageId}".`);
|
|
89
|
+
const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
90
|
+
problems.push({
|
|
91
|
+
fileName: normalize(sourceFile.fileName),
|
|
92
|
+
file: settings.relative(sourceFile.fileName),
|
|
93
|
+
line: line + 1,
|
|
94
|
+
column: character + 1,
|
|
95
|
+
messageId,
|
|
96
|
+
message,
|
|
97
|
+
chain,
|
|
98
|
+
strict: isStrict(sourceFile),
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
const reportAllowed = (sourceFile, node, messageId, chain) => {
|
|
102
|
+
const allowed = allowance(sourceFile, node);
|
|
103
|
+
if (allowed === null) report(sourceFile, node, messageId, chain);
|
|
104
|
+
else if (allowed === "") report(sourceFile, node, "allow", chain);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const memberRoots = settings.roots.filter((root) => root.kind === "method");
|
|
108
|
+
const callRoots = new Map();
|
|
109
|
+
for (const root of settings.roots)
|
|
110
|
+
if (root.kind !== "method") {
|
|
111
|
+
if (!callRoots.has(root.callee)) callRoots.set(root.callee, []);
|
|
112
|
+
callRoots.get(root.callee).push(root);
|
|
113
|
+
}
|
|
114
|
+
const memberRootLabel = (member) => {
|
|
115
|
+
const owner = member.parent;
|
|
116
|
+
if (!owner || !ts.isClassLike(owner)) return null;
|
|
117
|
+
const name = memberName(member);
|
|
118
|
+
for (const root of memberRoots)
|
|
119
|
+
if (root.methods.includes(name) && ownerMatches(owner, root.owner)) return root.label;
|
|
120
|
+
return null;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const rootsByLabel = new Map();
|
|
124
|
+
const addRoot = (found, node, label) => {
|
|
125
|
+
found.push(node);
|
|
126
|
+
if (!rootsByLabel.has(label)) rootsByLabel.set(label, new Set());
|
|
127
|
+
rootsByLabel.get(label).add(node);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const rootFunctions = (sourceFile) => {
|
|
131
|
+
const found = [];
|
|
132
|
+
const strict = isStrict(sourceFile);
|
|
133
|
+
const visit = (node) => {
|
|
134
|
+
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression))
|
|
135
|
+
for (const root of callRoots.get(node.expression.text) ?? []) {
|
|
136
|
+
const argument = node.arguments[root.index];
|
|
137
|
+
const target = argument && syntax.functionOf(argument);
|
|
138
|
+
// A scheduler root is a callback that schedules itself again from its own body.
|
|
139
|
+
if (
|
|
140
|
+
target &&
|
|
141
|
+
(root.kind === "argument" || ts.findAncestor(node, (current) => current === target))
|
|
142
|
+
)
|
|
143
|
+
addRoot(found, target, root.label);
|
|
144
|
+
}
|
|
145
|
+
if (isFunctionLike(node)) {
|
|
146
|
+
const ignore = syntax.findTag(node, "hotIgnore");
|
|
147
|
+
if (ignore && !tagText(ignore))
|
|
148
|
+
report(sourceFile, ignore, "reason", syntax.functionName(node));
|
|
149
|
+
}
|
|
150
|
+
// A root property such as `run = step` has the body of the function that it names.
|
|
151
|
+
if (
|
|
152
|
+
ts.isPropertyDeclaration(node) &&
|
|
153
|
+
node.initializer &&
|
|
154
|
+
ts.isIdentifier(withoutParentheses(node.initializer))
|
|
155
|
+
) {
|
|
156
|
+
const label = memberRootLabel(node);
|
|
157
|
+
const target = label && syntax.bodyOf(node);
|
|
158
|
+
if (target) addRoot(found, target, label);
|
|
159
|
+
}
|
|
160
|
+
if (isFunctionLike(node) && node.body) {
|
|
161
|
+
const member = classMember(node);
|
|
162
|
+
const label = member && memberRootLabel(member);
|
|
163
|
+
const hot = syntax.findTag(node, "hot");
|
|
164
|
+
// A root that names its caller, such as a worker message handler.
|
|
165
|
+
const tagged = hot !== null && tagText(hot) !== "";
|
|
166
|
+
if (label) addRoot(found, node, label);
|
|
167
|
+
if (tagged) addRoot(found, node, "tagged");
|
|
168
|
+
if (
|
|
169
|
+
!label &&
|
|
170
|
+
!tagged &&
|
|
171
|
+
strict &&
|
|
172
|
+
!isNested(node) &&
|
|
173
|
+
!ts.isConstructorDeclaration(node) &&
|
|
174
|
+
!syntax.hasTag(node, "hotIgnore")
|
|
175
|
+
)
|
|
176
|
+
addRoot(found, node, "strict");
|
|
177
|
+
}
|
|
178
|
+
ts.forEachChild(node, visit);
|
|
179
|
+
};
|
|
180
|
+
visit(sourceFile);
|
|
181
|
+
return found;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
// An interface or type member has no body. Map each project member to the object literal
|
|
185
|
+
// properties and class members that implement it, so a call through the member reaches them.
|
|
186
|
+
const implementations = () => {
|
|
187
|
+
const found = new Map();
|
|
188
|
+
const add = (type, name, implementation) => {
|
|
189
|
+
const target = implementation && syntax.bodyOf(implementation);
|
|
190
|
+
if (!target) return;
|
|
191
|
+
for (const member of type.getProperty(name)?.declarations ?? [])
|
|
192
|
+
if (
|
|
193
|
+
!syntax.bodyOf(member) &&
|
|
194
|
+
(ts.isPropertySignature(member) || ts.isMethodSignature(member)) &&
|
|
195
|
+
isProjectSource(member.getSourceFile())
|
|
196
|
+
) {
|
|
197
|
+
if (!found.has(member)) found.set(member, []);
|
|
198
|
+
found.get(member).push(target);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
const visit = (node) => {
|
|
202
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
203
|
+
const type = checker.getContextualType(node);
|
|
204
|
+
if (type)
|
|
205
|
+
for (const property of node.properties) {
|
|
206
|
+
if (!property.name || !ts.isIdentifier(property.name)) continue;
|
|
207
|
+
const implementation = ts.isShorthandPropertyAssignment(property)
|
|
208
|
+
? checker.getShorthandAssignmentValueSymbol(property)?.valueDeclaration
|
|
209
|
+
: property;
|
|
210
|
+
add(type, property.name.text, implementation);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (ts.isClassLike(node))
|
|
214
|
+
for (const clause of node.heritageClauses ?? [])
|
|
215
|
+
for (const heritage of clause.types) {
|
|
216
|
+
const type = checker.getTypeAtLocation(heritage.expression);
|
|
217
|
+
for (const member of node.members)
|
|
218
|
+
if (member.name && ts.isIdentifier(member.name)) add(type, member.name.text, member);
|
|
219
|
+
}
|
|
220
|
+
ts.forEachChild(node, visit);
|
|
221
|
+
};
|
|
222
|
+
for (const sourceFile of program.getSourceFiles())
|
|
223
|
+
if (isProjectSource(sourceFile)) visit(sourceFile);
|
|
224
|
+
return found;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const callers = new Map();
|
|
228
|
+
const callees = new Map();
|
|
229
|
+
const ignoredCalls = new Set();
|
|
230
|
+
// A closure that a hot function creates is already a problem, so it needs no tag.
|
|
231
|
+
const inlineClosures = new Set();
|
|
232
|
+
const untagged = [];
|
|
233
|
+
const queue = [];
|
|
234
|
+
|
|
235
|
+
const chain = (node) => {
|
|
236
|
+
const names = [];
|
|
237
|
+
for (let current = node; current; current = callers.get(current))
|
|
238
|
+
names.push(syntax.functionName(current));
|
|
239
|
+
return names.reverse().join(" > ");
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const enqueue = (node, caller) => {
|
|
243
|
+
if (!isProjectSource(node.getSourceFile())) return;
|
|
244
|
+
if (caller) {
|
|
245
|
+
if (!callees.has(caller)) callees.set(caller, new Set());
|
|
246
|
+
callees.get(caller).add(node);
|
|
247
|
+
}
|
|
248
|
+
if (syntax.hasTag(node, "hotIgnore")) {
|
|
249
|
+
ignoredCalls.add(node);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (callers.has(node)) return;
|
|
253
|
+
callers.set(node, caller);
|
|
254
|
+
queue.push(node);
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
for (const sourceFile of program.getSourceFiles()) {
|
|
258
|
+
if (!isProjectSource(sourceFile)) continue;
|
|
259
|
+
const context = {
|
|
260
|
+
checker,
|
|
261
|
+
isStrict: isStrict(sourceFile),
|
|
262
|
+
report: (node, messageId) => reportAllowed(sourceFile, node, messageId, "module"),
|
|
263
|
+
};
|
|
264
|
+
for (const hook of hooks) hook.checkSourceFile?.(sourceFile, context);
|
|
265
|
+
for (const root of rootFunctions(sourceFile)) enqueue(root, null);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const implemented = implementations();
|
|
269
|
+
const followDeclarations = (declarations, caller) => {
|
|
270
|
+
for (const declaration of declarations ?? []) {
|
|
271
|
+
const target = syntax.bodyOf(declaration);
|
|
272
|
+
if (target) enqueue(target, caller);
|
|
273
|
+
for (const implementation of implemented.get(memberOf(declaration)) ?? [])
|
|
274
|
+
enqueue(implementation, caller);
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const calleeName = (expression) =>
|
|
279
|
+
ts.isPropertyAccessExpression(expression)
|
|
280
|
+
? expression.name
|
|
281
|
+
: ts.isElementAccessExpression(expression)
|
|
282
|
+
? expression.argumentExpression
|
|
283
|
+
: expression;
|
|
284
|
+
|
|
285
|
+
const scan = (owner) => {
|
|
286
|
+
const sourceFile = owner.getSourceFile();
|
|
287
|
+
const path = chain(owner);
|
|
288
|
+
const context = {
|
|
289
|
+
checker,
|
|
290
|
+
owner,
|
|
291
|
+
chain: path,
|
|
292
|
+
isStrict: isStrict(sourceFile),
|
|
293
|
+
report: (node, messageId) => reportAllowed(sourceFile, node, messageId, path),
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
if (!syntax.hasTag(owner, "hot")) untagged.push(owner);
|
|
297
|
+
for (const hook of hooks) hook.checkFunction?.(owner, context);
|
|
298
|
+
|
|
299
|
+
const visit = (node) => {
|
|
300
|
+
// A throw and a catch block run only on a failure.
|
|
301
|
+
if (ts.isTypeNode(node) || ts.isThrowStatement(node) || ts.isCatchClause(node)) return;
|
|
302
|
+
if (node !== owner && isFunctionLike(node)) {
|
|
303
|
+
context.report(node, "closure");
|
|
304
|
+
inlineClosures.add(node);
|
|
305
|
+
enqueue(node, owner);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
for (const hook of hooks) hook.checkNode?.(node, context);
|
|
309
|
+
if (ts.isCallExpression(node)) {
|
|
310
|
+
// A function that hot code passes to a call can run in the hot path.
|
|
311
|
+
for (const argument of node.arguments)
|
|
312
|
+
if (
|
|
313
|
+
(ts.isIdentifier(argument) || ts.isPropertyAccessExpression(argument)) &&
|
|
314
|
+
checker.getTypeAtLocation(argument).getCallSignatures().length > 0
|
|
315
|
+
)
|
|
316
|
+
followDeclarations(symbolAt(checker, calleeName(argument))?.declarations, owner);
|
|
317
|
+
const declaration = resolvedDeclaration(checker, node);
|
|
318
|
+
// An interface member such as `level.draw` reaches its implementations.
|
|
319
|
+
if (declaration && !isLibraryDeclaration(declaration))
|
|
320
|
+
followDeclarations([declaration], owner);
|
|
321
|
+
else
|
|
322
|
+
followDeclarations(symbolAt(checker, calleeName(node.expression))?.declarations, owner);
|
|
323
|
+
}
|
|
324
|
+
if (
|
|
325
|
+
ts.isPropertyAccessExpression(node) &&
|
|
326
|
+
!(node.parent && ts.isCallExpression(node.parent) && node.parent.expression === node)
|
|
327
|
+
) {
|
|
328
|
+
const access = accessKind(node);
|
|
329
|
+
for (const declaration of symbolAt(checker, node.name)?.declarations ?? [])
|
|
330
|
+
for (const accessor of [declaration, ...(implemented.get(declaration) ?? [])])
|
|
331
|
+
if (
|
|
332
|
+
accessor.body &&
|
|
333
|
+
((access & READ && ts.isGetAccessorDeclaration(accessor)) ||
|
|
334
|
+
(access & WRITE && ts.isSetAccessorDeclaration(accessor)))
|
|
335
|
+
)
|
|
336
|
+
enqueue(accessor, owner);
|
|
337
|
+
}
|
|
338
|
+
ts.forEachChild(node, visit);
|
|
339
|
+
};
|
|
340
|
+
for (const parameter of owner.parameters) visit(parameter);
|
|
341
|
+
if (owner.body) visit(owner.body);
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
while (queue.length > 0) scan(queue.pop());
|
|
345
|
+
for (const owner of untagged)
|
|
346
|
+
if (!inlineClosures.has(owner))
|
|
347
|
+
report(owner.getSourceFile(), owner.name ?? owner, "untagged", chain(owner));
|
|
348
|
+
|
|
349
|
+
const graph = { roots: rootsByLabel, callees, chain };
|
|
350
|
+
const finishContext = {
|
|
351
|
+
checker,
|
|
352
|
+
isStrict,
|
|
353
|
+
report: (node, messageId, path) => reportAllowed(node.getSourceFile(), node, messageId, path),
|
|
354
|
+
};
|
|
355
|
+
for (const hook of hooks) hook.finish?.(graph, finishContext);
|
|
356
|
+
|
|
357
|
+
for (const sourceFile of program.getSourceFiles()) {
|
|
358
|
+
if (!isProjectSource(sourceFile)) continue;
|
|
359
|
+
const strict = isStrict(sourceFile);
|
|
360
|
+
const visit = (node) => {
|
|
361
|
+
for (const doc of node.jsDoc ?? [])
|
|
362
|
+
for (const tag of doc.tags ?? [])
|
|
363
|
+
if (
|
|
364
|
+
(tag.tagName.text === "hot" || tag.tagName.text === "hotIgnore") &&
|
|
365
|
+
!syntax.tagsFunction(node)
|
|
366
|
+
)
|
|
367
|
+
report(sourceFile, tag, "misplaced", syntax.functionName(node));
|
|
368
|
+
if (isFunctionLike(node) && node.body) {
|
|
369
|
+
const hot = syntax.findTag(node, "hot");
|
|
370
|
+
if (hot && !tagText(hot) && !callers.has(node))
|
|
371
|
+
report(sourceFile, hot, "staleHot", syntax.functionName(node));
|
|
372
|
+
// In a strict file, each function needs a tag, so no ignore tag is stale.
|
|
373
|
+
const ignore = syntax.findTag(node, "hotIgnore");
|
|
374
|
+
if (ignore && !strict && !ignoredCalls.has(node))
|
|
375
|
+
report(sourceFile, ignore, "staleIgnore", syntax.functionName(node));
|
|
376
|
+
}
|
|
377
|
+
ts.forEachChild(node, visit);
|
|
378
|
+
};
|
|
379
|
+
visit(sourceFile);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return problems.sort(
|
|
383
|
+
(first, second) =>
|
|
384
|
+
first.file.localeCompare(second.file) ||
|
|
385
|
+
first.line - second.line ||
|
|
386
|
+
first.column - second.column,
|
|
387
|
+
);
|
|
388
|
+
}
|
package/src/cli.mjs
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { cpSync, existsSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { parseArgs } from "node:util";
|
|
6
|
+
|
|
7
|
+
import { compareWithBaseline, countProblems } from "./baseline.mjs";
|
|
8
|
+
import { checkHotPaths } from "./checker.mjs";
|
|
9
|
+
import { CONFIG_FILES, createProgram, findConfig, loadConfig, readBaseline } from "./config.mjs";
|
|
10
|
+
|
|
11
|
+
const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
12
|
+
const NAME = "so-hot-right-now";
|
|
13
|
+
const PACKAGE = JSON.parse(readFileSync(path.join(PACKAGE_ROOT, "package.json"), "utf8")).name;
|
|
14
|
+
const SKILLS = path.join(PACKAGE_ROOT, "plugins", "so-hot-right-now", "skills");
|
|
15
|
+
|
|
16
|
+
const USAGE = `Usage: ${NAME} <command> [options]
|
|
17
|
+
|
|
18
|
+
Commands:
|
|
19
|
+
check Check the hot paths against the baseline. This is the default command.
|
|
20
|
+
report [text] Show each problem whose path contains the text, with its call chain.
|
|
21
|
+
baseline Write the current problem counts to the baseline file.
|
|
22
|
+
init Write a starter ${CONFIG_FILES[0]} in the current folder.
|
|
23
|
+
skills Copy the agent skills for Claude Code and Codex into the project.
|
|
24
|
+
|
|
25
|
+
Options:
|
|
26
|
+
--config <path> Use this config file. The default is the nearest ${CONFIG_FILES[0]}.
|
|
27
|
+
--format <format> Write "text" or "json". The default is "text".
|
|
28
|
+
--target <target> For "skills": "all" (the default), "claude" (.claude/skills),
|
|
29
|
+
"codex" (.agents/skills), or a folder.
|
|
30
|
+
--global For "skills": copy into the user folders (~/.claude/skills, ~/.agents/skills).
|
|
31
|
+
--help Show this help.
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
const STARTER_CONFIG = `import { defineConfig, presets } from "${PACKAGE}";
|
|
35
|
+
|
|
36
|
+
export default defineConfig({
|
|
37
|
+
tsconfig: "tsconfig.json",
|
|
38
|
+
baseline: "hot-path-baseline.json",
|
|
39
|
+
// Every function in these files is a root, and the baseline accepts no problems in them.
|
|
40
|
+
strict: [],
|
|
41
|
+
plugins: [presets.browser, presets.react],
|
|
42
|
+
// Add the entry points that your engine calls on each frame or tick.
|
|
43
|
+
// A \`/** @hot <caller> */\` tag on a function also makes it a root.
|
|
44
|
+
roots: [
|
|
45
|
+
// { kind: "method", owner: { implements: "System" }, methods: ["update"], label: "simulation" },
|
|
46
|
+
// { kind: "argument", callee: "useFrame", index: 0, label: "frame" },
|
|
47
|
+
],
|
|
48
|
+
});
|
|
49
|
+
`;
|
|
50
|
+
|
|
51
|
+
async function load(values) {
|
|
52
|
+
const configPath = values.config ? path.resolve(values.config) : findConfig(process.cwd());
|
|
53
|
+
if (!configPath)
|
|
54
|
+
throw new Error(`Found no ${CONFIG_FILES[0]}. Run \`${NAME} init\` to write one.`);
|
|
55
|
+
const loaded = await loadConfig(configPath);
|
|
56
|
+
const problems = checkHotPaths(createProgram(loaded.tsconfig), loaded.options);
|
|
57
|
+
return { loaded, problems };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function relativeTo(loaded, problem) {
|
|
61
|
+
return path.relative(process.cwd(), path.join(loaded.root, problem.file));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function check(values) {
|
|
65
|
+
const { loaded, problems } = await load(values);
|
|
66
|
+
const { grown, stale } = compareWithBaseline(problems, readBaseline(loaded.baseline));
|
|
67
|
+
if (values.format === "json") {
|
|
68
|
+
const newProblems = problems.filter((problem) =>
|
|
69
|
+
grown.some((group) => group.file === problem.file && group.messageId === problem.messageId),
|
|
70
|
+
);
|
|
71
|
+
console.log(JSON.stringify({ problems: newProblems, grown, stale }, null, 2));
|
|
72
|
+
} else {
|
|
73
|
+
for (const group of grown) {
|
|
74
|
+
const members = problems.filter(
|
|
75
|
+
(problem) => problem.file === group.file && problem.messageId === group.messageId,
|
|
76
|
+
);
|
|
77
|
+
console.error(
|
|
78
|
+
`${group.file}: ${group.count} "${group.messageId}" problem(s), ${group.allowed} allowed. ${members[0].message}`,
|
|
79
|
+
);
|
|
80
|
+
for (const problem of members)
|
|
81
|
+
console.error(
|
|
82
|
+
` ${relativeTo(loaded, problem)}:${problem.line}:${problem.column} (hot via ${problem.chain})`,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
for (const group of stale)
|
|
86
|
+
console.error(
|
|
87
|
+
`${group.file}: ${group.count} "${group.messageId}" problem(s), but the baseline allows ${group.allowed}. Run \`${NAME} baseline\`.`,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return grown.length + stale.length > 0 ? 1 : 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function report(values, filter) {
|
|
94
|
+
const { loaded, problems } = await load(values);
|
|
95
|
+
const shown = problems.filter((problem) => problem.file.includes(filter));
|
|
96
|
+
if (values.format === "json") console.log(JSON.stringify(shown, null, 2));
|
|
97
|
+
else {
|
|
98
|
+
for (const problem of shown)
|
|
99
|
+
console.log(
|
|
100
|
+
`${relativeTo(loaded, problem)}:${problem.line}:${problem.column} ${problem.messageId} ${problem.message} (hot via ${problem.chain})`,
|
|
101
|
+
);
|
|
102
|
+
console.log(`${shown.length} of ${problems.length} hot path problem(s).`);
|
|
103
|
+
}
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function writeBaseline(values) {
|
|
108
|
+
const { loaded, problems } = await load(values);
|
|
109
|
+
writeFileSync(loaded.baseline, `${JSON.stringify(countProblems(problems), null, 2)}\n`);
|
|
110
|
+
console.log(`Wrote ${path.relative(process.cwd(), loaded.baseline)}.`);
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function init() {
|
|
115
|
+
const target = path.resolve(CONFIG_FILES[0]);
|
|
116
|
+
if (existsSync(target)) throw new Error(`${CONFIG_FILES[0]} exists. Edit it instead.`);
|
|
117
|
+
writeFileSync(target, STARTER_CONFIG);
|
|
118
|
+
console.log(`Wrote ${CONFIG_FILES[0]}. Add your roots, then run \`${NAME} baseline\`.`);
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Claude Code reads `.claude/skills`. Codex and other Agent Skills tools read `.agents/skills`.
|
|
123
|
+
const SKILL_FOLDERS = { claude: ".claude/skills", codex: ".agents/skills" };
|
|
124
|
+
|
|
125
|
+
function skillDestinations(values) {
|
|
126
|
+
const base = values.global ? os.homedir() : process.cwd();
|
|
127
|
+
if (values.target === "all")
|
|
128
|
+
return Object.values(SKILL_FOLDERS).map((folder) => path.join(base, folder));
|
|
129
|
+
if (values.target in SKILL_FOLDERS) return [path.join(base, SKILL_FOLDERS[values.target])];
|
|
130
|
+
return [path.resolve(values.target)];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function installSkills(values) {
|
|
134
|
+
for (const destination of skillDestinations(values))
|
|
135
|
+
for (const skill of readdirSync(SKILLS)) {
|
|
136
|
+
const target = path.join(destination, skill);
|
|
137
|
+
// Remove the old copy, so that a file that an update deletes does not stay.
|
|
138
|
+
rmSync(target, { recursive: true, force: true });
|
|
139
|
+
cpSync(path.join(SKILLS, skill), target, { recursive: true });
|
|
140
|
+
console.log(`Copied ${skill} to ${path.relative(process.cwd(), destination) || "."}.`);
|
|
141
|
+
}
|
|
142
|
+
return 0;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export async function main(argv) {
|
|
146
|
+
const { values, positionals } = parseArgs({
|
|
147
|
+
args: argv,
|
|
148
|
+
allowPositionals: true,
|
|
149
|
+
options: {
|
|
150
|
+
config: { type: "string" },
|
|
151
|
+
format: { type: "string", default: "text" },
|
|
152
|
+
target: { type: "string", default: "all" },
|
|
153
|
+
global: { type: "boolean", default: false },
|
|
154
|
+
help: { type: "boolean", default: false },
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
if (values.format !== "text" && values.format !== "json")
|
|
158
|
+
throw new Error(`Use --format text or --format json, not "${values.format}".`);
|
|
159
|
+
const [command = "check", argument = ""] = positionals;
|
|
160
|
+
if (values.help) {
|
|
161
|
+
console.log(USAGE);
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
if (command === "check") return check(values);
|
|
165
|
+
if (command === "report") return report(values, argument);
|
|
166
|
+
if (command === "baseline") return writeBaseline(values);
|
|
167
|
+
if (command === "init") return init();
|
|
168
|
+
if (command === "skills") return installSkills(values);
|
|
169
|
+
console.error(USAGE);
|
|
170
|
+
return 2;
|
|
171
|
+
}
|
package/src/config.mjs
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
|
|
5
|
+
import ts from "typescript";
|
|
6
|
+
|
|
7
|
+
export const CONFIG_FILES = [
|
|
8
|
+
"hot-path.config.mjs",
|
|
9
|
+
"hot-path.config.js",
|
|
10
|
+
"hot-path.config.ts",
|
|
11
|
+
"hot-path.config.mts",
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
export function defineConfig(config) {
|
|
15
|
+
return config;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function findConfig(directory) {
|
|
19
|
+
for (let current = path.resolve(directory); ;) {
|
|
20
|
+
for (const name of CONFIG_FILES) {
|
|
21
|
+
const candidate = path.join(current, name);
|
|
22
|
+
if (existsSync(candidate)) return candidate;
|
|
23
|
+
}
|
|
24
|
+
const parent = path.dirname(current);
|
|
25
|
+
if (parent === current) return null;
|
|
26
|
+
current = parent;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// The config file's folder is the project root. Paths in the config are relative to it.
|
|
31
|
+
export async function loadConfig(configPath) {
|
|
32
|
+
const module = await import(pathToFileURL(configPath).href);
|
|
33
|
+
const config = module.default;
|
|
34
|
+
if (!config || typeof config !== "object")
|
|
35
|
+
throw new Error(`${configPath} must export a config object as its default export.`);
|
|
36
|
+
const root = path.dirname(configPath);
|
|
37
|
+
return {
|
|
38
|
+
root,
|
|
39
|
+
tsconfig: path.resolve(root, config.tsconfig ?? "tsconfig.json"),
|
|
40
|
+
baseline: path.resolve(root, config.baseline ?? "hot-path-baseline.json"),
|
|
41
|
+
options: {
|
|
42
|
+
root,
|
|
43
|
+
include: config.include,
|
|
44
|
+
exclude: config.exclude,
|
|
45
|
+
strict: config.strict,
|
|
46
|
+
plugins: config.plugins,
|
|
47
|
+
roots: config.roots,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createProgram(tsconfig) {
|
|
53
|
+
const parsed = ts.getParsedCommandLineOfConfigFile(
|
|
54
|
+
tsconfig,
|
|
55
|
+
{},
|
|
56
|
+
{
|
|
57
|
+
...ts.sys,
|
|
58
|
+
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
|
|
59
|
+
throw new Error(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
);
|
|
63
|
+
return ts.createProgram(parsed.fileNames, parsed.options);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function readBaseline(baselinePath) {
|
|
67
|
+
return existsSync(baselinePath) ? JSON.parse(readFileSync(baselinePath, "utf8")) : {};
|
|
68
|
+
}
|
package/src/helpers.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export const READ: 1;
|
|
4
|
+
export const WRITE: 2;
|
|
5
|
+
/** `READ`, `WRITE`, or both for a property access such as `count += 1`. */
|
|
6
|
+
export function accessKind(node: ts.Node): number;
|
|
7
|
+
/** The method name of a call such as `a.b()` or `a["b"]()`, or "" for another callee. */
|
|
8
|
+
export function calledMethod(callee: ts.Expression): string;
|
|
9
|
+
export function isAssignmentTarget(node: ts.Node): boolean;
|
|
10
|
+
export function isFunctionLike(node: ts.Node): node is ts.FunctionLikeDeclaration;
|
|
11
|
+
export function isStringLiteral(
|
|
12
|
+
node: ts.Node,
|
|
13
|
+
): node is ts.StringLiteral | ts.NoSubstitutionTemplateLiteral;
|
|
14
|
+
export function literalName(name: ts.PropertyName): string;
|
|
15
|
+
/** The symbol at a node, with an import alias resolved. */
|
|
16
|
+
export function symbolAt(checker: ts.TypeChecker, node: ts.Node): ts.Symbol | undefined;
|
|
17
|
+
export function withoutParentheses(expression: ts.Expression): ts.Expression;
|
|
18
|
+
|
|
19
|
+
export function hasMixedNumbers(checker: ts.TypeChecker, type: ts.Type): boolean;
|
|
20
|
+
export function isIndexable(checker: ts.TypeChecker, type: ts.Type): boolean;
|
|
21
|
+
/** True when a declaration comes from a `.d.ts` file, such as a built-in method. */
|
|
22
|
+
export function isLibraryDeclaration(declaration: ts.Declaration | undefined): boolean;
|
|
23
|
+
/** True when the type, a part of a union, or a type parameter constraint has the name. */
|
|
24
|
+
export function isNamedType(type: ts.Type, name: string): boolean;
|
|
25
|
+
export function isNumberType(type: ts.Type): boolean;
|
|
26
|
+
export function isStringType(type: ts.Type): boolean;
|
|
27
|
+
export function resolvedDeclaration(
|
|
28
|
+
checker: ts.TypeChecker,
|
|
29
|
+
call: ts.CallLikeExpression,
|
|
30
|
+
): ts.Declaration | undefined;
|
|
31
|
+
export function someType(type: ts.Type, test: (part: ts.Type) => boolean): boolean;
|
|
32
|
+
export function typeName(type: ts.Type): string;
|
package/src/helpers.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export {
|
|
2
|
+
READ,
|
|
3
|
+
WRITE,
|
|
4
|
+
accessKind,
|
|
5
|
+
calledMethod,
|
|
6
|
+
isAssignmentTarget,
|
|
7
|
+
isFunctionLike,
|
|
8
|
+
isStringLiteral,
|
|
9
|
+
literalName,
|
|
10
|
+
symbolAt,
|
|
11
|
+
withoutParentheses,
|
|
12
|
+
} from "./syntax.mjs";
|
|
13
|
+
export {
|
|
14
|
+
hasMixedNumbers,
|
|
15
|
+
isIndexable,
|
|
16
|
+
isLibraryDeclaration,
|
|
17
|
+
isNamedType,
|
|
18
|
+
isNumberType,
|
|
19
|
+
isStringType,
|
|
20
|
+
resolvedDeclaration,
|
|
21
|
+
someType,
|
|
22
|
+
typeName,
|
|
23
|
+
} from "./types.mjs";
|