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