mobx-tanstack-query-api 0.0.81 → 0.0.83

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.
@@ -1 +1 @@
1
- {"version":3,"file":"remove-unused-types.d.ts","sourceRoot":"","sources":["../../../src/codegen/utils/remove-unused-types.ts"],"names":[],"mappings":"AA2GA,eAAO,MAAM,iBAAiB,GAAU,SAAS;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,kBAO/D,CAAC"}
1
+ {"version":3,"file":"remove-unused-types.d.ts","sourceRoot":"","sources":["../../../src/codegen/utils/remove-unused-types.ts"],"names":[],"mappings":"AA6GA,eAAO,MAAM,iBAAiB,GAAU,SAAS;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,kBAK/D,CAAC"}
@@ -1,75 +1,76 @@
1
- import { Project, SyntaxKind, } from 'ts-morph';
1
+ /* eslint-disable no-constant-condition */
2
+ import { Project, SyntaxKind } from 'ts-morph';
2
3
  import path from 'node:path';
3
4
  const removeUnusedTypesItteration = async ({ dir }) => {
4
- // Создаем проект в памяти
5
5
  const project = new Project();
6
- // Добавляем все TS/TSX файлы из указанной директории
7
6
  project.addSourceFilesAtPaths([
8
7
  path.join(dir, '**/*.ts'),
9
8
  path.join(dir, '**/*.tsx'),
10
9
  ]);
11
- const sourceFiles = project.getSourceFiles();
12
- const typeDeclarations = new Map();
13
- const usedTypes = new Set();
14
- let dataContractsSourceFile;
15
- // Шаг 1: Собираем все объявления типов
16
- for (const file of sourceFiles) {
17
- if (file.getFilePath().includes(`${dir}/data-contracts.ts`)) {
18
- dataContractsSourceFile = file;
19
- }
20
- // Интерфейсы
21
- for (const intf of file.getInterfaces()) {
22
- const name = intf.getName();
23
- typeDeclarations.set(name, {
24
- node: intf,
25
- file: file.getFilePath(),
26
- line: intf.getStartLineNumber(),
27
- });
28
- }
29
- // Type-алиасы
30
- for (const typeAlias of file.getTypeAliases()) {
31
- const name = typeAlias.getName();
32
- typeDeclarations.set(name, {
33
- node: typeAlias,
34
- file: file.getFilePath(),
35
- line: typeAlias.getStartLineNumber(),
36
- });
10
+ const dataContractsSourceFile = project.getSourceFile((sourceFile) => sourceFile.getFilePath().includes(`${dir}/data-contracts.ts`));
11
+ if (!dataContractsSourceFile)
12
+ return;
13
+ const exportedDeclarations = dataContractsSourceFile.getExportedDeclarations();
14
+ const candidateTypes = new Map();
15
+ for (const [name, declarations] of exportedDeclarations) {
16
+ const validDeclarations = declarations.filter((decl) => decl.getKind() === SyntaxKind.InterfaceDeclaration ||
17
+ decl.getKind() === SyntaxKind.TypeAliasDeclaration);
18
+ if (validDeclarations.length > 0) {
19
+ candidateTypes.set(name, validDeclarations);
37
20
  }
38
21
  }
39
- // Шаг 2: Ищем использования типов
40
- for (const file of sourceFiles) {
41
- // Проверяем все идентификаторы в файле
22
+ if (candidateTypes.size === 0)
23
+ return;
24
+ const usedTypes = new Set();
25
+ const externalFiles = project
26
+ .getSourceFiles()
27
+ .filter((sf) => sf !== dataContractsSourceFile);
28
+ for (const file of externalFiles) {
42
29
  const identifiers = file.getDescendantsOfKind(SyntaxKind.Identifier);
43
30
  for (const identifier of identifiers) {
44
31
  const name = identifier.getText();
45
- if (typeDeclarations.has(name)) {
46
- const parent = identifier.getParent();
47
- const declaration = typeDeclarations.get(name).node;
48
- // Игнорируем само объявление типа
49
- const isDeclaration = parent === declaration;
50
- // Игнорируем экспорт типа
51
- const isExport = parent?.getKind() === SyntaxKind.ExportSpecifier;
52
- if (!isDeclaration && !isExport) {
53
- usedTypes.add(name);
32
+ if (candidateTypes.has(name)) {
33
+ usedTypes.add(name);
34
+ }
35
+ }
36
+ }
37
+ const dependencyGraph = new Map();
38
+ for (const [name, declarations] of candidateTypes) {
39
+ const dependencies = new Set();
40
+ for (const decl of declarations) {
41
+ const identifiers = decl.getDescendantsOfKind(SyntaxKind.Identifier);
42
+ for (const ident of identifiers) {
43
+ const refName = ident.getText();
44
+ if (candidateTypes.has(refName)) {
45
+ dependencies.add(refName);
54
46
  }
55
47
  }
56
48
  }
49
+ dependencyGraph.set(name, dependencies);
57
50
  }
58
- if (!dataContractsSourceFile) {
59
- return;
51
+ const queue = Array.from(usedTypes);
52
+ const visited = new Set(usedTypes);
53
+ while (queue.length > 0) {
54
+ const current = queue.shift();
55
+ if (dependencyGraph.has(current)) {
56
+ for (const dep of dependencyGraph.get(current)) {
57
+ if (!visited.has(dep)) {
58
+ visited.add(dep);
59
+ usedTypes.add(dep);
60
+ queue.push(dep);
61
+ }
62
+ }
63
+ }
60
64
  }
61
65
  let removedCount = 0;
62
- const exportedDeclarations = dataContractsSourceFile.getExportedDeclarations();
63
- // Шаг 3: Фильтруем неиспользуемые типы
64
- for (const [name] of typeDeclarations) {
65
- if (!usedTypes.has(name) && exportedDeclarations.has(name)) {
66
- const declarations = exportedDeclarations.get(name);
67
- declarations?.forEach((declaration) => {
68
- if ('remove' in declaration) {
69
- declaration.remove();
70
- removedCount++;
71
- }
72
- });
66
+ for (const [name, declarations] of candidateTypes) {
67
+ if (usedTypes.has(name))
68
+ continue;
69
+ for (const decl of declarations) {
70
+ if ('remove' in decl) {
71
+ decl.remove();
72
+ removedCount++;
73
+ }
73
74
  }
74
75
  }
75
76
  if (removedCount > 0) {
@@ -78,9 +79,9 @@ const removeUnusedTypesItteration = async ({ dir }) => {
78
79
  return removedCount;
79
80
  };
80
81
  export const removeUnusedTypes = async ({ dir }) => {
81
- let lastRemovedCount = null;
82
- while (lastRemovedCount === null || lastRemovedCount > 0) {
83
- const removedCount = await removeUnusedTypesItteration({ dir });
84
- lastRemovedCount = removedCount ?? 0;
82
+ while (true) {
83
+ const removedCount = (await removeUnusedTypesItteration({ dir })) ?? 0;
84
+ if (removedCount === 0)
85
+ break;
85
86
  }
86
87
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobx-tanstack-query-api",
3
- "version": "0.0.81",
3
+ "version": "0.0.83",
4
4
  "keywords": [],
5
5
  "author": "js2me",
6
6
  "license": "MIT",