mobx-tanstack-query-api 0.0.80 → 0.0.82

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