@esportsplus/typescript 0.21.0 → 0.22.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.
@@ -0,0 +1,18 @@
1
+ import { ts } from '../index.js';
2
+ type ImportInfo = {
3
+ end: number;
4
+ specifiers: Map<string, string>;
5
+ start: number;
6
+ };
7
+ type ModifyOptions = {
8
+ add?: Iterable<string>;
9
+ remove?: Iterable<string>;
10
+ };
11
+ declare const _default: {
12
+ find: (sourceFile: ts.SourceFile, packageName: string) => ImportInfo[];
13
+ isFromPackage: (node: ts.Identifier, packageName: string, checker?: ts.TypeChecker) => boolean;
14
+ modify: (sourceCode: string, sourceFile: ts.SourceFile, packageName: string, options: ModifyOptions) => string;
15
+ trace: (node: ts.Identifier, checker: ts.TypeChecker) => string | null;
16
+ };
17
+ export default _default;
18
+ export type { ImportInfo, ModifyOptions };
@@ -0,0 +1,82 @@
1
+ import { ts } from '../index.js';
2
+ import code from './code.js';
3
+ const find = (sourceFile, packageName) => {
4
+ let imports = [];
5
+ for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
6
+ let stmt = sourceFile.statements[i];
7
+ if (!ts.isImportDeclaration(stmt)) {
8
+ continue;
9
+ }
10
+ let moduleSpecifier = stmt.moduleSpecifier;
11
+ if (!ts.isStringLiteral(moduleSpecifier) || moduleSpecifier.text !== packageName) {
12
+ continue;
13
+ }
14
+ let bindings = stmt.importClause?.namedBindings, specifiers = new Map();
15
+ if (bindings && ts.isNamedImports(bindings)) {
16
+ for (let j = 0, m = bindings.elements.length; j < m; j++) {
17
+ let element = bindings.elements[j], name = element.name.text, propertyName = element.propertyName?.text || name;
18
+ specifiers.set(propertyName, name);
19
+ }
20
+ }
21
+ imports.push({ end: stmt.end, specifiers, start: stmt.getStart() });
22
+ }
23
+ return imports;
24
+ };
25
+ const isFromPackage = (node, packageName, checker) => {
26
+ if (!checker) {
27
+ return true;
28
+ }
29
+ let origin = trace(node, checker);
30
+ return origin !== null && origin.includes(packageName);
31
+ };
32
+ const modify = (sourceCode, sourceFile, packageName, options) => {
33
+ if (!options.add && !options.remove) {
34
+ return sourceCode;
35
+ }
36
+ let add = options.add ? new Set(options.add) : null, imports = find(sourceFile, packageName), remove = options.remove ? new Set(options.remove) : null;
37
+ if (imports.length === 0) {
38
+ if (!add || add.size === 0) {
39
+ return sourceCode;
40
+ }
41
+ return `import { ${[...add].sort().join(', ')} } from '${packageName}';\n` + sourceCode;
42
+ }
43
+ let specifiers = new Set();
44
+ for (let i = 0, n = imports.length; i < n; i++) {
45
+ for (let [name, alias] of imports[i].specifiers) {
46
+ if (!remove || (!remove.has(name) && !remove.has(alias))) {
47
+ specifiers.add(name === alias ? name : `${name} as ${alias}`);
48
+ }
49
+ }
50
+ }
51
+ if (add) {
52
+ for (let name of add) {
53
+ specifiers.add(name);
54
+ }
55
+ }
56
+ let replacements = [], sorted = [...specifiers].sort().join(', ');
57
+ for (let i = 0, n = imports.length; i < n; i++) {
58
+ replacements.push({
59
+ end: imports[i].end,
60
+ newText: i === 0 && specifiers.size > 0
61
+ ? `import { ${sorted} } from '${packageName}';`
62
+ : '',
63
+ start: imports[i].start
64
+ });
65
+ }
66
+ return code.replaceReverse(sourceCode, replacements);
67
+ };
68
+ const trace = (node, checker) => {
69
+ let symbol = checker.getSymbolAtLocation(node);
70
+ if (!symbol) {
71
+ return null;
72
+ }
73
+ if (symbol.flags & ts.SymbolFlags.Alias) {
74
+ symbol = checker.getAliasedSymbol(symbol);
75
+ }
76
+ let declarations = symbol.getDeclarations();
77
+ if (!declarations || declarations.length === 0) {
78
+ return null;
79
+ }
80
+ return declarations[0].getSourceFile().fileName;
81
+ };
82
+ export default { find, isFromPackage, modify, trace };
@@ -1,5 +1,6 @@
1
1
  export * as ast from './ast/index.js';
2
2
  export { default as code } from './code.js';
3
+ export { default as imports } from './imports.js';
3
4
  export { default as plugin } from './plugins/index.js';
4
5
  export { default as program } from './program.js';
5
6
  export { default as uid } from './uid.js';
@@ -1,5 +1,6 @@
1
1
  export * as ast from './ast/index.js';
2
2
  export { default as code } from './code.js';
3
+ export { default as imports } from './imports.js';
3
4
  export { default as plugin } from './plugins/index.js';
4
5
  export { default as program } from './program.js';
5
6
  export { default as uid } from './uid.js';
package/package.json CHANGED
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "type": "module",
39
39
  "types": "build/index.d.ts",
