@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/index.d.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export type HotFunction = ts.FunctionLikeDeclaration;
|
|
4
|
+
|
|
5
|
+
/** A class member that your engine calls on each frame or tick. Give exactly one owner key. */
|
|
6
|
+
export interface MethodRoot {
|
|
7
|
+
kind: "method";
|
|
8
|
+
owner: { implements: string } | { extends: string } | { name: string | RegExp };
|
|
9
|
+
methods: string[];
|
|
10
|
+
label: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A function that the code gives to a call, such as `useFrame(callback)`. */
|
|
14
|
+
export interface ArgumentRoot {
|
|
15
|
+
kind: "argument";
|
|
16
|
+
callee: string;
|
|
17
|
+
index: number;
|
|
18
|
+
label: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A callback that gives itself to a call from its own body, such as `requestAnimationFrame`. */
|
|
22
|
+
export interface SchedulerRoot {
|
|
23
|
+
kind: "scheduler";
|
|
24
|
+
callee: string;
|
|
25
|
+
index: number;
|
|
26
|
+
label: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type Root = MethodRoot | ArgumentRoot | SchedulerRoot;
|
|
30
|
+
|
|
31
|
+
export interface FunctionContext {
|
|
32
|
+
checker: ts.TypeChecker;
|
|
33
|
+
owner: HotFunction;
|
|
34
|
+
/** The call chain from a root to the owner, such as `Game.update > move`. */
|
|
35
|
+
chain: string;
|
|
36
|
+
isStrict: boolean;
|
|
37
|
+
/** Reports a problem. A `// hot-path: allow <reason>` line above the node accepts it. */
|
|
38
|
+
report(node: ts.Node, messageId: string): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface SourceFileContext {
|
|
42
|
+
checker: ts.TypeChecker;
|
|
43
|
+
isStrict: boolean;
|
|
44
|
+
report(node: ts.Node, messageId: string): void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface CallGraph {
|
|
48
|
+
/** The roots for each label. The checker adds the labels "tagged" and "strict". */
|
|
49
|
+
roots: ReadonlyMap<string, ReadonlySet<HotFunction>>;
|
|
50
|
+
/** The functions that each hot function calls, including `@hotIgnore` functions. */
|
|
51
|
+
callees: ReadonlyMap<HotFunction, ReadonlySet<HotFunction>>;
|
|
52
|
+
chain(fn: HotFunction): string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface FinishContext {
|
|
56
|
+
checker: ts.TypeChecker;
|
|
57
|
+
isStrict(sourceFile: ts.SourceFile): boolean;
|
|
58
|
+
report(node: ts.Node, messageId: string, chain: string): void;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface PluginHooks {
|
|
62
|
+
/** Runs once for each hot function. */
|
|
63
|
+
checkFunction?(fn: HotFunction, context: FunctionContext): void;
|
|
64
|
+
/** Runs for each node in the body of a hot function, except nested functions. */
|
|
65
|
+
checkNode?(node: ts.Node, context: FunctionContext): void;
|
|
66
|
+
/** Runs once for each project source file, hot or not. */
|
|
67
|
+
checkSourceFile?(sourceFile: ts.SourceFile, context: SourceFileContext): void;
|
|
68
|
+
/** Runs after the walk, with the call graph. */
|
|
69
|
+
finish?(graph: CallGraph, context: FinishContext): void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface Plugin {
|
|
73
|
+
name: string;
|
|
74
|
+
/** The message for each problem id. The ids are the baseline keys, so keep them stable. */
|
|
75
|
+
messages?: Record<string, string>;
|
|
76
|
+
roots?: Root[];
|
|
77
|
+
/** Calls that return the function that they receive, such as React's `useCallback`. */
|
|
78
|
+
functionHooks?: string[];
|
|
79
|
+
/** Makes the hooks for one check. Keep the state of a check in the closure. */
|
|
80
|
+
create?(program: ts.Program): PluginHooks;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface CheckOptions {
|
|
84
|
+
/** The folder that the globs and the problem paths are relative to. */
|
|
85
|
+
root: string;
|
|
86
|
+
/** The default is every file. */
|
|
87
|
+
include?: string[];
|
|
88
|
+
/** The default is `DEFAULT_EXCLUDE`. */
|
|
89
|
+
exclude?: string[];
|
|
90
|
+
/** Every function in these files is a root, and the baseline accepts no problems in them. */
|
|
91
|
+
strict?: string[];
|
|
92
|
+
plugins?: Plugin[];
|
|
93
|
+
roots?: Root[];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface Config extends Omit<CheckOptions, "root"> {
|
|
97
|
+
/** The default is `tsconfig.json`. */
|
|
98
|
+
tsconfig?: string;
|
|
99
|
+
/** The default is `hot-path-baseline.json`. */
|
|
100
|
+
baseline?: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface Problem {
|
|
104
|
+
fileName: string;
|
|
105
|
+
/** The path relative to the project root. */
|
|
106
|
+
file: string;
|
|
107
|
+
line: number;
|
|
108
|
+
column: number;
|
|
109
|
+
messageId: string;
|
|
110
|
+
message: string;
|
|
111
|
+
chain: string;
|
|
112
|
+
strict: boolean;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export type Baseline = Record<string, Record<string, number>>;
|
|
116
|
+
|
|
117
|
+
export interface BaselineGroup {
|
|
118
|
+
file: string;
|
|
119
|
+
messageId: string;
|
|
120
|
+
count: number;
|
|
121
|
+
allowed: number;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface LoadedConfig {
|
|
125
|
+
root: string;
|
|
126
|
+
tsconfig: string;
|
|
127
|
+
baseline: string;
|
|
128
|
+
options: CheckOptions;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export const DEFAULT_EXCLUDE: string[];
|
|
132
|
+
export const walkerMessages: Record<string, string>;
|
|
133
|
+
export const core: Plugin;
|
|
134
|
+
export const presets: {
|
|
135
|
+
browser: Plugin;
|
|
136
|
+
webgpu: Plugin;
|
|
137
|
+
react: Plugin;
|
|
138
|
+
audioWorklet: Plugin;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export function defineConfig(config: Config): Config;
|
|
142
|
+
export function findConfig(directory: string): string | null;
|
|
143
|
+
export function loadConfig(configPath: string): Promise<LoadedConfig>;
|
|
144
|
+
export function createProgram(tsconfig: string): ts.Program;
|
|
145
|
+
export function readBaseline(baselinePath: string): Baseline;
|
|
146
|
+
export function checkHotPaths(program: ts.Program, options: CheckOptions): Problem[];
|
|
147
|
+
export function countProblems(problems: Problem[]): Baseline;
|
|
148
|
+
export function compareWithBaseline(
|
|
149
|
+
problems: Problem[],
|
|
150
|
+
baseline: Baseline,
|
|
151
|
+
): { grown: BaselineGroup[]; stale: BaselineGroup[] };
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { browser } from "./rules/browser.mjs";
|
|
2
|
+
import { audioWorklet, react } from "./rules/frameworks.mjs";
|
|
3
|
+
import { webgpu } from "./rules/webgpu.mjs";
|
|
4
|
+
|
|
5
|
+
export { checkHotPaths, walkerMessages } from "./checker.mjs";
|
|
6
|
+
export { compareWithBaseline, countProblems } from "./baseline.mjs";
|
|
7
|
+
export { createProgram, defineConfig, findConfig, loadConfig, readBaseline } from "./config.mjs";
|
|
8
|
+
export { core } from "./rules/core.mjs";
|
|
9
|
+
export { DEFAULT_EXCLUDE } from "./options.mjs";
|
|
10
|
+
|
|
11
|
+
export const presets = { browser, webgpu, react, audioWorklet };
|
package/src/options.mjs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import picomatch from "picomatch";
|
|
4
|
+
|
|
5
|
+
import { core } from "./rules/core.mjs";
|
|
6
|
+
import { normalize } from "./syntax.mjs";
|
|
7
|
+
|
|
8
|
+
export const DEFAULT_EXCLUDE = [
|
|
9
|
+
"**/node_modules/**",
|
|
10
|
+
"**/__tests__/**",
|
|
11
|
+
"**/*.test.*",
|
|
12
|
+
"**/*.spec.*",
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
const ROOT_KINDS = new Set(["method", "argument", "scheduler"]);
|
|
16
|
+
|
|
17
|
+
function matcher(patterns) {
|
|
18
|
+
if (patterns.length === 0) return () => false;
|
|
19
|
+
return picomatch(patterns, { dot: true });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function validateRoot(root, plugin) {
|
|
23
|
+
const where = plugin ? `plugin "${plugin}"` : "config";
|
|
24
|
+
if (!ROOT_KINDS.has(root.kind))
|
|
25
|
+
throw new Error(`A root in the ${where} has an unknown kind "${root.kind}".`);
|
|
26
|
+
if (typeof root.label !== "string" || root.label === "")
|
|
27
|
+
throw new Error(`Give each root in the ${where} a label.`);
|
|
28
|
+
if (root.kind === "method") {
|
|
29
|
+
const owner = root.owner ?? {};
|
|
30
|
+
const keys = ["implements", "extends", "name"].filter((key) => owner[key] !== undefined);
|
|
31
|
+
if (keys.length !== 1)
|
|
32
|
+
throw new Error(
|
|
33
|
+
`Give the "${root.label}" root in the ${where} one owner: implements, extends, or name.`,
|
|
34
|
+
);
|
|
35
|
+
if (!Array.isArray(root.methods) || root.methods.length === 0)
|
|
36
|
+
throw new Error(`Give the "${root.label}" root in the ${where} its methods.`);
|
|
37
|
+
} else if (typeof root.callee !== "string" || !Number.isInteger(root.index))
|
|
38
|
+
throw new Error(
|
|
39
|
+
`Give the "${root.label}" root in the ${where} a callee and an argument index.`,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Each option has a default here, so the checker reads only complete options.
|
|
44
|
+
export function resolveOptions(options, walkerMessages) {
|
|
45
|
+
const root = normalize(path.resolve(options.root));
|
|
46
|
+
const include = matcher(options.include ?? ["**/*"]);
|
|
47
|
+
const exclude = matcher(options.exclude ?? DEFAULT_EXCLUDE);
|
|
48
|
+
const strict = matcher(options.strict ?? []);
|
|
49
|
+
const plugins = [core, ...(options.plugins ?? [])];
|
|
50
|
+
|
|
51
|
+
const messages = { ...walkerMessages };
|
|
52
|
+
for (const plugin of plugins)
|
|
53
|
+
for (const [id, message] of Object.entries(plugin.messages ?? {})) {
|
|
54
|
+
if (id in messages)
|
|
55
|
+
throw new Error(
|
|
56
|
+
`The plugin "${plugin.name}" defines the message "${id}" again. Give it a different id.`,
|
|
57
|
+
);
|
|
58
|
+
messages[id] = message;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const roots = [];
|
|
62
|
+
for (const entry of options.roots ?? []) {
|
|
63
|
+
validateRoot(entry, null);
|
|
64
|
+
roots.push(entry);
|
|
65
|
+
}
|
|
66
|
+
for (const plugin of plugins)
|
|
67
|
+
for (const entry of plugin.roots ?? []) {
|
|
68
|
+
validateRoot(entry, plugin.name);
|
|
69
|
+
roots.push(entry);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const relative = (fileName) => normalize(path.posix.relative(root, normalize(fileName)));
|
|
73
|
+
const inProject = (file) => !file.startsWith("../") && !path.posix.isAbsolute(file);
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
root,
|
|
77
|
+
relative,
|
|
78
|
+
isProjectFile: (file) => inProject(file) && include(file) && !exclude(file),
|
|
79
|
+
isStrictFile: (file) => inProject(file) && strict(file),
|
|
80
|
+
plugins,
|
|
81
|
+
messages,
|
|
82
|
+
roots,
|
|
83
|
+
functionHooks: new Set(plugins.flatMap((plugin) => plugin.functionHooks ?? [])),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
READ,
|
|
5
|
+
accessKind,
|
|
6
|
+
calledMethod,
|
|
7
|
+
isStringLiteral,
|
|
8
|
+
literalName,
|
|
9
|
+
withoutParentheses,
|
|
10
|
+
} from "../syntax.mjs";
|
|
11
|
+
import { isLibraryDeclaration, resolvedDeclaration, someType, typeName } from "../types.mjs";
|
|
12
|
+
|
|
13
|
+
// A read of these makes the browser compute the layout in the middle of a frame.
|
|
14
|
+
const LAYOUT_METHODS = new Set(["getBoundingClientRect", "getClientRects", "getComputedStyle"]);
|
|
15
|
+
const LAYOUT_PROPERTIES = new Set([
|
|
16
|
+
"offsetWidth",
|
|
17
|
+
"offsetHeight",
|
|
18
|
+
"offsetTop",
|
|
19
|
+
"offsetLeft",
|
|
20
|
+
"clientWidth",
|
|
21
|
+
"clientHeight",
|
|
22
|
+
"clientTop",
|
|
23
|
+
"clientLeft",
|
|
24
|
+
"scrollWidth",
|
|
25
|
+
"scrollHeight",
|
|
26
|
+
"scrollTop",
|
|
27
|
+
"scrollLeft",
|
|
28
|
+
"innerText",
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
function isDomType(type) {
|
|
32
|
+
return someType(type, (part) => {
|
|
33
|
+
const name = typeName(part);
|
|
34
|
+
return (
|
|
35
|
+
name === "Window" || name === "Element" || name === "Document" || name.endsWith("Element")
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const browser = {
|
|
41
|
+
name: "browser",
|
|
42
|
+
messages: {
|
|
43
|
+
layout:
|
|
44
|
+
"Do not read the DOM layout in a hot path. It makes the browser compute the layout in the middle of a frame. Read it on a resize or an event.",
|
|
45
|
+
},
|
|
46
|
+
// A frame callback that schedules itself again runs on each frame.
|
|
47
|
+
roots: [
|
|
48
|
+
{
|
|
49
|
+
kind: "scheduler",
|
|
50
|
+
callee: "requestAnimationFrame",
|
|
51
|
+
index: 0,
|
|
52
|
+
label: "frame",
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
create(program) {
|
|
56
|
+
const checker = program.getTypeChecker();
|
|
57
|
+
const isDom = (node) => isDomType(checker.getTypeAtLocation(node));
|
|
58
|
+
return {
|
|
59
|
+
checkNode(node, context) {
|
|
60
|
+
const report = context.report;
|
|
61
|
+
if (
|
|
62
|
+
ts.isElementAccessExpression(node) &&
|
|
63
|
+
isStringLiteral(node.argumentExpression) &&
|
|
64
|
+
LAYOUT_PROPERTIES.has(node.argumentExpression.text) &&
|
|
65
|
+
accessKind(node) & READ &&
|
|
66
|
+
isDom(node.expression)
|
|
67
|
+
)
|
|
68
|
+
report(node, "layout");
|
|
69
|
+
if (ts.isObjectBindingPattern(node) && isDom(node))
|
|
70
|
+
for (const element of node.elements)
|
|
71
|
+
if (
|
|
72
|
+
!element.dotDotDotToken &&
|
|
73
|
+
LAYOUT_PROPERTIES.has(literalName(element.propertyName ?? element.name))
|
|
74
|
+
)
|
|
75
|
+
report(element, "layout");
|
|
76
|
+
if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
77
|
+
const target = withoutParentheses(node.left);
|
|
78
|
+
if (ts.isObjectLiteralExpression(target) && isDom(node.right))
|
|
79
|
+
for (const property of target.properties)
|
|
80
|
+
if (
|
|
81
|
+
(ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) &&
|
|
82
|
+
LAYOUT_PROPERTIES.has(literalName(property.name))
|
|
83
|
+
)
|
|
84
|
+
report(property, "layout");
|
|
85
|
+
}
|
|
86
|
+
if (ts.isCallExpression(node)) {
|
|
87
|
+
const callee = node.expression;
|
|
88
|
+
if (
|
|
89
|
+
ts.isIdentifier(callee) &&
|
|
90
|
+
LAYOUT_METHODS.has(callee.text) &&
|
|
91
|
+
isLibraryDeclaration(resolvedDeclaration(checker, node))
|
|
92
|
+
)
|
|
93
|
+
report(node, "layout");
|
|
94
|
+
if (LAYOUT_METHODS.has(calledMethod(callee)) && isDom(callee.expression))
|
|
95
|
+
report(node, "layout");
|
|
96
|
+
}
|
|
97
|
+
if (
|
|
98
|
+
ts.isPropertyAccessExpression(node) &&
|
|
99
|
+
!(node.parent && ts.isCallExpression(node.parent) && node.parent.expression === node) &&
|
|
100
|
+
accessKind(node) & READ &&
|
|
101
|
+
LAYOUT_PROPERTIES.has(node.name.text) &&
|
|
102
|
+
isDom(node.expression)
|
|
103
|
+
)
|
|
104
|
+
report(node, "layout");
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
};
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
calledMethod,
|
|
5
|
+
isAssignmentTarget,
|
|
6
|
+
isStringLiteral,
|
|
7
|
+
withoutParentheses,
|
|
8
|
+
} from "../syntax.mjs";
|
|
9
|
+
import {
|
|
10
|
+
hasMixedNumbers,
|
|
11
|
+
hasNumericLength,
|
|
12
|
+
isIndexable,
|
|
13
|
+
isLibraryDeclaration,
|
|
14
|
+
isNamedType,
|
|
15
|
+
isNumberType,
|
|
16
|
+
isStringType,
|
|
17
|
+
resolvedDeclaration,
|
|
18
|
+
typeName,
|
|
19
|
+
} from "../types.mjs";
|
|
20
|
+
|
|
21
|
+
const ALLOCATING_METHODS = new Set([
|
|
22
|
+
"map",
|
|
23
|
+
"filter",
|
|
24
|
+
"slice",
|
|
25
|
+
"subarray",
|
|
26
|
+
"concat",
|
|
27
|
+
"flat",
|
|
28
|
+
"flatMap",
|
|
29
|
+
"toSorted",
|
|
30
|
+
"toReversed",
|
|
31
|
+
"toSpliced",
|
|
32
|
+
"with",
|
|
33
|
+
"split",
|
|
34
|
+
"join",
|
|
35
|
+
"bind",
|
|
36
|
+
"from",
|
|
37
|
+
"of",
|
|
38
|
+
"keys",
|
|
39
|
+
"values",
|
|
40
|
+
"entries",
|
|
41
|
+
"fromEntries",
|
|
42
|
+
"assign",
|
|
43
|
+
"structuredClone",
|
|
44
|
+
"stringify",
|
|
45
|
+
"parse",
|
|
46
|
+
"padStart",
|
|
47
|
+
"padEnd",
|
|
48
|
+
"toFixed",
|
|
49
|
+
"toString",
|
|
50
|
+
"trim",
|
|
51
|
+
"replace",
|
|
52
|
+
"replaceAll",
|
|
53
|
+
"toLowerCase",
|
|
54
|
+
"toUpperCase",
|
|
55
|
+
]);
|
|
56
|
+
const HASH_TYPES = new Set(["Map", "Set", "WeakMap", "WeakSet", "ReadonlyMap", "ReadonlySet"]);
|
|
57
|
+
const HASH_METHODS = new Set(["get", "set", "has", "delete", "add"]);
|
|
58
|
+
const STRING_COMPARE_METHODS = new Set([
|
|
59
|
+
"startsWith",
|
|
60
|
+
"endsWith",
|
|
61
|
+
"includes",
|
|
62
|
+
"indexOf",
|
|
63
|
+
"lastIndexOf",
|
|
64
|
+
"localeCompare",
|
|
65
|
+
"match",
|
|
66
|
+
"search",
|
|
67
|
+
"test",
|
|
68
|
+
]);
|
|
69
|
+
const PROMISE_METHODS = new Set(["then", "catch", "finally"]);
|
|
70
|
+
const EQUALITY = new Set([
|
|
71
|
+
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
|
72
|
+
ts.SyntaxKind.ExclamationEqualsEqualsToken,
|
|
73
|
+
ts.SyntaxKind.EqualsEqualsToken,
|
|
74
|
+
ts.SyntaxKind.ExclamationEqualsToken,
|
|
75
|
+
]);
|
|
76
|
+
const ORDERING = new Set([
|
|
77
|
+
ts.SyntaxKind.LessThanToken,
|
|
78
|
+
ts.SyntaxKind.LessThanEqualsToken,
|
|
79
|
+
ts.SyntaxKind.GreaterThanToken,
|
|
80
|
+
ts.SyntaxKind.GreaterThanEqualsToken,
|
|
81
|
+
]);
|
|
82
|
+
|
|
83
|
+
function isHashType(type) {
|
|
84
|
+
const parts = type.isUnion() ? type.types : [type];
|
|
85
|
+
return parts.some((part) => HASH_TYPES.has(typeName(part)));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const core = {
|
|
89
|
+
name: "core",
|
|
90
|
+
messages: {
|
|
91
|
+
new: "Do not call `new` in a hot path. Allocate once and reuse the object.",
|
|
92
|
+
object:
|
|
93
|
+
"Do not create an object literal in a hot path. Reuse a preallocated object or write to typed arrays.",
|
|
94
|
+
array: "Do not create an array literal in a hot path. Reuse a preallocated array.",
|
|
95
|
+
allocation:
|
|
96
|
+
"Do not call an allocating built-in in a hot path. Write into a reused buffer instead.",
|
|
97
|
+
string: "Do not build strings in a hot path. Use numeric keys or indexes.",
|
|
98
|
+
compare: "Do not compare strings in a hot path. Use an integer enum instead.",
|
|
99
|
+
hash: "Do not use a Map, Set, or string-keyed lookup in a hot path. Use an array indexed by a dense integer.",
|
|
100
|
+
iterator:
|
|
101
|
+
"Do not iterate a non-array in a hot path. Iterate an array or typed array with an index.",
|
|
102
|
+
spread: "Do not spread arguments in a hot path. Pass fixed arguments.",
|
|
103
|
+
rest: "Do not collect a rest parameter or a rest binding in a hot path. Pass fixed arguments and read the fields you need.",
|
|
104
|
+
generator: "Do not use a generator in a hot path. Iterate arrays with an index.",
|
|
105
|
+
async:
|
|
106
|
+
"An async function allocates a promise on each call. Reuse a promise, or accept it with a `hot-path: allow` reason.",
|
|
107
|
+
regex:
|
|
108
|
+
"Do not use a regex literal in a hot path. It makes a new RegExp each time. Move it to module scope.",
|
|
109
|
+
promise: "Do not chain a promise in a hot path. Each call allocates a promise.",
|
|
110
|
+
console: "Do not call `console` in a hot path.",
|
|
111
|
+
delete:
|
|
112
|
+
"Do not use `delete` in a hot path. It puts the object in slow dictionary mode. Set the property to a sentinel value.",
|
|
113
|
+
arguments: "Do not read `arguments` in a hot path. Use named parameters.",
|
|
114
|
+
numberKey: "Do not use a plain object as an integer-keyed map in a hot path. Use an array.",
|
|
115
|
+
mixed:
|
|
116
|
+
"Do not mix numbers with null or undefined in an array that a hot path reads. Use a typed array with a sentinel value.",
|
|
117
|
+
},
|
|
118
|
+
create(program) {
|
|
119
|
+
const checker = program.getTypeChecker();
|
|
120
|
+
const typeAt = (node) => checker.getTypeAtLocation(node);
|
|
121
|
+
|
|
122
|
+
const checkBinary = (node, report) => {
|
|
123
|
+
const operator = node.operatorToken.kind;
|
|
124
|
+
if (
|
|
125
|
+
(operator === ts.SyntaxKind.PlusToken || operator === ts.SyntaxKind.PlusEqualsToken) &&
|
|
126
|
+
(isStringType(typeAt(node.left)) || isStringType(typeAt(node.right)))
|
|
127
|
+
)
|
|
128
|
+
report(node, "string");
|
|
129
|
+
if (EQUALITY.has(operator)) {
|
|
130
|
+
const other = isStringLiteral(node.left)
|
|
131
|
+
? node.right
|
|
132
|
+
: isStringLiteral(node.right)
|
|
133
|
+
? node.left
|
|
134
|
+
: null;
|
|
135
|
+
if (other) {
|
|
136
|
+
if (!ts.isTypeOfExpression(other)) report(node, "compare");
|
|
137
|
+
} else if (
|
|
138
|
+
!ts.isTypeOfExpression(node.left) &&
|
|
139
|
+
!ts.isTypeOfExpression(node.right) &&
|
|
140
|
+
isStringType(typeAt(node.left)) &&
|
|
141
|
+
isStringType(typeAt(node.right))
|
|
142
|
+
)
|
|
143
|
+
report(node, "compare");
|
|
144
|
+
}
|
|
145
|
+
if (
|
|
146
|
+
ORDERING.has(operator) &&
|
|
147
|
+
isStringType(typeAt(node.left)) &&
|
|
148
|
+
isStringType(typeAt(node.right))
|
|
149
|
+
)
|
|
150
|
+
report(node, "compare");
|
|
151
|
+
if (operator === ts.SyntaxKind.InKeyword) report(node, "hash");
|
|
152
|
+
if (operator === ts.SyntaxKind.EqualsToken) {
|
|
153
|
+
const target = withoutParentheses(node.left);
|
|
154
|
+
if (ts.isArrayLiteralExpression(target) && hasMixedNumbers(checker, typeAt(node.right)))
|
|
155
|
+
report(target, "mixed");
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const checkCall = (node, report) => {
|
|
160
|
+
const callee = node.expression;
|
|
161
|
+
const method = calledMethod(callee);
|
|
162
|
+
if (method === "") return;
|
|
163
|
+
const receiver = typeAt(callee.expression);
|
|
164
|
+
if (PROMISE_METHODS.has(method) && isNamedType(receiver, "Promise")) report(node, "promise");
|
|
165
|
+
if (isNamedType(receiver, "Console")) report(node, "console");
|
|
166
|
+
const fromLibrary = isLibraryDeclaration(resolvedDeclaration(checker, node));
|
|
167
|
+
if (HASH_METHODS.has(method) && isHashType(receiver)) report(node, "hash");
|
|
168
|
+
else if (
|
|
169
|
+
STRING_COMPARE_METHODS.has(method) &&
|
|
170
|
+
fromLibrary &&
|
|
171
|
+
(isStringType(receiver) || typeName(receiver) === "RegExp")
|
|
172
|
+
)
|
|
173
|
+
report(node, "compare");
|
|
174
|
+
else if (ALLOCATING_METHODS.has(method) && fromLibrary) report(node, "allocation");
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
checkFunction(owner, context) {
|
|
179
|
+
if (owner.asteriskToken) context.report(owner, "generator");
|
|
180
|
+
for (const parameter of owner.parameters)
|
|
181
|
+
if (parameter.dotDotDotToken) context.report(parameter, "rest");
|
|
182
|
+
if (owner.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword))
|
|
183
|
+
context.report(owner, "async");
|
|
184
|
+
},
|
|
185
|
+
checkNode(node, context) {
|
|
186
|
+
const report = context.report;
|
|
187
|
+
if (ts.isNewExpression(node)) report(node, "new");
|
|
188
|
+
if (ts.isRegularExpressionLiteral(node)) report(node, "regex");
|
|
189
|
+
if (ts.isDeleteExpression(node)) report(node, "delete");
|
|
190
|
+
// The arguments object has no declaration. A property named `arguments` has one.
|
|
191
|
+
if (ts.isIdentifier(node) && node.text === "arguments") {
|
|
192
|
+
const symbol = checker.getSymbolAtLocation(node);
|
|
193
|
+
if (symbol !== undefined && !symbol.declarations?.length) report(node, "arguments");
|
|
194
|
+
}
|
|
195
|
+
if (ts.isElementAccessExpression(node)) {
|
|
196
|
+
const target = typeAt(node.expression);
|
|
197
|
+
const key = typeAt(node.argumentExpression);
|
|
198
|
+
if (
|
|
199
|
+
isNumberType(key) &&
|
|
200
|
+
!isIndexable(checker, target) &&
|
|
201
|
+
!isStringType(target) &&
|
|
202
|
+
!hasNumericLength(checker, target, node)
|
|
203
|
+
)
|
|
204
|
+
report(node, "numberKey");
|
|
205
|
+
if (hasMixedNumbers(checker, target)) report(node, "mixed");
|
|
206
|
+
if (
|
|
207
|
+
!isStringLiteral(node.argumentExpression) &&
|
|
208
|
+
isStringType(key) &&
|
|
209
|
+
!isIndexable(checker, target)
|
|
210
|
+
)
|
|
211
|
+
report(node, "hash");
|
|
212
|
+
}
|
|
213
|
+
// A destructuring read is a read of each name.
|
|
214
|
+
if (ts.isArrayBindingPattern(node) && hasMixedNumbers(checker, typeAt(node)))
|
|
215
|
+
report(node, "mixed");
|
|
216
|
+
if (
|
|
217
|
+
(ts.isBindingElement(node) && node.dotDotDotToken) ||
|
|
218
|
+
((ts.isSpreadElement(node) || ts.isSpreadAssignment(node)) && isAssignmentTarget(node))
|
|
219
|
+
)
|
|
220
|
+
report(node, "rest");
|
|
221
|
+
if (ts.isObjectLiteralExpression(node) && !isAssignmentTarget(node)) report(node, "object");
|
|
222
|
+
if (ts.isArrayLiteralExpression(node) && !isAssignmentTarget(node)) report(node, "array");
|
|
223
|
+
if (ts.isTemplateExpression(node)) report(node, "string");
|
|
224
|
+
if (ts.isBinaryExpression(node)) checkBinary(node, report);
|
|
225
|
+
if (
|
|
226
|
+
ts.isSwitchStatement(node) &&
|
|
227
|
+
node.caseBlock.clauses.some(
|
|
228
|
+
(clause) => ts.isCaseClause(clause) && isStringLiteral(clause.expression),
|
|
229
|
+
)
|
|
230
|
+
)
|
|
231
|
+
report(node, "compare");
|
|
232
|
+
if (ts.isForOfStatement(node)) {
|
|
233
|
+
const type = typeAt(node.expression);
|
|
234
|
+
if (!isIndexable(checker, type)) report(node.expression, "iterator");
|
|
235
|
+
if (hasMixedNumbers(checker, type)) report(node.expression, "mixed");
|
|
236
|
+
}
|
|
237
|
+
if (
|
|
238
|
+
ts.isSpreadElement(node) &&
|
|
239
|
+
node.parent &&
|
|
240
|
+
(ts.isCallExpression(node.parent) || ts.isNewExpression(node.parent))
|
|
241
|
+
)
|
|
242
|
+
report(node, "spread");
|
|
243
|
+
if (ts.isCallExpression(node)) checkCall(node, report);
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
},
|
|
247
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// `useCallback` returns the function that it receives, so a tag on the variable applies.
|
|
2
|
+
export const react = {
|
|
3
|
+
name: "react",
|
|
4
|
+
functionHooks: ["useCallback"],
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// The audio thread calls `process` on each worklet processor for each block of 128 samples.
|
|
8
|
+
export const audioWorklet = {
|
|
9
|
+
name: "audio-worklet",
|
|
10
|
+
roots: [
|
|
11
|
+
{
|
|
12
|
+
kind: "method",
|
|
13
|
+
owner: { extends: "AudioWorkletProcessor" },
|
|
14
|
+
methods: ["process"],
|
|
15
|
+
label: "audio",
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { calledMethod } from "../syntax.mjs";
|
|
4
|
+
import { someType, typeName } from "../types.mjs";
|
|
5
|
+
|
|
6
|
+
// A renderer can make these objects once. A command encoder and a pass are made each frame.
|
|
7
|
+
const GPU_CREATE_METHODS = new Set([
|
|
8
|
+
"createBindGroup",
|
|
9
|
+
"createBindGroupLayout",
|
|
10
|
+
"createBuffer",
|
|
11
|
+
"createComputePipeline",
|
|
12
|
+
"createComputePipelineAsync",
|
|
13
|
+
"createPipelineLayout",
|
|
14
|
+
"createQuerySet",
|
|
15
|
+
"createRenderBundleEncoder",
|
|
16
|
+
"createRenderPipeline",
|
|
17
|
+
"createRenderPipelineAsync",
|
|
18
|
+
"createSampler",
|
|
19
|
+
"createShaderModule",
|
|
20
|
+
"createTexture",
|
|
21
|
+
"createView",
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
function isGpuType(type) {
|
|
25
|
+
return someType(type, (part) => /^GPU[A-Z]/.test(typeName(part)));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const webgpu = {
|
|
29
|
+
name: "webgpu",
|
|
30
|
+
messages: {
|
|
31
|
+
gpu: "Do not create a WebGPU object in a hot path. Create it once and reuse it.",
|
|
32
|
+
},
|
|
33
|
+
create(program) {
|
|
34
|
+
const checker = program.getTypeChecker();
|
|
35
|
+
return {
|
|
36
|
+
checkNode(node, context) {
|
|
37
|
+
if (!ts.isCallExpression(node)) return;
|
|
38
|
+
const callee = node.expression;
|
|
39
|
+
if (
|
|
40
|
+
GPU_CREATE_METHODS.has(calledMethod(callee)) &&
|
|
41
|
+
isGpuType(checker.getTypeAtLocation(callee.expression))
|
|
42
|
+
)
|
|
43
|
+
context.report(node, "gpu");
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
};
|