@esportsplus/reactivity 0.23.3 → 0.23.4

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,70 +1,24 @@
1
- import { applyReplacements } from '@esportsplus/typescript/transformer';
2
- import ts from 'typescript';
3
- function findReactivityImport(sourceFile) {
4
- for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
5
- let stmt = sourceFile.statements[i];
6
- if (ts.isImportDeclaration(stmt) &&
7
- ts.isStringLiteral(stmt.moduleSpecifier) &&
8
- stmt.moduleSpecifier.text === '@esportsplus/reactivity' &&
9
- stmt.importClause?.namedBindings &&
10
- ts.isNamedImports(stmt.importClause.namedBindings)) {
11
- return stmt;
12
- }
13
- }
14
- return null;
15
- }
16
- function getExistingSpecifiers(namedImports) {
17
- let existing = new Set();
18
- for (let i = 0, n = namedImports.elements.length; i < n; i++) {
19
- let el = namedImports.elements[i], name = el.propertyName?.text ?? el.name.text;
20
- existing.add(name);
21
- }
22
- return existing;
23
- }
24
- function getFirstImportPos(sourceFile) {
25
- for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
26
- if (ts.isImportDeclaration(sourceFile.statements[i])) {
27
- return sourceFile.statements[i].getStart(sourceFile);
28
- }
29
- }
30
- return 0;
31
- }
1
+ import { addImport, applyReplacements } from '@esportsplus/typescript/transformer';
32
2
  const addMissingImports = (code, needed, extraImports) => {
33
- let sourceFile = ts.createSourceFile('temp.ts', code, ts.ScriptTarget.Latest, true), reactivityImport = findReactivityImport(sourceFile);
34
- if (!reactivityImport) {
35
- return code;
36
- }
37
- let extraSpecifiers = new Set(), namedImports = reactivityImport.importClause.namedBindings, existing = getExistingSpecifiers(namedImports), toAdd = [];
3
+ let extraSpecifiers = new Set(), reactivitySpecifiers = [];
38
4
  if (extraImports) {
39
5
  for (let i = 0, n = extraImports.length; i < n; i++) {
40
6
  extraSpecifiers.add(extraImports[i].specifier);
41
7
  }
42
8
  }
43
9
  for (let imp of needed) {
44
- if (!extraSpecifiers.has(imp) && !existing.has(imp)) {
45
- toAdd.push(imp);
10
+ if (!extraSpecifiers.has(imp)) {
11
+ reactivitySpecifiers.push(imp);
46
12
  }
47
13
  }
48
- if (toAdd.length > 0) {
49
- let combined = [];
50
- for (let item of existing) {
51
- combined.push(item);
52
- }
53
- for (let i = 0, n = toAdd.length; i < n; i++) {
54
- combined.push(toAdd[i]);
55
- }
56
- combined.sort();
57
- let newSpecifiers = `{ ${combined.join(', ')} }`, bindingsStart = namedImports.getStart(sourceFile), bindingsEnd = namedImports.getEnd();
58
- code = code.substring(0, bindingsStart) + newSpecifiers + code.substring(bindingsEnd);
14
+ if (reactivitySpecifiers.length > 0) {
15
+ code = addImport(code, '@esportsplus/reactivity', reactivitySpecifiers);
59
16
  }
60
17
  if (extraImports) {
61
- let insertPos = getFirstImportPos(ts.createSourceFile('temp.ts', code, ts.ScriptTarget.Latest, true));
62
18
  for (let i = 0, n = extraImports.length; i < n; i++) {
63
19
  let extra = extraImports[i];
64
- if (needed.has(extra.specifier) && !code.includes(extra.module)) {
65
- code = code.substring(0, insertPos) +
66
- `import { ${extra.specifier} } from '${extra.module}';\n` +
67
- code.substring(insertPos);
20
+ if (needed.has(extra.specifier)) {
21
+ code = addImport(code, extra.module, [extra.specifier]);
68
22
  }
69
23
  }
70
24
  }
package/package.json CHANGED
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "type": "module",
43
43
  "types": "build/index.d.ts",
44
- "version": "0.23.3",
44
+ "version": "0.23.4",
45
45
  "scripts": {
46
46
  "build": "tsc && tsc-alias",
47
47
  "build:test": "pnpm build && vite build --config test/vite.config.ts",
@@ -1,5 +1,4 @@
1
- import { applyReplacements, type Replacement } from '@esportsplus/typescript/transformer';
2
- import ts from 'typescript';
1
+ import { addImport, applyReplacements, type Replacement } from '@esportsplus/typescript/transformer';
3
2
 
4
3
 
5
4
  type ExtraImport = {
@@ -8,60 +7,9 @@ type ExtraImport = {
8
7
  };
9
8
 
10
9
 
11
- function findReactivityImport(sourceFile: ts.SourceFile): ts.ImportDeclaration | null {
12
- for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
13
- let stmt = sourceFile.statements[i];
14
-
15
- if (
16
- ts.isImportDeclaration(stmt) &&
17
- ts.isStringLiteral(stmt.moduleSpecifier) &&
18
- stmt.moduleSpecifier.text === '@esportsplus/reactivity' &&
19
- stmt.importClause?.namedBindings &&
20
- ts.isNamedImports(stmt.importClause.namedBindings)
21
- ) {
22
- return stmt;
23
- }
24
- }
25
-
26
- return null;
27
- }
28
-
29
- function getExistingSpecifiers(namedImports: ts.NamedImports): Set<string> {
30
- let existing = new Set<string>();
31
-
32
- for (let i = 0, n = namedImports.elements.length; i < n; i++) {
33
- let el = namedImports.elements[i],
34
- name = el.propertyName?.text ?? el.name.text;
35
-
36
- existing.add(name);
37
- }
38
-
39
- return existing;
40
- }
41
-
42
- function getFirstImportPos(sourceFile: ts.SourceFile): number {
43
- for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
44
- if (ts.isImportDeclaration(sourceFile.statements[i])) {
45
- return sourceFile.statements[i].getStart(sourceFile);
46
- }
47
- }
48
-
49
- return 0;
50
- }
51
-
52
-
53
10
  const addMissingImports = (code: string, needed: Set<string>, extraImports?: ExtraImport[]): string => {
54
- let sourceFile = ts.createSourceFile('temp.ts', code, ts.ScriptTarget.Latest, true),
55
- reactivityImport = findReactivityImport(sourceFile);
56
-
57
- if (!reactivityImport) {
58
- return code;
59
- }
60
-
61
11
  let extraSpecifiers = new Set<string>(),
62
- namedImports = reactivityImport.importClause!.namedBindings as ts.NamedImports,
63
- existing = getExistingSpecifiers(namedImports),
64
- toAdd: string[] = [];
12
+ reactivitySpecifiers: string[] = [];
65
13
 
66
14
  if (extraImports) {
67
15
  for (let i = 0, n = extraImports.length; i < n; i++) {
@@ -70,43 +18,21 @@ const addMissingImports = (code: string, needed: Set<string>, extraImports?: Ext
70
18
  }
71
19
 
72
20
  for (let imp of needed) {
73
- if (!extraSpecifiers.has(imp) && !existing.has(imp)) {
74
- toAdd.push(imp);
21
+ if (!extraSpecifiers.has(imp)) {
22
+ reactivitySpecifiers.push(imp);
75
23
  }
76
24
  }
77
25
 
78
- if (toAdd.length > 0) {
79
- let combined: string[] = [];
80
-
81
- for (let item of existing) {
82
- combined.push(item);
83
- }
84
-
85
- for (let i = 0, n = toAdd.length; i < n; i++) {
86
- combined.push(toAdd[i]);
87
- }
88
-
89
- combined.sort();
90
-
91
- let newSpecifiers = `{ ${combined.join(', ')} }`,
92
- bindingsStart = namedImports.getStart(sourceFile),
93
- bindingsEnd = namedImports.getEnd();
94
-
95
- code = code.substring(0, bindingsStart) + newSpecifiers + code.substring(bindingsEnd);
26
+ if (reactivitySpecifiers.length > 0) {
27
+ code = addImport(code, '@esportsplus/reactivity', reactivitySpecifiers);
96
28
  }
97
29
 
98
30
  if (extraImports) {
99
- let insertPos = getFirstImportPos(
100
- ts.createSourceFile('temp.ts', code, ts.ScriptTarget.Latest, true)
101
- );
102
-
103
31
  for (let i = 0, n = extraImports.length; i < n; i++) {
104
32
  let extra = extraImports[i];
105
33
 
106
- if (needed.has(extra.specifier) && !code.includes(extra.module)) {
107
- code = code.substring(0, insertPos) +
108
- `import { ${extra.specifier} } from '${extra.module}';\n` +
109
- code.substring(insertPos);
34
+ if (needed.has(extra.specifier)) {
35
+ code = addImport(code, extra.module, [extra.specifier]);
110
36
  }
111
37
  }
112
38
  }