40
- "version": "0.21.0",
40
+ "version": "0.22.0",
41
41
  "scripts": {
42
42
  "build": "tsc && tsc-alias",
43
43
  "-": "-"
@@ -0,0 +1,147 @@
1
+ import type { Replacement } from './types.js';
2
+ import { ts } from '~/index.js';
3
+ import code from './code.js';
4
+
5
+
6
+ type ImportInfo = {
7
+ end: number;
8
+ specifiers: Map<string, string>;
9
+ start: number;
10
+ };
11
+
12
+ type ModifyOptions = {
13
+ add?: Iterable<string>;
14
+ remove?: Iterable<string>;
15
+ };
16
+
17
+
18
+ // Find all named imports from a specific package
19
+ const find = (sourceFile: ts.SourceFile, packageName: string): ImportInfo[] => {
20
+ let imports: ImportInfo[] = [];
21
+
22
+ for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
23
+ let stmt = sourceFile.statements[i];
24
+
25
+ if (!ts.isImportDeclaration(stmt)) {
26
+ continue;
27
+ }
28
+
29
+ let moduleSpecifier = stmt.moduleSpecifier;
30
+
31
+ if (!ts.isStringLiteral(moduleSpecifier) || moduleSpecifier.text !== packageName) {
32
+ continue;
33
+ }
34
+
35
+ let bindings = stmt.importClause?.namedBindings,
36
+ specifiers = new Map<string, string>();
37
+
38
+ if (bindings && ts.isNamedImports(bindings)) {
39
+ for (let j = 0, m = bindings.elements.length; j < m; j++) {
40
+ let element = bindings.elements[j],
41
+ name = element.name.text,
42
+ propertyName = element.propertyName?.text || name;
43
+
44
+ specifiers.set(propertyName, name);
45
+ }
46
+ }
47
+
48
+ imports.push({ end: stmt.end, specifiers, start: stmt.getStart() });
49
+ }
50
+
51
+ return imports;
52
+ };
53
+
54
+ // Check if identifier traces back to a specific package
55
+ const isFromPackage = (node: ts.Identifier, packageName: string, checker?: ts.TypeChecker): boolean => {
56
+ if (!checker) {
57
+ return true;
58
+ }
59
+
60
+ let origin = trace(node, checker);
61
+
62
+ return origin !== null && origin.includes(packageName);
63
+ };
64
+
65
+
66
+ // Modify imports: remove specified, add needed, delete if empty
67
+ const modify = (
68
+ sourceCode: string,
69
+ sourceFile: ts.SourceFile,
70
+ packageName: string,
71
+ options: ModifyOptions
72
+ ): string => {
73
+ // Fast path: nothing to change
74
+ if (!options.add && !options.remove) {
75
+ return sourceCode;
76
+ }
77
+
78
+ let add = options.add ? new Set(options.add) : null,
79
+ imports = find(sourceFile, packageName),
80
+ remove = options.remove ? new Set(options.remove) : null;
81
+
82
+ if (imports.length === 0) {
83
+ if (!add || add.size === 0) {
84
+ return sourceCode;
85
+ }
86
+
87
+ return `import { ${[...add].sort().join(', ')} } from '${packageName}';\n` + sourceCode;
88
+ }
89
+
90
+ // Collect all non-removed specifiers from existing imports
91
+ let specifiers = new Set<string>();
92
+
93
+ for (let i = 0, n = imports.length; i < n; i++) {
94
+ for (let [name, alias] of imports[i].specifiers) {
95
+ if (!remove || (!remove.has(name) && !remove.has(alias))) {
96
+ specifiers.add(name === alias ? name : `${name} as ${alias}`);
97
+ }
98
+ }
99
+ }
100
+
101
+ if (add) {
102
+ for (let name of add) {
103
+ specifiers.add(name);
104
+ }
105
+ }
106
+
107
+ // Build replacements - replace first import, remove others
108
+ let replacements: Replacement[] = [],
109
+ sorted = [...specifiers].sort().join(', ');
110
+
111
+ for (let i = 0, n = imports.length; i < n; i++) {
112
+ replacements.push({
113
+ end: imports[i].end,
114
+ newText: i === 0 && specifiers.size > 0
115
+ ? `import { ${sorted} } from '${packageName}';`
116
+ : '',
117
+ start: imports[i].start
118
+ });
119
+ }
120
+
121
+ return code.replaceReverse(sourceCode, replacements);
122
+ };
123
+
124
+ // Trace symbol through re-exports to find original declaration source file
125
+ const trace = (node: ts.Identifier, checker: ts.TypeChecker): string | null => {
126
+ let symbol = checker.getSymbolAtLocation(node);
127
+
128
+ if (!symbol) {
129
+ return null;
130
+ }
131
+
132
+ if (symbol.flags & ts.SymbolFlags.Alias) {
133
+ symbol = checker.getAliasedSymbol(symbol);
134
+ }
135
+
136
+ let declarations = symbol.getDeclarations();
137
+
138
+ if (!declarations || declarations.length === 0) {
139
+ return null;
140
+ }
141
+
142
+ return declarations[0].getSourceFile().fileName;
143
+ };
144
+
145
+
146
+ export default { find, isFromPackage, modify, trace };
147
+ export type { ImportInfo, ModifyOptions };
@@ -1,5 +1,6 @@
1
1
  export * as ast from './ast';
2
2
  export { default as code } from './code';
3
+ export { default as imports } from './imports';
3
4
  export { default as plugin } from './plugins';
4
5
  export { default as program } from './program';
5
6
  export { default as uid } from './uid';