loadwise 0.9.0 → 0.10.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 +88 -19
- package/agent/hooks/check-edited-file.mjs +146 -28
- package/dist/bin/github-action.js +3 -74
- package/dist/bin/loadwise.js +12 -12
- package/dist/lib/accessibility/aria-tables.js +10 -6
- package/dist/lib/accessibility/document-reference-rules.js +13 -3
- package/dist/lib/accessibility/jsx-elements.js +34 -16
- package/dist/lib/accessibility/native-interactive.js +51 -13
- package/dist/lib/accessibility/rules.js +4 -2
- package/dist/lib/accessibility/site-fixes/aria-typo.js +65 -0
- package/dist/lib/accessibility/site-fixes/html-language.js +45 -9
- package/dist/lib/accessibility/template-elements.js +5 -3
- package/dist/lib/editor/actions.js +15 -9
- package/dist/lib/editor/diagnostics.js +110 -107
- package/dist/lib/editor/finding-comparison.js +4 -25
- package/dist/lib/editor/hover-markdown.js +104 -0
- package/dist/lib/editor/inline-fix.js +50 -0
- package/dist/lib/editor/inline-recommendation.js +27 -0
- package/dist/lib/editor/language-server.js +94 -18
- package/dist/lib/editor/scan-session.js +2 -1
- package/dist/lib/editor/standalone-performance-examples.js +48 -0
- package/dist/lib/maintainability/analysis/analyze-source.js +2 -0
- package/dist/lib/maintainability/architecture/architecture.js +50 -2
- package/dist/lib/maintainability/cli/inputs.js +12 -5
- package/dist/lib/maintainability/cli/site-fix.js +3 -0
- package/dist/lib/maintainability/cli.js +6 -3
- package/dist/lib/maintainability/examples.js +48 -0
- package/dist/lib/maintainability/quality/modern-node-callbacks.js +188 -0
- package/dist/lib/maintainability/quality/modern-syntax.js +301 -0
- package/dist/lib/maintainability/quality/tsconfig-strict.js +307 -15
- package/dist/lib/maintainability/rule-ids.js +18 -0
- package/dist/lib/maintainability/scan-changed.js +33 -2
- package/dist/lib/maintainability/scan.js +10 -1
- package/dist/lib/maintainability/site-fixes/unused-bindings.js +15 -6
- package/dist/lib/maintainability/unused/module-usage-signatures.js +116 -0
- package/dist/lib/maintainability/unused/module-usage.js +67 -106
- package/dist/lib/maintainability/unused/unused-bindings.js +143 -8
- package/dist/lib/maintainability/unused/unused-dependencies.js +218 -0
- package/dist/lib/maintainability/unused/unused-modules.js +4 -0
- package/dist/lib/performance/analysis/missing-packages.js +3 -0
- package/dist/lib/performance/analysis/source-text.js +2 -2
- package/dist/lib/performance/analyze.js +4 -1
- package/dist/lib/performance/async/floating-promises.js +25 -4
- package/dist/lib/performance/cli/cli-output.js +9 -3
- package/dist/lib/performance/cli/cli-policy.js +25 -2
- package/dist/lib/performance/cli/cli-scan.js +25 -24
- package/dist/lib/performance/cli/scan-outcome.js +19 -1
- package/dist/lib/performance/cli/shared-security-inputs.js +27 -0
- package/dist/lib/performance/cli/watch.js +27 -8
- package/dist/lib/performance/collections/collection-lookups.js +27 -10
- package/dist/lib/performance/collections/collection-reduce.js +32 -2
- package/dist/lib/performance/collections/collections.js +28 -0
- package/dist/lib/performance/collections/repeated.js +25 -1
- package/dist/lib/performance/contracts/cli.js +22 -11
- package/dist/lib/performance/contracts/git-files.js +46 -1
- package/dist/lib/performance/datastores/pooling.js +1 -17
- package/dist/lib/performance/datastores/postgres.js +41 -33
- package/dist/lib/performance/engine/native.js +5 -1
- package/dist/lib/performance/frontend/angular-template-calls.js +187 -0
- package/dist/lib/performance/frontend/frontend-dom-perf.js +68 -8
- package/dist/lib/performance/frontend/frontend-perf.js +115 -171
- package/dist/lib/performance/frontend/frontend-render-perf.js +7 -3
- package/dist/lib/performance/frontend/jsx-element-perf.js +171 -0
- package/dist/lib/performance/frontend/next-fetch-waterfall.js +346 -29
- package/dist/lib/performance/frontend/rxjs-subscriptions.js +155 -0
- package/dist/lib/performance/frontend/seo/client-fetched-content.js +196 -0
- package/dist/lib/performance/frontend/seo/navigation-without-link.js +116 -0
- package/dist/lib/performance/frontend/seo/next-routes.js +114 -0
- package/dist/lib/performance/frontend/seo/route-metadata.js +158 -0
- package/dist/lib/performance/frontend/seo/seo-rules.js +9 -0
- package/dist/lib/performance/frontend/seo/sitewide-noindex.js +103 -0
- package/dist/lib/performance/frontend/template-tracking.js +86 -2
- package/dist/lib/performance/frontend/vue-options-hooks.js +108 -0
- package/dist/lib/performance/orm/prisma.js +1 -25
- package/dist/lib/performance/projects.js +3 -2
- package/dist/lib/performance/query-loops/service-loops.js +38 -8
- package/dist/lib/performance/request/json-stringify-output.js +82 -4
- package/dist/lib/performance/request/promise-schedulers.js +180 -0
- package/dist/lib/performance/request/request-handlers.js +43 -4
- package/dist/lib/performance/request/unbounded-promise-all.js +13 -52
- package/dist/lib/performance/rule-ids.js +26 -2
- package/dist/lib/performance/site-fix.js +1 -0
- package/dist/lib/reliability/react-bugs.js +9 -0
- package/dist/lib/reliability/react-state-aliases.js +94 -4
- package/dist/lib/reliability/react-state.js +24 -5
- package/dist/lib/security/auth-crypto/secrets.js +121 -16
- package/dist/lib/security/cli.js +1 -1
- package/dist/lib/security/credential-entropy.js +12 -0
- package/dist/lib/security/deps/lockfiles.js +3 -1
- package/dist/lib/security/examples/files.js +110 -0
- package/dist/lib/security/examples/request-forgery.js +1 -1
- package/dist/lib/security/exposure/frontend-security.js +11 -2
- package/dist/lib/security/exposure/prototype-pollution.js +73 -4
- package/dist/lib/security/files/azure-pipelines.js +69 -0
- package/dist/lib/security/files/bitbucket-pipelines.js +32 -0
- package/dist/lib/security/files/ci-platforms.js +16 -0
- package/dist/lib/security/files/ci-shared.js +260 -0
- package/dist/lib/security/files/circleci.js +81 -0
- package/dist/lib/security/files/dockerfile.js +188 -3
- package/dist/lib/security/files/github-actions.js +490 -0
- package/dist/lib/security/files/gitlab-ci.js +90 -0
- package/dist/lib/security/files/hcl.js +264 -0
- package/dist/lib/security/files/hosting-config.js +159 -0
- package/dist/lib/security/files/http-headers.js +65 -0
- package/dist/lib/security/files/index.js +62 -7
- package/dist/lib/security/files/jenkinsfile.js +482 -0
- package/dist/lib/security/files/json-nodes.js +67 -0
- package/dist/lib/security/files/nginx.js +501 -0
- package/dist/lib/security/files/package-scripts.js +175 -0
- package/dist/lib/security/files/rule-ids.js +29 -0
- package/dist/lib/security/files/secret-values.js +31 -5
- package/dist/lib/security/files/secrets.js +44 -13
- package/dist/lib/security/files/terraform.js +345 -0
- package/dist/lib/security/injection/nosql.js +2 -1
- package/dist/lib/security/injection/sql.js +2 -2
- package/dist/lib/security/lexical-declaration.js +28 -0
- package/dist/lib/security/pg-identifier-guards.js +90 -1
- package/dist/lib/security/request-forgery/member-chain.js +41 -0
- package/dist/lib/security/request-forgery/open-redirect.js +45 -8
- package/dist/lib/security/request-forgery/shared.js +18 -3
- package/dist/lib/security/rule-descriptions.js +29 -0
- package/dist/lib/security/rule-ids.js +29 -0
- package/dist/lib/security/taint/original-string-validation.js +229 -0
- package/dist/lib/security/taint/validators.js +1 -1
- package/dist/lib/shared/agent/review-delivery.js +3 -1
- package/dist/lib/shared/analysis/ast-semantics.js +17 -1
- package/dist/lib/shared/analysis/hot-path-graph.js +55 -19
- package/dist/lib/shared/analysis/next-env-config.js +232 -0
- package/dist/lib/shared/analysis/scan-cache-git.js +114 -0
- package/dist/lib/shared/analysis/scan-cache-loadwise-packages.js +54 -0
- package/dist/lib/shared/analysis/scan-cache-project-inputs.js +221 -0
- package/dist/lib/shared/analysis/scan-cache-sources.js +235 -0
- package/dist/lib/shared/analysis/scan-cache-storage.js +24 -0
- package/dist/lib/shared/analysis/scan-cache.js +158 -174
- package/dist/lib/shared/check-watch.js +3 -3
- package/dist/lib/shared/check.js +25 -4
- package/dist/lib/shared/config.js +1 -1
- package/dist/lib/shared/conventional-projects.js +40 -0
- package/dist/lib/shared/fix/fix-command.js +2 -0
- package/dist/lib/shared/fix/fix-usage.js +47 -0
- package/dist/lib/shared/fix/project-file-access.js +43 -20
- package/dist/lib/shared/fix.js +4 -35
- package/dist/lib/shared/formats/security-cwe.js +29 -0
- package/dist/lib/shared/project-root.js +1 -1
- package/dist/lib/shared/reporting/action-comparison.js +78 -0
- package/dist/lib/shared/reporting/automatic-fix-signal.js +7 -2
- package/dist/lib/shared/reporting/check-text.js +7 -5
- package/dist/lib/shared/reporting/pillars.js +28 -8
- package/dist/lib/shared/reporting/source-report.js +4 -4
- package/dist/lib/shared/system/git.js +31 -16
- package/dist/lib/shared/system/glob.js +146 -19
- package/dist/lib/shared/system/terminal-style.js +2 -2
- package/dist/lib/shared/workspaces.js +6 -1
- package/package.json +4 -1
- package/schema/loadwise.config.schema.json +86 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// angular-template-method-call: a template binding that calls a component method (or reads a
|
|
2
|
+
// getter) whose body loops over a collection. Angular re-evaluates every binding on each change
|
|
3
|
+
// detection pass, so the loop reruns even when nothing it reads has changed.
|
|
4
|
+
import { ts } from '../engine/compiler.js';
|
|
5
|
+
import { propertyName } from '../schema/ts-helpers.js';
|
|
6
|
+
import { HTML_COMMENT, QUOTED_TEXT, metadataProperty, stringValueOf, } from './frontend-render-perf.js';
|
|
7
|
+
import { angularLocalNames } from './template-tracking.js';
|
|
8
|
+
import { walkSkippingFunctions } from './frontend-helpers.js';
|
|
9
|
+
// Array methods that visit every element (or sort them) each time they run.
|
|
10
|
+
const ITERATING_ARRAY_METHODS = new Set([
|
|
11
|
+
'every',
|
|
12
|
+
'filter',
|
|
13
|
+
'find',
|
|
14
|
+
'findIndex',
|
|
15
|
+
'findLast',
|
|
16
|
+
'findLastIndex',
|
|
17
|
+
'flatMap',
|
|
18
|
+
'forEach',
|
|
19
|
+
'map',
|
|
20
|
+
'reduce',
|
|
21
|
+
'reduceRight',
|
|
22
|
+
'some',
|
|
23
|
+
'sort',
|
|
24
|
+
'toSorted',
|
|
25
|
+
]);
|
|
26
|
+
// Interpolations, then property, `bind-` and structural attribute values. Event bindings `(click)` run only on
|
|
27
|
+
// their event, so they are never read.
|
|
28
|
+
const INTERPOLATION = /\{\{([\s\S]*?)\}\}/g;
|
|
29
|
+
const BOUND_ATTRIBUTE = /\s(?:\[[^\]\s(=]+\]|bind-[\w.-]+|\*[\w-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
|
|
30
|
+
const CONTROL_FLOW_BLOCK = /@(?:else\s+if|if|switch|case|for|defer)\s*\(/g;
|
|
31
|
+
const LET_DECLARATION = /@let\s+[$\w]+\s*=([^;]*);/g;
|
|
32
|
+
// The pipe name after a single `|` (`amount | currency`) names a pipe, never a component member; `||` is not a pipe.
|
|
33
|
+
const PIPE_NAME = /((?<!\|)\|(?!\|))\s*[$A-Z_a-z][$\w]*/g;
|
|
34
|
+
function isArrayValue(expression, checker) {
|
|
35
|
+
const type = checker.getNonNullableType(checker.getTypeAtLocation(expression));
|
|
36
|
+
const name = type.getSymbol()?.name;
|
|
37
|
+
return name === 'Array' || name === 'ReadonlyArray';
|
|
38
|
+
}
|
|
39
|
+
function isLoopStatement(node) {
|
|
40
|
+
return (ts.isForStatement(node) ||
|
|
41
|
+
ts.isForOfStatement(node) ||
|
|
42
|
+
ts.isForInStatement(node) ||
|
|
43
|
+
ts.isWhileStatement(node) ||
|
|
44
|
+
ts.isDoStatement(node));
|
|
45
|
+
}
|
|
46
|
+
// `this.other()` or `this.other`: a member the body uses, so a cheap wrapper around a loop counts too.
|
|
47
|
+
function thisMemberName(node) {
|
|
48
|
+
if (!ts.isPropertyAccessExpression(node) || node.expression.kind !== ts.SyntaxKind.ThisKeyword)
|
|
49
|
+
return;
|
|
50
|
+
return node.name.text;
|
|
51
|
+
}
|
|
52
|
+
// A getter runs when read; a method runs only when called, so `return this.calculate;` hands back the function.
|
|
53
|
+
function runsMember(node, member) {
|
|
54
|
+
if (ts.isGetAccessorDeclaration(member))
|
|
55
|
+
return true;
|
|
56
|
+
return ts.isCallExpression(node.parent) && node.parent.expression === node;
|
|
57
|
+
}
|
|
58
|
+
// Nested functions are skipped: a callback the member only defines or returns does not run when the binding is read,
|
|
59
|
+
// while one passed to an iterating array method is already counted at its call.
|
|
60
|
+
function iteratesDirectly(body, checker) {
|
|
61
|
+
let iterates = false;
|
|
62
|
+
walkSkippingFunctions(body, (node) => {
|
|
63
|
+
if (iterates)
|
|
64
|
+
return;
|
|
65
|
+
if (isLoopStatement(node))
|
|
66
|
+
iterates = true;
|
|
67
|
+
else if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) {
|
|
68
|
+
const callee = node.expression;
|
|
69
|
+
iterates = ITERATING_ARRAY_METHODS.has(callee.name.text) && isArrayValue(callee.expression, checker);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return iterates;
|
|
73
|
+
}
|
|
74
|
+
// Instance methods and getters with a body that loop, directly or through another such member.
|
|
75
|
+
function loopingMembers(classNode, checker) {
|
|
76
|
+
const members = new Map();
|
|
77
|
+
for (const member of classNode.members) {
|
|
78
|
+
const isCandidate = ts.isMethodDeclaration(member) || ts.isGetAccessorDeclaration(member);
|
|
79
|
+
if (!isCandidate || !member.body || !ts.isIdentifier(member.name))
|
|
80
|
+
continue;
|
|
81
|
+
if (member.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword))
|
|
82
|
+
continue;
|
|
83
|
+
members.set(member.name.text, { member, body: member.body });
|
|
84
|
+
}
|
|
85
|
+
const looping = new Map();
|
|
86
|
+
for (const [name, { member, body }] of members)
|
|
87
|
+
if (iteratesDirectly(body, checker))
|
|
88
|
+
looping.set(name, member);
|
|
89
|
+
let grew = true;
|
|
90
|
+
while (grew) {
|
|
91
|
+
grew = false;
|
|
92
|
+
for (const [name, { member, body }] of members) {
|
|
93
|
+
if (looping.has(name))
|
|
94
|
+
continue;
|
|
95
|
+
let usesLooping = false;
|
|
96
|
+
walkSkippingFunctions(body, (node) => {
|
|
97
|
+
const used = thisMemberName(node);
|
|
98
|
+
const usedMember = used === undefined ? undefined : looping.get(used);
|
|
99
|
+
if (usedMember && runsMember(node, usedMember))
|
|
100
|
+
usesLooping = true;
|
|
101
|
+
});
|
|
102
|
+
if (usesLooping) {
|
|
103
|
+
looping.set(name, member);
|
|
104
|
+
grew = true;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return looping;
|
|
109
|
+
}
|
|
110
|
+
// The text after an opening parenthesis up to its matching close.
|
|
111
|
+
function parenthesized(text, start) {
|
|
112
|
+
let depth = 1;
|
|
113
|
+
for (let index = start; index < text.length; index++) {
|
|
114
|
+
if (text[index] === '(')
|
|
115
|
+
depth++;
|
|
116
|
+
else if (text[index] === ')' && --depth === 0)
|
|
117
|
+
return text.slice(start, index);
|
|
118
|
+
}
|
|
119
|
+
return text.slice(start);
|
|
120
|
+
}
|
|
121
|
+
function boundExpressions(template) {
|
|
122
|
+
const text = template.replaceAll(HTML_COMMENT, '');
|
|
123
|
+
const expressions = [
|
|
124
|
+
...Array.from(text.matchAll(INTERPOLATION), (match) => match[1]),
|
|
125
|
+
...Array.from(text.matchAll(BOUND_ATTRIBUTE), (match) => match[1] ?? match[2]),
|
|
126
|
+
...Array.from(text.matchAll(CONTROL_FLOW_BLOCK), (match) => parenthesized(text, match.index + match[0].length)),
|
|
127
|
+
...Array.from(text.matchAll(LET_DECLARATION), (match) => match[1]),
|
|
128
|
+
];
|
|
129
|
+
return expressions.map((expression) => expression.replaceAll(QUOTED_TEXT, "''").replaceAll(PIPE_NAME, (_match, operator) => operator));
|
|
130
|
+
}
|
|
131
|
+
// `total()` or `this.total()` for a method; any read of `total` for a getter.
|
|
132
|
+
function referencePattern(name, member) {
|
|
133
|
+
const escaped = name.replaceAll('$', String.raw `\$`);
|
|
134
|
+
const call = ts.isMethodDeclaration(member) ? String.raw `\s*\(` : '';
|
|
135
|
+
return new RegExp(String.raw `(?:(?<![.\w$])|(?<=(?<![.\w$])this\s*\.\s*))${escaped}(?![\w$])${call}`, 'g');
|
|
136
|
+
}
|
|
137
|
+
const OBJECT_KEY_BEFORE = /[{,]\s*$/;
|
|
138
|
+
const OBJECT_KEY_AFTER = /^\s*:/;
|
|
139
|
+
// An object-literal key such as `total` in `[ngClass]="{ total: big }"` names a class, not the getter.
|
|
140
|
+
function readsMember(expression, pattern) {
|
|
141
|
+
for (const match of expression.matchAll(pattern)) {
|
|
142
|
+
const isObjectKey = OBJECT_KEY_BEFORE.test(expression.slice(0, match.index)) &&
|
|
143
|
+
OBJECT_KEY_AFTER.test(expression.slice(match.index + match[0].length));
|
|
144
|
+
if (!isObjectKey)
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
// Only an explicit `ChangeDetectionStrategy.OnPush`; `.Default` keeps checking on every pass.
|
|
150
|
+
function isOnPush(metadata) {
|
|
151
|
+
return metadata.properties.some((property) => propertyName(property) === 'changeDetection' &&
|
|
152
|
+
ts.isPropertyAssignment(property) &&
|
|
153
|
+
property.initializer.getText().trimEnd().endsWith('.OnPush'));
|
|
154
|
+
}
|
|
155
|
+
function templateTexts(metadata, external) {
|
|
156
|
+
const texts = [];
|
|
157
|
+
const inline = metadataProperty(metadata, 'template');
|
|
158
|
+
const inlineText = inline && stringValueOf(inline.initializer);
|
|
159
|
+
if (inlineText)
|
|
160
|
+
texts.push(inlineText);
|
|
161
|
+
if (external)
|
|
162
|
+
texts.push(external.text);
|
|
163
|
+
return texts;
|
|
164
|
+
}
|
|
165
|
+
function checkTemplateMethodCalls(classNode, metadata, template, context) {
|
|
166
|
+
const texts = templateTexts(metadata, template);
|
|
167
|
+
if (!texts.length)
|
|
168
|
+
return;
|
|
169
|
+
const looping = loopingMembers(classNode, context.checker);
|
|
170
|
+
if (!looping.size)
|
|
171
|
+
return;
|
|
172
|
+
const locals = new Set(texts.flatMap((text) => [...angularLocalNames(text)]));
|
|
173
|
+
const expressions = texts.flatMap(boundExpressions);
|
|
174
|
+
const onPush = isOnPush(metadata);
|
|
175
|
+
for (const [name, member] of looping) {
|
|
176
|
+
if (locals.has(name))
|
|
177
|
+
continue;
|
|
178
|
+
const pattern = referencePattern(name, member);
|
|
179
|
+
if (!expressions.some((expression) => readsMember(expression, pattern)))
|
|
180
|
+
continue;
|
|
181
|
+
const reference = ts.isMethodDeclaration(member) ? `${name}()` : name;
|
|
182
|
+
context.report(member.name, 'angular-template-method-call', `The template binds \`${reference}\`, which loops over a collection, so Angular reruns that loop on every change detection pass of this component even when nothing it reads has changed. Compute the value once when its inputs change: a \`computed()\` signal, a field updated where the data changes, or a pure pipe.`, onPush
|
|
183
|
+
? 'With a non-default change detection strategy the binding reruns only when this component is checked; dismiss if the collection stays small.'
|
|
184
|
+
: undefined);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
export { checkTemplateMethodCalls };
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// the import it comes from), never on the name alone.
|
|
7
7
|
import { isFunctionWithBody } from '../analysis/call-syntax.js';
|
|
8
8
|
import { ts } from '../engine/compiler.js';
|
|
9
|
-
import { initializerOf, objectProperty, resolvedSymbol } from '../schema/ts-helpers.js';
|
|
9
|
+
import { initializerOf, objectProperty, propertyName, resolvedSymbol } from '../schema/ts-helpers.js';
|
|
10
10
|
import { ANGULAR_MODULES, REACT_EFFECTS, REACT_MODULES, VUE_MODULES, calleeName, declaredInDomLibrary, domSymbolOf, importedName, isBrowserGlobal, isGlobalEventTarget, isVueImport, ownerNameOf, walkAll, walkSkippingFunctions, } from './frontend-helpers.js';
|
|
11
11
|
import { proveBarrelSubpaths } from './barrel-subpaths.js';
|
|
12
12
|
import { isMeasuredTextBinaryProbe } from './layout-binary-probe.js';
|
|
@@ -58,6 +58,7 @@ const DOM_WRITE_PROPS = new Set(['className', 'innerHTML', 'textContent']);
|
|
|
58
58
|
// Location.replace navigates away and is not a DOM mutation, so methods only count on these owners.
|
|
59
59
|
const DOM_MUTATION_OWNERS = new Set(['Element', 'Node', 'DOMTokenList', 'ChildNode', 'ParentNode']);
|
|
60
60
|
const SCHEDULING_CALLS = new Set(['requestAnimationFrame', 'setTimeout', 'clearTimeout']);
|
|
61
|
+
const ELEMENT_ITERABLES = new Set(['Array', 'ReadonlyArray', 'NodeList', 'NodeListOf']);
|
|
61
62
|
const LISTENER_REASON = 'Fine when this runs once for the lifetime of the app (bootstrap code); a leak when the function runs per component or per navigation.';
|
|
62
63
|
const CHEAP_HANDLER_REASON = 'The handler measures no layout, so it may be cheap enough to run on every event; dismiss it if it is.';
|
|
63
64
|
const UNKNOWN_HANDLER_REASON = 'The handler body is not visible here, so its cost could not be judged.';
|
|
@@ -120,11 +121,66 @@ function hasEnclosingLoop(loopNode) {
|
|
|
120
121
|
function checkLayoutThrashing(loopNode, context) {
|
|
121
122
|
if (hasEnclosingLoop(loopNode))
|
|
122
123
|
return;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
124
|
+
// The enclosing forEach callback's walk already covers this loop.
|
|
125
|
+
const callback = enclosingFunction(loopNode);
|
|
126
|
+
// `cards.forEach(((card) => ...) as Handler)`: the call sits above any parentheses or type assertions.
|
|
127
|
+
let caller = callback?.parent;
|
|
128
|
+
while (caller && ts.isOuterExpression(caller))
|
|
129
|
+
caller = caller.parent;
|
|
130
|
+
const scanned = caller && ts.isCallExpression(caller) ? forEachScan(caller, context.checker) : undefined;
|
|
131
|
+
if (scanned?.some((node) => loopNode.pos >= node.pos && loopNode.end <= node.end))
|
|
132
|
+
return;
|
|
133
|
+
reportLayoutThrashing(loopNode, [loopNode.statement], context);
|
|
134
|
+
}
|
|
135
|
+
// A function declared in the callback only waits when it is called, so its `await` does not end the prefix.
|
|
136
|
+
function hasAwait(node) {
|
|
137
|
+
if (ts.isFunctionLike(node))
|
|
138
|
+
return false;
|
|
139
|
+
let found = false;
|
|
140
|
+
walkSkippingFunctions(node, (inner) => {
|
|
141
|
+
found ||= ts.isAwaitExpression(inner) || (ts.isForOfStatement(inner) && inner.awaitModifier !== undefined);
|
|
127
142
|
});
|
|
143
|
+
return found;
|
|
144
|
+
}
|
|
145
|
+
// An async callback runs synchronously up to its first `await`, and `forEach` starts the next element right after,
|
|
146
|
+
// so only the statements before the first one that may wait repeat like a loop body.
|
|
147
|
+
function synchronousPrefix(body) {
|
|
148
|
+
if (!ts.isBlock(body))
|
|
149
|
+
return hasAwait(body) ? [] : [body];
|
|
150
|
+
const firstWait = body.statements.findIndex(hasAwait);
|
|
151
|
+
return firstWait === -1 ? [...body.statements] : body.statements.slice(0, firstWait);
|
|
152
|
+
}
|
|
153
|
+
// `cards.forEach((card) => ...)` and `grid.querySelectorAll(...).forEach(...)` run their callback once per element,
|
|
154
|
+
// just like a loop body. A generator's body never runs, so only plain and async callbacks count. Returns the parts
|
|
155
|
+
// of the callback that run per element.
|
|
156
|
+
function forEachScan(call, checker) {
|
|
157
|
+
const callee = ts.skipOuterExpressions(call.expression);
|
|
158
|
+
if (!ts.isPropertyAccessExpression(callee) || callee.name.text !== 'forEach')
|
|
159
|
+
return;
|
|
160
|
+
const callback = call.arguments[0] && ts.skipOuterExpressions(call.arguments[0]);
|
|
161
|
+
if (!callback || !(ts.isArrowFunction(callback) || ts.isFunctionExpression(callback)))
|
|
162
|
+
return;
|
|
163
|
+
if (ts.isFunctionExpression(callback) && callback.asteriskToken)
|
|
164
|
+
return;
|
|
165
|
+
const receiverType = checker.getNonNullableType(checker.getTypeAtLocation(callee.expression));
|
|
166
|
+
if (!ELEMENT_ITERABLES.has(receiverType.getSymbol()?.name ?? ''))
|
|
167
|
+
return;
|
|
168
|
+
const isAsync = callback.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword);
|
|
169
|
+
return isAsync ? synchronousPrefix(callback.body) : [callback.body];
|
|
170
|
+
}
|
|
171
|
+
function checkForEachLayoutThrashing(call, context) {
|
|
172
|
+
// `(card) => () => { ... }` only returns a function, and a function declared in the body only runs when called.
|
|
173
|
+
const scanned = forEachScan(call, context.checker)?.filter((node) => !ts.isFunctionLike(ts.skipOuterExpressions(node)));
|
|
174
|
+
if (scanned?.length)
|
|
175
|
+
reportLayoutThrashing(call, scanned, context);
|
|
176
|
+
}
|
|
177
|
+
function reportLayoutThrashing(loopNode, body, context) {
|
|
178
|
+
const found = { domWrite: false };
|
|
179
|
+
for (const part of body)
|
|
180
|
+
walkSkippingFunctions(part, (node) => {
|
|
181
|
+
found.layoutRead ??= layoutReadOf(node, context.checker);
|
|
182
|
+
found.domWrite ||= isDomWrite(node, context.checker);
|
|
183
|
+
});
|
|
128
184
|
const { layoutRead, domWrite } = found;
|
|
129
185
|
if (!layoutRead || !domWrite)
|
|
130
186
|
return;
|
|
@@ -251,8 +307,12 @@ function insideComponentLifecycle(node, checker) {
|
|
|
251
307
|
}
|
|
252
308
|
function optionReleasesListener(call) {
|
|
253
309
|
const options = optionsOf(call);
|
|
254
|
-
|
|
255
|
-
|
|
310
|
+
if (!options)
|
|
311
|
+
return false;
|
|
312
|
+
// `{ signal }` is as common as `{ signal: controller.signal }`.
|
|
313
|
+
const passesSignal = options.properties.some((property) => (ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) &&
|
|
314
|
+
propertyName(property) === 'signal');
|
|
315
|
+
return passesSignal || objectProperty(options, 'once')?.kind === ts.SyntaxKind.TrueKeyword;
|
|
256
316
|
}
|
|
257
317
|
function checkListenerCleanup(call, callee, eventName, context) {
|
|
258
318
|
const handler = ts.skipOuterExpressions(call.arguments[1]);
|
|
@@ -323,4 +383,4 @@ function checkHeavyBarrelImport(node, context) {
|
|
|
323
383
|
const subpaths = proveBarrelSubpaths(node, context.program.getCompilerOptions());
|
|
324
384
|
context.report(node, 'frontend-barrel-heavy-import', `Importing the root of \`${node.moduleSpecifier.text}\` pulls a large module graph into the client bundle and defeats tree-shaking. ${barrel.advice}`, barrel.reason, subpaths && { barrelPreview: { before: node.getText(), subpaths } });
|
|
325
385
|
}
|
|
326
|
-
export { checkAddEventListener, checkHeavyBarrelImport, checkLayoutThrashing, isLoop };
|
|
386
|
+
export { checkAddEventListener, checkForEachLayoutThrashing, checkHeavyBarrelImport, checkLayoutThrashing, isLoop };
|