@trebired/code-discipline 2.3.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +49 -2
- package/dist/checks/fix-folderization.js +1 -1
- package/dist/checks/index.d.ts +1 -0
- package/dist/checks/index.d.ts.map +1 -1
- package/dist/checks/index.js +93 -70
- package/dist/checks/index.js.map +1 -1
- package/dist/checks/rule-slugs.d.ts +3 -3
- package/dist/checks/rule-slugs.d.ts.map +1 -1
- package/dist/checks/rule-slugs.js +12 -8
- package/dist/checks/rule-slugs.js.map +1 -1
- package/dist/checks/rules/comments/ranges.d.ts +10 -0
- package/dist/checks/rules/comments/ranges.d.ts.map +1 -0
- package/dist/checks/rules/{comment-stripping.js → comments/ranges.js} +51 -107
- package/dist/checks/rules/comments/ranges.js.map +1 -0
- package/dist/checks/rules/{comment-stripping.d.ts → comments/stripping.d.ts} +3 -2
- package/dist/checks/rules/comments/stripping.d.ts.map +1 -0
- package/dist/checks/rules/comments/stripping.js +81 -0
- package/dist/checks/rules/comments/stripping.js.map +1 -0
- package/dist/checks/rules/dry/candidates.d.ts +8 -0
- package/dist/checks/rules/dry/candidates.d.ts.map +1 -0
- package/dist/checks/rules/dry/candidates.js +98 -0
- package/dist/checks/rules/dry/candidates.js.map +1 -0
- package/dist/checks/rules/dry/fingerprint.d.ts +15 -0
- package/dist/checks/rules/dry/fingerprint.d.ts.map +1 -0
- package/dist/checks/rules/dry/fingerprint.js +80 -0
- package/dist/checks/rules/dry/fingerprint.js.map +1 -0
- package/dist/checks/rules/dry/fix.d.ts +5 -0
- package/dist/checks/rules/dry/fix.d.ts.map +1 -0
- package/dist/checks/rules/dry/fix.js +252 -0
- package/dist/checks/rules/dry/fix.js.map +1 -0
- package/dist/checks/rules/dry/helpers.d.ts +5 -0
- package/dist/checks/rules/dry/helpers.d.ts.map +1 -0
- package/dist/checks/rules/dry/helpers.js +99 -0
- package/dist/checks/rules/dry/helpers.js.map +1 -0
- package/dist/checks/rules/dry/index.d.ts +7 -0
- package/dist/checks/rules/dry/index.d.ts.map +1 -0
- package/dist/checks/rules/dry/index.js +14 -0
- package/dist/checks/rules/dry/index.js.map +1 -0
- package/dist/checks/rules/dry/model.d.ts +37 -0
- package/dist/checks/rules/dry/model.d.ts.map +1 -0
- package/dist/checks/rules/dry/model.js +16 -0
- package/dist/checks/rules/dry/model.js.map +1 -0
- package/dist/checks/rules/dry/serialize/bindings.d.ts +8 -0
- package/dist/checks/rules/dry/serialize/bindings.d.ts.map +1 -0
- package/dist/checks/rules/dry/serialize/bindings.js +62 -0
- package/dist/checks/rules/dry/serialize/bindings.js.map +1 -0
- package/dist/checks/rules/dry/serialize/context.d.ts +18 -0
- package/dist/checks/rules/dry/serialize/context.d.ts.map +1 -0
- package/dist/checks/rules/dry/serialize/context.js +103 -0
- package/dist/checks/rules/dry/serialize/context.js.map +1 -0
- package/dist/checks/rules/dry/serialize/expressions.d.ts +10 -0
- package/dist/checks/rules/dry/serialize/expressions.d.ts.map +1 -0
- package/dist/checks/rules/dry/serialize/expressions.js +161 -0
- package/dist/checks/rules/dry/serialize/expressions.js.map +1 -0
- package/dist/checks/rules/dry/serialize/node.d.ts +5 -0
- package/dist/checks/rules/dry/serialize/node.d.ts.map +1 -0
- package/dist/checks/rules/dry/serialize/node.js +43 -0
- package/dist/checks/rules/dry/serialize/node.js.map +1 -0
- package/dist/checks/rules/dry/serialize/statements.d.ts +7 -0
- package/dist/checks/rules/dry/serialize/statements.d.ts.map +1 -0
- package/dist/checks/rules/dry/serialize/statements.js +99 -0
- package/dist/checks/rules/dry/serialize/statements.js.map +1 -0
- package/dist/checks/rules/evasion-guards/functions.d.ts +14 -0
- package/dist/checks/rules/evasion-guards/functions.d.ts.map +1 -0
- package/dist/checks/rules/evasion-guards/functions.js +83 -0
- package/dist/checks/rules/evasion-guards/functions.js.map +1 -0
- package/dist/checks/rules/evasion-guards/index.d.ts +6 -0
- package/dist/checks/rules/evasion-guards/index.d.ts.map +1 -0
- package/dist/checks/rules/evasion-guards/index.js +126 -0
- package/dist/checks/rules/evasion-guards/index.js.map +1 -0
- package/dist/checks/rules/evasion-guards/runtime.d.ts +6 -0
- package/dist/checks/rules/evasion-guards/runtime.d.ts.map +1 -0
- package/dist/checks/rules/evasion-guards/runtime.js +51 -0
- package/dist/checks/rules/evasion-guards/runtime.js.map +1 -0
- package/dist/checks/rules/evasion-guards/strip.d.ts +11 -0
- package/dist/checks/rules/evasion-guards/strip.d.ts.map +1 -0
- package/dist/checks/rules/evasion-guards/strip.js +89 -0
- package/dist/checks/rules/evasion-guards/strip.js.map +1 -0
- package/dist/checks/rules/folderize/compound-files.d.ts +6 -0
- package/dist/checks/rules/folderize/compound-files.d.ts.map +1 -0
- package/dist/checks/rules/{folderize-compound-files.js → folderize/compound-files.js} +10 -2
- package/dist/checks/rules/folderize/compound-files.js.map +1 -0
- package/dist/checks/rules/{folderize-plan.d.ts → folderize/plan.d.ts} +3 -3
- package/dist/checks/rules/folderize/plan.d.ts.map +1 -0
- package/dist/checks/rules/{folderize-plan.js → folderize/plan.js} +2 -2
- package/dist/checks/rules/folderize/plan.js.map +1 -0
- package/dist/checks/rules/max/file-lines.d.ts +6 -0
- package/dist/checks/rules/max/file-lines.d.ts.map +1 -0
- package/dist/checks/rules/{max-file-lines.js → max/file-lines.js} +9 -1
- package/dist/checks/rules/max/file-lines.js.map +1 -0
- package/dist/checks/rules/max/function-lines.d.ts +6 -0
- package/dist/checks/rules/max/function-lines.d.ts.map +1 -0
- package/dist/checks/rules/{max-function-lines.js → max/function-lines.js} +79 -61
- package/dist/checks/rules/max/function-lines.js.map +1 -0
- package/dist/checks/rules/remove-comments.d.ts.map +1 -1
- package/dist/checks/rules/remove-comments.js +10 -1
- package/dist/checks/rules/remove-comments.js.map +1 -1
- package/dist/checks/types.d.ts +25 -2
- package/dist/checks/types.d.ts.map +1 -1
- package/dist/cli/progress.d.ts +11 -0
- package/dist/cli/progress.d.ts.map +1 -0
- package/dist/cli/progress.js +65 -0
- package/dist/cli/progress.js.map +1 -0
- package/dist/cli/run-cli.d.ts.map +1 -1
- package/dist/cli/run-cli.js +91 -66
- package/dist/cli/run-cli.js.map +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +68 -62
- package/dist/config/index.js.map +1 -1
- package/dist/config/{normalize-check-options.d.ts → normalize/check-options.d.ts} +2 -2
- package/dist/config/normalize/check-options.d.ts.map +1 -0
- package/dist/config/{normalize-check-options.js → normalize/check-options.js} +6 -5
- package/dist/config/normalize/check-options.js.map +1 -0
- package/dist/config/{normalize-rule-options.d.ts → normalize/rule-options.d.ts} +9 -8
- package/dist/config/normalize/rule-options.d.ts.map +1 -0
- package/dist/config/{normalize-rule-options.js → normalize/rule-options.js} +37 -4
- package/dist/config/normalize/rule-options.js.map +1 -0
- package/dist/config/{normalize-source-options.d.ts → normalize/source-options.d.ts} +4 -2
- package/dist/config/normalize/source-options.d.ts.map +1 -0
- package/dist/config/{normalize-source-options.js → normalize/source-options.js} +6 -5
- package/dist/config/normalize/source-options.js.map +1 -0
- package/dist/config/{normalize-sync-imports-options.d.ts → normalize/sync-imports-options.d.ts} +2 -2
- package/dist/config/normalize/sync-imports-options.d.ts.map +1 -0
- package/dist/config/{normalize-sync-imports-options.js → normalize/sync-imports-options.js} +5 -5
- package/dist/config/normalize/sync-imports-options.js.map +1 -0
- package/dist/imports/check-sync-imports.js +1 -1
- package/dist/imports/check-sync-imports.js.map +1 -1
- package/dist/imports/scan.d.ts.map +1 -1
- package/dist/imports/scan.js +127 -26
- package/dist/imports/scan.js.map +1 -1
- package/dist/imports/sync-imports.d.ts.map +1 -1
- package/dist/imports/sync-imports.js +59 -47
- package/dist/imports/sync-imports.js.map +1 -1
- package/dist/imports/types.d.ts +26 -1
- package/dist/imports/types.d.ts.map +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/native/native.d.ts +18 -0
- package/dist/native/native.d.ts.map +1 -0
- package/dist/native/native.js +78 -0
- package/dist/native/native.js.map +1 -0
- package/dist/run.d.ts +2 -0
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +4 -0
- package/dist/run.js.map +1 -1
- package/dist/runtime/{runtime-imports-sync.d.ts → imports-sync.d.ts} +1 -1
- package/dist/runtime/imports-sync.d.ts.map +1 -0
- package/dist/runtime/{runtime-imports-sync.js → imports-sync.js} +33 -21
- package/dist/runtime/imports-sync.js.map +1 -0
- package/dist/shared/discipline-types.d.ts +1 -1
- package/dist/shared/discipline-types.d.ts.map +1 -1
- package/dist/shared/logging.d.ts.map +1 -1
- package/dist/shared/logging.js +28 -13
- package/dist/shared/logging.js.map +1 -1
- package/dist/shared/utils.d.ts +4 -4
- package/dist/shared/utils.d.ts.map +1 -1
- package/dist/shared/utils.js +4 -4
- package/dist/shared/utils.js.map +1 -1
- package/native/darwin-arm64.node +0 -0
- package/native/darwin-x64.node +0 -0
- package/native/linux-arm64-gnu.node +0 -0
- package/native/linux-x64-gnu.node +0 -0
- package/package.json +11 -4
- package/dist/checks/rules/comment-stripping.d.ts.map +0 -1
- package/dist/checks/rules/comment-stripping.js.map +0 -1
- package/dist/checks/rules/dry.d.ts +0 -7
- package/dist/checks/rules/dry.d.ts.map +0 -1
- package/dist/checks/rules/dry.js +0 -905
- package/dist/checks/rules/dry.js.map +0 -1
- package/dist/checks/rules/folderize-compound-files.d.ts +0 -6
- package/dist/checks/rules/folderize-compound-files.d.ts.map +0 -1
- package/dist/checks/rules/folderize-compound-files.js.map +0 -1
- package/dist/checks/rules/folderize-plan.d.ts.map +0 -1
- package/dist/checks/rules/folderize-plan.js.map +0 -1
- package/dist/checks/rules/max-file-lines.d.ts +0 -6
- package/dist/checks/rules/max-file-lines.d.ts.map +0 -1
- package/dist/checks/rules/max-file-lines.js.map +0 -1
- package/dist/checks/rules/max-function-lines.d.ts +0 -6
- package/dist/checks/rules/max-function-lines.d.ts.map +0 -1
- package/dist/checks/rules/max-function-lines.js.map +0 -1
- package/dist/config/normalize-check-options.d.ts.map +0 -1
- package/dist/config/normalize-check-options.js.map +0 -1
- package/dist/config/normalize-rule-options.d.ts.map +0 -1
- package/dist/config/normalize-rule-options.js.map +0 -1
- package/dist/config/normalize-source-options.d.ts.map +0 -1
- package/dist/config/normalize-source-options.js.map +0 -1
- package/dist/config/normalize-sync-imports-options.d.ts.map +0 -1
- package/dist/config/normalize-sync-imports-options.js.map +0 -1
- package/dist/runtime/runtime-imports-sync.d.ts.map +0 -1
- package/dist/runtime/runtime-imports-sync.js.map +0 -1
package/dist/checks/rules/dry.js
DELETED
|
@@ -1,905 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs/promises";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import ts from "typescript";
|
|
4
|
-
import { parseSource } from "../../imports/module-specifiers.js";
|
|
5
|
-
import { resolveRelativeImport } from "../../imports/resolve.js";
|
|
6
|
-
import { isTypeScriptFamilyExtension } from "../../shared/languages.js";
|
|
7
|
-
import { FixFailureError, InvalidCodeDisciplineConfigError, } from "../../shared/errors.js";
|
|
8
|
-
import { isFile, stableSerialize, stripKnownExtension, toPosixPath, } from "../../shared/utils.js";
|
|
9
|
-
const DRY_RESOLUTION_EXTENSIONS = [
|
|
10
|
-
".ts",
|
|
11
|
-
".tsx",
|
|
12
|
-
".mts",
|
|
13
|
-
".js",
|
|
14
|
-
".jsx",
|
|
15
|
-
".mjs",
|
|
16
|
-
".cts",
|
|
17
|
-
".cjs",
|
|
18
|
-
];
|
|
19
|
-
function filterDrySourceFiles(sourceFiles) {
|
|
20
|
-
return sourceFiles.filter((file) => isTypeScriptFamilyExtension(file.extension));
|
|
21
|
-
}
|
|
22
|
-
const SAFE_GLOBAL_IDENTIFIERS = new Set([
|
|
23
|
-
"Array",
|
|
24
|
-
"BigInt",
|
|
25
|
-
"Boolean",
|
|
26
|
-
"Date",
|
|
27
|
-
"Error",
|
|
28
|
-
"JSON",
|
|
29
|
-
"Map",
|
|
30
|
-
"Math",
|
|
31
|
-
"Number",
|
|
32
|
-
"Object",
|
|
33
|
-
"Promise",
|
|
34
|
-
"RangeError",
|
|
35
|
-
"RegExp",
|
|
36
|
-
"Set",
|
|
37
|
-
"String",
|
|
38
|
-
"Symbol",
|
|
39
|
-
"SyntaxError",
|
|
40
|
-
"TypeError",
|
|
41
|
-
"URL",
|
|
42
|
-
"URLSearchParams",
|
|
43
|
-
"WeakMap",
|
|
44
|
-
"WeakSet",
|
|
45
|
-
"console",
|
|
46
|
-
"decodeURI",
|
|
47
|
-
"decodeURIComponent",
|
|
48
|
-
"encodeURI",
|
|
49
|
-
"encodeURIComponent",
|
|
50
|
-
"isFinite",
|
|
51
|
-
"isNaN",
|
|
52
|
-
"parseFloat",
|
|
53
|
-
"parseInt",
|
|
54
|
-
]);
|
|
55
|
-
function createSerializeContext() {
|
|
56
|
-
return {
|
|
57
|
-
nextBindingIndex: 0,
|
|
58
|
-
scopes: [],
|
|
59
|
-
usesOuterScope: false,
|
|
60
|
-
usesRestrictedRuntime: false,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
function pushScope(context) {
|
|
64
|
-
context.scopes.push(new Map());
|
|
65
|
-
}
|
|
66
|
-
function popScope(context) {
|
|
67
|
-
context.scopes.pop();
|
|
68
|
-
}
|
|
69
|
-
function declareBinding(context, name) {
|
|
70
|
-
const canonical = `v${context.nextBindingIndex}`;
|
|
71
|
-
context.nextBindingIndex += 1;
|
|
72
|
-
context.scopes[context.scopes.length - 1]?.set(name, canonical);
|
|
73
|
-
return canonical;
|
|
74
|
-
}
|
|
75
|
-
function lookupBinding(context, name) {
|
|
76
|
-
for (let index = context.scopes.length - 1; index >= 0; index -= 1) {
|
|
77
|
-
const match = context.scopes[index]?.get(name);
|
|
78
|
-
if (match)
|
|
79
|
-
return match;
|
|
80
|
-
}
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
function isReferenceIdentifier(node) {
|
|
84
|
-
const parent = node.parent;
|
|
85
|
-
if (!parent)
|
|
86
|
-
return true;
|
|
87
|
-
if ((ts.isFunctionDeclaration(parent)
|
|
88
|
-
|| ts.isFunctionExpression(parent)
|
|
89
|
-
|| ts.isArrowFunction(parent)
|
|
90
|
-
|| ts.isMethodDeclaration(parent)
|
|
91
|
-
|| ts.isGetAccessorDeclaration(parent)
|
|
92
|
-
|| ts.isSetAccessorDeclaration(parent)
|
|
93
|
-
|| ts.isParameter(parent)
|
|
94
|
-
|| ts.isVariableDeclaration(parent)
|
|
95
|
-
|| ts.isBindingElement(parent))
|
|
96
|
-
&& parent.name === node) {
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
if ((ts.isPropertyAssignment(parent) || ts.isPropertyDeclaration(parent) || ts.isMethodDeclaration(parent)) && parent.name === node) {
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
if ((ts.isPropertyAccessExpression(parent) && parent.name === node) || (ts.isQualifiedName(parent) && parent.right === node)) {
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
if (ts.isShorthandPropertyAssignment(parent) && parent.name === node) {
|
|
106
|
-
return true;
|
|
107
|
-
}
|
|
108
|
-
if ((ts.isImportSpecifier(parent) && (parent.name === node || parent.propertyName === node))
|
|
109
|
-
|| (ts.isImportClause(parent) && parent.name === node)
|
|
110
|
-
|| (ts.isNamespaceImport(parent) && parent.name === node)
|
|
111
|
-
|| (ts.isExportSpecifier(parent) && (parent.name === node || parent.propertyName === node))) {
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
function serializeBindingName(name, context, sourceFile) {
|
|
117
|
-
if (ts.isIdentifier(name)) {
|
|
118
|
-
return ["id", declareBinding(context, name.text)];
|
|
119
|
-
}
|
|
120
|
-
if (ts.isObjectBindingPattern(name)) {
|
|
121
|
-
return [
|
|
122
|
-
"object-binding",
|
|
123
|
-
name.elements.map((element) => serializeBindingElement(element, context, sourceFile)),
|
|
124
|
-
];
|
|
125
|
-
}
|
|
126
|
-
return [
|
|
127
|
-
"array-binding",
|
|
128
|
-
name.elements.map((element) => (element && ts.isBindingElement(element)) ? serializeBindingElement(element, context, sourceFile) : ["hole"]),
|
|
129
|
-
];
|
|
130
|
-
}
|
|
131
|
-
function serializeBindingElement(element, context, sourceFile) {
|
|
132
|
-
return [
|
|
133
|
-
"binding-element",
|
|
134
|
-
element.dotDotDotToken ? "rest" : "value",
|
|
135
|
-
element.propertyName ? serializePropertyName(element.propertyName, context, sourceFile) : null,
|
|
136
|
-
serializeBindingName(element.name, context, sourceFile),
|
|
137
|
-
element.initializer ? serializeNode(element.initializer, context, sourceFile) : null,
|
|
138
|
-
];
|
|
139
|
-
}
|
|
140
|
-
function serializePropertyName(name, context, sourceFile) {
|
|
141
|
-
if (ts.isIdentifier(name) || ts.isPrivateIdentifier(name)) {
|
|
142
|
-
return name.text;
|
|
143
|
-
}
|
|
144
|
-
if (ts.isStringLiteral(name) || ts.isNumericLiteral(name)) {
|
|
145
|
-
return name.text;
|
|
146
|
-
}
|
|
147
|
-
if (ts.isComputedPropertyName(name)) {
|
|
148
|
-
return ["computed", serializeNode(name.expression, context, sourceFile)];
|
|
149
|
-
}
|
|
150
|
-
return name.getText(sourceFile);
|
|
151
|
-
}
|
|
152
|
-
function serializeFunctionLike(node, context, sourceFile) {
|
|
153
|
-
const isAsync = Boolean(node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword));
|
|
154
|
-
const isGenerator = ts.isFunctionLike(node) && "asteriskToken" in node && Boolean(node.asteriskToken);
|
|
155
|
-
pushScope(context);
|
|
156
|
-
const parameters = node.parameters.map((parameter) => [
|
|
157
|
-
"param",
|
|
158
|
-
parameter.dotDotDotToken ? "rest" : "value",
|
|
159
|
-
serializeBindingName(parameter.name, context, sourceFile),
|
|
160
|
-
parameter.initializer ? serializeNode(parameter.initializer, context, sourceFile) : null,
|
|
161
|
-
]);
|
|
162
|
-
const body = node.body
|
|
163
|
-
? serializeNode(node.body, context, sourceFile)
|
|
164
|
-
: null;
|
|
165
|
-
popScope(context);
|
|
166
|
-
return [
|
|
167
|
-
"function",
|
|
168
|
-
isAsync,
|
|
169
|
-
isGenerator,
|
|
170
|
-
parameters,
|
|
171
|
-
body,
|
|
172
|
-
];
|
|
173
|
-
}
|
|
174
|
-
function serializeNode(node, context, sourceFile) {
|
|
175
|
-
if (!node)
|
|
176
|
-
return null;
|
|
177
|
-
if (ts.isTypeNode(node)
|
|
178
|
-
|| ts.isTypeParameterDeclaration(node)
|
|
179
|
-
|| ts.isTypeAliasDeclaration(node)
|
|
180
|
-
|| ts.isInterfaceDeclaration(node)
|
|
181
|
-
|| ts.isImportTypeNode(node)) {
|
|
182
|
-
return null;
|
|
183
|
-
}
|
|
184
|
-
if (ts.isBlock(node)) {
|
|
185
|
-
pushScope(context);
|
|
186
|
-
const result = ["block", node.statements.map((statement) => serializeNode(statement, context, sourceFile))];
|
|
187
|
-
popScope(context);
|
|
188
|
-
return result;
|
|
189
|
-
}
|
|
190
|
-
if (ts.isReturnStatement(node)) {
|
|
191
|
-
return ["return", serializeNode(node.expression, context, sourceFile)];
|
|
192
|
-
}
|
|
193
|
-
if (ts.isExpressionStatement(node)) {
|
|
194
|
-
return ["expr", serializeNode(node.expression, context, sourceFile)];
|
|
195
|
-
}
|
|
196
|
-
if (ts.isVariableStatement(node)) {
|
|
197
|
-
return ["vars", node.declarationList.flags, node.declarationList.declarations.map((declaration) => serializeNode(declaration, context, sourceFile))];
|
|
198
|
-
}
|
|
199
|
-
if (ts.isVariableDeclaration(node)) {
|
|
200
|
-
return [
|
|
201
|
-
"var",
|
|
202
|
-
serializeBindingName(node.name, context, sourceFile),
|
|
203
|
-
serializeNode(node.initializer, context, sourceFile),
|
|
204
|
-
];
|
|
205
|
-
}
|
|
206
|
-
if (ts.isIfStatement(node)) {
|
|
207
|
-
return [
|
|
208
|
-
"if",
|
|
209
|
-
serializeNode(node.expression, context, sourceFile),
|
|
210
|
-
serializeNode(node.thenStatement, context, sourceFile),
|
|
211
|
-
serializeNode(node.elseStatement, context, sourceFile),
|
|
212
|
-
];
|
|
213
|
-
}
|
|
214
|
-
if (ts.isForStatement(node)) {
|
|
215
|
-
return [
|
|
216
|
-
"for",
|
|
217
|
-
serializeNode(node.initializer, context, sourceFile),
|
|
218
|
-
serializeNode(node.condition, context, sourceFile),
|
|
219
|
-
serializeNode(node.incrementor, context, sourceFile),
|
|
220
|
-
serializeNode(node.statement, context, sourceFile),
|
|
221
|
-
];
|
|
222
|
-
}
|
|
223
|
-
if (ts.isForOfStatement(node) || ts.isForInStatement(node)) {
|
|
224
|
-
return [
|
|
225
|
-
ts.isForOfStatement(node) ? "for-of" : "for-in",
|
|
226
|
-
serializeNode(node.initializer, context, sourceFile),
|
|
227
|
-
serializeNode(node.expression, context, sourceFile),
|
|
228
|
-
serializeNode(node.statement, context, sourceFile),
|
|
229
|
-
];
|
|
230
|
-
}
|
|
231
|
-
if (ts.isWhileStatement(node) || ts.isDoStatement(node)) {
|
|
232
|
-
return [
|
|
233
|
-
ts.isWhileStatement(node) ? "while" : "do",
|
|
234
|
-
serializeNode(node.expression, context, sourceFile),
|
|
235
|
-
serializeNode(node.statement, context, sourceFile),
|
|
236
|
-
];
|
|
237
|
-
}
|
|
238
|
-
if (ts.isThrowStatement(node)) {
|
|
239
|
-
return ["throw", serializeNode(node.expression, context, sourceFile)];
|
|
240
|
-
}
|
|
241
|
-
if (ts.isTryStatement(node)) {
|
|
242
|
-
return [
|
|
243
|
-
"try",
|
|
244
|
-
serializeNode(node.tryBlock, context, sourceFile),
|
|
245
|
-
node.catchClause
|
|
246
|
-
? [
|
|
247
|
-
"catch",
|
|
248
|
-
node.catchClause.variableDeclaration
|
|
249
|
-
? serializeBindingName(node.catchClause.variableDeclaration.name, context, sourceFile)
|
|
250
|
-
: null,
|
|
251
|
-
serializeNode(node.catchClause.block, context, sourceFile),
|
|
252
|
-
]
|
|
253
|
-
: null,
|
|
254
|
-
serializeNode(node.finallyBlock, context, sourceFile),
|
|
255
|
-
];
|
|
256
|
-
}
|
|
257
|
-
if (ts.isSwitchStatement(node)) {
|
|
258
|
-
return [
|
|
259
|
-
"switch",
|
|
260
|
-
serializeNode(node.expression, context, sourceFile),
|
|
261
|
-
node.caseBlock.clauses.map((clause) => (ts.isCaseClause(clause)
|
|
262
|
-
? ["case", serializeNode(clause.expression, context, sourceFile), clause.statements.map((statement) => serializeNode(statement, context, sourceFile))]
|
|
263
|
-
: ["default", clause.statements.map((statement) => serializeNode(statement, context, sourceFile))])),
|
|
264
|
-
];
|
|
265
|
-
}
|
|
266
|
-
if (ts.isParenthesizedExpression(node)) {
|
|
267
|
-
return serializeNode(node.expression, context, sourceFile);
|
|
268
|
-
}
|
|
269
|
-
if (ts.isIdentifier(node)) {
|
|
270
|
-
if (node.text === "arguments") {
|
|
271
|
-
context.usesRestrictedRuntime = true;
|
|
272
|
-
}
|
|
273
|
-
if (!isReferenceIdentifier(node)) {
|
|
274
|
-
return node.text;
|
|
275
|
-
}
|
|
276
|
-
const local = lookupBinding(context, node.text);
|
|
277
|
-
if (local) {
|
|
278
|
-
return ["local", local];
|
|
279
|
-
}
|
|
280
|
-
if (node.text === "arguments") {
|
|
281
|
-
return ["restricted", "arguments"];
|
|
282
|
-
}
|
|
283
|
-
if (SAFE_GLOBAL_IDENTIFIERS.has(node.text)) {
|
|
284
|
-
return ["global", node.text];
|
|
285
|
-
}
|
|
286
|
-
context.usesOuterScope = true;
|
|
287
|
-
return ["outer", node.text];
|
|
288
|
-
}
|
|
289
|
-
if (node.kind === ts.SyntaxKind.ThisKeyword || node.kind === ts.SyntaxKind.SuperKeyword) {
|
|
290
|
-
context.usesRestrictedRuntime = true;
|
|
291
|
-
return ["restricted", ts.SyntaxKind[node.kind]];
|
|
292
|
-
}
|
|
293
|
-
if (ts.isMetaProperty(node) && node.keywordToken === ts.SyntaxKind.NewKeyword && node.name.text === "target") {
|
|
294
|
-
context.usesRestrictedRuntime = true;
|
|
295
|
-
return ["restricted", "new.target"];
|
|
296
|
-
}
|
|
297
|
-
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node) || ts.isNumericLiteral(node)) {
|
|
298
|
-
return ["literal", node.text];
|
|
299
|
-
}
|
|
300
|
-
if (ts.isBigIntLiteral(node)) {
|
|
301
|
-
return ["literal", node.text];
|
|
302
|
-
}
|
|
303
|
-
if (node.kind === ts.SyntaxKind.TrueKeyword || node.kind === ts.SyntaxKind.FalseKeyword || node.kind === ts.SyntaxKind.NullKeyword) {
|
|
304
|
-
return ["literal", ts.tokenToString(node.kind) ?? ts.SyntaxKind[node.kind]];
|
|
305
|
-
}
|
|
306
|
-
if (ts.isTemplateExpression(node)) {
|
|
307
|
-
return [
|
|
308
|
-
"template",
|
|
309
|
-
node.head.text,
|
|
310
|
-
node.templateSpans.map((span) => [
|
|
311
|
-
serializeNode(span.expression, context, sourceFile),
|
|
312
|
-
span.literal.text,
|
|
313
|
-
]),
|
|
314
|
-
];
|
|
315
|
-
}
|
|
316
|
-
if (ts.isArrayLiteralExpression(node)) {
|
|
317
|
-
return [
|
|
318
|
-
"array",
|
|
319
|
-
node.elements.map((element) => (ts.isSpreadElement(element)
|
|
320
|
-
? ["spread", serializeNode(element.expression, context, sourceFile)]
|
|
321
|
-
: serializeNode(element, context, sourceFile))),
|
|
322
|
-
];
|
|
323
|
-
}
|
|
324
|
-
if (ts.isObjectLiteralExpression(node)) {
|
|
325
|
-
return [
|
|
326
|
-
"object",
|
|
327
|
-
node.properties.map((property) => {
|
|
328
|
-
if (ts.isPropertyAssignment(property)) {
|
|
329
|
-
return ["prop", serializePropertyName(property.name, context, sourceFile), serializeNode(property.initializer, context, sourceFile)];
|
|
330
|
-
}
|
|
331
|
-
if (ts.isShorthandPropertyAssignment(property)) {
|
|
332
|
-
return [
|
|
333
|
-
"shorthand",
|
|
334
|
-
property.name.text,
|
|
335
|
-
serializeNode(property.name, context, sourceFile),
|
|
336
|
-
];
|
|
337
|
-
}
|
|
338
|
-
if (ts.isSpreadAssignment(property)) {
|
|
339
|
-
return ["spread-assignment", serializeNode(property.expression, context, sourceFile)];
|
|
340
|
-
}
|
|
341
|
-
if (ts.isMethodDeclaration(property)) {
|
|
342
|
-
return ["method", serializePropertyName(property.name, context, sourceFile), serializeFunctionLike(property, context, sourceFile)];
|
|
343
|
-
}
|
|
344
|
-
return ["property", property.getText(sourceFile)];
|
|
345
|
-
}),
|
|
346
|
-
];
|
|
347
|
-
}
|
|
348
|
-
if (ts.isPropertyAccessExpression(node)) {
|
|
349
|
-
return ["property-access", serializeNode(node.expression, context, sourceFile), node.name.text];
|
|
350
|
-
}
|
|
351
|
-
if (ts.isElementAccessExpression(node)) {
|
|
352
|
-
return ["element-access", serializeNode(node.expression, context, sourceFile), serializeNode(node.argumentExpression, context, sourceFile)];
|
|
353
|
-
}
|
|
354
|
-
if (ts.isCallExpression(node)) {
|
|
355
|
-
return [
|
|
356
|
-
"call",
|
|
357
|
-
serializeNode(node.expression, context, sourceFile),
|
|
358
|
-
node.arguments.map((argument) => serializeNode(argument, context, sourceFile)),
|
|
359
|
-
];
|
|
360
|
-
}
|
|
361
|
-
if (ts.isNewExpression(node)) {
|
|
362
|
-
return [
|
|
363
|
-
"new",
|
|
364
|
-
serializeNode(node.expression, context, sourceFile),
|
|
365
|
-
(node.arguments ?? []).map((argument) => serializeNode(argument, context, sourceFile)),
|
|
366
|
-
];
|
|
367
|
-
}
|
|
368
|
-
if (ts.isAwaitExpression(node)) {
|
|
369
|
-
return ["await", serializeNode(node.expression, context, sourceFile)];
|
|
370
|
-
}
|
|
371
|
-
if (ts.isYieldExpression(node)) {
|
|
372
|
-
return ["yield", node.asteriskToken ? "*" : "", serializeNode(node.expression, context, sourceFile)];
|
|
373
|
-
}
|
|
374
|
-
if (ts.isConditionalExpression(node)) {
|
|
375
|
-
return [
|
|
376
|
-
"conditional",
|
|
377
|
-
serializeNode(node.condition, context, sourceFile),
|
|
378
|
-
serializeNode(node.whenTrue, context, sourceFile),
|
|
379
|
-
serializeNode(node.whenFalse, context, sourceFile),
|
|
380
|
-
];
|
|
381
|
-
}
|
|
382
|
-
if (ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node)) {
|
|
383
|
-
return [
|
|
384
|
-
ts.isPrefixUnaryExpression(node) ? "prefix" : "postfix",
|
|
385
|
-
ts.tokenToString(node.operator) ?? String(node.operator),
|
|
386
|
-
serializeNode(node.operand, context, sourceFile),
|
|
387
|
-
];
|
|
388
|
-
}
|
|
389
|
-
if (ts.isBinaryExpression(node)) {
|
|
390
|
-
return [
|
|
391
|
-
"binary",
|
|
392
|
-
ts.tokenToString(node.operatorToken.kind) ?? String(node.operatorToken.kind),
|
|
393
|
-
serializeNode(node.left, context, sourceFile),
|
|
394
|
-
serializeNode(node.right, context, sourceFile),
|
|
395
|
-
];
|
|
396
|
-
}
|
|
397
|
-
if (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isArrowFunction(node)) {
|
|
398
|
-
return serializeFunctionLike(node, context, sourceFile);
|
|
399
|
-
}
|
|
400
|
-
if (ts.isAsExpression(node) || ts.isTypeAssertionExpression(node) || ts.isNonNullExpression(node) || ts.isSatisfiesExpression(node)) {
|
|
401
|
-
return serializeNode(node.expression, context, sourceFile);
|
|
402
|
-
}
|
|
403
|
-
if (ts.isClassExpression(node) || ts.isClassDeclaration(node)) {
|
|
404
|
-
return ["class", node.name?.text ?? null];
|
|
405
|
-
}
|
|
406
|
-
return [
|
|
407
|
-
ts.SyntaxKind[node.kind],
|
|
408
|
-
node.getChildren(sourceFile).map((child) => serializeNode(child, context, sourceFile)),
|
|
409
|
-
];
|
|
410
|
-
}
|
|
411
|
-
function createFunctionFingerprint(node, sourceFile) {
|
|
412
|
-
const context = createSerializeContext();
|
|
413
|
-
const normalized = serializeFunctionLike(node, context, sourceFile);
|
|
414
|
-
return {
|
|
415
|
-
fingerprint: stableSerialize(normalized),
|
|
416
|
-
usesOuterScope: context.usesOuterScope,
|
|
417
|
-
usesRestrictedRuntime: context.usesRestrictedRuntime,
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
function resolveStandaloneName(node) {
|
|
421
|
-
if ((ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) && node.name) {
|
|
422
|
-
return node.name.text;
|
|
423
|
-
}
|
|
424
|
-
const parent = node.parent;
|
|
425
|
-
if (parent && ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name)) {
|
|
426
|
-
return parent.name.text;
|
|
427
|
-
}
|
|
428
|
-
return undefined;
|
|
429
|
-
}
|
|
430
|
-
function resolveClassification(node) {
|
|
431
|
-
if (ts.isMethodDeclaration(node) || ts.isGetAccessorDeclaration(node) || ts.isSetAccessorDeclaration(node) || ts.isConstructorDeclaration(node)) {
|
|
432
|
-
return "method";
|
|
433
|
-
}
|
|
434
|
-
const parent = node.parent;
|
|
435
|
-
if (parent && ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name)) {
|
|
436
|
-
return "standalone";
|
|
437
|
-
}
|
|
438
|
-
if (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isArrowFunction(node)) {
|
|
439
|
-
return resolveStandaloneName(node) ? "standalone" : "unsupported";
|
|
440
|
-
}
|
|
441
|
-
return "unsupported";
|
|
442
|
-
}
|
|
443
|
-
function resolveRemovalRange(node, sourceFile) {
|
|
444
|
-
if (ts.isFunctionDeclaration(node) && node.parent) {
|
|
445
|
-
return {
|
|
446
|
-
start: node.getFullStart(),
|
|
447
|
-
end: expandRemovalEnd(sourceFile.text, node.getEnd()),
|
|
448
|
-
ok: true,
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
|
-
const parent = node.parent;
|
|
452
|
-
if (parent && ts.isVariableDeclaration(parent)) {
|
|
453
|
-
const declarationList = parent.parent;
|
|
454
|
-
const statement = declarationList?.parent;
|
|
455
|
-
const isSingleDeclaration = ts.isVariableDeclarationList(declarationList)
|
|
456
|
-
&& declarationList.declarations.length === 1
|
|
457
|
-
&& ts.isVariableStatement(statement);
|
|
458
|
-
if (!isSingleDeclaration) {
|
|
459
|
-
return {
|
|
460
|
-
start: parent.getStart(sourceFile),
|
|
461
|
-
end: parent.getEnd(),
|
|
462
|
-
ok: false,
|
|
463
|
-
};
|
|
464
|
-
}
|
|
465
|
-
return {
|
|
466
|
-
start: statement.getFullStart(),
|
|
467
|
-
end: expandRemovalEnd(sourceFile.text, statement.getEnd()),
|
|
468
|
-
ok: true,
|
|
469
|
-
};
|
|
470
|
-
}
|
|
471
|
-
return {
|
|
472
|
-
start: node.getStart(sourceFile),
|
|
473
|
-
end: node.getEnd(),
|
|
474
|
-
ok: false,
|
|
475
|
-
};
|
|
476
|
-
}
|
|
477
|
-
function expandRemovalEnd(text, end) {
|
|
478
|
-
if (text.slice(end, end + 2) === "\r\n")
|
|
479
|
-
return end + 2;
|
|
480
|
-
if (text[end] === "\n")
|
|
481
|
-
return end + 1;
|
|
482
|
-
return end;
|
|
483
|
-
}
|
|
484
|
-
async function resolveDryHelperModulePath(reference, options) {
|
|
485
|
-
const baseDir = options.configPath ? path.dirname(options.configPath) : options.projectRoot;
|
|
486
|
-
const basePath = path.isAbsolute(reference.from)
|
|
487
|
-
? path.resolve(reference.from)
|
|
488
|
-
: path.resolve(baseDir, reference.from);
|
|
489
|
-
const candidates = [basePath];
|
|
490
|
-
if (!path.extname(basePath)) {
|
|
491
|
-
for (const extension of DRY_RESOLUTION_EXTENSIONS) {
|
|
492
|
-
candidates.push(`${basePath}${extension}`);
|
|
493
|
-
}
|
|
494
|
-
for (const extension of DRY_RESOLUTION_EXTENSIONS) {
|
|
495
|
-
candidates.push(path.join(basePath, `index${extension}`));
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
for (const candidate of candidates) {
|
|
499
|
-
if (await isFile(candidate)) {
|
|
500
|
-
return candidate;
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
throw new InvalidCodeDisciplineConfigError(`dry helper module was not found: ${reference.from}`, {
|
|
504
|
-
rule: "dry",
|
|
505
|
-
from: reference.from,
|
|
506
|
-
});
|
|
507
|
-
}
|
|
508
|
-
function extractSupportedExportedFunction(sourceFile, exportName) {
|
|
509
|
-
for (const statement of sourceFile.statements) {
|
|
510
|
-
if (exportName === "default" && ts.isExportAssignment(statement)) {
|
|
511
|
-
const expression = statement.expression;
|
|
512
|
-
if (ts.isArrowFunction(expression) || ts.isFunctionExpression(expression)) {
|
|
513
|
-
return expression;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
if (ts.isFunctionDeclaration(statement) && statement.name?.text === exportName && statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) {
|
|
517
|
-
return statement;
|
|
518
|
-
}
|
|
519
|
-
if (ts.isVariableStatement(statement) && statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) {
|
|
520
|
-
for (const declaration of statement.declarationList.declarations) {
|
|
521
|
-
if (!ts.isIdentifier(declaration.name) || declaration.name.text !== exportName || !declaration.initializer)
|
|
522
|
-
continue;
|
|
523
|
-
if (ts.isArrowFunction(declaration.initializer) || ts.isFunctionExpression(declaration.initializer)) {
|
|
524
|
-
return declaration.initializer;
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
if (exportName === "default" && ts.isFunctionDeclaration(statement) && statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword)) {
|
|
529
|
-
return statement;
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
return null;
|
|
533
|
-
}
|
|
534
|
-
async function resolveDryHelpers(rule, options) {
|
|
535
|
-
const helpers = new Map();
|
|
536
|
-
for (const reference of rule.helpers) {
|
|
537
|
-
const absolutePath = await resolveDryHelperModulePath(reference, options);
|
|
538
|
-
const sourceText = await fs.readFile(absolutePath, "utf8");
|
|
539
|
-
const sourceFile = parseSource(sourceText, absolutePath);
|
|
540
|
-
const exportedFunction = extractSupportedExportedFunction(sourceFile, reference.exportName);
|
|
541
|
-
if (!exportedFunction) {
|
|
542
|
-
throw new InvalidCodeDisciplineConfigError(`dry helper export is not a supported function: ${reference.from}#${reference.exportName}`, {
|
|
543
|
-
rule: "dry",
|
|
544
|
-
exportName: reference.exportName,
|
|
545
|
-
from: reference.from,
|
|
546
|
-
});
|
|
547
|
-
}
|
|
548
|
-
const fingerprintState = createFunctionFingerprint(exportedFunction, sourceFile);
|
|
549
|
-
const helperKey = reference.key || `${reference.from}#${reference.exportName}`;
|
|
550
|
-
const localName = reference.exportName === "default"
|
|
551
|
-
? resolveStandaloneName(exportedFunction) || "default"
|
|
552
|
-
: reference.exportName;
|
|
553
|
-
if (helpers.has(fingerprintState.fingerprint)) {
|
|
554
|
-
const existing = helpers.get(fingerprintState.fingerprint);
|
|
555
|
-
throw new InvalidCodeDisciplineConfigError(`dry helper fingerprint collision: ${helperKey} conflicts with ${existing.helperKey}`, {
|
|
556
|
-
rule: "dry",
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
helpers.set(fingerprintState.fingerprint, {
|
|
560
|
-
absolutePath,
|
|
561
|
-
exportName: reference.exportName,
|
|
562
|
-
fingerprint: fingerprintState.fingerprint,
|
|
563
|
-
filePath: toPosixPath(path.relative(options.projectRoot, absolutePath)),
|
|
564
|
-
helperKey,
|
|
565
|
-
importPath: reference.from,
|
|
566
|
-
localName,
|
|
567
|
-
nodeEnd: exportedFunction.getEnd(),
|
|
568
|
-
nodeStart: exportedFunction.getStart(sourceFile),
|
|
569
|
-
});
|
|
570
|
-
}
|
|
571
|
-
return helpers;
|
|
572
|
-
}
|
|
573
|
-
function createDryViolation(candidate, options) {
|
|
574
|
-
return {
|
|
575
|
-
rule: "dry",
|
|
576
|
-
fix: true,
|
|
577
|
-
filePath: candidate.filePath,
|
|
578
|
-
message: `${candidate.localName ?? "anonymous function"} duplicates registered helper ${candidate.helper.helperKey}`,
|
|
579
|
-
details: {
|
|
580
|
-
fixable: candidate.safeToFix,
|
|
581
|
-
helper: candidate.helper.helperKey,
|
|
582
|
-
helperFile: candidate.helper.filePath,
|
|
583
|
-
reason: candidate.nonFixableReason,
|
|
584
|
-
},
|
|
585
|
-
};
|
|
586
|
-
}
|
|
587
|
-
async function collectDryCandidates(sourceFiles, helpers, options) {
|
|
588
|
-
const results = [];
|
|
589
|
-
for (const file of sourceFiles) {
|
|
590
|
-
const text = await fs.readFile(file.absolutePath, "utf8");
|
|
591
|
-
const sourceFile = parseSource(text, file.absolutePath);
|
|
592
|
-
function visit(node) {
|
|
593
|
-
if ((ts.isFunctionDeclaration(node) && node.body)
|
|
594
|
-
|| (ts.isFunctionExpression(node) && node.body)
|
|
595
|
-
|| (ts.isArrowFunction(node) && node.body)
|
|
596
|
-
|| (ts.isMethodDeclaration(node) && node.body)
|
|
597
|
-
|| (ts.isGetAccessorDeclaration(node) && node.body)
|
|
598
|
-
|| (ts.isSetAccessorDeclaration(node) && node.body)
|
|
599
|
-
|| (ts.isConstructorDeclaration(node) && node.body)) {
|
|
600
|
-
const classification = resolveClassification(node);
|
|
601
|
-
const localName = resolveStandaloneName(node);
|
|
602
|
-
const removal = resolveRemovalRange(node, sourceFile);
|
|
603
|
-
const fingerprintState = createFunctionFingerprint(node, sourceFile);
|
|
604
|
-
const helper = helpers.get(fingerprintState.fingerprint);
|
|
605
|
-
if (helper) {
|
|
606
|
-
const isSelf = helper.absolutePath === file.absolutePath
|
|
607
|
-
&& helper.nodeStart === node.getStart(sourceFile)
|
|
608
|
-
&& helper.nodeEnd === node.getEnd();
|
|
609
|
-
if (!isSelf) {
|
|
610
|
-
const safeToFix = classification === "standalone"
|
|
611
|
-
&& removal.ok
|
|
612
|
-
&& !fingerprintState.usesOuterScope
|
|
613
|
-
&& !fingerprintState.usesRestrictedRuntime
|
|
614
|
-
&& Boolean(localName);
|
|
615
|
-
let nonFixableReason;
|
|
616
|
-
if (classification === "method") {
|
|
617
|
-
nonFixableReason = "methods are report-only in v1";
|
|
618
|
-
}
|
|
619
|
-
else if (classification !== "standalone") {
|
|
620
|
-
nonFixableReason = "unsupported function shape";
|
|
621
|
-
}
|
|
622
|
-
else if (!removal.ok) {
|
|
623
|
-
nonFixableReason = "duplicate declaration cannot be removed safely";
|
|
624
|
-
}
|
|
625
|
-
else if (fingerprintState.usesOuterScope) {
|
|
626
|
-
nonFixableReason = "duplicate captures outer scope";
|
|
627
|
-
}
|
|
628
|
-
else if (fingerprintState.usesRestrictedRuntime) {
|
|
629
|
-
nonFixableReason = "duplicate depends on this, super, arguments, or new.target";
|
|
630
|
-
}
|
|
631
|
-
results.push({
|
|
632
|
-
absolutePath: file.absolutePath,
|
|
633
|
-
classification,
|
|
634
|
-
fingerprint: fingerprintState.fingerprint,
|
|
635
|
-
filePath: file.relativeFromProjectRoot,
|
|
636
|
-
helper,
|
|
637
|
-
localName,
|
|
638
|
-
nonFixableReason,
|
|
639
|
-
removalEnd: removal.end,
|
|
640
|
-
removalStart: removal.start,
|
|
641
|
-
safeToFix,
|
|
642
|
-
sourceFile,
|
|
643
|
-
usesOuterScope: fingerprintState.usesOuterScope,
|
|
644
|
-
usesRestrictedRuntime: fingerprintState.usesRestrictedRuntime,
|
|
645
|
-
});
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
ts.forEachChild(node, visit);
|
|
650
|
-
}
|
|
651
|
-
visit(sourceFile);
|
|
652
|
-
}
|
|
653
|
-
return results;
|
|
654
|
-
}
|
|
655
|
-
function formatRelativeImport(fromAbsolutePath, toAbsolutePath, extensions) {
|
|
656
|
-
let relativePath = toPosixPath(path.relative(path.dirname(fromAbsolutePath), toAbsolutePath));
|
|
657
|
-
if (!relativePath.startsWith(".")) {
|
|
658
|
-
relativePath = `./${relativePath}`;
|
|
659
|
-
}
|
|
660
|
-
const withoutExtension = stripKnownExtension(relativePath, extensions);
|
|
661
|
-
return withoutExtension.replace(/\/index$/, "") || ".";
|
|
662
|
-
}
|
|
663
|
-
async function collectExistingImports(sourceFile, filePath, options) {
|
|
664
|
-
const bindingsByTarget = new Map();
|
|
665
|
-
for (const statement of sourceFile.statements) {
|
|
666
|
-
if (!ts.isImportDeclaration(statement) || !statement.moduleSpecifier || !ts.isStringLiteral(statement.moduleSpecifier)) {
|
|
667
|
-
continue;
|
|
668
|
-
}
|
|
669
|
-
const resolved = await resolveRelativeImport(statement.moduleSpecifier.text, filePath, {
|
|
670
|
-
sourceRoot: options.sourceRoot,
|
|
671
|
-
sourceExtensions: options.sourceExtensions,
|
|
672
|
-
});
|
|
673
|
-
if (!resolved)
|
|
674
|
-
continue;
|
|
675
|
-
const bindings = bindingsByTarget.get(resolved) ?? [];
|
|
676
|
-
const clause = statement.importClause;
|
|
677
|
-
if (!clause)
|
|
678
|
-
continue;
|
|
679
|
-
if (clause.name) {
|
|
680
|
-
bindings.push({
|
|
681
|
-
exportName: "default",
|
|
682
|
-
localName: clause.name.text,
|
|
683
|
-
});
|
|
684
|
-
}
|
|
685
|
-
if (clause.namedBindings && ts.isNamedImports(clause.namedBindings)) {
|
|
686
|
-
for (const element of clause.namedBindings.elements) {
|
|
687
|
-
bindings.push({
|
|
688
|
-
exportName: element.propertyName?.text ?? element.name.text,
|
|
689
|
-
localName: element.name.text,
|
|
690
|
-
});
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
bindingsByTarget.set(resolved, bindings);
|
|
694
|
-
}
|
|
695
|
-
return bindingsByTarget;
|
|
696
|
-
}
|
|
697
|
-
function collectTopLevelValueBindings(sourceFile) {
|
|
698
|
-
const names = new Set();
|
|
699
|
-
for (const statement of sourceFile.statements) {
|
|
700
|
-
if (ts.isFunctionDeclaration(statement) && statement.name) {
|
|
701
|
-
names.add(statement.name.text);
|
|
702
|
-
continue;
|
|
703
|
-
}
|
|
704
|
-
if (ts.isClassDeclaration(statement) && statement.name) {
|
|
705
|
-
names.add(statement.name.text);
|
|
706
|
-
continue;
|
|
707
|
-
}
|
|
708
|
-
if (ts.isImportDeclaration(statement) && statement.importClause) {
|
|
709
|
-
if (statement.importClause.name) {
|
|
710
|
-
names.add(statement.importClause.name.text);
|
|
711
|
-
}
|
|
712
|
-
if (statement.importClause.namedBindings) {
|
|
713
|
-
if (ts.isNamespaceImport(statement.importClause.namedBindings)) {
|
|
714
|
-
names.add(statement.importClause.namedBindings.name.text);
|
|
715
|
-
}
|
|
716
|
-
else {
|
|
717
|
-
for (const element of statement.importClause.namedBindings.elements) {
|
|
718
|
-
names.add(element.name.text);
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
continue;
|
|
723
|
-
}
|
|
724
|
-
if (ts.isVariableStatement(statement)) {
|
|
725
|
-
for (const declaration of statement.declarationList.declarations) {
|
|
726
|
-
collectBoundNamesFromBindingName(declaration.name, names);
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
return names;
|
|
731
|
-
}
|
|
732
|
-
function collectBoundNamesFromBindingName(name, target) {
|
|
733
|
-
if (ts.isIdentifier(name)) {
|
|
734
|
-
target.add(name.text);
|
|
735
|
-
return;
|
|
736
|
-
}
|
|
737
|
-
if (ts.isObjectBindingPattern(name) || ts.isArrayBindingPattern(name)) {
|
|
738
|
-
for (const element of name.elements) {
|
|
739
|
-
if (!element || !ts.isBindingElement(element))
|
|
740
|
-
continue;
|
|
741
|
-
collectBoundNamesFromBindingName(element.name, target);
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
|
-
function findImportInsertionOffset(sourceFile, text) {
|
|
746
|
-
const imports = sourceFile.statements.filter((statement) => ts.isImportDeclaration(statement));
|
|
747
|
-
if (imports.length > 0) {
|
|
748
|
-
return imports[imports.length - 1].getEnd();
|
|
749
|
-
}
|
|
750
|
-
let offset = 0;
|
|
751
|
-
if (text.startsWith("#!")) {
|
|
752
|
-
const newlineIndex = text.indexOf("\n");
|
|
753
|
-
offset = newlineIndex >= 0 ? newlineIndex + 1 : text.length;
|
|
754
|
-
}
|
|
755
|
-
for (const statement of sourceFile.statements) {
|
|
756
|
-
if (ts.isExpressionStatement(statement) && ts.isStringLiteral(statement.expression)) {
|
|
757
|
-
offset = statement.getEnd();
|
|
758
|
-
continue;
|
|
759
|
-
}
|
|
760
|
-
break;
|
|
761
|
-
}
|
|
762
|
-
return offset;
|
|
763
|
-
}
|
|
764
|
-
async function fixDryRule(sourceFiles, options) {
|
|
765
|
-
const rule = options.rules.dry;
|
|
766
|
-
if (!rule) {
|
|
767
|
-
return {
|
|
768
|
-
ok: true,
|
|
769
|
-
violationCount: 0,
|
|
770
|
-
violations: [],
|
|
771
|
-
};
|
|
772
|
-
}
|
|
773
|
-
const helpers = await resolveDryHelpers(rule, options);
|
|
774
|
-
const candidates = await collectDryCandidates(filterDrySourceFiles(sourceFiles), helpers, options);
|
|
775
|
-
const violations = candidates.map((candidate) => createDryViolation(candidate, options));
|
|
776
|
-
if (violations.length === 0) {
|
|
777
|
-
return {
|
|
778
|
-
ok: true,
|
|
779
|
-
violationCount: 0,
|
|
780
|
-
violations: [],
|
|
781
|
-
added_imports: 0,
|
|
782
|
-
removed_duplicates: 0,
|
|
783
|
-
};
|
|
784
|
-
}
|
|
785
|
-
const candidatesByFile = new Map();
|
|
786
|
-
const fixedCandidates = new Set();
|
|
787
|
-
for (const candidate of candidates.filter((entry) => entry.safeToFix)) {
|
|
788
|
-
const rows = candidatesByFile.get(candidate.absolutePath) ?? [];
|
|
789
|
-
rows.push(candidate);
|
|
790
|
-
candidatesByFile.set(candidate.absolutePath, rows);
|
|
791
|
-
}
|
|
792
|
-
let addedImports = 0;
|
|
793
|
-
let removedDuplicates = 0;
|
|
794
|
-
let rewrittenFiles = 0;
|
|
795
|
-
try {
|
|
796
|
-
for (const [absolutePath, fileCandidates] of candidatesByFile) {
|
|
797
|
-
const originalText = await fs.readFile(absolutePath, "utf8");
|
|
798
|
-
const sourceFile = fileCandidates[0].sourceFile;
|
|
799
|
-
const existingImports = await collectExistingImports(sourceFile, absolutePath, options);
|
|
800
|
-
const topLevelBindings = collectTopLevelValueBindings(sourceFile);
|
|
801
|
-
const pendingImports = new Map();
|
|
802
|
-
const removals = [];
|
|
803
|
-
for (const statement of sourceFile.statements) {
|
|
804
|
-
if (ts.isFunctionDeclaration(statement) && statement.name) {
|
|
805
|
-
const matchingCandidate = fileCandidates.find((candidate) => candidate.localName === statement.name?.text && candidate.removalStart === statement.getFullStart());
|
|
806
|
-
if (matchingCandidate) {
|
|
807
|
-
topLevelBindings.delete(statement.name.text);
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
if (ts.isVariableStatement(statement) && statement.declarationList.declarations.length === 1) {
|
|
811
|
-
const declaration = statement.declarationList.declarations[0];
|
|
812
|
-
if (!ts.isIdentifier(declaration.name))
|
|
813
|
-
continue;
|
|
814
|
-
const variableName = declaration.name.text;
|
|
815
|
-
const matchingCandidate = fileCandidates.find((candidate) => candidate.localName === variableName && candidate.removalStart === statement.getFullStart());
|
|
816
|
-
if (matchingCandidate) {
|
|
817
|
-
topLevelBindings.delete(variableName);
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
}
|
|
821
|
-
for (const candidate of fileCandidates.sort((left, right) => right.removalStart - left.removalStart)) {
|
|
822
|
-
if (!candidate.localName)
|
|
823
|
-
continue;
|
|
824
|
-
if (candidate.helper.absolutePath === absolutePath) {
|
|
825
|
-
if (candidate.localName !== candidate.helper.localName) {
|
|
826
|
-
continue;
|
|
827
|
-
}
|
|
828
|
-
removals.push({
|
|
829
|
-
start: candidate.removalStart,
|
|
830
|
-
end: candidate.removalEnd,
|
|
831
|
-
});
|
|
832
|
-
fixedCandidates.add(candidate);
|
|
833
|
-
removedDuplicates += 1;
|
|
834
|
-
continue;
|
|
835
|
-
}
|
|
836
|
-
const existingHelperBindings = existingImports.get(candidate.helper.absolutePath) ?? [];
|
|
837
|
-
const alreadyImported = existingHelperBindings.some((binding) => binding.exportName === candidate.helper.exportName && binding.localName === candidate.localName);
|
|
838
|
-
if (!alreadyImported) {
|
|
839
|
-
if (topLevelBindings.has(candidate.localName)) {
|
|
840
|
-
continue;
|
|
841
|
-
}
|
|
842
|
-
const importSpecifier = formatRelativeImport(absolutePath, candidate.helper.absolutePath, options.sourceExtensions);
|
|
843
|
-
const importLine = candidate.helper.exportName === "default"
|
|
844
|
-
? `import ${candidate.localName} from "${importSpecifier}";\n`
|
|
845
|
-
: candidate.localName === candidate.helper.exportName
|
|
846
|
-
? `import { ${candidate.helper.exportName} } from "${importSpecifier}";\n`
|
|
847
|
-
: `import { ${candidate.helper.exportName} as ${candidate.localName} } from "${importSpecifier}";\n`;
|
|
848
|
-
pendingImports.set(`${candidate.helper.absolutePath}::${candidate.localName}`, importLine);
|
|
849
|
-
topLevelBindings.add(candidate.localName);
|
|
850
|
-
}
|
|
851
|
-
removals.push({
|
|
852
|
-
start: candidate.removalStart,
|
|
853
|
-
end: candidate.removalEnd,
|
|
854
|
-
});
|
|
855
|
-
fixedCandidates.add(candidate);
|
|
856
|
-
removedDuplicates += 1;
|
|
857
|
-
}
|
|
858
|
-
if (removals.length === 0 && pendingImports.size === 0) {
|
|
859
|
-
continue;
|
|
860
|
-
}
|
|
861
|
-
let nextText = originalText;
|
|
862
|
-
for (const removal of removals.sort((left, right) => right.start - left.start)) {
|
|
863
|
-
nextText = `${nextText.slice(0, removal.start)}${nextText.slice(removal.end)}`;
|
|
864
|
-
}
|
|
865
|
-
if (pendingImports.size > 0) {
|
|
866
|
-
const insertionOffset = findImportInsertionOffset(sourceFile, originalText);
|
|
867
|
-
const prefix = nextText.slice(0, insertionOffset);
|
|
868
|
-
const suffix = nextText.slice(insertionOffset);
|
|
869
|
-
const importBlock = `${prefix.endsWith("\n") || insertionOffset === 0 ? "" : "\n"}${[...pendingImports.values()].join("")}`;
|
|
870
|
-
nextText = `${prefix}${importBlock}${suffix.startsWith("\n") ? "" : "\n"}${suffix}`;
|
|
871
|
-
addedImports += pendingImports.size;
|
|
872
|
-
}
|
|
873
|
-
if (nextText !== originalText) {
|
|
874
|
-
await fs.writeFile(absolutePath, nextText);
|
|
875
|
-
rewrittenFiles += 1;
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
catch (error) {
|
|
880
|
-
throw new FixFailureError("DRY fix failed", {
|
|
881
|
-
cause: error instanceof Error ? error.message : String(error),
|
|
882
|
-
});
|
|
883
|
-
}
|
|
884
|
-
const remainingViolations = candidates
|
|
885
|
-
.filter((candidate) => !fixedCandidates.has(candidate))
|
|
886
|
-
.map((candidate) => createDryViolation(candidate, options));
|
|
887
|
-
return {
|
|
888
|
-
ok: remainingViolations.length === 0,
|
|
889
|
-
violationCount: remainingViolations.length,
|
|
890
|
-
violations: remainingViolations,
|
|
891
|
-
added_imports: addedImports,
|
|
892
|
-
removed_duplicates: removedDuplicates,
|
|
893
|
-
rewritten_files: rewrittenFiles,
|
|
894
|
-
};
|
|
895
|
-
}
|
|
896
|
-
async function collectDryViolations(sourceFiles, options) {
|
|
897
|
-
const rule = options.rules.dry;
|
|
898
|
-
if (!rule)
|
|
899
|
-
return [];
|
|
900
|
-
const helpers = await resolveDryHelpers(rule, options);
|
|
901
|
-
const candidates = await collectDryCandidates(filterDrySourceFiles(sourceFiles), helpers, options);
|
|
902
|
-
return candidates.map((candidate) => createDryViolation(candidate, options));
|
|
903
|
-
}
|
|
904
|
-
export { collectDryViolations, fixDryRule };
|
|
905
|
-
//# sourceMappingURL=dry.js.map
|