@staff0rd/assist 0.170.2 → 0.170.3
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/dist/index.js +93 -72
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import { Command } from "commander";
|
|
|
6
6
|
// package.json
|
|
7
7
|
var package_default = {
|
|
8
8
|
name: "@staff0rd/assist",
|
|
9
|
-
version: "0.170.
|
|
9
|
+
version: "0.170.3",
|
|
10
10
|
type: "module",
|
|
11
11
|
main: "dist/index.js",
|
|
12
12
|
bin: {
|
|
@@ -7996,7 +7996,7 @@ function isNameReferencedInSource(sourceFile, name) {
|
|
|
7996
7996
|
}
|
|
7997
7997
|
async function applyExtraction(functionName, sourceFile, destPath, plan2, project) {
|
|
7998
7998
|
project.createSourceFile(destPath, plan2.destContent, { overwrite: false });
|
|
7999
|
-
for (const fn of [plan2.target, ...plan2.
|
|
7999
|
+
for (const fn of [plan2.target, ...plan2.functionsToRemove]) {
|
|
8000
8000
|
fn.remove();
|
|
8001
8001
|
}
|
|
8002
8002
|
for (const stmt of plan2.statementsToRemove) {
|
|
@@ -8033,7 +8033,7 @@ async function applyExtraction(functionName, sourceFile, destPath, plan2, projec
|
|
|
8033
8033
|
|
|
8034
8034
|
// src/commands/refactor/extract/collectDependencies.ts
|
|
8035
8035
|
import {
|
|
8036
|
-
SyntaxKind as
|
|
8036
|
+
SyntaxKind as SyntaxKind7
|
|
8037
8037
|
} from "ts-morph";
|
|
8038
8038
|
|
|
8039
8039
|
// src/commands/refactor/extract/collectPrivateFunctions.ts
|
|
@@ -8072,6 +8072,41 @@ function collectPrivateFunctions(target, fnMap) {
|
|
|
8072
8072
|
});
|
|
8073
8073
|
}
|
|
8074
8074
|
|
|
8075
|
+
// src/commands/refactor/extract/collectPrivateStatements.ts
|
|
8076
|
+
import { SyntaxKind as SyntaxKind5 } from "ts-morph";
|
|
8077
|
+
function getReferencedIdentifiers(fn) {
|
|
8078
|
+
const names = /* @__PURE__ */ new Set();
|
|
8079
|
+
for (const id of fn.getDescendantsOfKind(SyntaxKind5.Identifier)) {
|
|
8080
|
+
names.add(id.getText());
|
|
8081
|
+
}
|
|
8082
|
+
return names;
|
|
8083
|
+
}
|
|
8084
|
+
function collectPrivateStatements(functions, stmtMap) {
|
|
8085
|
+
const seen = /* @__PURE__ */ new Set();
|
|
8086
|
+
for (const fn of functions) {
|
|
8087
|
+
for (const name of getReferencedIdentifiers(fn)) {
|
|
8088
|
+
const stmt = stmtMap.get(name);
|
|
8089
|
+
if (stmt) seen.add(stmt);
|
|
8090
|
+
}
|
|
8091
|
+
}
|
|
8092
|
+
return [...seen];
|
|
8093
|
+
}
|
|
8094
|
+
|
|
8095
|
+
// src/commands/refactor/extract/getFunctionMap.ts
|
|
8096
|
+
import {
|
|
8097
|
+
SyntaxKind as SyntaxKind6
|
|
8098
|
+
} from "ts-morph";
|
|
8099
|
+
function getFunctionMap(sourceFile) {
|
|
8100
|
+
const map = /* @__PURE__ */ new Map();
|
|
8101
|
+
for (const fn of sourceFile.getDescendantsOfKind(
|
|
8102
|
+
SyntaxKind6.FunctionDeclaration
|
|
8103
|
+
)) {
|
|
8104
|
+
const name = fn.getName();
|
|
8105
|
+
if (name) map.set(name, fn);
|
|
8106
|
+
}
|
|
8107
|
+
return map;
|
|
8108
|
+
}
|
|
8109
|
+
|
|
8075
8110
|
// src/commands/refactor/extract/getPrivateStatementMap.ts
|
|
8076
8111
|
function getPrivateStatementMap(sourceFile) {
|
|
8077
8112
|
const map = /* @__PURE__ */ new Map();
|
|
@@ -8089,73 +8124,61 @@ function getPrivateStatementMap(sourceFile) {
|
|
|
8089
8124
|
}
|
|
8090
8125
|
|
|
8091
8126
|
// src/commands/refactor/extract/collectDependencies.ts
|
|
8092
|
-
function
|
|
8093
|
-
|
|
8094
|
-
for (const id of fn.getDescendantsOfKind(SyntaxKind5.Identifier)) {
|
|
8095
|
-
names.add(id.getText());
|
|
8096
|
-
}
|
|
8097
|
-
return names;
|
|
8098
|
-
}
|
|
8099
|
-
function getFunctionMap(sourceFile) {
|
|
8100
|
-
const map = /* @__PURE__ */ new Map();
|
|
8101
|
-
for (const fn of sourceFile.getDescendantsOfKind(
|
|
8102
|
-
SyntaxKind5.FunctionDeclaration
|
|
8103
|
-
)) {
|
|
8104
|
-
const name = fn.getName();
|
|
8105
|
-
if (name) map.set(name, fn);
|
|
8106
|
-
}
|
|
8107
|
-
return map;
|
|
8127
|
+
function getRemainingFunctions(sourceFile, extracted) {
|
|
8128
|
+
return sourceFile.getDescendantsOfKind(SyntaxKind7.FunctionDeclaration).filter((fn) => !extracted.has(fn));
|
|
8108
8129
|
}
|
|
8109
|
-
function
|
|
8110
|
-
const
|
|
8111
|
-
for (const fn of
|
|
8112
|
-
for (const
|
|
8113
|
-
|
|
8114
|
-
if (stmt) seen.add(stmt);
|
|
8130
|
+
function collectFnsUsedByRemaining(remaining, fnMap) {
|
|
8131
|
+
const used = /* @__PURE__ */ new Set();
|
|
8132
|
+
for (const fn of remaining) {
|
|
8133
|
+
for (const dep of collectPrivateFunctions(fn, fnMap)) {
|
|
8134
|
+
used.add(dep);
|
|
8115
8135
|
}
|
|
8116
8136
|
}
|
|
8117
|
-
return
|
|
8118
|
-
}
|
|
8119
|
-
function getRemainingFunctions(sourceFile, extracted) {
|
|
8120
|
-
return sourceFile.getDescendantsOfKind(SyntaxKind5.FunctionDeclaration).filter((fn) => !extracted.has(fn));
|
|
8137
|
+
return used;
|
|
8121
8138
|
}
|
|
8122
8139
|
function collectDependencies(target, sourceFile) {
|
|
8123
8140
|
const fnMap = getFunctionMap(sourceFile);
|
|
8124
8141
|
const depFunctions = collectPrivateFunctions(target, fnMap);
|
|
8125
8142
|
const allExtracted = [target, ...depFunctions];
|
|
8126
8143
|
const stmtMap = getPrivateStatementMap(sourceFile);
|
|
8127
|
-
const
|
|
8128
|
-
const
|
|
8129
|
-
const
|
|
8130
|
-
const
|
|
8131
|
-
const
|
|
8144
|
+
const stmtsToCopy = collectPrivateStatements(allExtracted, stmtMap);
|
|
8145
|
+
const remaining = getRemainingFunctions(sourceFile, new Set(allExtracted));
|
|
8146
|
+
const fnsUsedByRemaining = collectFnsUsedByRemaining(remaining, fnMap);
|
|
8147
|
+
const fnsToRemove = depFunctions.filter((fn) => !fnsUsedByRemaining.has(fn));
|
|
8148
|
+
const staysInSource = [
|
|
8149
|
+
...remaining,
|
|
8150
|
+
...depFunctions.filter((fn) => fnsUsedByRemaining.has(fn))
|
|
8151
|
+
];
|
|
8152
|
+
const stmtsToRemove = stmtsToCopy.filter(
|
|
8153
|
+
(s) => !new Set(collectPrivateStatements(staysInSource, stmtMap)).has(s)
|
|
8154
|
+
);
|
|
8132
8155
|
return {
|
|
8133
|
-
functions: depFunctions,
|
|
8134
|
-
statements: { toCopy, toRemove }
|
|
8156
|
+
functions: { toCopy: depFunctions, toRemove: fnsToRemove },
|
|
8157
|
+
statements: { toCopy: stmtsToCopy, toRemove: stmtsToRemove }
|
|
8135
8158
|
};
|
|
8136
8159
|
}
|
|
8137
8160
|
|
|
8138
8161
|
// src/commands/refactor/extract/findFunction.ts
|
|
8139
8162
|
import {
|
|
8140
|
-
SyntaxKind as
|
|
8163
|
+
SyntaxKind as SyntaxKind8
|
|
8141
8164
|
} from "ts-morph";
|
|
8142
8165
|
function findFunction(sourceFile, functionName) {
|
|
8143
|
-
return sourceFile.getDescendantsOfKind(
|
|
8166
|
+
return sourceFile.getDescendantsOfKind(SyntaxKind8.FunctionDeclaration).find((fn) => fn.getName() === functionName);
|
|
8144
8167
|
}
|
|
8145
8168
|
|
|
8146
8169
|
// src/commands/refactor/extract/getExportedDependencyNames.ts
|
|
8147
|
-
import { SyntaxKind as
|
|
8170
|
+
import { SyntaxKind as SyntaxKind9 } from "ts-morph";
|
|
8148
8171
|
function getExportedDependencyNames(target, sourceFile) {
|
|
8149
8172
|
const calledNames = /* @__PURE__ */ new Set();
|
|
8150
|
-
for (const call of target.getDescendantsOfKind(
|
|
8173
|
+
for (const call of target.getDescendantsOfKind(SyntaxKind9.CallExpression)) {
|
|
8151
8174
|
const expr = call.getExpression();
|
|
8152
|
-
if (expr.getKind() ===
|
|
8175
|
+
if (expr.getKind() === SyntaxKind9.Identifier) {
|
|
8153
8176
|
calledNames.add(expr.getText());
|
|
8154
8177
|
}
|
|
8155
8178
|
}
|
|
8156
8179
|
const exported = [];
|
|
8157
8180
|
for (const fn of sourceFile.getDescendantsOfKind(
|
|
8158
|
-
|
|
8181
|
+
SyntaxKind9.FunctionDeclaration
|
|
8159
8182
|
)) {
|
|
8160
8183
|
const name = fn.getName();
|
|
8161
8184
|
if (name && calledNames.has(name) && fn.isExported()) {
|
|
@@ -8166,9 +8189,9 @@ function getExportedDependencyNames(target, sourceFile) {
|
|
|
8166
8189
|
}
|
|
8167
8190
|
|
|
8168
8191
|
// src/commands/refactor/extract/getStatementNames.ts
|
|
8169
|
-
import { SyntaxKind as
|
|
8192
|
+
import { SyntaxKind as SyntaxKind10 } from "ts-morph";
|
|
8170
8193
|
function getStatementNames(node) {
|
|
8171
|
-
if (node.getKind() ===
|
|
8194
|
+
if (node.getKind() === SyntaxKind10.VariableStatement) {
|
|
8172
8195
|
const stmt = node;
|
|
8173
8196
|
return stmt.getDeclarations().map((d) => d.getName());
|
|
8174
8197
|
}
|
|
@@ -8178,7 +8201,7 @@ function getStatementNames(node) {
|
|
|
8178
8201
|
|
|
8179
8202
|
// src/commands/refactor/extract/resolveImports.ts
|
|
8180
8203
|
import {
|
|
8181
|
-
SyntaxKind as
|
|
8204
|
+
SyntaxKind as SyntaxKind11
|
|
8182
8205
|
} from "ts-morph";
|
|
8183
8206
|
|
|
8184
8207
|
// src/commands/refactor/extract/matchImport.ts
|
|
@@ -8222,7 +8245,7 @@ function matchImport(importDecl, neededNames) {
|
|
|
8222
8245
|
function getReferencedNames(nodes) {
|
|
8223
8246
|
const names = /* @__PURE__ */ new Set();
|
|
8224
8247
|
for (const node of nodes) {
|
|
8225
|
-
for (const id of node.getDescendantsOfKind(
|
|
8248
|
+
for (const id of node.getDescendantsOfKind(SyntaxKind11.Identifier)) {
|
|
8226
8249
|
names.add(id.getText());
|
|
8227
8250
|
}
|
|
8228
8251
|
}
|
|
@@ -8237,7 +8260,7 @@ function getLocallyDeclaredNames(functions) {
|
|
|
8237
8260
|
names.add(param.getName());
|
|
8238
8261
|
}
|
|
8239
8262
|
for (const varDecl of fn.getDescendantsOfKind(
|
|
8240
|
-
|
|
8263
|
+
SyntaxKind11.VariableDeclaration
|
|
8241
8264
|
)) {
|
|
8242
8265
|
names.add(varDecl.getName());
|
|
8243
8266
|
}
|
|
@@ -8274,26 +8297,24 @@ function extractTexts(target, allFunctions, statements) {
|
|
|
8274
8297
|
function analyseTarget(sourceFile, functionName) {
|
|
8275
8298
|
const target = findFunction(sourceFile, functionName);
|
|
8276
8299
|
if (!target) throw new Error(`Function "${functionName}" not found`);
|
|
8277
|
-
const { functions
|
|
8278
|
-
|
|
8279
|
-
sourceFile
|
|
8280
|
-
);
|
|
8281
|
-
const all = [target, ...dependencies];
|
|
8300
|
+
const { functions, statements } = collectDependencies(target, sourceFile);
|
|
8301
|
+
const all = [target, ...functions.toCopy];
|
|
8282
8302
|
return {
|
|
8283
8303
|
target,
|
|
8284
|
-
dependencies,
|
|
8304
|
+
dependencies: functions.toCopy,
|
|
8305
|
+
functionsToRemove: functions.toRemove,
|
|
8285
8306
|
statementsToCopy: statements.toCopy,
|
|
8286
8307
|
statementsToRemove: statements.toRemove,
|
|
8287
8308
|
imports: resolveImports(
|
|
8288
8309
|
target,
|
|
8289
|
-
|
|
8310
|
+
functions.toCopy,
|
|
8290
8311
|
sourceFile,
|
|
8291
8312
|
statements.toCopy
|
|
8292
8313
|
),
|
|
8293
8314
|
exportedDeps: getExportedDependencyNames(target, sourceFile),
|
|
8294
8315
|
extractedNames: [
|
|
8295
8316
|
...statements.toRemove.flatMap(getStatementNames),
|
|
8296
|
-
...
|
|
8317
|
+
...[target, ...functions.toRemove].map((fn) => fn.getName()).filter(Boolean)
|
|
8297
8318
|
],
|
|
8298
8319
|
functionTexts: extractTexts(target, all, statements.toCopy)
|
|
8299
8320
|
};
|
|
@@ -8394,16 +8415,16 @@ function rewriteImportPaths(imports, sourcePath, destPath) {
|
|
|
8394
8415
|
}
|
|
8395
8416
|
|
|
8396
8417
|
// src/commands/refactor/extract/sourceReferencesName.ts
|
|
8397
|
-
import { SyntaxKind as
|
|
8418
|
+
import { SyntaxKind as SyntaxKind12 } from "ts-morph";
|
|
8398
8419
|
var DECLARATION_KINDS = /* @__PURE__ */ new Set([
|
|
8399
|
-
|
|
8400
|
-
|
|
8401
|
-
|
|
8420
|
+
SyntaxKind12.FunctionDeclaration,
|
|
8421
|
+
SyntaxKind12.ImportSpecifier,
|
|
8422
|
+
SyntaxKind12.ExportSpecifier
|
|
8402
8423
|
]);
|
|
8403
8424
|
function isInsideExtractedFunction(node, extracted) {
|
|
8404
8425
|
let current = node;
|
|
8405
8426
|
while (current) {
|
|
8406
|
-
if (current.getKind() !==
|
|
8427
|
+
if (current.getKind() !== SyntaxKind12.FunctionDeclaration) {
|
|
8407
8428
|
current = current.getParent();
|
|
8408
8429
|
continue;
|
|
8409
8430
|
}
|
|
@@ -8415,7 +8436,7 @@ function isInsideExtractedFunction(node, extracted) {
|
|
|
8415
8436
|
}
|
|
8416
8437
|
function sourceReferencesName(sourceFile, functionName, extractedNames) {
|
|
8417
8438
|
const extracted = new Set(extractedNames);
|
|
8418
|
-
for (const id of sourceFile.getDescendantsOfKind(
|
|
8439
|
+
for (const id of sourceFile.getDescendantsOfKind(SyntaxKind12.Identifier)) {
|
|
8419
8440
|
if (id.getText() !== functionName) continue;
|
|
8420
8441
|
const parent = id.getParent();
|
|
8421
8442
|
if (!parent || DECLARATION_KINDS.has(parent.getKind())) continue;
|
|
@@ -8620,24 +8641,24 @@ import chalk107 from "chalk";
|
|
|
8620
8641
|
import { Project as Project3 } from "ts-morph";
|
|
8621
8642
|
|
|
8622
8643
|
// src/commands/refactor/renameSymbol/findSymbol.ts
|
|
8623
|
-
import { SyntaxKind as
|
|
8644
|
+
import { SyntaxKind as SyntaxKind13 } from "ts-morph";
|
|
8624
8645
|
var declarationKinds = [
|
|
8625
|
-
|
|
8626
|
-
|
|
8627
|
-
|
|
8628
|
-
|
|
8629
|
-
|
|
8630
|
-
|
|
8631
|
-
|
|
8632
|
-
|
|
8633
|
-
|
|
8646
|
+
SyntaxKind13.VariableDeclaration,
|
|
8647
|
+
SyntaxKind13.FunctionDeclaration,
|
|
8648
|
+
SyntaxKind13.ClassDeclaration,
|
|
8649
|
+
SyntaxKind13.InterfaceDeclaration,
|
|
8650
|
+
SyntaxKind13.TypeAliasDeclaration,
|
|
8651
|
+
SyntaxKind13.EnumDeclaration,
|
|
8652
|
+
SyntaxKind13.PropertyDeclaration,
|
|
8653
|
+
SyntaxKind13.MethodDeclaration,
|
|
8654
|
+
SyntaxKind13.Parameter
|
|
8634
8655
|
];
|
|
8635
8656
|
function isDeclaration(identifier) {
|
|
8636
8657
|
const parent = identifier.getParent();
|
|
8637
8658
|
return parent !== void 0 && declarationKinds.includes(parent.getKind());
|
|
8638
8659
|
}
|
|
8639
8660
|
function findSymbol(sourceFile, symbolName) {
|
|
8640
|
-
for (const id of sourceFile.getDescendantsOfKind(
|
|
8661
|
+
for (const id of sourceFile.getDescendantsOfKind(SyntaxKind13.Identifier)) {
|
|
8641
8662
|
if (id.getText() === symbolName && isDeclaration(id)) return id;
|
|
8642
8663
|
}
|
|
8643
8664
|
return void 0;
|