@esportsplus/typescript 0.25.1 → 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,8 +11,7 @@ type ModifyOptions = {
11
11
  };
12
12
  declare const _default: {
13
13
  find: (sourceFile: ts.SourceFile, packageName: string) => ImportInfo[];
14
- isFromPackage: (node: ts.Identifier, packageName: string, checker?: ts.TypeChecker) => boolean;
15
- isSymbolFromPackage: (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;
16
15
  modify: (sourceCode: string, sourceFile: ts.SourceFile, packageName: string, options: ModifyOptions) => string;
17
16
  trace: (node: ts.Identifier, checker: ts.TypeChecker) => string | null;
18
17
  };
@@ -22,34 +22,35 @@ const find = (sourceFile, packageName) => {
22
22
  }
23
23
  return imports;
24
24
  };
25
- const isFromPackage = (node, packageName, checker) => {
26
- if (!checker) {
27
- return true;
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
+ }
28
30
  }
29
- let origin = trace(node, checker);
30
- return origin === null || origin.includes(packageName);
31
- };
32
- const isSymbolFromPackage = (checker, node, packageName, symbolName) => {
33
31
  let symbol = checker.getSymbolAtLocation(node);
34
32
  if (!symbol) {
33
+ if (packageImports && ts.isIdentifier(node) && packageImports.has(node.text)) {
34
+ return true;
35
+ }
35
36
  return false;
36
37
  }
37
38
  if (symbol.flags & ts.SymbolFlags.Alias) {
38
39
  symbol = checker.getAliasedSymbol(symbol);
39
40
  }
40
41
  if (symbolName && symbol.name !== symbolName) {
41
- return false;
42
+ return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
42
43
  }
43
44
  let declarations = symbol.getDeclarations();
44
45
  if (!declarations || declarations.length === 0) {
45
- return false;
46
+ return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
46
47
  }
47
48
  for (let i = 0, n = declarations.length; i < n; i++) {
48
- if (declarations[i].getSourceFile().fileName.includes(packageName)) {
49
+ if (declarations[i].getSourceFile().fileName.includes(pkg)) {
49
50
  return true;
50
51
  }
51
52
  }
52
- return false;
53
+ return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
53
54
  };
54
55
  const modify = (sourceCode, sourceFile, packageName, options) => {
55
56
  let { namespace } = options;
@@ -114,4 +115,4 @@ const trace = (node, checker) => {
114
115
  }
115
116
  return declarations[0].getSourceFile().fileName;
116
117
  };
117
- export default { find, isFromPackage, isSymbolFromPackage, 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.1",
40
+ "version": "0.26.0",
41
41
  "scripts": {
42
42
  "build": "tsc && tsc-alias",
43
43
  "-": "-"
@@ -52,29 +52,29 @@ const find = (sourceFile: ts.SourceFile, packageName: string): ImportInfo[] => {
52
52
  return imports;
53
53
  };
54
54
 
55
- // Check if identifier traces back to a specific package
56
- const isFromPackage = (node: ts.Identifier, packageName: string, checker?: ts.TypeChecker): boolean => {
57
- if (!checker) {
58
- return true;
59
- }
60
-
61
- let origin = trace(node, checker);
62
-
63
- // If can't resolve symbol (e.g., sourceFile not in program), assume valid
64
- // False positives cause compile errors; false negatives silently skip transforms
65
- return origin === null || origin.includes(packageName);
66
- };
67
-
68
55
  // Check if node's symbol originates from a specific package (with optional symbol name validation)
69
- const isSymbolFromPackage = (
56
+ const inPackage = (
70
57
  checker: ts.TypeChecker,
71
58
  node: ts.Node,
72
- packageName: string,
73
- symbolName?: string
59
+ pkg: string,
60
+ symbolName?: string,
61
+ packageImports?: Set<string>
74
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
+
75
70
  let symbol = checker.getSymbolAtLocation(node);
76
71
 
77
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
+
78
78
  return false;
79
79
  }
80
80
 
@@ -85,23 +85,23 @@ const isSymbolFromPackage = (
85
85
 
86
86
  // Check symbol name if specified
87
87
  if (symbolName && symbol.name !== symbolName) {
88
- return false;
88
+ return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
89
89
  }
90
90
 
91
91
  let declarations = symbol.getDeclarations();
92
92
 
93
93
  if (!declarations || declarations.length === 0) {
94
- return false;
94
+ return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
95
95
  }
96
96
 
97
97
  // Check if any declaration is from the expected package
98
98
  for (let i = 0, n = declarations.length; i < n; i++) {
99
- if (declarations[i].getSourceFile().fileName.includes(packageName)) {
99
+ if (declarations[i].getSourceFile().fileName.includes(pkg)) {
100
100
  return true;
101
101
  }
102
102
  }
103
103
 
104
- return false;
104
+ return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
105
105
  };
106
106
 
107
107
  // Modify imports: remove specified, add needed, delete if empty
@@ -204,5 +204,5 @@ const trace = (node: ts.Identifier, checker: ts.TypeChecker): string | null => {
204
204
  };
205
205
 
206
206
 
207
- export default { find, isFromPackage, isSymbolFromPackage, modify, trace };
207
+ export default { find, inPackage, modify, trace };
208
208
  export type { ImportInfo, ModifyOptions };