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,116 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { declaredName } from '../architecture/architecture.js';
|
|
3
|
+
import { walkNodes } from '../analysis/walk.js';
|
|
4
|
+
import { hasModifier } from '../quality/modifiers.js';
|
|
5
|
+
import { bindingNames } from './module-usage-dynamic.js';
|
|
6
|
+
function declaredExports(statement) {
|
|
7
|
+
if (!hasModifier(statement, ts.SyntaxKind.ExportKeyword))
|
|
8
|
+
return [];
|
|
9
|
+
if (hasModifier(statement, ts.SyntaxKind.DefaultKeyword))
|
|
10
|
+
return [{ name: 'default', node: declaredName(statement) ?? statement }];
|
|
11
|
+
if (ts.isVariableStatement(statement))
|
|
12
|
+
return statement.declarationList.declarations
|
|
13
|
+
.flatMap((declaration) => bindingNames(declaration.name))
|
|
14
|
+
.map((name) => ({ name: name.text, node: name }));
|
|
15
|
+
const name = declaredName(statement);
|
|
16
|
+
if (name && ts.isIdentifier(name))
|
|
17
|
+
return [{ name: name.text, node: name }];
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
function functionSignature(declaration) {
|
|
21
|
+
return [
|
|
22
|
+
...(declaration.typeParameters ?? []),
|
|
23
|
+
...declaration.parameters.flatMap((parameter) => (parameter.type ? [parameter.type] : [])),
|
|
24
|
+
...(declaration.type ? [declaration.type] : []),
|
|
25
|
+
];
|
|
26
|
+
}
|
|
27
|
+
function isPrivateMember(member) {
|
|
28
|
+
return (hasModifier(member, ts.SyntaxKind.PrivateKeyword) || Boolean(member.name && ts.isPrivateIdentifier(member.name)));
|
|
29
|
+
}
|
|
30
|
+
function memberSignature(member) {
|
|
31
|
+
if (isPrivateMember(member))
|
|
32
|
+
return [];
|
|
33
|
+
if (ts.isFunctionLike(member))
|
|
34
|
+
return functionSignature(member);
|
|
35
|
+
if (!ts.isPropertyDeclaration(member))
|
|
36
|
+
return [];
|
|
37
|
+
return member.type ? [member.type] : [];
|
|
38
|
+
}
|
|
39
|
+
// The parts of an exported declaration a consumer sees: its type, its parameter and return types,
|
|
40
|
+
// the public members and base classes of a class. A name used there is part of the export's public
|
|
41
|
+
// contract.
|
|
42
|
+
function signatureParts(statement) {
|
|
43
|
+
if (ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement))
|
|
44
|
+
return [statement];
|
|
45
|
+
if (ts.isFunctionDeclaration(statement))
|
|
46
|
+
return functionSignature(statement);
|
|
47
|
+
if (ts.isClassDeclaration(statement))
|
|
48
|
+
return [
|
|
49
|
+
...(statement.typeParameters ?? []),
|
|
50
|
+
...(statement.heritageClauses ?? []),
|
|
51
|
+
...statement.members.flatMap(memberSignature),
|
|
52
|
+
];
|
|
53
|
+
if (!ts.isVariableStatement(statement))
|
|
54
|
+
return [];
|
|
55
|
+
return statement.declarationList.declarations.flatMap((declaration) => {
|
|
56
|
+
const initializer = declaration.initializer && ts.skipOuterExpressions(declaration.initializer);
|
|
57
|
+
const functionParts = initializer && ts.isFunctionLike(initializer) ? functionSignature(initializer) : [];
|
|
58
|
+
return [...(declaration.type ? [declaration.type] : []), ...functionParts];
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
// A type name, `typeof X`, or any reference inside a base-class expression such as `extends
|
|
62
|
+
// createZodDto(Schema)`.
|
|
63
|
+
function isContractReference(identifier) {
|
|
64
|
+
const parent = identifier.parent;
|
|
65
|
+
if (ts.isTypeReferenceNode(parent) || ts.isTypeQueryNode(parent) || ts.isExpressionWithTypeArguments(parent))
|
|
66
|
+
return true;
|
|
67
|
+
if (ts.isQualifiedName(parent))
|
|
68
|
+
return parent.left === identifier;
|
|
69
|
+
if (ts.isPropertyAccessExpression(parent) && parent.name === identifier)
|
|
70
|
+
return false;
|
|
71
|
+
return Boolean(ts.findAncestor(identifier, ts.isHeritageClause));
|
|
72
|
+
}
|
|
73
|
+
function declarationSignatures(statement) {
|
|
74
|
+
if (ts.isVariableStatement(statement) &&
|
|
75
|
+
hasModifier(statement, ts.SyntaxKind.ExportKeyword) &&
|
|
76
|
+
!hasModifier(statement, ts.SyntaxKind.DefaultKeyword)) {
|
|
77
|
+
return statement.declarationList.declarations.flatMap((declaration) => {
|
|
78
|
+
const initializer = declaration.initializer && ts.skipOuterExpressions(declaration.initializer);
|
|
79
|
+
const functionParts = initializer && ts.isFunctionLike(initializer) ? functionSignature(initializer) : [];
|
|
80
|
+
const parts = [...(declaration.type ? [declaration.type] : []), ...functionParts];
|
|
81
|
+
if (parts.length === 0)
|
|
82
|
+
return [];
|
|
83
|
+
return bindingNames(declaration.name).map((name) => ({ owner: name.text, parts }));
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
const [outer] = declaredExports(statement);
|
|
87
|
+
return outer ? [{ owner: outer.name, parts: signatureParts(statement) }] : [];
|
|
88
|
+
}
|
|
89
|
+
// For each exported declaration, the other exports whose signature names it.
|
|
90
|
+
function signatureExposure(statements) {
|
|
91
|
+
const exportNameByLocal = new Map();
|
|
92
|
+
for (const statement of statements) {
|
|
93
|
+
for (const { name, node } of declaredExports(statement))
|
|
94
|
+
if (ts.isIdentifier(node))
|
|
95
|
+
exportNameByLocal.set(node.text, name);
|
|
96
|
+
}
|
|
97
|
+
const exposedBy = new Map();
|
|
98
|
+
for (const statement of statements) {
|
|
99
|
+
for (const { owner, parts } of declarationSignatures(statement)) {
|
|
100
|
+
for (const part of parts) {
|
|
101
|
+
walkNodes(part, (node) => {
|
|
102
|
+
if (!ts.isIdentifier(node))
|
|
103
|
+
return;
|
|
104
|
+
const inner = exportNameByLocal.get(node.text);
|
|
105
|
+
if (!inner || inner === owner || !isContractReference(node))
|
|
106
|
+
return;
|
|
107
|
+
const owners = exposedBy.get(inner) ?? new Set();
|
|
108
|
+
owners.add(owner);
|
|
109
|
+
exposedBy.set(inner, owners);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return exposedBy;
|
|
115
|
+
}
|
|
116
|
+
export { declaredExports, signatureExposure };
|
|
@@ -1,121 +1,79 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import { isBuiltin } from 'node:module';
|
|
3
3
|
import ts from 'typescript';
|
|
4
|
-
import { declaredName } from '../architecture/architecture.js';
|
|
5
4
|
import { walkNodes } from '../analysis/walk.js';
|
|
6
5
|
import { hasModifier } from '../quality/modifiers.js';
|
|
7
|
-
import {
|
|
6
|
+
import { isShadowed, metaUrlLoad, nodeWorkerModule, numericConstant, resolvedPath, specifierHead, startsInTemporaryDirectory, urlModule, workerBindingScopes, } from './module-usage-dynamic.js';
|
|
7
|
+
import { declaredExports, signatureExposure } from './module-usage-signatures.js';
|
|
8
8
|
import { recordLocalNamedExportFixes, recordLocalNamedExports } from './module-usage-named-exports.js';
|
|
9
9
|
const nodeShebang = /^#![ \t]*(?:\/[^\s]*\/env[ \t]+(?:-S[ \t]+)?node|\/(?:[^/\s]+\/)*node)(?=[ \t\r\n]|$)/;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
...(declaration.typeParameters ?? []),
|
|
27
|
-
...declaration.parameters.flatMap((parameter) => (parameter.type ? [parameter.type] : [])),
|
|
28
|
-
...(declaration.type ? [declaration.type] : []),
|
|
29
|
-
];
|
|
30
|
-
}
|
|
31
|
-
function isPrivateMember(member) {
|
|
32
|
-
return (hasModifier(member, ts.SyntaxKind.PrivateKeyword) || Boolean(member.name && ts.isPrivateIdentifier(member.name)));
|
|
33
|
-
}
|
|
34
|
-
function memberSignature(member) {
|
|
35
|
-
if (isPrivateMember(member))
|
|
36
|
-
return [];
|
|
37
|
-
if (ts.isFunctionLike(member))
|
|
38
|
-
return functionSignature(member);
|
|
39
|
-
if (!ts.isPropertyDeclaration(member))
|
|
40
|
-
return [];
|
|
41
|
-
return member.type ? [member.type] : [];
|
|
42
|
-
}
|
|
43
|
-
// The parts of an exported declaration a consumer sees: its type, its parameter and return types,
|
|
44
|
-
// the public members and base classes of a class. A name used there is part of the export's public
|
|
45
|
-
// contract.
|
|
46
|
-
function signatureParts(statement) {
|
|
47
|
-
if (ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement))
|
|
48
|
-
return [statement];
|
|
49
|
-
if (ts.isFunctionDeclaration(statement))
|
|
50
|
-
return functionSignature(statement);
|
|
51
|
-
if (ts.isClassDeclaration(statement))
|
|
52
|
-
return [
|
|
53
|
-
...(statement.typeParameters ?? []),
|
|
54
|
-
...(statement.heritageClauses ?? []),
|
|
55
|
-
...statement.members.flatMap(memberSignature),
|
|
56
|
-
];
|
|
57
|
-
if (!ts.isVariableStatement(statement))
|
|
58
|
-
return [];
|
|
59
|
-
return statement.declarationList.declarations.flatMap((declaration) => {
|
|
60
|
-
const initializer = declaration.initializer && ts.skipOuterExpressions(declaration.initializer);
|
|
61
|
-
const functionParts = initializer && ts.isFunctionLike(initializer) ? functionSignature(initializer) : [];
|
|
62
|
-
return [...(declaration.type ? [declaration.type] : []), ...functionParts];
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
// A type name, `typeof X`, or any reference inside a base-class expression such as `extends
|
|
66
|
-
// createZodDto(Schema)`.
|
|
67
|
-
function isContractReference(identifier) {
|
|
68
|
-
const parent = identifier.parent;
|
|
69
|
-
if (ts.isTypeReferenceNode(parent) || ts.isTypeQueryNode(parent) || ts.isExpressionWithTypeArguments(parent))
|
|
70
|
-
return true;
|
|
71
|
-
if (ts.isQualifiedName(parent))
|
|
72
|
-
return parent.left === identifier;
|
|
73
|
-
if (ts.isPropertyAccessExpression(parent) && parent.name === identifier)
|
|
74
|
-
return false;
|
|
75
|
-
return Boolean(ts.findAncestor(identifier, ts.isHeritageClause));
|
|
76
|
-
}
|
|
77
|
-
function declarationSignatures(statement) {
|
|
78
|
-
if (ts.isVariableStatement(statement) &&
|
|
79
|
-
hasModifier(statement, ts.SyntaxKind.ExportKeyword) &&
|
|
80
|
-
!hasModifier(statement, ts.SyntaxKind.DefaultKeyword)) {
|
|
81
|
-
return statement.declarationList.declarations.flatMap((declaration) => {
|
|
82
|
-
const initializer = declaration.initializer && ts.skipOuterExpressions(declaration.initializer);
|
|
83
|
-
const functionParts = initializer && ts.isFunctionLike(initializer) ? functionSignature(initializer) : [];
|
|
84
|
-
const parts = [...(declaration.type ? [declaration.type] : []), ...functionParts];
|
|
85
|
-
if (parts.length === 0)
|
|
86
|
-
return [];
|
|
87
|
-
return bindingNames(declaration.name).map((name) => ({ owner: name.text, parts }));
|
|
88
|
-
});
|
|
10
|
+
// `const { a, b: c } = await import('./m')` reads only `a` and `b`. Any other shape, a rest
|
|
11
|
+
// element, a computed key or a `default` key (which can stand for a CommonJS module's whole
|
|
12
|
+
// exports) uses the whole module. Awaiting the import also calls an exported `then`.
|
|
13
|
+
function destructuredImportNames(call) {
|
|
14
|
+
if (call.expression.kind !== ts.SyntaxKind.ImportKeyword)
|
|
15
|
+
return 'all';
|
|
16
|
+
const awaited = call.parent;
|
|
17
|
+
const declaration = ts.isAwaitExpression(awaited) ? awaited.parent : undefined;
|
|
18
|
+
if (!declaration || !ts.isVariableDeclaration(declaration) || !ts.isObjectBindingPattern(declaration.name))
|
|
19
|
+
return 'all';
|
|
20
|
+
const names = ['then'];
|
|
21
|
+
for (const element of declaration.name.elements) {
|
|
22
|
+
const key = element.propertyName ?? element.name;
|
|
23
|
+
if (element.dotDotDotToken || !(ts.isIdentifier(key) || ts.isStringLiteral(key)) || key.text === 'default')
|
|
24
|
+
return 'all';
|
|
25
|
+
names.push(key.text);
|
|
89
26
|
}
|
|
90
|
-
|
|
91
|
-
return outer ? [{ owner: outer.name, parts: signatureParts(statement) }] : [];
|
|
27
|
+
return names;
|
|
92
28
|
}
|
|
93
|
-
//
|
|
94
|
-
|
|
95
|
-
|
|
29
|
+
// The local function behind an exported `then`: `export function then`, `export const then = function`
|
|
30
|
+
// or `export { run as then }`.
|
|
31
|
+
function exportedThen(statements) {
|
|
32
|
+
let local = 'then';
|
|
96
33
|
for (const statement of statements) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
34
|
+
if (!ts.isExportDeclaration(statement) || statement.moduleSpecifier || !statement.exportClause)
|
|
35
|
+
continue;
|
|
36
|
+
if (!ts.isNamedExports(statement.exportClause))
|
|
37
|
+
continue;
|
|
38
|
+
const item = statement.exportClause.elements.find((element) => element.name.text === 'then');
|
|
39
|
+
if (item)
|
|
40
|
+
local = (item.propertyName ?? item.name).text;
|
|
100
41
|
}
|
|
101
|
-
const exposedBy = new Map();
|
|
102
42
|
for (const statement of statements) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
43
|
+
// An overloaded `then` lists its body-less signatures before the implementation.
|
|
44
|
+
if (ts.isFunctionDeclaration(statement) && statement.name?.text === local && statement.body)
|
|
45
|
+
return statement;
|
|
46
|
+
if (!ts.isVariableStatement(statement))
|
|
47
|
+
continue;
|
|
48
|
+
const declaration = statement.declarationList.declarations.find((candidate) => ts.isIdentifier(candidate.name) && candidate.name.text === local);
|
|
49
|
+
if (declaration?.initializer)
|
|
50
|
+
return declaration.initializer;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Awaiting `import()` calls an exported `then` with the module namespace as `this`, so a `then` that
|
|
54
|
+
// reads `this` can reach every export. Arrow functions take `this` from the module, which is undefined, and
|
|
55
|
+
// nested functions, methods, accessors and classes bind their own.
|
|
56
|
+
function readsNamespaceThis(then) {
|
|
57
|
+
if (!ts.isFunctionDeclaration(then) && !ts.isFunctionExpression(then))
|
|
58
|
+
return false;
|
|
59
|
+
// An explicit stack keeps operator chains thousands deep off the call stack.
|
|
60
|
+
const pending = then.body ? [then.body] : [];
|
|
61
|
+
for (let node = pending.pop(); node; node = pending.pop()) {
|
|
62
|
+
if (node.kind === ts.SyntaxKind.ThisKeyword)
|
|
63
|
+
return true;
|
|
64
|
+
if ((ts.isFunctionLike(node) && !ts.isArrowFunction(node)) || ts.isClassLike(node))
|
|
65
|
+
continue;
|
|
66
|
+
ts.forEachChild(node, (child) => {
|
|
67
|
+
pending.push(child);
|
|
68
|
+
});
|
|
117
69
|
}
|
|
118
|
-
return
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
function thenReadsModule(statements, exports) {
|
|
73
|
+
if (!exports.some(({ name }) => name === 'then'))
|
|
74
|
+
return false;
|
|
75
|
+
const then = exportedThen(statements);
|
|
76
|
+
return then !== undefined && readsNamespaceThis(then);
|
|
119
77
|
}
|
|
120
78
|
const reflectionNames = new Set(['module', 'exports', 'require', 'eval', 'Function']);
|
|
121
79
|
const reflectionText = /\b(?:module|exports|require|eval|Function)\b|\bimport\s*\(/;
|
|
@@ -377,6 +335,8 @@ function collectModuleUsage(sourceFile, file, resolve) {
|
|
|
377
335
|
? clause.elements.map((item) => (item.propertyName ?? item.name).text)
|
|
378
336
|
: 'all';
|
|
379
337
|
const to = resolveUsage(literal, names);
|
|
338
|
+
if (!to && !/^[./]/.test(literal.text))
|
|
339
|
+
usage.unresolved.push({ specifier: literal.text, names });
|
|
380
340
|
if (!to || to === file)
|
|
381
341
|
return;
|
|
382
342
|
if (!clause)
|
|
@@ -431,6 +391,7 @@ function collectModuleUsage(sourceFile, file, resolve) {
|
|
|
431
391
|
}
|
|
432
392
|
for (const statement of sourceFile.statements)
|
|
433
393
|
recordStatement(statement);
|
|
394
|
+
usage.thenReadsModule = thenReadsModule(sourceFile.statements, usage.exports);
|
|
434
395
|
for (const [name, owners] of signatureExposure(sourceFile.statements)) {
|
|
435
396
|
for (const exported of usage.exports)
|
|
436
397
|
if (exported.name === name)
|
|
@@ -512,7 +473,7 @@ function collectModuleUsage(sourceFile, file, resolve) {
|
|
|
512
473
|
return;
|
|
513
474
|
const [argument] = node.arguments;
|
|
514
475
|
if (argument && ts.isStringLiteralLike(argument))
|
|
515
|
-
addImport(argument,
|
|
476
|
+
addImport(argument, destructuredImportNames(node), false, true);
|
|
516
477
|
else if (argument)
|
|
517
478
|
recordDynamicLoad(argument, node);
|
|
518
479
|
}
|
|
@@ -2,6 +2,7 @@ import ts from 'typescript';
|
|
|
2
2
|
import { walkNodes } from '../analysis/walk.js';
|
|
3
3
|
import { hasModifier } from '../quality/modifiers.js';
|
|
4
4
|
import { generatedSource } from '../quality/duplicates.js';
|
|
5
|
+
import { bindingNames } from './module-usage-dynamic.js';
|
|
5
6
|
import { isVendoredMinified } from '../../shared/analysis/execution-context.js';
|
|
6
7
|
import { NO_UNUSED_RULES, eslintDisabledLines, isBundledLibrary, isVendoredOrSamplePath, } from '../analysis/source-exclusions.js';
|
|
7
8
|
const automaticJsxRuntimes = [ts.JsxEmit.ReactJSX, ts.JsxEmit.ReactJSXDev];
|
|
@@ -12,7 +13,7 @@ function jsDocBlocksOf(text) {
|
|
|
12
13
|
}
|
|
13
14
|
function gatherFacts(sourceFile) {
|
|
14
15
|
const facts = {
|
|
15
|
-
|
|
16
|
+
identifiers: new Map(),
|
|
16
17
|
variableDeclarations: [],
|
|
17
18
|
functionDeclarations: [],
|
|
18
19
|
hasJsx: false,
|
|
@@ -21,7 +22,11 @@ function gatherFacts(sourceFile) {
|
|
|
21
22
|
};
|
|
22
23
|
walkNodes(sourceFile, (node) => {
|
|
23
24
|
if (ts.isIdentifier(node)) {
|
|
24
|
-
|
|
25
|
+
const occurrences = facts.identifiers.get(node.text);
|
|
26
|
+
if (occurrences)
|
|
27
|
+
occurrences.push(node);
|
|
28
|
+
else
|
|
29
|
+
facts.identifiers.set(node.text, [node]);
|
|
25
30
|
// Direct eval and `with` can read any local by name.
|
|
26
31
|
if (node.text === 'eval')
|
|
27
32
|
facts.hasDynamicScope = true;
|
|
@@ -47,6 +52,129 @@ function factsOf(sourceFile) {
|
|
|
47
52
|
factsBySourceFile.set(sourceFile, facts);
|
|
48
53
|
return facts;
|
|
49
54
|
}
|
|
55
|
+
function occurrencesOf(facts, name) {
|
|
56
|
+
return facts.identifiers.get(name) ?? [];
|
|
57
|
+
}
|
|
58
|
+
// `order.total`, `{ total: 1 }`, `{ total: sum } = order`, `Ns.Total`, `<Row total={1} />` and member names name a
|
|
59
|
+
// property, never a binding in scope.
|
|
60
|
+
function namesMember(identifier) {
|
|
61
|
+
const parent = identifier.parent;
|
|
62
|
+
if (ts.isPropertyAccessExpression(parent))
|
|
63
|
+
return parent.name === identifier;
|
|
64
|
+
if (ts.isQualifiedName(parent))
|
|
65
|
+
return parent.right === identifier;
|
|
66
|
+
if (ts.isBindingElement(parent))
|
|
67
|
+
return parent.propertyName === identifier;
|
|
68
|
+
if (ts.isJsxAttribute(parent))
|
|
69
|
+
return parent.name === identifier;
|
|
70
|
+
return ((ts.isPropertyAssignment(parent) ||
|
|
71
|
+
ts.isMethodDeclaration(parent) ||
|
|
72
|
+
ts.isPropertyDeclaration(parent) ||
|
|
73
|
+
ts.isPropertySignature(parent) ||
|
|
74
|
+
ts.isMethodSignature(parent) ||
|
|
75
|
+
ts.isGetAccessorDeclaration(parent) ||
|
|
76
|
+
ts.isSetAccessorDeclaration(parent) ||
|
|
77
|
+
ts.isEnumMember(parent)) &&
|
|
78
|
+
parent.name === identifier);
|
|
79
|
+
}
|
|
80
|
+
// The name a parameter, variable, function or class declaration introduces.
|
|
81
|
+
function declaresBinding(identifier) {
|
|
82
|
+
const parent = identifier.parent;
|
|
83
|
+
return ((ts.isParameter(parent) ||
|
|
84
|
+
ts.isVariableDeclaration(parent) ||
|
|
85
|
+
ts.isBindingElement(parent) ||
|
|
86
|
+
ts.isFunctionDeclaration(parent) ||
|
|
87
|
+
ts.isFunctionExpression(parent) ||
|
|
88
|
+
ts.isClassDeclaration(parent) ||
|
|
89
|
+
ts.isClassExpression(parent)) &&
|
|
90
|
+
parent.name === identifier);
|
|
91
|
+
}
|
|
92
|
+
function bindsName(name, text) {
|
|
93
|
+
return bindingNames(name).some((binding) => binding.text === text);
|
|
94
|
+
}
|
|
95
|
+
function statementDeclares(statement, text) {
|
|
96
|
+
if (ts.isVariableStatement(statement))
|
|
97
|
+
return statement.declarationList.declarations.some((declaration) => bindsName(declaration.name, text));
|
|
98
|
+
return ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement) || ts.isEnumDeclaration(statement)) &&
|
|
99
|
+
statement.name?.text === text);
|
|
100
|
+
}
|
|
101
|
+
// Decorators and computed member names run outside the function, so its parameters reach only the
|
|
102
|
+
// body and parameter initializers. `via` is the child the climb came from, `viaChild` the one below it.
|
|
103
|
+
function insideParameterScope(scope, via, viaChild) {
|
|
104
|
+
if ('body' in scope && via === scope.body)
|
|
105
|
+
return true;
|
|
106
|
+
return ts.isParameter(via) && !ts.isDecorator(viaChild);
|
|
107
|
+
}
|
|
108
|
+
// The names a `var` declares anywhere in a function body, outside nested functions and class static
|
|
109
|
+
// blocks, since each binds its name for the whole body.
|
|
110
|
+
const hoistedVarsByBody = new WeakMap();
|
|
111
|
+
function hoistedVarNames(body) {
|
|
112
|
+
const cached = hoistedVarsByBody.get(body);
|
|
113
|
+
if (cached)
|
|
114
|
+
return cached;
|
|
115
|
+
const names = new Set();
|
|
116
|
+
(function visit(node) {
|
|
117
|
+
if (ts.isFunctionLike(node) || ts.isClassStaticBlockDeclaration(node))
|
|
118
|
+
return;
|
|
119
|
+
if (ts.isVariableDeclarationList(node) && !(node.flags & ts.NodeFlags.BlockScoped)) {
|
|
120
|
+
for (const declaration of node.declarations)
|
|
121
|
+
for (const binding of bindingNames(declaration.name))
|
|
122
|
+
names.add(binding.text);
|
|
123
|
+
}
|
|
124
|
+
ts.forEachChild(node, visit);
|
|
125
|
+
})(body);
|
|
126
|
+
hoistedVarsByBody.set(body, names);
|
|
127
|
+
return names;
|
|
128
|
+
}
|
|
129
|
+
// Whether this node, below the module scope, declares `text` for the code it contains. Unknown
|
|
130
|
+
// scopes answer no, which keeps the import counted as used. Parameter initializers run before the
|
|
131
|
+
// body, so a body `var` hides the import only from the body.
|
|
132
|
+
function scopeDeclares(scope, text, via, viaChild) {
|
|
133
|
+
if (ts.isFunctionLike(scope)) {
|
|
134
|
+
if (!insideParameterScope(scope, via, viaChild))
|
|
135
|
+
return false;
|
|
136
|
+
if (ts.isFunctionExpression(scope) && scope.name?.text === text)
|
|
137
|
+
return true;
|
|
138
|
+
if ('body' in scope && scope.body && via === scope.body && hoistedVarNames(scope.body).has(text))
|
|
139
|
+
return true;
|
|
140
|
+
return scope.parameters.some((parameter) => bindsName(parameter.name, text));
|
|
141
|
+
}
|
|
142
|
+
if (ts.isClassStaticBlockDeclaration(scope))
|
|
143
|
+
return hoistedVarNames(scope.body).has(text);
|
|
144
|
+
if (ts.isClassExpression(scope))
|
|
145
|
+
return scope.name?.text === text;
|
|
146
|
+
if (ts.isCatchClause(scope))
|
|
147
|
+
return scope.variableDeclaration !== undefined && bindsName(scope.variableDeclaration.name, text);
|
|
148
|
+
if (ts.isForStatement(scope) || ts.isForInStatement(scope) || ts.isForOfStatement(scope)) {
|
|
149
|
+
const initializer = scope.initializer;
|
|
150
|
+
return (initializer !== undefined &&
|
|
151
|
+
ts.isVariableDeclarationList(initializer) &&
|
|
152
|
+
initializer.declarations.some((declaration) => bindsName(declaration.name, text)));
|
|
153
|
+
}
|
|
154
|
+
if (ts.isBlock(scope))
|
|
155
|
+
return scope.statements.some((statement) => statementDeclares(statement, text));
|
|
156
|
+
if (ts.isCaseBlock(scope))
|
|
157
|
+
return scope.clauses.some((clause) => clause.statements.some((statement) => statementDeclares(statement, text)));
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
function shadowedLocally(reference) {
|
|
161
|
+
let viaChild = reference;
|
|
162
|
+
let via = reference;
|
|
163
|
+
for (let scope = reference.parent; !ts.isSourceFile(scope); scope = scope.parent) {
|
|
164
|
+
if (scopeDeclares(scope, reference.text, via, viaChild))
|
|
165
|
+
return true;
|
|
166
|
+
viaChild = via;
|
|
167
|
+
via = scope;
|
|
168
|
+
}
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
// A parameter `format` or a nested `const format` hides an imported `format`, so its reads are not
|
|
172
|
+
// reads of the import. A type position resolves names in a separate space and always counts.
|
|
173
|
+
function readsImport(occurrence) {
|
|
174
|
+
if (namesMember(occurrence) || declaresBinding(occurrence))
|
|
175
|
+
return false;
|
|
176
|
+
return Boolean(ts.findAncestor(occurrence, ts.isTypeNode)) || !shadowedLocally(occurrence);
|
|
177
|
+
}
|
|
50
178
|
function escapeForPattern(name) {
|
|
51
179
|
return name.replaceAll(/[$\\^.*+?()[\]{}|]/g, String.raw `\$&`);
|
|
52
180
|
}
|
|
@@ -80,13 +208,13 @@ function importBindingsOf(declaration) {
|
|
|
80
208
|
return [];
|
|
81
209
|
const bindings = [];
|
|
82
210
|
if (clause.name)
|
|
83
|
-
bindings.push({ name: clause.name.text, anchor: clause.name, declaration });
|
|
211
|
+
bindings.push({ name: clause.name.text, identifier: clause.name, anchor: clause.name, declaration });
|
|
84
212
|
const named = clause.namedBindings;
|
|
85
213
|
if (named && ts.isNamespaceImport(named))
|
|
86
|
-
bindings.push({ name: named.name.text, anchor: named, declaration });
|
|
214
|
+
bindings.push({ name: named.name.text, identifier: named.name, anchor: named, declaration });
|
|
87
215
|
if (named && ts.isNamedImports(named)) {
|
|
88
216
|
for (const element of named.elements)
|
|
89
|
-
bindings.push({ name: element.name.text, anchor: element, declaration });
|
|
217
|
+
bindings.push({ name: element.name.text, identifier: element.name, anchor: element, declaration });
|
|
90
218
|
}
|
|
91
219
|
return bindings;
|
|
92
220
|
}
|
|
@@ -111,7 +239,10 @@ function unusedImportBindings(sourceFile, options) {
|
|
|
111
239
|
if (!ts.isImportDeclaration(statement))
|
|
112
240
|
continue;
|
|
113
241
|
for (const binding of importBindingsOf(statement)) {
|
|
114
|
-
if (
|
|
242
|
+
if (keptByJsx.has(binding.name))
|
|
243
|
+
continue;
|
|
244
|
+
const read = occurrencesOf(known, binding.name).some((occurrence) => occurrence !== binding.identifier && readsImport(occurrence));
|
|
245
|
+
if (read)
|
|
115
246
|
continue;
|
|
116
247
|
if (mentionedInJsDoc(known, binding.name))
|
|
117
248
|
continue;
|
|
@@ -151,7 +282,11 @@ function unusedLocalFindings(sourceFile, file, facts) {
|
|
|
151
282
|
const findings = [];
|
|
152
283
|
for (const declaration of facts.variableDeclarations) {
|
|
153
284
|
const identifier = reportableLocalName(declaration, sourceFile);
|
|
154
|
-
if (!identifier
|
|
285
|
+
if (!identifier)
|
|
286
|
+
continue;
|
|
287
|
+
// Without scope analysis, any other occurrence that is not a property name may read this local.
|
|
288
|
+
const read = occurrencesOf(facts, identifier.text).some((occurrence) => occurrence !== identifier && !namesMember(occurrence));
|
|
289
|
+
if (read)
|
|
155
290
|
continue;
|
|
156
291
|
const name = identifier.text;
|
|
157
292
|
if (mentionedInJsDoc(facts, name))
|
|
@@ -181,7 +316,7 @@ function unusedFunctionFindings(sourceFile, file, options) {
|
|
|
181
316
|
const findings = [];
|
|
182
317
|
for (const declaration of facts.functionDeclarations) {
|
|
183
318
|
const name = declaration.name;
|
|
184
|
-
if (!name || name.text.startsWith('_') || facts
|
|
319
|
+
if (!name || name.text.startsWith('_') || occurrencesOf(facts, name.text).length !== 1)
|
|
185
320
|
continue;
|
|
186
321
|
if (jsxFactories?.has(name.text) ||
|
|
187
322
|
mentionedInJsDoc(facts, name.text) ||
|