@esportsplus/typescript 0.25.2 → 0.26.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.
@@ -11,7 +11,7 @@ type ModifyOptions = {
11
11
  };
12
12
  declare const _default: {
13
13
  find: (sourceFile: ts.SourceFile, packageName: string) => ImportInfo[];
14
- isFromPackage: (checker: ts.TypeChecker, node: ts.Node, packageName: string, symbolName?: string) => boolean;
14
+ inPackage: (checker: ts.TypeChecker, node: ts.Node, pkg: string, symbolName?: string, packageImports?: Set<string>) => boolean;
15
15
  modify: (sourceCode: string, sourceFile: ts.SourceFile, packageName: string, options: ModifyOptions) => string;
16
16
  trace: (node: ts.Identifier, checker: ts.TypeChecker) => string | null;
17
17
  };
@@ -22,27 +22,35 @@ const find = (sourceFile, packageName) => {
22
22
  }
23
23
  return imports;
24
24
  };
25
- const isFromPackage = (checker, node, packageName, symbolName) => {
25
+ const inPackage = (checker, node, pkg, symbolName, packageImports) => {
26
+ if (packageImports && ts.isIdentifier(node) && packageImports.has(node.text)) {
27
+ if (!symbolName || node.text === symbolName) {
28
+ return true;
29
+ }
30
+ }
26
31
  let symbol = checker.getSymbolAtLocation(node);
27
32
  if (!symbol) {
33
+ if (packageImports && ts.isIdentifier(node) && packageImports.has(node.text)) {
34
+ return true;
35
+ }
28
36
  return false;
29
37
  }
30
38
  if (symbol.flags & ts.SymbolFlags.Alias) {
31
39
  symbol = checker.getAliasedSymbol(symbol);
32
40
  }
33
41
  if (symbolName && symbol.name !== symbolName) {
34
- return false;
42
+ return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
35
43
  }
36
44
  let declarations = symbol.getDeclarations();
37
45
  if (!declarations || declarations.length === 0) {
38
- return false;
46
+ return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
39
47
  }
40
48
  for (let i = 0, n = declarations.length; i < n; i++) {
41
- if (declarations[i].getSourceFile().fileName.includes(packageName)) {
49
+ if (declarations[i].getSourceFile().fileName.includes(pkg)) {
42
50
  return true;
43
51
  }
44
52
  }
45
- return false;
53
+ return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
46
54
  };
47
55
  const modify = (sourceCode, sourceFile, packageName, options) => {
48
56
  let { namespace } = options;
@@ -107,4 +115,4 @@ const trace = (node, checker) => {
107
115
  }
108
116
  return declarations[0].getSourceFile().fileName;
109
117
  };
110
- export default { find, isFromPackage, modify, trace };
118
+ export default { find, inPackage, modify, trace };
package/package.json CHANGED
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "type": "module",
39
39
  "types": "build/index.d.ts",
40
- "version": "0.25.2",
40
+ "version": "0.26.0",
41
41
  "scripts": {
42
42
  "build": "tsc && tsc-alias",
43
43
  "-": "-"
@@ -53,15 +53,28 @@ const find = (sourceFile: ts.SourceFile, packageName: string): ImportInfo[] => {
53
53
  };
54
54
 
55
55
  // Check if node's symbol originates from a specific package (with optional symbol name validation)
56
- const isFromPackage = (
56
+ const inPackage = (
57
57
  checker: ts.TypeChecker,
58
58
  node: ts.Node,
59
- packageName: string,
60
- symbolName?: string
59
+ pkg: string,
60
+ symbolName?: string,
61
+ packageImports?: Set<string>
61
62
  ): boolean => {
63
+ // Fast path: identifier matches known import and expected name
64
+ if (packageImports && ts.isIdentifier(node) && packageImports.has(node.text)) {
65
+ if (!symbolName || node.text === symbolName) {
66
+ return true;
67
+ }
68
+ }
69
+
62
70
  let symbol = checker.getSymbolAtLocation(node);
63
71
 
64
72
  if (!symbol) {
73
+ // Fallback: aliased import - check if local name is in imports
74
+ if (packageImports && ts.isIdentifier(node) && packageImports.has(node.text)) {
75
+ return true;
76
+ }
77
+
65
78
  return false;
66
79
  }
67
80
 
@@ -72,23 +85,23 @@ const isFromPackage = (
72
85
 
73
86
  // Check symbol name if specified
74
87
  if (symbolName && symbol.name !== symbolName) {
75
- return false;
88
+ return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
76
89
  }
77
90
 
78
91
  let declarations = symbol.getDeclarations();
79
92
 
80
93
  if (!declarations || declarations.length === 0) {
81
- return false;
94
+ return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
82
95
  }
83
96
 
84
97
  // Check if any declaration is from the expected package
85
98
  for (let i = 0, n = declarations.length; i < n; i++) {
86
- if (declarations[i].getSourceFile().fileName.includes(packageName)) {
99
+ if (declarations[i].getSourceFile().fileName.includes(pkg)) {
87
100
  return true;
88
101
  }
89
102
  }
90
103
 
91
- return false;
104
+ return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
92
105
  };
93
106
 
94
107
  // Modify imports: remove specified, add needed, delete if empty
@@ -191,5 +204,5 @@ const trace = (node: ts.Identifier, checker: ts.TypeChecker): string | null => {
191
204
  };
192
205
 
193
206
 
194
- export default { find, isFromPackage, modify, trace };
207
+ export default { find, inPackage, modify, trace };
195
208
  export type { ImportInfo, ModifyOptions };