@esportsplus/typescript 0.27.2 → 0.27.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.
@@ -23,46 +23,79 @@ const all = (file, pkg) => {
23
23
  return imports;
24
24
  };
25
25
  const includes = (checker, node, pkg, symbolName) => {
26
+ if (!ts.isIdentifier(node)) {
27
+ return false;
28
+ }
29
+ if (symbolName && node.text !== symbolName) {
30
+ return false;
31
+ }
26
32
  let file = node.getSourceFile(), imports = cache.get(file);
27
33
  if (!imports) {
28
34
  imports = new Map();
29
35
  cache.set(file, imports);
30
36
  }
31
- let varnames = imports.get(pkg);
32
- if (!varnames) {
33
- varnames = new Set();
34
- for (let info of all(file, pkg)) {
35
- for (let [, varname] of info.specifiers) {
36
- varnames.add(varname);
37
+ let names = imports.get(pkg);
38
+ if (!names) {
39
+ names = new Set();
40
+ let packages = all(file, pkg);
41
+ for (let i = 0, n = packages.length; i < n; i++) {
42
+ for (let [, localName] of packages[i].specifiers) {
43
+ names.add(localName);
37
44
  }
38
45
  }
39
- imports.set(pkg, varnames);
46
+ imports.set(pkg, names);
40
47
  }
41
- if (ts.isIdentifier(node) && varnames.has(node.text) && (!symbolName || node.text === symbolName)) {
48
+ if (names.has(node.text)) {
49
+ let symbol = checker?.getSymbolAtLocation(node);
50
+ if (symbol) {
51
+ let declarations = symbol.getDeclarations();
52
+ if (declarations && declarations.length > 0) {
53
+ for (let i = 0, n = declarations.length; i < n; i++) {
54
+ let decl = declarations[i];
55
+ if (ts.isImportSpecifier(decl)) {
56
+ let importDecl = decl.parent?.parent?.parent;
57
+ if (importDecl && ts.isImportDeclaration(importDecl) && ts.isStringLiteral(importDecl.moduleSpecifier)) {
58
+ if (importDecl.moduleSpecifier.text === pkg) {
59
+ return true;
60
+ }
61
+ }
62
+ }
63
+ if (decl.getSourceFile().fileName.includes(pkg)) {
64
+ return true;
65
+ }
66
+ }
67
+ }
68
+ }
42
69
  return true;
43
70
  }
44
- let symbol = checker.getSymbolAtLocation(node);
71
+ let symbol = checker?.getSymbolAtLocation(node);
45
72
  if (!symbol) {
46
- if (ts.isIdentifier(node) && varnames.has(node.text)) {
47
- return true;
48
- }
49
73
  return false;
50
74
  }
51
- if (symbol.flags & ts.SymbolFlags.Alias) {
52
- symbol = checker.getAliasedSymbol(symbol);
53
- }
54
- if (symbolName && symbol.name !== symbolName) {
55
- return ts.isIdentifier(node) && varnames.has(node.text);
56
- }
57
75
  let declarations = symbol.getDeclarations();
58
- if (!declarations || declarations.length === 0) {
59
- return ts.isIdentifier(node) && varnames.has(node.text);
76
+ if (declarations && declarations.length > 0) {
77
+ for (let i = 0, n = declarations.length; i < n; i++) {
78
+ let decl = declarations[i];
79
+ if (decl.getSourceFile().fileName.includes(pkg)) {
80
+ return true;
81
+ }
82
+ }
60
83
  }
61
- for (let i = 0, n = declarations.length; i < n; i++) {
62
- if (declarations[i].getSourceFile().fileName.includes(pkg)) {
63
- return true;
84
+ try {
85
+ let aliased = checker.getAliasedSymbol(symbol);
86
+ if (aliased && aliased !== symbol) {
87
+ let aliasedDecls = aliased.getDeclarations();
88
+ if (aliasedDecls) {
89
+ for (let i = 0, n = aliasedDecls.length; i < n; i++) {
90
+ if (aliasedDecls[i].getSourceFile().fileName.includes(pkg)) {
91
+ return true;
92
+ }
93
+ }
94
+ }
64
95
  }
65
96
  }
66
- return ts.isIdentifier(node) && varnames.has(node.text);
97
+ catch {
98
+ }
99
+ return false;
67
100
  };
68
101
  export default { all, includes };
package/package.json CHANGED
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "type": "module",
39
39
  "types": "build/index.d.ts",
40
- "version": "0.27.2",
40
+ "version": "0.27.4",
41
41
  "scripts": {
42
42
  "build": "tsc && tsc-alias",
43
43
  "-": "-"
@@ -55,6 +55,14 @@ const all = (file: ts.SourceFile, pkg: string): ImportInfo[] => {
55
55
 
56
56
  // Check if node's symbol originates from a specific package (with optional symbol name validation)
57
57
  const includes = (checker: ts.TypeChecker, node: ts.Node, pkg: string, symbolName?: string): boolean => {
58
+ if (!ts.isIdentifier(node)) {
59
+ return false;
60
+ }
61
+
62
+ if (symbolName && node.text !== symbolName) {
63
+ return false;
64
+ }
65
+
58
66
  let file = node.getSourceFile(),
59
67
  imports = cache.get(file);
60
68
 
@@ -63,60 +71,95 @@ const includes = (checker: ts.TypeChecker, node: ts.Node, pkg: string, symbolNam
63
71
  cache.set(file, imports);
64
72
  }
65
73
 
66
- let varnames = imports.get(pkg);
74
+ let names = imports.get(pkg);
75
+
76
+ if (!names) {
77
+ names = new Set();
67
78
 
68
- if (!varnames) {
69
- varnames = new Set();
79
+ let packages = all(file, pkg);
70
80
 
71
- for (let info of all(file, pkg)) {
72
- for (let [, varname] of info.specifiers) {
73
- varnames.add(varname);
81
+ for (let i = 0, n = packages.length; i < n; i++) {
82
+ for (let [, localName] of packages[i].specifiers) {
83
+ names.add(localName);
74
84
  }
75
85
  }
76
86
 
77
- imports.set(pkg, varnames);
87
+ imports.set(pkg, names);
78
88
  }
79
89
 
80
- // Fast path: identifier matches known import and expected name
81
- if (ts.isIdentifier(node) && varnames.has(node.text) && (!symbolName || node.text === symbolName)) {
82
- return true;
83
- }
90
+ // Fast path: direct import from package
91
+ if (names.has(node.text)) {
92
+ let symbol = checker?.getSymbolAtLocation(node);
84
93
 
85
- let symbol = checker.getSymbolAtLocation(node);
94
+ if (symbol) {
95
+ let declarations = symbol.getDeclarations();
86
96
 
87
- if (!symbol) {
88
- // Fallback: aliased import - check if local name is in imports
89
- if (ts.isIdentifier(node) && varnames.has(node.text)) {
90
- return true;
97
+ if (declarations && declarations.length > 0) {
98
+ for (let i = 0, n = declarations.length; i < n; i++) {
99
+ let decl = declarations[i];
100
+
101
+ if (ts.isImportSpecifier(decl)) {
102
+ let importDecl = decl.parent?.parent?.parent;
103
+
104
+ if (importDecl && ts.isImportDeclaration(importDecl) && ts.isStringLiteral(importDecl.moduleSpecifier)) {
105
+ if (importDecl.moduleSpecifier.text === pkg) {
106
+ return true;
107
+ }
108
+ }
109
+ }
110
+
111
+ if (decl.getSourceFile().fileName.includes(pkg)) {
112
+ return true;
113
+ }
114
+ }
115
+ }
91
116
  }
92
117
 
93
- return false;
118
+ // If checker failed but name matches direct import, trust it
119
+ return true;
94
120
  }
95
121
 
96
- // Follow aliases to original symbol (handles re-exports and aliased imports)
97
- if (symbol.flags & ts.SymbolFlags.Alias) {
98
- symbol = checker.getAliasedSymbol(symbol);
99
- }
122
+ // Slow path: check for re-exports via aliased symbol
123
+ let symbol = checker?.getSymbolAtLocation(node);
100
124
 
101
- // Check symbol name if specified
102
- if (symbolName && symbol.name !== symbolName) {
103
- return ts.isIdentifier(node) && varnames.has(node.text);
125
+ if (!symbol) {
126
+ return false;
104
127
  }
105
128
 
129
+ // Check declarations
106
130
  let declarations = symbol.getDeclarations();
107
131
 
108
- if (!declarations || declarations.length === 0) {
109
- return ts.isIdentifier(node) && varnames.has(node.text);
132
+ if (declarations && declarations.length > 0) {
133
+ for (let i = 0, n = declarations.length; i < n; i++) {
134
+ let decl = declarations[i];
135
+
136
+ if (decl.getSourceFile().fileName.includes(pkg)) {
137
+ return true;
138
+ }
139
+ }
110
140
  }
111
141
 
112
- // Check if any declaration is from the expected package
113
- for (let i = 0, n = declarations.length; i < n; i++) {
114
- if (declarations[i].getSourceFile().fileName.includes(pkg)) {
115
- return true;
142
+ // Check aliased symbol for re-exports
143
+ try {
144
+ let aliased = checker.getAliasedSymbol(symbol);
145
+
146
+ if (aliased && aliased !== symbol) {
147
+ let aliasedDecls = aliased.getDeclarations();
148
+
149
+ if (aliasedDecls) {
150
+ for (let i = 0, n = aliasedDecls.length; i < n; i++) {
151
+ if (aliasedDecls[i].getSourceFile().fileName.includes(pkg)) {
152
+ return true;
153
+ }
154
+ }
155
+ }
116
156
  }
117
157
  }
158
+ catch {
159
+ // getAliasedSymbol can throw for non-alias symbols
160
+ }
118
161
 
119
- return ts.isIdentifier(node) && varnames.has(node.text);
162
+ return false;
120
163
  };
121
164
 
122
165