@esportsplus/typescript 0.25.2 → 0.26.1
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.
- package/build/compiler/code.d.ts +4 -4
- package/build/compiler/code.js +13 -11
- package/build/compiler/coordinator.js +64 -5
- package/build/compiler/imports.d.ts +1 -3
- package/build/compiler/imports.js +14 -70
- package/package.json +1 -1
- package/src/compiler/code.ts +14 -14
- package/src/compiler/coordinator.ts +106 -11
- package/src/compiler/imports.ts +21 -109
package/build/compiler/code.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
declare const code: {
|
|
2
|
+
(literals: TemplateStringsArray, ...values: unknown[]): string;
|
|
3
|
+
escape(str: string): string;
|
|
4
4
|
};
|
|
5
|
-
export default
|
|
5
|
+
export default code;
|
package/build/compiler/code.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const SINGLE_QUOTE_REGEX = /'/g;
|
|
2
|
+
const code = (literals, ...values) => {
|
|
3
|
+
let buffer = '';
|
|
4
|
+
for (let i = 0, n = literals.length; i < n; i++) {
|
|
5
|
+
buffer += literals[i];
|
|
6
|
+
if (values[i] !== undefined) {
|
|
7
|
+
buffer += values[i];
|
|
8
|
+
}
|
|
4
9
|
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
result = result.substring(0, r.start) + r.newText + result.substring(r.end);
|
|
10
|
-
}
|
|
11
|
-
return result;
|
|
10
|
+
return buffer;
|
|
11
|
+
};
|
|
12
|
+
code.escape = (str) => {
|
|
13
|
+
return str.replace(SINGLE_QUOTE_REGEX, "\\'");
|
|
12
14
|
};
|
|
13
|
-
export default
|
|
15
|
+
export default code;
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { ts } from '../index.js';
|
|
2
|
-
import code from './code.js';
|
|
3
2
|
import imports from './imports.js';
|
|
4
3
|
function applyImports(sourceCode, sourceFile, intents) {
|
|
5
4
|
let result = sourceCode;
|
|
6
5
|
for (let i = 0, n = intents.length; i < n; i++) {
|
|
7
6
|
let intent = intents[i];
|
|
8
|
-
result =
|
|
7
|
+
result = modify(result, sourceFile, intent.package, {
|
|
9
8
|
add: intent.add,
|
|
10
9
|
namespace: intent.namespace,
|
|
11
10
|
remove: intent.remove
|
|
@@ -20,12 +19,11 @@ function applyIntents(sourceCode, sourceFile, intents) {
|
|
|
20
19
|
if (intents.length === 0) {
|
|
21
20
|
return sourceCode;
|
|
22
21
|
}
|
|
23
|
-
|
|
22
|
+
return replaceReverse(sourceCode, intents.map(intent => ({
|
|
24
23
|
end: intent.node.end,
|
|
25
24
|
newText: intent.generate(sourceFile),
|
|
26
25
|
start: intent.node.getStart(sourceFile)
|
|
27
|
-
}));
|
|
28
|
-
return code.replaceReverse(sourceCode, replacements);
|
|
26
|
+
})));
|
|
29
27
|
}
|
|
30
28
|
function applyPrepend(sourceCode, sourceFile, prepend) {
|
|
31
29
|
if (prepend.length === 0) {
|
|
@@ -58,6 +56,67 @@ function hasPattern(sourceCode, patterns) {
|
|
|
58
56
|
}
|
|
59
57
|
return false;
|
|
60
58
|
}
|
|
59
|
+
const modify = (sourceCode, sourceFile, packageName, options) => {
|
|
60
|
+
let { namespace } = options;
|
|
61
|
+
if (!options.add && !options.namespace && !options.remove) {
|
|
62
|
+
return sourceCode;
|
|
63
|
+
}
|
|
64
|
+
let add = options.add ? new Set(options.add) : null, found = imports.find(sourceFile, packageName), remove = options.remove ? new Set(options.remove) : null;
|
|
65
|
+
if (found.length === 0) {
|
|
66
|
+
let statements = [];
|
|
67
|
+
if (namespace) {
|
|
68
|
+
statements.push(`import * as ${namespace} from '${packageName}';`);
|
|
69
|
+
}
|
|
70
|
+
if (add && add.size > 0) {
|
|
71
|
+
statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
|
|
72
|
+
}
|
|
73
|
+
if (statements.length === 0) {
|
|
74
|
+
return sourceCode;
|
|
75
|
+
}
|
|
76
|
+
return statements.join('\n') + '\n' + sourceCode;
|
|
77
|
+
}
|
|
78
|
+
let specifiers = new Set();
|
|
79
|
+
for (let i = 0, n = found.length; i < n; i++) {
|
|
80
|
+
for (let [name, alias] of found[i].specifiers) {
|
|
81
|
+
if (!remove || (!remove.has(name) && !remove.has(alias))) {
|
|
82
|
+
specifiers.add(name === alias ? name : `${name} as ${alias}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (add) {
|
|
87
|
+
for (let name of add) {
|
|
88
|
+
specifiers.add(name);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
let statements = [];
|
|
92
|
+
if (namespace) {
|
|
93
|
+
statements.push(`import * as ${namespace} from '${packageName}';`);
|
|
94
|
+
}
|
|
95
|
+
if (specifiers.size > 0) {
|
|
96
|
+
statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
|
|
97
|
+
}
|
|
98
|
+
let replacements = [];
|
|
99
|
+
for (let i = 0, n = found.length; i < n; i++) {
|
|
100
|
+
replacements.push({
|
|
101
|
+
end: found[i].end,
|
|
102
|
+
newText: i === 0 ? statements.join('\n') : '',
|
|
103
|
+
start: found[i].start
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return replaceReverse(sourceCode, replacements);
|
|
107
|
+
};
|
|
108
|
+
const replaceReverse = (code, replacements) => {
|
|
109
|
+
if (replacements.length === 0) {
|
|
110
|
+
return code;
|
|
111
|
+
}
|
|
112
|
+
replacements.sort((a, b) => b.start - a.start);
|
|
113
|
+
let result = code;
|
|
114
|
+
for (let i = 0, n = replacements.length; i < n; i++) {
|
|
115
|
+
let r = replacements[i];
|
|
116
|
+
result = result.substring(0, r.start) + r.newText + result.substring(r.end);
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
};
|
|
61
120
|
const transform = (plugins, sourceCode, sourceFile, program, shared) => {
|
|
62
121
|
if (plugins.length === 0) {
|
|
63
122
|
return { changed: false, code: sourceCode, sourceFile };
|
|
@@ -11,9 +11,7 @@ type ModifyOptions = {
|
|
|
11
11
|
};
|
|
12
12
|
declare const _default: {
|
|
13
13
|
find: (sourceFile: ts.SourceFile, packageName: string) => ImportInfo[];
|
|
14
|
-
|
|
15
|
-
modify: (sourceCode: string, sourceFile: ts.SourceFile, packageName: string, options: ModifyOptions) => string;
|
|
16
|
-
trace: (node: ts.Identifier, checker: ts.TypeChecker) => string | null;
|
|
14
|
+
inPackage: (checker: ts.TypeChecker, node: ts.Node, pkg: string, symbolName?: string, packageImports?: Set<string>) => boolean;
|
|
17
15
|
};
|
|
18
16
|
export default _default;
|
|
19
17
|
export type { ImportInfo, ModifyOptions };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ts } from '../index.js';
|
|
2
|
-
import code from './code.js';
|
|
3
2
|
const find = (sourceFile, packageName) => {
|
|
4
3
|
let imports = [];
|
|
5
4
|
for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
|
|
@@ -22,89 +21,34 @@ const find = (sourceFile, packageName) => {
|
|
|
22
21
|
}
|
|
23
22
|
return imports;
|
|
24
23
|
};
|
|
25
|
-
const
|
|
24
|
+
const inPackage = (checker, node, pkg, symbolName, packageImports) => {
|
|
25
|
+
if (packageImports && ts.isIdentifier(node) && packageImports.has(node.text)) {
|
|
26
|
+
if (!symbolName || node.text === symbolName) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
26
30
|
let symbol = checker.getSymbolAtLocation(node);
|
|
27
31
|
if (!symbol) {
|
|
32
|
+
if (packageImports && ts.isIdentifier(node) && packageImports.has(node.text)) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
28
35
|
return false;
|
|
29
36
|
}
|
|
30
37
|
if (symbol.flags & ts.SymbolFlags.Alias) {
|
|
31
38
|
symbol = checker.getAliasedSymbol(symbol);
|
|
32
39
|
}
|
|
33
40
|
if (symbolName && symbol.name !== symbolName) {
|
|
34
|
-
return false;
|
|
41
|
+
return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
|
|
35
42
|
}
|
|
36
43
|
let declarations = symbol.getDeclarations();
|
|
37
44
|
if (!declarations || declarations.length === 0) {
|
|
38
|
-
return false;
|
|
45
|
+
return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
|
|
39
46
|
}
|
|
40
47
|
for (let i = 0, n = declarations.length; i < n; i++) {
|
|
41
|
-
if (declarations[i].getSourceFile().fileName.includes(
|
|
48
|
+
if (declarations[i].getSourceFile().fileName.includes(pkg)) {
|
|
42
49
|
return true;
|
|
43
50
|
}
|
|
44
51
|
}
|
|
45
|
-
return false;
|
|
46
|
-
};
|
|
47
|
-
const modify = (sourceCode, sourceFile, packageName, options) => {
|
|
48
|
-
let { namespace } = options;
|
|
49
|
-
if (!options.add && !options.namespace && !options.remove) {
|
|
50
|
-
return sourceCode;
|
|
51
|
-
}
|
|
52
|
-
let add = options.add ? new Set(options.add) : null, imports = find(sourceFile, packageName), remove = options.remove ? new Set(options.remove) : null;
|
|
53
|
-
if (imports.length === 0) {
|
|
54
|
-
let statements = [];
|
|
55
|
-
if (namespace) {
|
|
56
|
-
statements.push(`import * as ${namespace} from '${packageName}';`);
|
|
57
|
-
}
|
|
58
|
-
if (add && add.size > 0) {
|
|
59
|
-
statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
|
|
60
|
-
}
|
|
61
|
-
if (statements.length === 0) {
|
|
62
|
-
return sourceCode;
|
|
63
|
-
}
|
|
64
|
-
return statements.join('\n') + '\n' + sourceCode;
|
|
65
|
-
}
|
|
66
|
-
let specifiers = new Set();
|
|
67
|
-
for (let i = 0, n = imports.length; i < n; i++) {
|
|
68
|
-
for (let [name, alias] of imports[i].specifiers) {
|
|
69
|
-
if (!remove || (!remove.has(name) && !remove.has(alias))) {
|
|
70
|
-
specifiers.add(name === alias ? name : `${name} as ${alias}`);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
if (add) {
|
|
75
|
-
for (let name of add) {
|
|
76
|
-
specifiers.add(name);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
let statements = [];
|
|
80
|
-
if (namespace) {
|
|
81
|
-
statements.push(`import * as ${namespace} from '${packageName}';`);
|
|
82
|
-
}
|
|
83
|
-
if (specifiers.size > 0) {
|
|
84
|
-
statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
|
|
85
|
-
}
|
|
86
|
-
let replacements = [];
|
|
87
|
-
for (let i = 0, n = imports.length; i < n; i++) {
|
|
88
|
-
replacements.push({
|
|
89
|
-
end: imports[i].end,
|
|
90
|
-
newText: i === 0 ? statements.join('\n') : '',
|
|
91
|
-
start: imports[i].start
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
return code.replaceReverse(sourceCode, replacements);
|
|
95
|
-
};
|
|
96
|
-
const trace = (node, checker) => {
|
|
97
|
-
let symbol = checker.getSymbolAtLocation(node);
|
|
98
|
-
if (!symbol) {
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
if (symbol.flags & ts.SymbolFlags.Alias) {
|
|
102
|
-
symbol = checker.getAliasedSymbol(symbol);
|
|
103
|
-
}
|
|
104
|
-
let declarations = symbol.getDeclarations();
|
|
105
|
-
if (!declarations || declarations.length === 0) {
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
|
-
return declarations[0].getSourceFile().fileName;
|
|
52
|
+
return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
|
|
109
53
|
};
|
|
110
|
-
export default { find,
|
|
54
|
+
export default { find, inPackage };
|
package/package.json
CHANGED
package/src/compiler/code.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
const SINGLE_QUOTE_REGEX = /'/g;
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
return code;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
replacements.sort((a, b) => b.start - a.start);
|
|
10
|
-
|
|
11
|
-
let result = code;
|
|
4
|
+
const code = (literals: TemplateStringsArray, ...values: unknown[]): string => {
|
|
5
|
+
let buffer = '';
|
|
12
6
|
|
|
13
|
-
for (let i = 0, n =
|
|
14
|
-
|
|
7
|
+
for (let i = 0, n = literals.length; i < n; i++) {
|
|
8
|
+
buffer += literals[i];
|
|
15
9
|
|
|
16
|
-
|
|
10
|
+
if (values[i] !== undefined) {
|
|
11
|
+
buffer += values[i];
|
|
12
|
+
}
|
|
17
13
|
}
|
|
18
14
|
|
|
19
|
-
return
|
|
15
|
+
return buffer;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
code.escape = (str: string): string => {
|
|
19
|
+
return str.replace(SINGLE_QUOTE_REGEX, "\\'");
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
|
|
23
|
-
export default
|
|
23
|
+
export default code;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { ImportIntent, Plugin, ReplacementIntent, SharedContext } from './types';
|
|
1
|
+
import type { ImportIntent, Plugin, Replacement, ReplacementIntent, SharedContext } from './types';
|
|
2
2
|
import { ts } from '~/index';
|
|
3
|
-
import
|
|
4
|
-
import imports from './imports';
|
|
3
|
+
import imports, { ModifyOptions } from './imports';
|
|
5
4
|
|
|
6
5
|
|
|
7
6
|
type CoordinatorResult = {
|
|
@@ -21,7 +20,7 @@ function applyImports(
|
|
|
21
20
|
for (let i = 0, n = intents.length; i < n; i++) {
|
|
22
21
|
let intent = intents[i];
|
|
23
22
|
|
|
24
|
-
result =
|
|
23
|
+
result = modify(result, sourceFile, intent.package, {
|
|
25
24
|
add: intent.add,
|
|
26
25
|
namespace: intent.namespace,
|
|
27
26
|
remove: intent.remove
|
|
@@ -49,13 +48,14 @@ function applyIntents(
|
|
|
49
48
|
return sourceCode;
|
|
50
49
|
}
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
51
|
+
return replaceReverse(
|
|
52
|
+
sourceCode,
|
|
53
|
+
intents.map(intent => ({
|
|
54
|
+
end: intent.node.end,
|
|
55
|
+
newText: intent.generate(sourceFile),
|
|
56
|
+
start: intent.node.getStart(sourceFile)
|
|
57
|
+
}))
|
|
58
|
+
);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
function applyPrepend(sourceCode: string, sourceFile: ts.SourceFile, prepend: string[]): string {
|
|
@@ -100,6 +100,101 @@ function hasPattern(sourceCode: string, patterns: string[]): boolean {
|
|
|
100
100
|
return false;
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
+
const modify = (
|
|
104
|
+
sourceCode: string,
|
|
105
|
+
sourceFile: ts.SourceFile,
|
|
106
|
+
packageName: string,
|
|
107
|
+
options: ModifyOptions
|
|
108
|
+
): string => {
|
|
109
|
+
let { namespace } = options;
|
|
110
|
+
|
|
111
|
+
// Fast path: nothing to change
|
|
112
|
+
if (!options.add && !options.namespace && !options.remove) {
|
|
113
|
+
return sourceCode;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
let add = options.add ? new Set(options.add) : null,
|
|
117
|
+
found = imports.find(sourceFile, packageName),
|
|
118
|
+
remove = options.remove ? new Set(options.remove) : null;
|
|
119
|
+
|
|
120
|
+
if (found.length === 0) {
|
|
121
|
+
let statements: string[] = [];
|
|
122
|
+
|
|
123
|
+
if (namespace) {
|
|
124
|
+
statements.push(`import * as ${namespace} from '${packageName}';`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (add && add.size > 0) {
|
|
128
|
+
statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (statements.length === 0) {
|
|
132
|
+
return sourceCode;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return statements.join('\n') + '\n' + sourceCode;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Collect all non-removed specifiers from existing imports
|
|
139
|
+
let specifiers = new Set<string>();
|
|
140
|
+
|
|
141
|
+
for (let i = 0, n = found.length; i < n; i++) {
|
|
142
|
+
for (let [name, alias] of found[i].specifiers) {
|
|
143
|
+
if (!remove || (!remove.has(name) && !remove.has(alias))) {
|
|
144
|
+
specifiers.add(name === alias ? name : `${name} as ${alias}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (add) {
|
|
150
|
+
for (let name of add) {
|
|
151
|
+
specifiers.add(name);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Build replacement text - namespace import first, then named imports
|
|
156
|
+
let statements: string[] = [];
|
|
157
|
+
|
|
158
|
+
if (namespace) {
|
|
159
|
+
statements.push(`import * as ${namespace} from '${packageName}';`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (specifiers.size > 0) {
|
|
163
|
+
statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Build replacements - replace first import, remove others
|
|
167
|
+
let replacements: Replacement[] = [];
|
|
168
|
+
|
|
169
|
+
for (let i = 0, n = found.length; i < n; i++) {
|
|
170
|
+
replacements.push({
|
|
171
|
+
end: found[i].end,
|
|
172
|
+
newText: i === 0 ? statements.join('\n') : '',
|
|
173
|
+
start: found[i].start
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return replaceReverse(sourceCode, replacements);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const replaceReverse = (code: string, replacements: Replacement[]): string => {
|
|
181
|
+
if (replacements.length === 0) {
|
|
182
|
+
return code;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
replacements.sort((a, b) => b.start - a.start);
|
|
186
|
+
|
|
187
|
+
let result = code;
|
|
188
|
+
|
|
189
|
+
for (let i = 0, n = replacements.length; i < n; i++) {
|
|
190
|
+
let r = replacements[i];
|
|
191
|
+
|
|
192
|
+
result = result.substring(0, r.start) + r.newText + result.substring(r.end);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return result;
|
|
196
|
+
};
|
|
197
|
+
|
|
103
198
|
|
|
104
199
|
/**
|
|
105
200
|
* Transform source through all plugins sequentially.
|
package/src/compiler/imports.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import type { Replacement } from './types';
|
|
2
1
|
import { ts } from '~/index';
|
|
3
|
-
import code from './code';
|
|
4
2
|
|
|
5
3
|
|
|
6
4
|
type ImportInfo = {
|
|
@@ -53,15 +51,28 @@ const find = (sourceFile: ts.SourceFile, packageName: string): ImportInfo[] => {
|
|
|
53
51
|
};
|
|
54
52
|
|
|
55
53
|
// Check if node's symbol originates from a specific package (with optional symbol name validation)
|
|
56
|
-
const
|
|
54
|
+
const inPackage = (
|
|
57
55
|
checker: ts.TypeChecker,
|
|
58
56
|
node: ts.Node,
|
|
59
|
-
|
|
60
|
-
symbolName?: string
|
|
57
|
+
pkg: string,
|
|
58
|
+
symbolName?: string,
|
|
59
|
+
packageImports?: Set<string>
|
|
61
60
|
): boolean => {
|
|
61
|
+
// Fast path: identifier matches known import and expected name
|
|
62
|
+
if (packageImports && ts.isIdentifier(node) && packageImports.has(node.text)) {
|
|
63
|
+
if (!symbolName || node.text === symbolName) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
62
68
|
let symbol = checker.getSymbolAtLocation(node);
|
|
63
69
|
|
|
64
70
|
if (!symbol) {
|
|
71
|
+
// Fallback: aliased import - check if local name is in imports
|
|
72
|
+
if (packageImports && ts.isIdentifier(node) && packageImports.has(node.text)) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
65
76
|
return false;
|
|
66
77
|
}
|
|
67
78
|
|
|
@@ -72,124 +83,25 @@ const isFromPackage = (
|
|
|
72
83
|
|
|
73
84
|
// Check symbol name if specified
|
|
74
85
|
if (symbolName && symbol.name !== symbolName) {
|
|
75
|
-
return false;
|
|
86
|
+
return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
|
|
76
87
|
}
|
|
77
88
|
|
|
78
89
|
let declarations = symbol.getDeclarations();
|
|
79
90
|
|
|
80
91
|
if (!declarations || declarations.length === 0) {
|
|
81
|
-
return false;
|
|
92
|
+
return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
|
|
82
93
|
}
|
|
83
94
|
|
|
84
95
|
// Check if any declaration is from the expected package
|
|
85
96
|
for (let i = 0, n = declarations.length; i < n; i++) {
|
|
86
|
-
if (declarations[i].getSourceFile().fileName.includes(
|
|
97
|
+
if (declarations[i].getSourceFile().fileName.includes(pkg)) {
|
|
87
98
|
return true;
|
|
88
99
|
}
|
|
89
100
|
}
|
|
90
101
|
|
|
91
|
-
return false;
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
// Modify imports: remove specified, add needed, delete if empty
|
|
95
|
-
const modify = (
|
|
96
|
-
sourceCode: string,
|
|
97
|
-
sourceFile: ts.SourceFile,
|
|
98
|
-
packageName: string,
|
|
99
|
-
options: ModifyOptions
|
|
100
|
-
): string => {
|
|
101
|
-
let { namespace } = options;
|
|
102
|
-
|
|
103
|
-
// Fast path: nothing to change
|
|
104
|
-
if (!options.add && !options.namespace && !options.remove) {
|
|
105
|
-
return sourceCode;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
let add = options.add ? new Set(options.add) : null,
|
|
109
|
-
imports = find(sourceFile, packageName),
|
|
110
|
-
remove = options.remove ? new Set(options.remove) : null;
|
|
111
|
-
|
|
112
|
-
if (imports.length === 0) {
|
|
113
|
-
let statements: string[] = [];
|
|
114
|
-
|
|
115
|
-
if (namespace) {
|
|
116
|
-
statements.push(`import * as ${namespace} from '${packageName}';`);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (add && add.size > 0) {
|
|
120
|
-
statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (statements.length === 0) {
|
|
124
|
-
return sourceCode;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return statements.join('\n') + '\n' + sourceCode;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Collect all non-removed specifiers from existing imports
|
|
131
|
-
let specifiers = new Set<string>();
|
|
132
|
-
|
|
133
|
-
for (let i = 0, n = imports.length; i < n; i++) {
|
|
134
|
-
for (let [name, alias] of imports[i].specifiers) {
|
|
135
|
-
if (!remove || (!remove.has(name) && !remove.has(alias))) {
|
|
136
|
-
specifiers.add(name === alias ? name : `${name} as ${alias}`);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (add) {
|
|
142
|
-
for (let name of add) {
|
|
143
|
-
specifiers.add(name);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// Build replacement text - namespace import first, then named imports
|
|
148
|
-
let statements: string[] = [];
|
|
149
|
-
|
|
150
|
-
if (namespace) {
|
|
151
|
-
statements.push(`import * as ${namespace} from '${packageName}';`);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (specifiers.size > 0) {
|
|
155
|
-
statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// Build replacements - replace first import, remove others
|
|
159
|
-
let replacements: Replacement[] = [];
|
|
160
|
-
|
|
161
|
-
for (let i = 0, n = imports.length; i < n; i++) {
|
|
162
|
-
replacements.push({
|
|
163
|
-
end: imports[i].end,
|
|
164
|
-
newText: i === 0 ? statements.join('\n') : '',
|
|
165
|
-
start: imports[i].start
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return code.replaceReverse(sourceCode, replacements);
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
// Trace symbol through re-exports to find original declaration source file
|
|
173
|
-
const trace = (node: ts.Identifier, checker: ts.TypeChecker): string | null => {
|
|
174
|
-
let symbol = checker.getSymbolAtLocation(node);
|
|
175
|
-
|
|
176
|
-
if (!symbol) {
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (symbol.flags & ts.SymbolFlags.Alias) {
|
|
181
|
-
symbol = checker.getAliasedSymbol(symbol);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
let declarations = symbol.getDeclarations();
|
|
185
|
-
|
|
186
|
-
if (!declarations || declarations.length === 0) {
|
|
187
|
-
return null;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return declarations[0].getSourceFile().fileName;
|
|
102
|
+
return packageImports ? ts.isIdentifier(node) && packageImports.has(node.text) : false;
|
|
191
103
|
};
|
|
192
104
|
|
|
193
105
|
|
|
194
|
-
export default { find,
|
|
106
|
+
export default { find, inPackage };
|
|
195
107
|
export type { ImportInfo, ModifyOptions };
|