@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/syntax.mjs
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export const READ = 1;
|
|
4
|
+
export const WRITE = 2;
|
|
5
|
+
|
|
6
|
+
export function normalize(fileName) {
|
|
7
|
+
return fileName.replaceAll("\\", "/");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function isFunctionLike(node) {
|
|
11
|
+
return (
|
|
12
|
+
ts.isFunctionDeclaration(node) ||
|
|
13
|
+
ts.isMethodDeclaration(node) ||
|
|
14
|
+
ts.isConstructorDeclaration(node) ||
|
|
15
|
+
ts.isGetAccessorDeclaration(node) ||
|
|
16
|
+
ts.isSetAccessorDeclaration(node) ||
|
|
17
|
+
ts.isArrowFunction(node) ||
|
|
18
|
+
ts.isFunctionExpression(node)
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function tagText(tag) {
|
|
23
|
+
return ts.getTextOfJSDocComment(tag.comment)?.trim() ?? "";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function isNested(node) {
|
|
27
|
+
return ts.findAncestor(node.parent, isFunctionLike) !== undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// The class member that a function implements: a method, or a property whose initializer is
|
|
31
|
+
// the function.
|
|
32
|
+
export function classMember(node) {
|
|
33
|
+
if (ts.isMethodDeclaration(node)) return node;
|
|
34
|
+
if (
|
|
35
|
+
(ts.isArrowFunction(node) || ts.isFunctionExpression(node)) &&
|
|
36
|
+
node.parent &&
|
|
37
|
+
ts.isPropertyDeclaration(node.parent) &&
|
|
38
|
+
node.parent.initializer === node
|
|
39
|
+
)
|
|
40
|
+
return node.parent;
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function memberName(member) {
|
|
45
|
+
return member.name && ts.isIdentifier(member.name) ? member.name.text : "";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function heritageNames(classNode, token) {
|
|
49
|
+
const names = [];
|
|
50
|
+
for (const clause of classNode.heritageClauses ?? [])
|
|
51
|
+
if (clause.token === token)
|
|
52
|
+
for (const type of clause.types)
|
|
53
|
+
if (ts.isIdentifier(type.expression)) names.push(type.expression.text);
|
|
54
|
+
return names;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The text of a property name such as `a`, `"a"`, or `["a"]`, or "" for a computed name.
|
|
58
|
+
export function literalName(name) {
|
|
59
|
+
if (ts.isIdentifier(name) || ts.isStringLiteral(name)) return name.text;
|
|
60
|
+
if (ts.isComputedPropertyName(name) && ts.isStringLiteral(name.expression))
|
|
61
|
+
return name.expression.text;
|
|
62
|
+
return "";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function withoutParentheses(expression) {
|
|
66
|
+
let current = expression;
|
|
67
|
+
while (ts.isParenthesizedExpression(current)) current = current.expression;
|
|
68
|
+
return current;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function isStringLiteral(node) {
|
|
72
|
+
return ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function isAssignmentTarget(node) {
|
|
76
|
+
let current = node;
|
|
77
|
+
while (
|
|
78
|
+
current.parent &&
|
|
79
|
+
(ts.isArrayLiteralExpression(current.parent) ||
|
|
80
|
+
ts.isObjectLiteralExpression(current.parent) ||
|
|
81
|
+
ts.isPropertyAssignment(current.parent) ||
|
|
82
|
+
ts.isShorthandPropertyAssignment(current.parent) ||
|
|
83
|
+
ts.isSpreadElement(current.parent) ||
|
|
84
|
+
ts.isSpreadAssignment(current.parent))
|
|
85
|
+
)
|
|
86
|
+
current = current.parent;
|
|
87
|
+
const parent = current.parent;
|
|
88
|
+
return (
|
|
89
|
+
(parent &&
|
|
90
|
+
ts.isBinaryExpression(parent) &&
|
|
91
|
+
parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
92
|
+
parent.left === current) ||
|
|
93
|
+
(parent && ts.isForOfStatement(parent) && parent.initializer === current)
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// A property access reads the property, writes it, or both, as in `count += 1`.
|
|
98
|
+
export function accessKind(node) {
|
|
99
|
+
let target = node;
|
|
100
|
+
while (target.parent && ts.isParenthesizedExpression(target.parent)) target = target.parent;
|
|
101
|
+
const parent = target.parent;
|
|
102
|
+
if (
|
|
103
|
+
parent &&
|
|
104
|
+
ts.isBinaryExpression(parent) &&
|
|
105
|
+
parent.left === target &&
|
|
106
|
+
parent.operatorToken.kind >= ts.SyntaxKind.FirstAssignment &&
|
|
107
|
+
parent.operatorToken.kind <= ts.SyntaxKind.LastAssignment
|
|
108
|
+
)
|
|
109
|
+
return parent.operatorToken.kind === ts.SyntaxKind.EqualsToken ? WRITE : READ | WRITE;
|
|
110
|
+
if (
|
|
111
|
+
parent &&
|
|
112
|
+
(ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent)) &&
|
|
113
|
+
(parent.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
114
|
+
parent.operator === ts.SyntaxKind.MinusMinusToken)
|
|
115
|
+
)
|
|
116
|
+
return READ | WRITE;
|
|
117
|
+
return isAssignmentTarget(target) ? WRITE : READ;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function symbolAt(checker, node) {
|
|
121
|
+
const symbol = checker.getSymbolAtLocation(node);
|
|
122
|
+
return symbol && symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// A function hook such as React's `useCallback` returns the function that it receives, so a
|
|
126
|
+
// tag on the variable that holds the hook result applies to the function.
|
|
127
|
+
export function createSyntax(checker, functionHooks) {
|
|
128
|
+
const isFunctionHook = (node) =>
|
|
129
|
+
ts.isCallExpression(node) &&
|
|
130
|
+
ts.isIdentifier(node.expression) &&
|
|
131
|
+
functionHooks.has(node.expression.text);
|
|
132
|
+
|
|
133
|
+
const holdsFunction = (node) =>
|
|
134
|
+
ts.isParenthesizedExpression(node) ||
|
|
135
|
+
ts.isExportAssignment(node) ||
|
|
136
|
+
ts.isVariableDeclaration(node) ||
|
|
137
|
+
ts.isVariableDeclarationList(node) ||
|
|
138
|
+
ts.isVariableStatement(node) ||
|
|
139
|
+
ts.isPropertyDeclaration(node) ||
|
|
140
|
+
ts.isPropertyAssignment(node) ||
|
|
141
|
+
isFunctionHook(node);
|
|
142
|
+
|
|
143
|
+
// A tag belongs to the function, or to the declaration whose initializer is the function.
|
|
144
|
+
// A class or an object tag does not apply to its members.
|
|
145
|
+
const findTag = (node, name) => {
|
|
146
|
+
for (let current = node; current; current = current.parent) {
|
|
147
|
+
if (current !== node && !holdsFunction(current)) return null;
|
|
148
|
+
const tag = ts.getJSDocTags(current).find((candidate) => candidate.tagName.text === name);
|
|
149
|
+
if (tag) return tag;
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// `seen` stops a cycle of names such as `var a = b; var b = a;`.
|
|
155
|
+
const resolveBody = (declaration, seen) => {
|
|
156
|
+
if (seen.has(declaration)) return null;
|
|
157
|
+
seen.add(declaration);
|
|
158
|
+
if (isFunctionLike(declaration) && declaration.body) return declaration;
|
|
159
|
+
if (
|
|
160
|
+
!ts.isVariableDeclaration(declaration) &&
|
|
161
|
+
!ts.isPropertyDeclaration(declaration) &&
|
|
162
|
+
!ts.isPropertyAssignment(declaration)
|
|
163
|
+
)
|
|
164
|
+
return null;
|
|
165
|
+
let initializer = declaration.initializer;
|
|
166
|
+
while (initializer && ts.isParenthesizedExpression(initializer))
|
|
167
|
+
initializer = initializer.expression;
|
|
168
|
+
if (initializer && isFunctionHook(initializer)) initializer = initializer.arguments[0];
|
|
169
|
+
if (!initializer) return null;
|
|
170
|
+
if (ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer)) return initializer;
|
|
171
|
+
if (!ts.isIdentifier(initializer)) return null;
|
|
172
|
+
const target = symbolAt(checker, initializer)?.valueDeclaration;
|
|
173
|
+
return target ? resolveBody(target, seen) : null;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// A declaration whose initializer names a function, such as `prepare = allocate`, has the
|
|
177
|
+
// body of that function.
|
|
178
|
+
const bodyOf = (declaration) => resolveBody(declaration, new Set());
|
|
179
|
+
|
|
180
|
+
const functionOf = (expression) => {
|
|
181
|
+
if (ts.isArrowFunction(expression) || ts.isFunctionExpression(expression)) return expression;
|
|
182
|
+
if (!ts.isIdentifier(expression) && !ts.isPropertyAccessExpression(expression)) return null;
|
|
183
|
+
const declaration = symbolAt(
|
|
184
|
+
checker,
|
|
185
|
+
ts.isPropertyAccessExpression(expression) ? expression.name : expression,
|
|
186
|
+
)?.valueDeclaration;
|
|
187
|
+
return declaration ? bodyOf(declaration) : null;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const tagsFunction = (node) => {
|
|
191
|
+
if (isFunctionLike(node)) return true;
|
|
192
|
+
if (ts.isVariableStatement(node))
|
|
193
|
+
return node.declarationList.declarations.some((declaration) => bodyOf(declaration) !== null);
|
|
194
|
+
if (
|
|
195
|
+
ts.isVariableDeclaration(node) ||
|
|
196
|
+
ts.isPropertyDeclaration(node) ||
|
|
197
|
+
ts.isPropertyAssignment(node)
|
|
198
|
+
)
|
|
199
|
+
return bodyOf(node) !== null;
|
|
200
|
+
if (ts.isExportAssignment(node)) return isFunctionLike(withoutParentheses(node.expression));
|
|
201
|
+
return false;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const functionName = (node) => {
|
|
205
|
+
const own = node.name && ts.isIdentifier(node.name) ? node.name.text : null;
|
|
206
|
+
const owner = node.parent && ts.isClassLike(node.parent) ? node.parent : null;
|
|
207
|
+
if (ts.isConstructorDeclaration(node)) return `${owner?.name?.text ?? "class"}.constructor`;
|
|
208
|
+
if (own && owner?.name) return `${owner.name.text}.${own}`;
|
|
209
|
+
if (own) return own;
|
|
210
|
+
const holder = node.parent && isFunctionHook(node.parent) ? node.parent.parent : node.parent;
|
|
211
|
+
if (
|
|
212
|
+
holder &&
|
|
213
|
+
(ts.isVariableDeclaration(holder) ||
|
|
214
|
+
ts.isPropertyDeclaration(holder) ||
|
|
215
|
+
ts.isPropertyAssignment(holder))
|
|
216
|
+
)
|
|
217
|
+
return holder.name.getText();
|
|
218
|
+
return "function";
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
findTag,
|
|
223
|
+
hasTag: (node, name) => findTag(node, name) !== null,
|
|
224
|
+
bodyOf,
|
|
225
|
+
functionOf,
|
|
226
|
+
tagsFunction,
|
|
227
|
+
functionName,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// The method name of a call such as `a.b()` or `a["b"]()`, or "" for another callee.
|
|
232
|
+
export function calledMethod(callee) {
|
|
233
|
+
if (ts.isPropertyAccessExpression(callee)) return callee.name.text;
|
|
234
|
+
if (ts.isElementAccessExpression(callee) && isStringLiteral(callee.argumentExpression))
|
|
235
|
+
return callee.argumentExpression.text;
|
|
236
|
+
return "";
|
|
237
|
+
}
|
package/src/types.mjs
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export function typeName(type) {
|
|
4
|
+
return type.getSymbol()?.getName() ?? type.aliasSymbol?.getName() ?? "";
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// A receiver such as `texture?.` or `A | B` matches when one of its parts matches.
|
|
8
|
+
export function someType(type, test) {
|
|
9
|
+
if (type.isUnion() || type.isIntersection())
|
|
10
|
+
return type.types.some((part) => someType(part, test));
|
|
11
|
+
// A generic receiver such as `T extends HTMLElement` matches by its constraint.
|
|
12
|
+
if (type.flags & ts.TypeFlags.TypeParameter) {
|
|
13
|
+
const constraint = type.getConstraint();
|
|
14
|
+
return constraint !== undefined && someType(constraint, test);
|
|
15
|
+
}
|
|
16
|
+
return test(type);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function isNamedType(type, name) {
|
|
20
|
+
return someType(type, (part) => typeName(part) === name);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function isNullish(type) {
|
|
24
|
+
return (type.flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) !== 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function isNumberType(type) {
|
|
28
|
+
if (type.isUnion()) return type.types.every(isNumberType);
|
|
29
|
+
// A branded number such as `number & { brand }` is a number.
|
|
30
|
+
if (type.isIntersection()) return type.types.some(isNumberType);
|
|
31
|
+
if (type.flags & ts.TypeFlags.TypeParameter) {
|
|
32
|
+
const constraint = type.getConstraint();
|
|
33
|
+
return constraint !== undefined && isNumberType(constraint);
|
|
34
|
+
}
|
|
35
|
+
return (type.flags & ts.TypeFlags.NumberLike) !== 0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function isStringType(type) {
|
|
39
|
+
const parts = type.isUnion() ? type.types : [type];
|
|
40
|
+
return parts.some((part) => (part.flags & ts.TypeFlags.StringLike) !== 0);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function isIndexable(checker, type) {
|
|
44
|
+
const parts = type.isUnion() ? type.types : [type];
|
|
45
|
+
return parts.every(
|
|
46
|
+
(part) =>
|
|
47
|
+
checker.isArrayType(part) ||
|
|
48
|
+
checker.isTupleType(part) ||
|
|
49
|
+
/^(?:Float|Int|Uint|BigInt|BigUint)\d+(?:Clamped)?Array$/.test(typeName(part)) ||
|
|
50
|
+
isNullish(part),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// An array-like type has a numeric `length`.
|
|
55
|
+
export function hasNumericLength(checker, type, node) {
|
|
56
|
+
const length = type.getProperty("length");
|
|
57
|
+
return length !== undefined && isNumberType(checker.getTypeOfSymbolAtLocation(length, node));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function mixesNumbers(element) {
|
|
61
|
+
if (!element?.isUnion()) return false;
|
|
62
|
+
return element.types.some(isNumberType) && element.types.some(isNullish);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// An array or tuple of numbers that also holds null or undefined loses the packed-double
|
|
66
|
+
// storage in V8.
|
|
67
|
+
export function hasMixedNumbers(checker, type) {
|
|
68
|
+
if (type.flags & ts.TypeFlags.TypeParameter) {
|
|
69
|
+
const constraint = type.getConstraint();
|
|
70
|
+
return constraint !== undefined && hasMixedNumbers(checker, constraint);
|
|
71
|
+
}
|
|
72
|
+
if (type.isUnion())
|
|
73
|
+
return type.types.some((part) => !isNullish(part) && hasMixedNumbers(checker, part));
|
|
74
|
+
if (checker.isArrayType(type)) return mixesNumbers(checker.getTypeArguments(type)[0]);
|
|
75
|
+
if (checker.isTupleType(type))
|
|
76
|
+
return mixesNumbers(checker.getUnionType(checker.getTypeArguments(type)));
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function isLibraryDeclaration(declaration) {
|
|
81
|
+
const file = declaration?.getSourceFile();
|
|
82
|
+
return !file || file.isDeclarationFile;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function resolvedDeclaration(checker, call) {
|
|
86
|
+
return checker.getResolvedSignature(call)?.getDeclaration();
|
|
87
|
+
}
|