@esportsplus/typescript 0.13.0 → 0.13.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.
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type { ImportModification, NodeMatch, QuickCheckPattern, Replacement, VisitorCallback, VisitorPredicate } from './types.js';
|
|
1
|
+
import type { QuickCheckPattern, Replacement } from './types.js';
|
|
3
2
|
import program from './program.js';
|
|
4
3
|
declare const addImport: (code: string, module: string, specifiers: string[]) => string;
|
|
5
4
|
declare const applyReplacements: (code: string, replacements: Replacement[]) => string;
|
|
6
5
|
declare const applyReplacementsReverse: (code: string, replacements: Replacement[]) => string;
|
|
7
|
-
declare const collectNodes: <T>(sourceFile: ts.SourceFile, predicate: (node: ts.Node) => T | null) => NodeMatch<T>[];
|
|
8
6
|
declare const mightNeedTransform: (code: string, check: QuickCheckPattern) => boolean;
|
|
9
7
|
declare const uid: (prefix: string, updateUUID?: boolean) => string;
|
|
10
|
-
|
|
11
|
-
declare const visitAst: <T>(sourceFile: ts.SourceFile, callback: VisitorCallback<T>, state: T, predicate?: VisitorPredicate) => T;
|
|
12
|
-
declare const visitAstWithDepth: <T>(sourceFile: ts.SourceFile, callback: (node: ts.Node, depth: number, state: T) => void, state: T, depthTrigger: (node: ts.Node) => boolean) => T;
|
|
13
|
-
export { addImport, applyReplacements, applyReplacementsReverse, collectNodes, mightNeedTransform, program, uid, updateImports, visitAst, visitAstWithDepth };
|
|
8
|
+
export { addImport, applyReplacements, applyReplacementsReverse, mightNeedTransform, program, uid };
|
|
14
9
|
export type * from './types.js';
|
|
15
10
|
export * from './constants.js';
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { uuid } from '@esportsplus/utilities';
|
|
2
|
-
import ts from 'typescript';
|
|
3
2
|
import { BRACES_CONTENT_REGEX, REGEX_ESCAPE_PATTERN, UUID_DASH_REGEX } from './constants.js';
|
|
4
3
|
import program from './program.js';
|
|
5
4
|
let i = 0, uidSuffix = uuid().replace(UUID_DASH_REGEX, '');
|
|
@@ -93,23 +92,6 @@ const applyReplacementsReverse = (code, replacements) => {
|
|
|
93
92
|
}
|
|
94
93
|
return result;
|
|
95
94
|
};
|
|
96
|
-
const collectNodes = (sourceFile, predicate) => {
|
|
97
|
-
let matches = [];
|
|
98
|
-
function visit(node) {
|
|
99
|
-
let data = predicate(node);
|
|
100
|
-
if (data !== null) {
|
|
101
|
-
matches.push({
|
|
102
|
-
data,
|
|
103
|
-
end: node.end,
|
|
104
|
-
node,
|
|
105
|
-
start: node.getStart(sourceFile)
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
ts.forEachChild(node, visit);
|
|
109
|
-
}
|
|
110
|
-
visit(sourceFile);
|
|
111
|
-
return matches;
|
|
112
|
-
};
|
|
113
95
|
const mightNeedTransform = (code, check) => {
|
|
114
96
|
if (check.regex) {
|
|
115
97
|
return check.regex.test(code);
|
|
@@ -126,35 +108,5 @@ const mightNeedTransform = (code, check) => {
|
|
|
126
108
|
const uid = (prefix, updateUUID = false) => {
|
|
127
109
|
return prefix + '_' + (updateUUID ? uuid().replace(UUID_DASH_REGEX, '') : uidSuffix) + '_' + (i++).toString(36);
|
|
128
110
|
};
|
|
129
|
-
|
|
130
|
-
let { module, specifiers } = modification;
|
|
131
|
-
if (specifiers.size === 0) {
|
|
132
|
-
return code;
|
|
133
|
-
}
|
|
134
|
-
let escapedModule = module.replace(REGEX_ESCAPE_PATTERN, '\\$&'), importRegex = buildImportRegex(escapedModule);
|
|
135
|
-
return updateImportsWithRegex(code, specifiers, importRegex);
|
|
136
|
-
};
|
|
137
|
-
const visitAst = (sourceFile, callback, state, predicate) => {
|
|
138
|
-
function visit(node) {
|
|
139
|
-
if (!predicate || predicate(node)) {
|
|
140
|
-
callback(node, state);
|
|
141
|
-
}
|
|
142
|
-
ts.forEachChild(node, visit);
|
|
143
|
-
}
|
|
144
|
-
visit(sourceFile);
|
|
145
|
-
return state;
|
|
146
|
-
};
|
|
147
|
-
const visitAstWithDepth = (sourceFile, callback, state, depthTrigger) => {
|
|
148
|
-
let depthStack = [0];
|
|
149
|
-
function visit(node) {
|
|
150
|
-
let depth = depthStack[depthStack.length - 1], nextDepth = depthTrigger(node) ? depth + 1 : depth;
|
|
151
|
-
callback(node, depth, state);
|
|
152
|
-
depthStack.push(nextDepth);
|
|
153
|
-
ts.forEachChild(node, visit);
|
|
154
|
-
depthStack.pop();
|
|
155
|
-
}
|
|
156
|
-
visit(sourceFile);
|
|
157
|
-
return state;
|
|
158
|
-
};
|
|
159
|
-
export { addImport, applyReplacements, applyReplacementsReverse, collectNodes, mightNeedTransform, program, uid, updateImports, visitAst, visitAstWithDepth };
|
|
111
|
+
export { addImport, applyReplacements, applyReplacementsReverse, mightNeedTransform, program, uid };
|
|
160
112
|
export * from './constants.js';
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
import ts from 'typescript';
|
|
2
|
-
type ImportModification = {
|
|
3
|
-
module: string;
|
|
4
|
-
specifiers: Set<string>;
|
|
5
|
-
};
|
|
6
|
-
type NodeMatch<T> = Range & {
|
|
7
|
-
data: T;
|
|
8
|
-
node: ts.Node;
|
|
9
|
-
};
|
|
10
1
|
type QuickCheckPattern = {
|
|
11
2
|
patterns?: string[];
|
|
12
3
|
regex?: RegExp;
|
|
@@ -18,6 +9,4 @@ type Range = {
|
|
|
18
9
|
type Replacement = Range & {
|
|
19
10
|
newText: string;
|
|
20
11
|
};
|
|
21
|
-
type
|
|
22
|
-
type VisitorPredicate = (node: ts.Node) => boolean;
|
|
23
|
-
export type { ImportModification, NodeMatch, QuickCheckPattern, Range, Replacement, VisitorCallback, VisitorPredicate };
|
|
12
|
+
export type { QuickCheckPattern, Range, Replacement };
|
package/package.json
CHANGED
package/src/transformer/index.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { uuid } from '@esportsplus/utilities';
|
|
2
|
-
import ts from 'typescript';
|
|
3
2
|
import { BRACES_CONTENT_REGEX, REGEX_ESCAPE_PATTERN, UUID_DASH_REGEX } from './constants.js';
|
|
4
|
-
import type {
|
|
3
|
+
import type { QuickCheckPattern, Replacement } from './types.js';
|
|
5
4
|
import program from './program';
|
|
6
5
|
|
|
7
6
|
|
|
@@ -146,29 +145,6 @@ const applyReplacementsReverse = (code: string, replacements: Replacement[]): st
|
|
|
146
145
|
return result;
|
|
147
146
|
};
|
|
148
147
|
|
|
149
|
-
const collectNodes = <T>(sourceFile: ts.SourceFile, predicate: (node: ts.Node) => T | null): NodeMatch<T>[] => {
|
|
150
|
-
let matches: NodeMatch<T>[] = [];
|
|
151
|
-
|
|
152
|
-
function visit(node: ts.Node): void {
|
|
153
|
-
let data = predicate(node);
|
|
154
|
-
|
|
155
|
-
if (data !== null) {
|
|
156
|
-
matches.push({
|
|
157
|
-
data,
|
|
158
|
-
end: node.end,
|
|
159
|
-
node,
|
|
160
|
-
start: node.getStart(sourceFile)
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
ts.forEachChild(node, visit);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
visit(sourceFile);
|
|
168
|
-
|
|
169
|
-
return matches;
|
|
170
|
-
};
|
|
171
|
-
|
|
172
148
|
const mightNeedTransform = (code: string, check: QuickCheckPattern): boolean => {
|
|
173
149
|
if (check.regex) {
|
|
174
150
|
return check.regex.test(code);
|
|
@@ -189,69 +165,12 @@ const uid = (prefix: string, updateUUID = false): string => {
|
|
|
189
165
|
return prefix + '_' + (updateUUID ? uuid().replace(UUID_DASH_REGEX, '') : uidSuffix) + '_' + (i++).toString(36);
|
|
190
166
|
};
|
|
191
167
|
|
|
192
|
-
const updateImports = (code: string, modification: ImportModification): string => {
|
|
193
|
-
let { module, specifiers } = modification;
|
|
194
|
-
|
|
195
|
-
if (specifiers.size === 0) {
|
|
196
|
-
return code;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
let escapedModule = module.replace(REGEX_ESCAPE_PATTERN, '\\$&'),
|
|
200
|
-
importRegex = buildImportRegex(escapedModule);
|
|
201
|
-
|
|
202
|
-
return updateImportsWithRegex(code, specifiers, importRegex);
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
const visitAst = <T>(
|
|
206
|
-
sourceFile: ts.SourceFile,
|
|
207
|
-
callback: VisitorCallback<T>,
|
|
208
|
-
state: T,
|
|
209
|
-
predicate?: VisitorPredicate
|
|
210
|
-
): T => {
|
|
211
|
-
function visit(node: ts.Node): void {
|
|
212
|
-
if (!predicate || predicate(node)) {
|
|
213
|
-
callback(node, state);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
ts.forEachChild(node, visit);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
visit(sourceFile);
|
|
220
|
-
|
|
221
|
-
return state;
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
const visitAstWithDepth = <T>(
|
|
225
|
-
sourceFile: ts.SourceFile,
|
|
226
|
-
callback: (node: ts.Node, depth: number, state: T) => void,
|
|
227
|
-
state: T,
|
|
228
|
-
depthTrigger: (node: ts.Node) => boolean
|
|
229
|
-
): T => {
|
|
230
|
-
let depthStack: number[] = [0];
|
|
231
|
-
|
|
232
|
-
function visit(node: ts.Node): void {
|
|
233
|
-
let depth = depthStack[depthStack.length - 1],
|
|
234
|
-
nextDepth = depthTrigger(node) ? depth + 1 : depth;
|
|
235
|
-
|
|
236
|
-
callback(node, depth, state);
|
|
237
|
-
depthStack.push(nextDepth);
|
|
238
|
-
ts.forEachChild(node, visit);
|
|
239
|
-
depthStack.pop();
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
visit(sourceFile);
|
|
243
|
-
|
|
244
|
-
return state;
|
|
245
|
-
};
|
|
246
|
-
|
|
247
168
|
|
|
248
169
|
export {
|
|
249
170
|
addImport, applyReplacements, applyReplacementsReverse,
|
|
250
|
-
collectNodes,
|
|
251
171
|
mightNeedTransform,
|
|
252
172
|
program,
|
|
253
|
-
uid
|
|
254
|
-
visitAst, visitAstWithDepth
|
|
173
|
+
uid
|
|
255
174
|
};
|
|
256
175
|
export type * from './types';
|
|
257
|
-
export * from './constants';
|
|
176
|
+
export * from './constants';
|
package/src/transformer/types.ts
CHANGED
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
import ts from 'typescript';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type ImportModification = {
|
|
5
|
-
module: string;
|
|
6
|
-
specifiers: Set<string>;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
type NodeMatch<T> = Range & {
|
|
10
|
-
data: T;
|
|
11
|
-
node: ts.Node;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
1
|
type QuickCheckPattern = {
|
|
15
2
|
patterns?: string[];
|
|
16
3
|
regex?: RegExp;
|
|
@@ -25,15 +12,5 @@ type Replacement = Range & {
|
|
|
25
12
|
newText: string;
|
|
26
13
|
};
|
|
27
14
|
|
|
28
|
-
type VisitorCallback<T> = (node: ts.Node, state: T) => void;
|
|
29
|
-
|
|
30
|
-
type VisitorPredicate = (node: ts.Node) => boolean;
|
|
31
|
-
|
|
32
15
|
|
|
33
|
-
export type {
|
|
34
|
-
ImportModification,
|
|
35
|
-
NodeMatch,
|
|
36
|
-
QuickCheckPattern,
|
|
37
|
-
Range, Replacement,
|
|
38
|
-
VisitorCallback, VisitorPredicate
|
|
39
|
-
};
|
|
16
|
+
export type { QuickCheckPattern, Range, Replacement };
|