@esportsplus/typescript 0.28.1 → 0.28.2
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.
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ts } from '../index.js';
|
|
2
2
|
import imports from './imports.js';
|
|
3
|
-
const DIRECTORY_SEPARATOR = /\\/g;
|
|
4
3
|
function applyImports(code, file, intents) {
|
|
5
4
|
for (let i = 0, n = intents.length; i < n; i++) {
|
|
6
5
|
let intent = intents[i];
|
|
@@ -44,24 +43,6 @@ function applyPrepend(code, file, prepend) {
|
|
|
44
43
|
}
|
|
45
44
|
return code.slice(0, position) + prepend.join('\n') + code.slice(position);
|
|
46
45
|
}
|
|
47
|
-
function createUpdatedProgram(originalProgram, fileName, newCode) {
|
|
48
|
-
let options = originalProgram.getCompilerOptions(), originalHost = ts.createCompilerHost(options), originalGetSourceFile = originalHost.getSourceFile.bind(originalHost), originalReadFile = originalHost.readFile.bind(originalHost);
|
|
49
|
-
originalHost.getSourceFile = (name, languageVersion, onError, shouldCreateNewSourceFile) => {
|
|
50
|
-
if (name === fileName ||
|
|
51
|
-
name.replace(DIRECTORY_SEPARATOR, '/') === fileName.replace(DIRECTORY_SEPARATOR, '/')) {
|
|
52
|
-
return ts.createSourceFile(name, newCode, languageVersion, true);
|
|
53
|
-
}
|
|
54
|
-
return originalGetSourceFile(name, languageVersion, onError, shouldCreateNewSourceFile);
|
|
55
|
-
};
|
|
56
|
-
originalHost.readFile = (name) => {
|
|
57
|
-
if (name === fileName ||
|
|
58
|
-
name.replace(DIRECTORY_SEPARATOR, '/') === fileName.replace(DIRECTORY_SEPARATOR, '/')) {
|
|
59
|
-
return newCode;
|
|
60
|
-
}
|
|
61
|
-
return originalReadFile(name);
|
|
62
|
-
};
|
|
63
|
-
return ts.createProgram(originalProgram.getRootFileNames(), options, originalHost, originalProgram);
|
|
64
|
-
}
|
|
65
46
|
function hasPattern(code, patterns) {
|
|
66
47
|
for (let i = 0, n = patterns.length; i < n; i++) {
|
|
67
48
|
if (code.indexOf(patterns[i]) !== -1) {
|
|
@@ -136,42 +117,34 @@ const transform = (plugins, code, file, program, shared) => {
|
|
|
136
117
|
if (plugins.length === 0) {
|
|
137
118
|
return { changed: false, code, sourceFile: file };
|
|
138
119
|
}
|
|
139
|
-
let
|
|
120
|
+
let checker = program.getTypeChecker(), currentCode = code, currentFile = file, fileName = file.fileName;
|
|
140
121
|
for (let i = 0, n = plugins.length; i < n; i++) {
|
|
141
122
|
let plugin = plugins[i];
|
|
142
123
|
if (plugin.patterns && !hasPattern(currentCode, plugin.patterns)) {
|
|
143
124
|
continue;
|
|
144
125
|
}
|
|
145
126
|
let { imports, prepend, replacements } = plugin.transform({
|
|
146
|
-
checker
|
|
127
|
+
checker,
|
|
147
128
|
code: currentCode,
|
|
148
|
-
program
|
|
129
|
+
program,
|
|
149
130
|
shared,
|
|
150
131
|
sourceFile: currentFile
|
|
151
132
|
});
|
|
152
|
-
let pluginChanged = false;
|
|
153
133
|
if (replacements?.length) {
|
|
154
134
|
currentCode = applyIntents(currentCode, currentFile, replacements);
|
|
155
135
|
currentFile = ts.createSourceFile(fileName, currentCode, currentFile.languageVersion, true);
|
|
156
|
-
pluginChanged = true;
|
|
157
136
|
}
|
|
158
137
|
if (prepend?.length) {
|
|
159
138
|
currentCode = applyPrepend(currentCode, currentFile, prepend);
|
|
160
139
|
currentFile = ts.createSourceFile(fileName, currentCode, currentFile.languageVersion, true);
|
|
161
|
-
pluginChanged = true;
|
|
162
140
|
}
|
|
163
141
|
if (imports?.length) {
|
|
164
142
|
currentCode = applyImports(currentCode, currentFile, imports);
|
|
165
143
|
currentFile = ts.createSourceFile(fileName, currentCode, currentFile.languageVersion, true);
|
|
166
|
-
pluginChanged = true;
|
|
167
|
-
}
|
|
168
|
-
if (pluginChanged) {
|
|
169
|
-
transformed = true;
|
|
170
|
-
currentProgram = createUpdatedProgram(currentProgram, fileName, currentCode);
|
|
171
144
|
}
|
|
172
145
|
}
|
|
173
146
|
return {
|
|
174
|
-
changed:
|
|
147
|
+
changed: currentCode !== code,
|
|
175
148
|
code: currentCode,
|
|
176
149
|
sourceFile: currentFile
|
|
177
150
|
};
|
package/package.json
CHANGED
|
@@ -10,9 +10,6 @@ type CoordinatorResult = {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
const DIRECTORY_SEPARATOR = /\\/g;
|
|
14
|
-
|
|
15
|
-
|
|
16
13
|
function applyImports(code: string, file: ts.SourceFile, intents: ImportIntent[]): string {
|
|
17
14
|
for (let i = 0, n = intents.length; i < n; i++) {
|
|
18
15
|
let intent = intents[i];
|
|
@@ -76,51 +73,6 @@ function applyPrepend(code: string, file: ts.SourceFile, prepend: string[]): str
|
|
|
76
73
|
return code.slice(0, position) + prepend.join('\n') + code.slice(position);
|
|
77
74
|
}
|
|
78
75
|
|
|
79
|
-
function createUpdatedProgram(
|
|
80
|
-
originalProgram: ts.Program,
|
|
81
|
-
fileName: string,
|
|
82
|
-
newCode: string
|
|
83
|
-
): ts.Program {
|
|
84
|
-
let options = originalProgram.getCompilerOptions(),
|
|
85
|
-
originalHost = ts.createCompilerHost(options),
|
|
86
|
-
originalGetSourceFile = originalHost.getSourceFile.bind(originalHost),
|
|
87
|
-
originalReadFile = originalHost.readFile.bind(originalHost);
|
|
88
|
-
|
|
89
|
-
originalHost.getSourceFile = (
|
|
90
|
-
name: string,
|
|
91
|
-
languageVersion: ts.ScriptTarget,
|
|
92
|
-
onError?: (message: string) => void,
|
|
93
|
-
shouldCreateNewSourceFile?: boolean
|
|
94
|
-
): ts.SourceFile | undefined => {
|
|
95
|
-
if (
|
|
96
|
-
name === fileName ||
|
|
97
|
-
name.replace(DIRECTORY_SEPARATOR, '/') === fileName.replace(DIRECTORY_SEPARATOR, '/')
|
|
98
|
-
) {
|
|
99
|
-
return ts.createSourceFile(name, newCode, languageVersion, true);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return originalGetSourceFile(name, languageVersion, onError, shouldCreateNewSourceFile);
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
originalHost.readFile = (name: string): string | undefined => {
|
|
106
|
-
if (
|
|
107
|
-
name === fileName ||
|
|
108
|
-
name.replace(DIRECTORY_SEPARATOR, '/') === fileName.replace(DIRECTORY_SEPARATOR, '/')
|
|
109
|
-
) {
|
|
110
|
-
return newCode;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return originalReadFile(name);
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
return ts.createProgram(
|
|
117
|
-
originalProgram.getRootFileNames(),
|
|
118
|
-
options,
|
|
119
|
-
originalHost,
|
|
120
|
-
originalProgram
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
76
|
function hasPattern(code: string, patterns: string[]): boolean {
|
|
125
77
|
for (let i = 0, n = patterns.length; i < n; i++) {
|
|
126
78
|
if (code.indexOf(patterns[i]) !== -1) {
|
|
@@ -159,7 +111,6 @@ function modify(code: string, file: ts.SourceFile, pkg: string, options: ModifyO
|
|
|
159
111
|
}
|
|
160
112
|
|
|
161
113
|
let remove = options.remove ? new Set(options.remove) : null,
|
|
162
|
-
// Collect all non-removed specifiers from existing imports
|
|
163
114
|
specifiers = new Set<string>();
|
|
164
115
|
|
|
165
116
|
for (let i = 0, n = found.length; i < n; i++) {
|
|
@@ -176,7 +127,6 @@ function modify(code: string, file: ts.SourceFile, pkg: string, options: ModifyO
|
|
|
176
127
|
}
|
|
177
128
|
}
|
|
178
129
|
|
|
179
|
-
// Build replacement text - namespace import first, then named imports
|
|
180
130
|
let statements: string[] = [];
|
|
181
131
|
|
|
182
132
|
if (namespace) {
|
|
@@ -187,7 +137,6 @@ function modify(code: string, file: ts.SourceFile, pkg: string, options: ModifyO
|
|
|
187
137
|
statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${pkg}';`);
|
|
188
138
|
}
|
|
189
139
|
|
|
190
|
-
// Build replacements - replace first import, remove others
|
|
191
140
|
let replacements: Replacement[] = [];
|
|
192
141
|
|
|
193
142
|
for (let i = 0, n = found.length; i < n; i++) {
|
|
@@ -222,7 +171,8 @@ function replaceReverse(code: string, replacements: Replacement[]): string {
|
|
|
222
171
|
|
|
223
172
|
/**
|
|
224
173
|
* Transform source through all plugins sequentially.
|
|
225
|
-
* Each plugin receives fresh AST
|
|
174
|
+
* Each plugin receives fresh AST with accurate positions.
|
|
175
|
+
* All plugins share the original program type checker for import resolution.
|
|
226
176
|
*/
|
|
227
177
|
const transform = (
|
|
228
178
|
plugins: Plugin[],
|
|
@@ -235,11 +185,10 @@ const transform = (
|
|
|
235
185
|
return { changed: false, code, sourceFile: file };
|
|
236
186
|
}
|
|
237
187
|
|
|
238
|
-
let
|
|
188
|
+
let checker = program.getTypeChecker(),
|
|
189
|
+
currentCode = code,
|
|
239
190
|
currentFile = file,
|
|
240
|
-
|
|
241
|
-
fileName = file.fileName,
|
|
242
|
-
transformed = false;
|
|
191
|
+
fileName = file.fileName;
|
|
243
192
|
|
|
244
193
|
for (let i = 0, n = plugins.length; i < n; i++) {
|
|
245
194
|
let plugin = plugins[i];
|
|
@@ -249,15 +198,13 @@ const transform = (
|
|
|
249
198
|
}
|
|
250
199
|
|
|
251
200
|
let { imports, prepend, replacements } = plugin.transform({
|
|
252
|
-
checker
|
|
201
|
+
checker,
|
|
253
202
|
code: currentCode,
|
|
254
|
-
program
|
|
203
|
+
program,
|
|
255
204
|
shared,
|
|
256
205
|
sourceFile: currentFile
|
|
257
206
|
});
|
|
258
207
|
|
|
259
|
-
let pluginChanged = false;
|
|
260
|
-
|
|
261
208
|
if (replacements?.length) {
|
|
262
209
|
currentCode = applyIntents(currentCode, currentFile, replacements);
|
|
263
210
|
currentFile = ts.createSourceFile(
|
|
@@ -266,7 +213,6 @@ const transform = (
|
|
|
266
213
|
currentFile.languageVersion,
|
|
267
214
|
true
|
|
268
215
|
);
|
|
269
|
-
pluginChanged = true;
|
|
270
216
|
}
|
|
271
217
|
|
|
272
218
|
if (prepend?.length) {
|
|
@@ -277,7 +223,6 @@ const transform = (
|
|
|
277
223
|
currentFile.languageVersion,
|
|
278
224
|
true
|
|
279
225
|
);
|
|
280
|
-
pluginChanged = true;
|
|
281
226
|
}
|
|
282
227
|
|
|
283
228
|
if (imports?.length) {
|
|
@@ -288,18 +233,11 @@ const transform = (
|
|
|
288
233
|
currentFile.languageVersion,
|
|
289
234
|
true
|
|
290
235
|
);
|
|
291
|
-
pluginChanged = true;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// Rebuild program with updated source so next plugin gets valid checker
|
|
295
|
-
if (pluginChanged) {
|
|
296
|
-
transformed = true;
|
|
297
|
-
currentProgram = createUpdatedProgram(currentProgram, fileName, currentCode);
|
|
298
236
|
}
|
|
299
237
|
}
|
|
300
238
|
|
|
301
239
|
return {
|
|
302
|
-
changed:
|
|
240
|
+
changed: currentCode !== code,
|
|
303
241
|
code: currentCode,
|
|
304
242
|
sourceFile: currentFile
|
|
305
243
|
};
|
|
@@ -307,4 +245,4 @@ const transform = (
|
|
|
307
245
|
|
|
308
246
|
|
|
309
247
|
export default { transform };
|
|
310
|
-
export type { CoordinatorResult };
|
|
248
|
+
export type { CoordinatorResult };
|