loadwise 0.6.2 → 0.7.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/README.md +152 -100
- package/dist/bin/loadwise.js +126 -49
- package/dist/lib/accessibility/frontend-detect.js +6 -5
- package/dist/lib/accessibility/usage.js +10 -2
- package/dist/lib/editor/configured-setup.js +61 -0
- package/dist/lib/editor/editor-launcher.js +165 -0
- package/dist/lib/editor/fix-edits.js +19 -0
- package/dist/lib/editor/jetbrains-setup.js +53 -0
- package/dist/lib/editor/language-server.js +31 -14
- package/dist/lib/editor/managed-config.js +37 -0
- package/dist/lib/editor/native-setup.js +85 -0
- package/dist/lib/editor/release-vsix.js +61 -0
- package/dist/lib/editor/server-commands.js +2 -0
- package/dist/lib/editor/setup-server.js +87 -0
- package/dist/lib/editor/setup-vim.js +61 -0
- package/dist/lib/editor/setup.js +131 -0
- package/dist/lib/editor/stdio-setup.js +6 -0
- package/dist/lib/maintainability/cli/inputs.js +3 -2
- package/dist/lib/maintainability/cli.js +14 -5
- package/dist/lib/maintainability/examples.js +4 -0
- package/dist/lib/maintainability/rule-ids.js +3 -0
- package/dist/lib/maintainability/site-fixes/unused-bindings.js +138 -3
- package/dist/lib/maintainability/unused/unused-bindings.js +39 -4
- package/dist/lib/performance/cli/changed-lines.js +8 -1
- package/dist/lib/performance/cli/cli-arguments.js +19 -8
- package/dist/lib/performance/cli/cli-output.js +3 -0
- package/dist/lib/performance/cli.js +8 -4
- package/dist/lib/performance/datastores/pg-integrity.js +7 -0
- package/dist/lib/performance/datastores/pooling.js +6 -1
- package/dist/lib/performance/engine/native-syntax.js +3 -0
- package/dist/lib/performance/frontend/frontend-perf.js +80 -20
- package/dist/lib/performance/request/bulk-listener-registration.js +111 -0
- package/dist/lib/performance/request/fastify-receivers.js +93 -0
- package/dist/lib/performance/request/fresh-loop-socket.js +103 -0
- package/dist/lib/performance/request/mongoose-lean.js +140 -53
- package/dist/lib/performance/request/node-event-listener-leak.js +99 -5
- package/dist/lib/performance/request/phase5-ast.js +6 -7
- package/dist/lib/performance/request/phase5-rules.js +133 -13
- package/dist/lib/performance/request/request-handlers.js +120 -23
- package/dist/lib/performance/site-fixes/collection-existence.js +2 -1
- package/dist/lib/performance/site-fixes/collections.js +1 -2
- package/dist/lib/performance/site-fixes/frontend-fixes.js +10 -6
- package/dist/lib/performance/site-fixes/helpers.js +3 -4
- package/dist/lib/performance/site-fixes/integrity.js +2 -4
- package/dist/lib/reliability/react-bugs.js +11 -3
- package/dist/lib/reliability/usage.js +16 -5
- package/dist/lib/security/cli.js +15 -4
- package/dist/lib/shared/agent/agent-init.js +26 -4
- package/dist/lib/shared/agent/agent-prompt.js +74 -0
- package/dist/lib/shared/agent/agent-session.js +6 -1
- package/dist/lib/shared/agent/agent-verify.js +54 -10
- package/dist/lib/shared/agent/review-delivery.js +1 -1
- package/dist/lib/shared/analysis/scan-cache.js +3 -2
- package/dist/lib/shared/check.js +172 -20
- package/dist/lib/shared/fix/fix-convergence.js +15 -29
- package/dist/lib/shared/fix/fix-handoff.js +16 -11
- package/dist/lib/shared/fix/fix-imports.js +47 -38
- package/dist/lib/shared/fix/fix-presentation-prettier.js +1 -46
- package/dist/lib/shared/fix/fix-presentation-rows.js +45 -1
- package/dist/lib/shared/fix/fix-presentation.js +4 -3
- package/dist/lib/shared/fix/fix-safety.js +26 -1
- package/dist/lib/shared/fix/fix-scope.js +82 -0
- package/dist/lib/shared/fix/fix-terminal.js +28 -1
- package/dist/lib/shared/fix/project-file-access.js +17 -26
- package/dist/lib/shared/fix/read-fix-text.js +20 -0
- package/dist/lib/shared/fix.js +215 -102
- package/dist/lib/shared/report-fp.js +93 -0
- package/dist/lib/shared/reporting/automatic-fix-signal.js +10 -0
- package/dist/lib/shared/reporting/check-text.js +50 -0
- package/dist/lib/shared/reporting/pillars.js +45 -16
- package/dist/lib/shared/reporting/source-report.js +2 -0
- package/dist/lib/shared/system/atomic-write.js +5 -2
- package/dist/lib/shared/system/sentry.js +203 -0
- package/dist/lib/shared/system/shell-argument.js +5 -1
- package/dist/lib/shared/system/terminal-safe.js +3 -2
- package/dist/lib/shared/system/terminal-style.js +131 -0
- package/docs/assets/cli-workflow.gif +0 -0
- package/integrations/sublime/plugin.py +22 -0
- package/integrations/zed/Cargo.lock +845 -0
- package/integrations/zed/Cargo.toml +11 -0
- package/integrations/zed/extension.toml +11 -0
- package/integrations/zed/src/lib.rs +25 -0
- package/package.json +11 -3
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/commands/loadwise.md +11 -6
- package/schema/loadwise.config.schema.json +2 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { ts } from '../engine/compiler.js';
|
|
2
|
+
import { unwrapExpression } from '../../shared/analysis/ast-semantics.js';
|
|
3
|
+
import { constSource, enclosingLoop, isGlobalMethodCall, MAX_SOURCE_DEPTH, valueSymbol } from './phase5-ast.js';
|
|
4
|
+
// Follow the entries of a caller-supplied collection, including a scalar normalized to a singleton list.
|
|
5
|
+
function fromBatchInput(value, method, checker, key, depth = 0) {
|
|
6
|
+
const expression = unwrapExpression(value);
|
|
7
|
+
if (ts.isIdentifier(expression)) {
|
|
8
|
+
const declaration = valueSymbol(expression, checker)?.valueDeclaration;
|
|
9
|
+
if (declaration && ts.isParameter(declaration))
|
|
10
|
+
return !key && declaration.parent === method;
|
|
11
|
+
const source = depth < MAX_SOURCE_DEPTH && constSource(expression, checker);
|
|
12
|
+
return !!source && fromBatchInput(source, method, checker, key, depth + 1);
|
|
13
|
+
}
|
|
14
|
+
if (ts.isElementAccessExpression(expression) || ts.isPropertyAccessExpression(expression)) {
|
|
15
|
+
const indexedByKey = ts.isElementAccessExpression(expression) &&
|
|
16
|
+
ts.isIdentifier(expression.argumentExpression) &&
|
|
17
|
+
valueSymbol(expression.argumentExpression, checker)?.valueDeclaration === key;
|
|
18
|
+
return fromBatchInput(expression.expression, method, checker, indexedByKey ? undefined : key, depth);
|
|
19
|
+
}
|
|
20
|
+
if (ts.isConditionalExpression(expression)) {
|
|
21
|
+
return (fromBatchInput(expression.whenTrue, method, checker, key, depth) &&
|
|
22
|
+
fromBatchInput(expression.whenFalse, method, checker, key, depth));
|
|
23
|
+
}
|
|
24
|
+
return (ts.isArrayLiteralExpression(expression) &&
|
|
25
|
+
expression.elements.length === 1 &&
|
|
26
|
+
fromBatchInput(expression.elements[0], method, checker, key, depth));
|
|
27
|
+
}
|
|
28
|
+
function iterationEntry(loop) {
|
|
29
|
+
if (ts.isVariableDeclarationList(loop.initializer) && loop.initializer.flags & ts.NodeFlags.Const)
|
|
30
|
+
return loop.initializer.declarations[0];
|
|
31
|
+
}
|
|
32
|
+
function returnsRemoval(method, checker) {
|
|
33
|
+
const last = method.body?.statements.at(-1);
|
|
34
|
+
if (!last || !ts.isReturnStatement(last))
|
|
35
|
+
return false;
|
|
36
|
+
const disposer = unwrapExpression(last.expression);
|
|
37
|
+
if (!disposer || !ts.isArrowFunction(disposer) || disposer.parameters.length)
|
|
38
|
+
return false;
|
|
39
|
+
const body = disposer.body;
|
|
40
|
+
const statement = ts.isBlock(body) && body.statements.length === 1 ? body.statements[0] : undefined;
|
|
41
|
+
const removal = unwrapExpression(statement && ts.isExpressionStatement(statement) ? statement.expression : body);
|
|
42
|
+
if (!ts.isCallExpression(removal))
|
|
43
|
+
return false;
|
|
44
|
+
const callee = unwrapExpression(removal.expression);
|
|
45
|
+
if (!ts.isPropertyAccessExpression(callee) ||
|
|
46
|
+
callee.expression.kind !== ts.SyntaxKind.ThisKeyword ||
|
|
47
|
+
callee.name.text !== 'removeListener')
|
|
48
|
+
return false;
|
|
49
|
+
const [event, callback] = removal.arguments;
|
|
50
|
+
const refersTo = (value, index) => method.parameters[index] !== undefined &&
|
|
51
|
+
value !== undefined &&
|
|
52
|
+
ts.isIdentifier(value) &&
|
|
53
|
+
valueSymbol(value, checker)?.valueDeclaration === method.parameters[index];
|
|
54
|
+
if (!refersTo(event, 0) || !callback)
|
|
55
|
+
return false;
|
|
56
|
+
if (refersTo(callback, 1))
|
|
57
|
+
return true;
|
|
58
|
+
return (ts.isBinaryExpression(callback) &&
|
|
59
|
+
callback.operatorToken.kind === ts.SyntaxKind.BarBarToken &&
|
|
60
|
+
refersTo(callback.left, 2) &&
|
|
61
|
+
refersTo(callback.right, 1));
|
|
62
|
+
}
|
|
63
|
+
// A bulk registration API deliberately retains each supplied callback. Verify the receiver's own
|
|
64
|
+
// singular API returns its removal callback before treating this loop as delegation.
|
|
65
|
+
export function isBulkListenerRegistration(call, loop, checker) {
|
|
66
|
+
const callee = unwrapExpression(call.expression);
|
|
67
|
+
if (!ts.isPropertyAccessExpression(callee) ||
|
|
68
|
+
callee.name.text !== 'addListener' ||
|
|
69
|
+
callee.expression.kind !== ts.SyntaxKind.ThisKeyword ||
|
|
70
|
+
!ts.isForOfStatement(loop))
|
|
71
|
+
return false;
|
|
72
|
+
const method = ts.findAncestor(call, ts.isFunctionLike);
|
|
73
|
+
if (!method ||
|
|
74
|
+
!ts.isMethodDeclaration(method) ||
|
|
75
|
+
!ts.isIdentifier(method.name) ||
|
|
76
|
+
method.name.text !== 'addListeners' ||
|
|
77
|
+
!ts.isClassLike(method.parent))
|
|
78
|
+
return false;
|
|
79
|
+
const registration = checker.getSymbolAtLocation(callee.name)?.valueDeclaration;
|
|
80
|
+
if (!registration || !ts.isMethodDeclaration(registration) || registration.parent !== method.parent)
|
|
81
|
+
return false;
|
|
82
|
+
const entry = iterationEntry(loop);
|
|
83
|
+
if (!entry)
|
|
84
|
+
return false;
|
|
85
|
+
const outer = enclosingLoop(loop);
|
|
86
|
+
if (outer && !ts.isForOfStatement(outer))
|
|
87
|
+
return false;
|
|
88
|
+
const key = outer && iterationEntry(outer);
|
|
89
|
+
if (outer) {
|
|
90
|
+
// Object.keys produces a fresh list with one entry per key. A supplied list can repeat a
|
|
91
|
+
// key and register every listener in that group more than once.
|
|
92
|
+
const event = unwrapExpression(call.arguments[0]);
|
|
93
|
+
const keySource = unwrapExpression(outer.expression);
|
|
94
|
+
if (!key ||
|
|
95
|
+
enclosingLoop(outer) ||
|
|
96
|
+
!ts.isCallExpression(keySource) ||
|
|
97
|
+
!isGlobalMethodCall(keySource, 'Object', 'keys', checker) ||
|
|
98
|
+
!event ||
|
|
99
|
+
!ts.isIdentifier(event) ||
|
|
100
|
+
valueSymbol(event, checker)?.valueDeclaration !== key)
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
const callback = unwrapExpression(call.arguments[1]);
|
|
104
|
+
if (!callback)
|
|
105
|
+
return false;
|
|
106
|
+
const origin = ts.isPropertyAccessExpression(callback) ? callback.expression : callback;
|
|
107
|
+
return (ts.isIdentifier(origin) &&
|
|
108
|
+
valueSymbol(origin, checker)?.valueDeclaration === entry &&
|
|
109
|
+
fromBatchInput(loop.expression, method, checker, key) &&
|
|
110
|
+
returnsRemoval(registration, checker));
|
|
111
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { ts } from '../engine/compiler.js';
|
|
2
|
+
const bindingsByFile = new WeakMap();
|
|
3
|
+
// A name declared in more than one scope is ambiguous without a checker. Skip it rather than
|
|
4
|
+
// mistaking a shadowed parameter, import, or local for a Fastify binding.
|
|
5
|
+
function bindingsOf(file) {
|
|
6
|
+
const cached = bindingsByFile.get(file);
|
|
7
|
+
if (cached)
|
|
8
|
+
return cached;
|
|
9
|
+
const bindings = new Map();
|
|
10
|
+
function visit(node) {
|
|
11
|
+
if (ts.isIdentifier(node) && isDeclarationName(node)) {
|
|
12
|
+
bindings.set(node.text, bindings.has(node.text) ? undefined : node);
|
|
13
|
+
}
|
|
14
|
+
ts.forEachChild(node, visit);
|
|
15
|
+
}
|
|
16
|
+
visit(file);
|
|
17
|
+
bindingsByFile.set(file, bindings);
|
|
18
|
+
return bindings;
|
|
19
|
+
}
|
|
20
|
+
function isDeclarationName(node) {
|
|
21
|
+
const parent = node.parent;
|
|
22
|
+
return ('name' in parent &&
|
|
23
|
+
parent.name === node &&
|
|
24
|
+
(ts.isVariableDeclaration(parent) ||
|
|
25
|
+
ts.isBindingElement(parent) ||
|
|
26
|
+
ts.isParameter(parent) ||
|
|
27
|
+
ts.isImportClause(parent) ||
|
|
28
|
+
ts.isImportSpecifier(parent) ||
|
|
29
|
+
ts.isNamespaceImport(parent) ||
|
|
30
|
+
ts.isImportEqualsDeclaration(parent) ||
|
|
31
|
+
ts.isClassLike(parent) ||
|
|
32
|
+
ts.isFunctionLike(parent) ||
|
|
33
|
+
ts.isEnumDeclaration(parent) ||
|
|
34
|
+
ts.isModuleDeclaration(parent)));
|
|
35
|
+
}
|
|
36
|
+
function importedFastifyFactory(name) {
|
|
37
|
+
const binding = name.parent;
|
|
38
|
+
if (ts.isImportClause(binding)) {
|
|
39
|
+
const { moduleSpecifier } = binding.parent;
|
|
40
|
+
return !binding.isTypeOnly && ts.isStringLiteralLike(moduleSpecifier) && moduleSpecifier.text === 'fastify';
|
|
41
|
+
}
|
|
42
|
+
if (!ts.isImportSpecifier(binding) || binding.isTypeOnly)
|
|
43
|
+
return false;
|
|
44
|
+
const clause = binding.parent.parent;
|
|
45
|
+
const { moduleSpecifier } = clause.parent;
|
|
46
|
+
const imported = (binding.propertyName ?? binding.name).text;
|
|
47
|
+
return (!clause.isTypeOnly &&
|
|
48
|
+
(imported === 'default' || imported === 'fastify') &&
|
|
49
|
+
ts.isStringLiteralLike(moduleSpecifier) &&
|
|
50
|
+
moduleSpecifier.text === 'fastify');
|
|
51
|
+
}
|
|
52
|
+
function isFastifyRequire(expression, bindings) {
|
|
53
|
+
if (!ts.isCallExpression(expression))
|
|
54
|
+
return false;
|
|
55
|
+
const { expression: callee, arguments: args } = expression;
|
|
56
|
+
const [module] = args;
|
|
57
|
+
return (ts.isIdentifier(callee) &&
|
|
58
|
+
callee.text === 'require' &&
|
|
59
|
+
!bindings.has('require') &&
|
|
60
|
+
args.length === 1 &&
|
|
61
|
+
ts.isStringLiteralLike(module) &&
|
|
62
|
+
module.text === 'fastify');
|
|
63
|
+
}
|
|
64
|
+
function constantInitializer(identifier, bindings) {
|
|
65
|
+
const declaration = bindings.get(identifier.text)?.parent;
|
|
66
|
+
if (!declaration || !ts.isVariableDeclaration(declaration) || !declaration.initializer)
|
|
67
|
+
return;
|
|
68
|
+
if (!(declaration.parent.flags & ts.NodeFlags.Const))
|
|
69
|
+
return;
|
|
70
|
+
return ts.skipOuterExpressions(declaration.initializer);
|
|
71
|
+
}
|
|
72
|
+
function createsFastifyInstance(expression, bindings) {
|
|
73
|
+
if (!ts.isCallExpression(expression))
|
|
74
|
+
return false;
|
|
75
|
+
const factory = ts.skipOuterExpressions(expression.expression);
|
|
76
|
+
if (isFastifyRequire(factory, bindings))
|
|
77
|
+
return true;
|
|
78
|
+
if (!ts.isIdentifier(factory))
|
|
79
|
+
return false;
|
|
80
|
+
const binding = bindings.get(factory.text);
|
|
81
|
+
if (binding && importedFastifyFactory(binding))
|
|
82
|
+
return true;
|
|
83
|
+
const initializer = constantInitializer(factory, bindings);
|
|
84
|
+
return initializer !== undefined && isFastifyRequire(initializer, bindings);
|
|
85
|
+
}
|
|
86
|
+
export function isProvenFastifyReceiver(expression) {
|
|
87
|
+
const receiver = ts.skipOuterExpressions(expression);
|
|
88
|
+
if (!ts.isIdentifier(receiver) && !ts.isCallExpression(receiver))
|
|
89
|
+
return false;
|
|
90
|
+
const bindings = bindingsOf(receiver.getSourceFile());
|
|
91
|
+
const value = ts.isIdentifier(receiver) ? constantInitializer(receiver, bindings) : receiver;
|
|
92
|
+
return value !== undefined && createsFastifyInstance(value, bindings);
|
|
93
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { ts } from '../engine/compiler.js';
|
|
2
|
+
import { typeParts } from '../collections/collection-bindings.js';
|
|
3
|
+
import { resolveImportedBinding, unwrapExpression } from '../../shared/analysis/ast-semantics.js';
|
|
4
|
+
import { identifiersByName, valueSymbol } from './phase5-ast.js';
|
|
5
|
+
const guardOperators = new Set(['&&', '||', '===', '!==', '<', '>', '<=', '>=']);
|
|
6
|
+
function hasDeclaredAccessor(property, checker) {
|
|
7
|
+
return !!checker
|
|
8
|
+
.getSymbolAtLocation(property.name)
|
|
9
|
+
?.declarations?.some((declaration) => ts.isGetAccessorDeclaration(declaration) || ts.isSetAccessorDeclaration(declaration));
|
|
10
|
+
}
|
|
11
|
+
function createsNetSocket(expression, checker) {
|
|
12
|
+
const allocation = unwrapExpression(expression);
|
|
13
|
+
if (!ts.isCallExpression(allocation) && !ts.isNewExpression(allocation))
|
|
14
|
+
return false;
|
|
15
|
+
const callee = unwrapExpression(allocation.expression);
|
|
16
|
+
if (ts.isPropertyAccessExpression(callee) && callee.name.text === 'Socket') {
|
|
17
|
+
const owner = resolveImportedBinding(unwrapExpression(callee.expression), checker);
|
|
18
|
+
return owner?.module === 'net' && (owner.exported === '*' || owner.exported === 'default');
|
|
19
|
+
}
|
|
20
|
+
const binding = resolveImportedBinding(callee, checker);
|
|
21
|
+
return binding?.module === 'net' && binding.exported === 'Socket';
|
|
22
|
+
}
|
|
23
|
+
function assignsFreshSocket(statement, receiver, checker) {
|
|
24
|
+
if (!ts.isExpressionStatement(statement))
|
|
25
|
+
return false;
|
|
26
|
+
const assignment = unwrapExpression(statement.expression);
|
|
27
|
+
if (!ts.isBinaryExpression(assignment) || assignment.operatorToken.kind !== ts.SyntaxKind.EqualsToken)
|
|
28
|
+
return false;
|
|
29
|
+
const target = unwrapExpression(assignment.left);
|
|
30
|
+
if (!ts.isPropertyAccessExpression(target) || target.name.text !== receiver.name.text)
|
|
31
|
+
return false;
|
|
32
|
+
if (!ts.isIdentifier(target.expression) || !ts.isIdentifier(receiver.expression))
|
|
33
|
+
return false;
|
|
34
|
+
const owner = valueSymbol(receiver.expression, checker);
|
|
35
|
+
const declaration = owner?.valueDeclaration;
|
|
36
|
+
if (!declaration || !ts.isVariableDeclaration(declaration) || !(declaration.parent.flags & ts.NodeFlags.Const))
|
|
37
|
+
return false;
|
|
38
|
+
if (valueSymbol(target.expression, checker) !== owner || hasDeclaredAccessor(receiver, checker))
|
|
39
|
+
return false;
|
|
40
|
+
const initializer = unwrapExpression(assignment.right);
|
|
41
|
+
if (createsNetSocket(initializer, checker))
|
|
42
|
+
return true;
|
|
43
|
+
if (!ts.isCallExpression(initializer) || !ts.isIdentifier(initializer.expression))
|
|
44
|
+
return false;
|
|
45
|
+
const factorySymbol = valueSymbol(initializer.expression, checker);
|
|
46
|
+
const factory = factorySymbol?.valueDeclaration;
|
|
47
|
+
if (!factory || !ts.isFunctionDeclaration(factory) || factory.body?.statements.length !== 1)
|
|
48
|
+
return false;
|
|
49
|
+
const [result] = factory.body.statements;
|
|
50
|
+
if (!ts.isReturnStatement(result) || !result.expression || !createsNetSocket(result.expression, checker))
|
|
51
|
+
return false;
|
|
52
|
+
const references = identifiersByName(factory.getSourceFile()).get(initializer.expression.text);
|
|
53
|
+
return !references?.some((reference) => ts.isAssignmentTarget(reference) && valueSymbol(reference, checker) === factorySymbol);
|
|
54
|
+
}
|
|
55
|
+
function simpleGuard(condition, checker) {
|
|
56
|
+
const value = unwrapExpression(condition);
|
|
57
|
+
if (ts.isIdentifier(value) || ts.isNumericLiteral(value) || value.kind === ts.SyntaxKind.ThisKeyword)
|
|
58
|
+
return true;
|
|
59
|
+
if (ts.isPropertyAccessExpression(value))
|
|
60
|
+
return !hasDeclaredAccessor(value, checker) && simpleGuard(value.expression, checker);
|
|
61
|
+
return (ts.isBinaryExpression(value) &&
|
|
62
|
+
guardOperators.has(value.operatorToken.getText()) &&
|
|
63
|
+
simpleGuard(value.left, checker) &&
|
|
64
|
+
simpleGuard(value.right, checker));
|
|
65
|
+
}
|
|
66
|
+
function updatesOtherCounter(statement, receiver, checker) {
|
|
67
|
+
if (!ts.isExpressionStatement(statement))
|
|
68
|
+
return false;
|
|
69
|
+
const update = statement.expression;
|
|
70
|
+
if (!ts.isPostfixUnaryExpression(update))
|
|
71
|
+
return false;
|
|
72
|
+
const counter = update.operand;
|
|
73
|
+
return (ts.isPropertyAccessExpression(counter) &&
|
|
74
|
+
counter.expression.kind === ts.SyntaxKind.ThisKeyword &&
|
|
75
|
+
counter.name.text !== receiver.name.text &&
|
|
76
|
+
!!checker.getSymbolAtLocation(counter.name)?.declarations?.length &&
|
|
77
|
+
!hasDeclaredAccessor(counter, checker) &&
|
|
78
|
+
typeParts(checker.getTypeAtLocation(counter)).every((member) => (member.flags & (ts.TypeFlags.NumberLike | ts.TypeFlags.BigIntLike)) !== 0));
|
|
79
|
+
}
|
|
80
|
+
// Walk only the straight-line path in this iteration. A write in a sibling branch,
|
|
81
|
+
// an intervening call, or an allocation outside the innermost loop cannot establish freshness.
|
|
82
|
+
function isFreshLoopSocket(receiver, call, loop, checker) {
|
|
83
|
+
if (!ts.isPropertyAccessExpression(receiver) || !ts.isIdentifier(receiver.expression))
|
|
84
|
+
return false;
|
|
85
|
+
if (!ts.isExpressionStatement(call.parent))
|
|
86
|
+
return false;
|
|
87
|
+
let statement = call.parent;
|
|
88
|
+
while (ts.isBlock(statement.parent) && statement.parent !== loop) {
|
|
89
|
+
const block = statement.parent;
|
|
90
|
+
for (let index = block.statements.indexOf(statement) - 1; index >= 0; index--) {
|
|
91
|
+
const prior = block.statements[index];
|
|
92
|
+
if (updatesOtherCounter(prior, receiver, checker))
|
|
93
|
+
continue;
|
|
94
|
+
return assignsFreshSocket(prior, receiver, checker);
|
|
95
|
+
}
|
|
96
|
+
const guard = block.parent;
|
|
97
|
+
if (!ts.isIfStatement(guard) || !simpleGuard(guard.expression, checker))
|
|
98
|
+
return false;
|
|
99
|
+
statement = guard;
|
|
100
|
+
}
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
export { isFreshLoopSocket };
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { ts } from '../engine/compiler.js';
|
|
2
|
-
import {
|
|
2
|
+
import { outsideWrappers } from '../query-loops/query-results.js';
|
|
3
|
+
import { declaredInPackage, symbolDeclarations } from '../schema/ts-helpers.js';
|
|
4
|
+
import { constInitializer, staticMemberName, staticPropertyName, unwrapExpression, } from '../../shared/analysis/ast-semantics.js';
|
|
5
|
+
import { bindingUses, mayWriteLimit } from './http-options-writes.js';
|
|
3
6
|
const MONGOOSE_PACKAGES = ['mongoose'];
|
|
4
7
|
const MONGOOSE_FIND_METHODS = new Set(['find', 'findOne', 'findById']);
|
|
8
|
+
const LEAN_OPTION = 'lean';
|
|
9
|
+
const MAX_OPTIONS_DEPTH = 8;
|
|
5
10
|
const MONGOOSE_QUERY_MODIFIERS = new Set([
|
|
6
11
|
'select',
|
|
7
12
|
'sort',
|
|
@@ -17,6 +22,7 @@ const MONGOOSE_QUERY_MODIFIERS = new Set([
|
|
|
17
22
|
'maxTimeMS',
|
|
18
23
|
'read',
|
|
19
24
|
'batchSize',
|
|
25
|
+
'setOptions',
|
|
20
26
|
]);
|
|
21
27
|
const MONGOOSE_MUTATION_METHODS = new Set(['save', 'markModified', 'remove', 'deleteOne']);
|
|
22
28
|
const SERIALIZATION_METHODS = new Set(['json', 'send', 'jsonp']);
|
|
@@ -26,21 +32,62 @@ function hasMongooseImport(sourceFile) {
|
|
|
26
32
|
ts.isStringLiteral(stmt.moduleSpecifier) &&
|
|
27
33
|
stmt.moduleSpecifier.text === 'mongoose');
|
|
28
34
|
}
|
|
29
|
-
function
|
|
35
|
+
function isModelRead(call, callee, sourceFile, checker, declarations) {
|
|
30
36
|
if (!MONGOOSE_FIND_METHODS.has(callee.name.text))
|
|
31
37
|
return false;
|
|
32
|
-
if (declaredInPackage(callee,
|
|
38
|
+
if (declaredInPackage(callee, checker, MONGOOSE_PACKAGES))
|
|
33
39
|
return true;
|
|
34
|
-
if (!hasMongooseImport(sourceFile))
|
|
40
|
+
if ((declarations ?? symbolDeclarations(callee, checker)).length !== 0 || !hasMongooseImport(sourceFile))
|
|
35
41
|
return false;
|
|
36
42
|
const firstArg = call.arguments[0];
|
|
37
43
|
if (firstArg && (ts.isArrowFunction(firstArg) || ts.isFunctionExpression(firstArg)))
|
|
38
44
|
return false;
|
|
39
45
|
return true;
|
|
40
46
|
}
|
|
41
|
-
function
|
|
47
|
+
function leanEnabled(expression, checker, depth = 0) {
|
|
48
|
+
const value = unwrapExpression(expression);
|
|
49
|
+
if (depth < MAX_OPTIONS_DEPTH && ts.isIdentifier(value)) {
|
|
50
|
+
const initializer = constInitializer(value, checker);
|
|
51
|
+
return initializer !== undefined && leanEnabled(initializer, checker, depth + 1);
|
|
52
|
+
}
|
|
53
|
+
return value.kind === ts.SyntaxKind.TrueKeyword || ts.isObjectLiteralExpression(value);
|
|
54
|
+
}
|
|
55
|
+
// Undefined means no override. Unknown writes, spreads or values cannot establish lean mode.
|
|
56
|
+
function readLeanOption(expression, checker, depth = 0) {
|
|
57
|
+
if (!expression)
|
|
58
|
+
return;
|
|
59
|
+
if (depth >= MAX_OPTIONS_DEPTH)
|
|
60
|
+
return false;
|
|
61
|
+
let options = unwrapExpression(expression);
|
|
62
|
+
if (ts.isIdentifier(options)) {
|
|
63
|
+
const literal = constInitializer(options, checker);
|
|
64
|
+
if (!literal ||
|
|
65
|
+
!ts.isObjectLiteralExpression(literal) ||
|
|
66
|
+
mayWriteLimit(literal, options, [LEAN_OPTION], [LEAN_OPTION], checker)) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
options = literal;
|
|
70
|
+
}
|
|
71
|
+
if (!ts.isObjectLiteralExpression(options))
|
|
72
|
+
return false;
|
|
73
|
+
for (const property of options.properties.toReversed()) {
|
|
74
|
+
if (ts.isSpreadAssignment(property)) {
|
|
75
|
+
const spreadLean = readLeanOption(property.expression, checker, depth + 1);
|
|
76
|
+
if (spreadLean !== undefined)
|
|
77
|
+
return spreadLean;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const name = staticPropertyName(property.name);
|
|
81
|
+
if (name === undefined && property.name && ts.isComputedPropertyName(property.name))
|
|
82
|
+
return false;
|
|
83
|
+
if (name === LEAN_OPTION)
|
|
84
|
+
return ts.isPropertyAssignment(property) && leanEnabled(property.initializer, checker);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function unwrapQueryChain(call, checker, modelRead = true) {
|
|
42
88
|
let current = call;
|
|
43
|
-
|
|
89
|
+
// The second model argument is projection, even for an object with a field named "lean".
|
|
90
|
+
let hasLean = modelRead && readLeanOption(call.arguments[2], checker) === true;
|
|
44
91
|
while (current.parent) {
|
|
45
92
|
if (!ts.isPropertyAccessExpression(current.parent) || current.parent.expression !== current)
|
|
46
93
|
break;
|
|
@@ -48,8 +95,10 @@ function unwrapQueryChain(call) {
|
|
|
48
95
|
if (!nextCall || !ts.isCallExpression(nextCall) || nextCall.expression !== current.parent)
|
|
49
96
|
break;
|
|
50
97
|
const method = current.parent.name.text;
|
|
51
|
-
if (method ===
|
|
52
|
-
hasLean =
|
|
98
|
+
if (method === LEAN_OPTION)
|
|
99
|
+
hasLean = !nextCall.arguments[0] || leanEnabled(nextCall.arguments[0], checker);
|
|
100
|
+
if (method === 'setOptions')
|
|
101
|
+
hasLean = readLeanOption(nextCall.arguments[0], checker) ?? hasLean;
|
|
53
102
|
if (!MONGOOSE_QUERY_MODIFIERS.has(method))
|
|
54
103
|
break;
|
|
55
104
|
current = nextCall;
|
|
@@ -60,6 +109,59 @@ function unwrapQueryChain(call) {
|
|
|
60
109
|
}
|
|
61
110
|
return { outer, hasLean };
|
|
62
111
|
}
|
|
112
|
+
function initializedVariable(initializer) {
|
|
113
|
+
const parent = initializer.parent;
|
|
114
|
+
return parent && ts.isVariableDeclaration(parent) && parent.initializer === initializer ? parent : undefined;
|
|
115
|
+
}
|
|
116
|
+
// A repository method is distinct from a model. Report a wrapper only when its implementation
|
|
117
|
+
// shows a hydrated model document that it immediately uses as a plain object.
|
|
118
|
+
function convertsReadDocument(outer, checker) {
|
|
119
|
+
const declaration = initializedVariable(outer);
|
|
120
|
+
if (!declaration)
|
|
121
|
+
return false;
|
|
122
|
+
const converted = (value) => {
|
|
123
|
+
const document = initializedVariable(value);
|
|
124
|
+
if (!document)
|
|
125
|
+
return false;
|
|
126
|
+
const uses = bindingUses(document, checker);
|
|
127
|
+
return (uses.length > 0 &&
|
|
128
|
+
uses.every((use) => {
|
|
129
|
+
const reference = outsideWrappers(use);
|
|
130
|
+
const member = reference.parent;
|
|
131
|
+
return ((ts.isPropertyAccessExpression(member) || ts.isElementAccessExpression(member)) &&
|
|
132
|
+
member.expression === reference &&
|
|
133
|
+
staticMemberName(member) === 'toObject' &&
|
|
134
|
+
ts.isCallExpression(member.parent) &&
|
|
135
|
+
member.parent.expression === member);
|
|
136
|
+
}));
|
|
137
|
+
};
|
|
138
|
+
if (ts.isAwaitExpression(unwrapExpression(outer)))
|
|
139
|
+
return converted(outer);
|
|
140
|
+
return bindingUses(declaration, checker).some((use) => {
|
|
141
|
+
const awaited = use.parent;
|
|
142
|
+
return ts.isAwaitExpression(awaited) && awaited.expression === use && converted(awaited);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
function hasHydratingRepositoryRead(declarations, checker) {
|
|
146
|
+
const implementation = declarations.find((node) => ts.isMethodDeclaration(node) && node.body);
|
|
147
|
+
if (!implementation || !ts.isMethodDeclaration(implementation) || !implementation.body)
|
|
148
|
+
return false;
|
|
149
|
+
const sourceFile = implementation.getSourceFile();
|
|
150
|
+
function findRead(node) {
|
|
151
|
+
if (ts.isFunctionLike(node))
|
|
152
|
+
return;
|
|
153
|
+
if (ts.isCallExpression(node)) {
|
|
154
|
+
const callee = unwrapExpression(node.expression);
|
|
155
|
+
if (ts.isPropertyAccessExpression(callee) && isModelRead(node, callee, sourceFile, checker)) {
|
|
156
|
+
const { outer, hasLean } = unwrapQueryChain(node, checker);
|
|
157
|
+
if (!hasLean && convertsReadDocument(outer, checker))
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return ts.forEachChild(node, findRead);
|
|
162
|
+
}
|
|
163
|
+
return ts.forEachChild(implementation.body, findRead) === true;
|
|
164
|
+
}
|
|
63
165
|
function isJsonSerializationCall(node, argNode) {
|
|
64
166
|
if (!ts.isCallExpression(node) || !node.arguments.some((arg) => arg === argNode))
|
|
65
167
|
return false;
|
|
@@ -85,65 +187,50 @@ function isDirectReadReturnOrSerialize(outer) {
|
|
|
85
187
|
return true;
|
|
86
188
|
return false;
|
|
87
189
|
}
|
|
88
|
-
function isVariableReturnedOrSerialized(
|
|
89
|
-
let mutated = false;
|
|
190
|
+
function isVariableReturnedOrSerialized(declaration, checker) {
|
|
90
191
|
let returnedOrSerialized = false;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (ts.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
192
|
+
for (const use of bindingUses(declaration, checker)) {
|
|
193
|
+
const result = outsideWrappers(use);
|
|
194
|
+
const parent = result.parent;
|
|
195
|
+
if (ts.isAssignmentTarget(result))
|
|
196
|
+
return false;
|
|
197
|
+
if ((ts.isPropertyAccessExpression(parent) || ts.isElementAccessExpression(parent)) &&
|
|
198
|
+
parent.expression === result) {
|
|
199
|
+
const member = outsideWrappers(parent);
|
|
200
|
+
const consumer = member.parent;
|
|
201
|
+
const method = staticMemberName(parent);
|
|
202
|
+
if (ts.isAssignmentTarget(member) ||
|
|
203
|
+
ts.isDeleteExpression(consumer) ||
|
|
204
|
+
(ts.isCallExpression(consumer) &&
|
|
205
|
+
consumer.expression === member &&
|
|
206
|
+
method !== undefined &&
|
|
207
|
+
MONGOOSE_MUTATION_METHODS.has(method))) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
109
210
|
}
|
|
110
|
-
if (ts.isReturnStatement(
|
|
111
|
-
node.expression &&
|
|
112
|
-
ts.isIdentifier(node.expression) &&
|
|
113
|
-
node.expression.text === varName) {
|
|
211
|
+
if ((ts.isReturnStatement(parent) && parent.expression === result) || isJsonSerializationCall(parent, result)) {
|
|
114
212
|
returnedOrSerialized = true;
|
|
115
213
|
}
|
|
116
|
-
if (ts.isCallExpression(node)) {
|
|
117
|
-
for (const arg of node.arguments) {
|
|
118
|
-
if (ts.isIdentifier(arg) && arg.text === varName && isJsonSerializationCall(node, arg)) {
|
|
119
|
-
returnedOrSerialized = true;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
ts.forEachChild(node, visit);
|
|
124
214
|
}
|
|
125
|
-
|
|
126
|
-
return !mutated && returnedOrSerialized;
|
|
215
|
+
return returnedOrSerialized;
|
|
127
216
|
}
|
|
128
217
|
function checkMongooseMissingLean(call, sourceFile, context) {
|
|
129
218
|
const callee = ts.skipOuterExpressions(call.expression);
|
|
130
219
|
if (!ts.isPropertyAccessExpression(callee))
|
|
131
220
|
return;
|
|
132
|
-
if (!
|
|
221
|
+
if (!MONGOOSE_FIND_METHODS.has(callee.name.text))
|
|
222
|
+
return;
|
|
223
|
+
const { checker } = context;
|
|
224
|
+
const declarations = symbolDeclarations(callee, checker);
|
|
225
|
+
const modelRead = isModelRead(call, callee, sourceFile, checker, declarations);
|
|
226
|
+
if (!modelRead && (!hasMongooseImport(sourceFile) || !hasHydratingRepositoryRead(declarations, checker)))
|
|
133
227
|
return;
|
|
134
|
-
const { outer, hasLean } = unwrapQueryChain(call);
|
|
228
|
+
const { outer, hasLean } = unwrapQueryChain(call, checker, modelRead);
|
|
135
229
|
if (hasLean)
|
|
136
230
|
return;
|
|
137
|
-
|
|
231
|
+
const declaration = initializedVariable(outer);
|
|
232
|
+
if (isDirectReadReturnOrSerialize(outer) || (declaration && isVariableReturnedOrSerialized(declaration, checker))) {
|
|
138
233
|
context.report(call, 'mongoose-missing-lean', 'Mongoose read query is returned or serialized without .lean(), paying full Document hydration overhead.');
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
const decl = outer.parent;
|
|
142
|
-
if (decl && ts.isVariableDeclaration(decl) && decl.initializer === outer && ts.isIdentifier(decl.name)) {
|
|
143
|
-
const scope = ts.findAncestor(decl, ts.isFunctionLike) ?? sourceFile;
|
|
144
|
-
if (isVariableReturnedOrSerialized(decl.name.text, scope)) {
|
|
145
|
-
context.report(call, 'mongoose-missing-lean', 'Mongoose read query is returned or serialized without .lean(), paying full Document hydration overhead.');
|
|
146
|
-
}
|
|
147
234
|
}
|
|
148
235
|
}
|
|
149
236
|
export { checkMongooseMissingLean };
|