@suseejs/duplicates 0.1.1 → 0.1.3
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/dist/index.cjs +213 -340
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -4
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +4 -3
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +169 -293
- package/dist/index.mjs.map +1 -1
- package/package.json +63 -62
package/dist/index.cjs
CHANGED
|
@@ -1,65 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
-
};
|
|
41
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
2
|
// cSpell:disable
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
function
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
setPrefix
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
var _fix;
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
const resolves = require("@phothinmaung/resolves");
|
|
5
|
+
const transformFunction = require("@suseejs/transformer");
|
|
6
|
+
const utili = require("@suseejs/utils");
|
|
7
|
+
const ts = require("typescript");
|
|
8
|
+
const utilities = utili.default;
|
|
9
|
+
// construct maps
|
|
10
|
+
const namesMap = new Map();
|
|
11
|
+
const callNameMap = [];
|
|
12
|
+
const importNameMap = [];
|
|
13
|
+
const exportNameMap = [];
|
|
14
|
+
function __uniqueName() {
|
|
15
|
+
const storedPrefix = new Map();
|
|
16
|
+
const obj = {
|
|
17
|
+
setPrefix({ key, value }) {
|
|
18
|
+
const names = [];
|
|
19
|
+
let _fix;
|
|
61
20
|
if (storedPrefix.has(key)) {
|
|
62
|
-
console.warn(
|
|
21
|
+
console.warn(`${key} already exist`);
|
|
63
22
|
throw new Error();
|
|
64
23
|
}
|
|
65
24
|
else {
|
|
@@ -67,16 +26,16 @@ function uniqueName() {
|
|
|
67
26
|
storedPrefix.set(key, value);
|
|
68
27
|
}
|
|
69
28
|
function getName(input) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
?
|
|
73
|
-
:
|
|
29
|
+
const length = names.length;
|
|
30
|
+
const _name = _fix
|
|
31
|
+
? `${_fix}${input}_${length + 1}`
|
|
32
|
+
: `$nyein${input}_${length + 1}`;
|
|
74
33
|
names.push(_name);
|
|
75
34
|
return _name;
|
|
76
35
|
}
|
|
77
|
-
return { getName
|
|
36
|
+
return { getName };
|
|
78
37
|
},
|
|
79
|
-
getPrefix
|
|
38
|
+
getPrefix(key) {
|
|
80
39
|
if (storedPrefix.has(key)) {
|
|
81
40
|
return storedPrefix.get(key);
|
|
82
41
|
}
|
|
@@ -84,53 +43,45 @@ function uniqueName() {
|
|
|
84
43
|
};
|
|
85
44
|
return obj;
|
|
86
45
|
}
|
|
87
|
-
|
|
46
|
+
const dupName = __uniqueName().setPrefix({
|
|
88
47
|
key: "DuplicatesNames",
|
|
89
|
-
value: "
|
|
48
|
+
value: "__duplicatesNames__",
|
|
90
49
|
});
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
50
|
+
const normalizePathKey = (filePath) => {
|
|
51
|
+
const parsed = path.parse(filePath);
|
|
52
|
+
let noExt = path.join(parsed.dir, parsed.name);
|
|
94
53
|
if (parsed.name === "index") {
|
|
95
54
|
noExt = parsed.dir;
|
|
96
55
|
}
|
|
97
|
-
return
|
|
56
|
+
return path.normalize(noExt);
|
|
98
57
|
};
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (
|
|
58
|
+
const getFileKey = (filePath) => normalizePathKey(filePath);
|
|
59
|
+
const getModuleKeyFromSpecifier = (moduleSpecifier, sourceFile, containingFile) => {
|
|
60
|
+
let spec = "";
|
|
61
|
+
if (ts.isStringLiteral(moduleSpecifier)) {
|
|
103
62
|
spec = moduleSpecifier.text;
|
|
104
63
|
}
|
|
105
64
|
else {
|
|
106
65
|
spec = moduleSpecifier.getText(sourceFile).replace(/^['"]|['"]$/g, "");
|
|
107
66
|
}
|
|
108
67
|
if (spec.startsWith(".") || spec.startsWith("/")) {
|
|
109
|
-
|
|
110
|
-
return normalizePathKey(
|
|
68
|
+
const baseDir = path.dirname(containingFile);
|
|
69
|
+
return normalizePathKey(path.resolve(baseDir, spec));
|
|
111
70
|
}
|
|
112
71
|
return spec;
|
|
113
72
|
};
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
var factory = context.factory;
|
|
127
|
-
var visitor = function (node) {
|
|
128
|
-
if (typescript_1.default.isCallExpression(node)) {
|
|
129
|
-
if (typescript_1.default.isIdentifier(node.expression)) {
|
|
130
|
-
var base_1 = node.expression.text;
|
|
131
|
-
var new_name = null;
|
|
132
|
-
var mapping = callNameMap.find(function (m) { return m.base === base_1 && m.file === file; });
|
|
133
|
-
var importMapping = importNameMap.find(function (m) { return m.base === base_1 && m.file === file; });
|
|
73
|
+
const callExpression = (compilerOptions) => {
|
|
74
|
+
return ({ file, content, ...rest }) => {
|
|
75
|
+
const sourceFile = ts.createSourceFile(file, content, ts.ScriptTarget.Latest, true);
|
|
76
|
+
const transformer = (context) => {
|
|
77
|
+
const { factory } = context;
|
|
78
|
+
const visitor = (node) => {
|
|
79
|
+
if (ts.isCallExpression(node)) {
|
|
80
|
+
if (ts.isIdentifier(node.expression)) {
|
|
81
|
+
const base = node.expression.text;
|
|
82
|
+
let new_name = null;
|
|
83
|
+
const mapping = callNameMap.find((m) => m.base === base && m.file === file);
|
|
84
|
+
const importMapping = importNameMap.find((m) => m.base === base && m.file === file);
|
|
134
85
|
if (mapping) {
|
|
135
86
|
new_name = mapping.newName;
|
|
136
87
|
}
|
|
@@ -143,12 +94,12 @@ var callExpression = function (callNameMap, importNameMap, compilerOptions) {
|
|
|
143
94
|
}
|
|
144
95
|
}
|
|
145
96
|
}
|
|
146
|
-
else if (
|
|
147
|
-
if (
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
97
|
+
else if (ts.isPropertyAccessExpression(node)) {
|
|
98
|
+
if (ts.isIdentifier(node.expression)) {
|
|
99
|
+
const base = node.expression.text;
|
|
100
|
+
let new_name = null;
|
|
101
|
+
const mapping = callNameMap.find((m) => m.base === base && m.file === file);
|
|
102
|
+
const importMapping = importNameMap.find((m) => m.base === base && m.file === file);
|
|
152
103
|
if (mapping) {
|
|
153
104
|
new_name = mapping.newName;
|
|
154
105
|
}
|
|
@@ -160,12 +111,12 @@ var callExpression = function (callNameMap, importNameMap, compilerOptions) {
|
|
|
160
111
|
}
|
|
161
112
|
}
|
|
162
113
|
}
|
|
163
|
-
else if (
|
|
164
|
-
if (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
114
|
+
else if (ts.isNewExpression(node)) {
|
|
115
|
+
if (ts.isIdentifier(node.expression)) {
|
|
116
|
+
const base = node.expression.text;
|
|
117
|
+
let new_name = null;
|
|
118
|
+
const mapping = callNameMap.find((m) => m.base === base && m.file === file);
|
|
119
|
+
const importMapping = importNameMap.find((m) => m.base === base && m.file === file);
|
|
169
120
|
if (mapping) {
|
|
170
121
|
new_name = mapping.newName;
|
|
171
122
|
}
|
|
@@ -178,42 +129,31 @@ var callExpression = function (callNameMap, importNameMap, compilerOptions) {
|
|
|
178
129
|
}
|
|
179
130
|
}
|
|
180
131
|
/* ----------------------Returns for visitor function------------------------------- */
|
|
181
|
-
return
|
|
132
|
+
return ts.visitEachChild(node, visitor, context);
|
|
182
133
|
}; // visitor;
|
|
183
134
|
/* --------------------Returns for transformer function--------------------------------- */
|
|
184
|
-
return
|
|
135
|
+
return (rootNode) => ts.visitNode(rootNode, visitor);
|
|
185
136
|
}; // transformer;
|
|
186
137
|
/* --------------------Returns for main handler function--------------------------------- */
|
|
187
|
-
|
|
188
|
-
return { file
|
|
138
|
+
const _content = transformFunction(transformer, sourceFile, compilerOptions);
|
|
139
|
+
return { file, content: _content, ...rest };
|
|
189
140
|
}; // returns
|
|
190
141
|
};
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
var file = _a.file, content = _a.content;
|
|
204
|
-
var sourceFile = typescript_1.default.createSourceFile(file, content, typescript_1.default.ScriptTarget.Latest, true);
|
|
205
|
-
var transformer = function (context) {
|
|
206
|
-
var factory = context.factory;
|
|
207
|
-
var visitor = function (node) {
|
|
208
|
-
if (typescript_1.default.isExportSpecifier(node)) {
|
|
209
|
-
if (typescript_1.default.isIdentifier(node.name)) {
|
|
210
|
-
var base_4 = node.name.text;
|
|
211
|
-
var new_name = null;
|
|
212
|
-
var mapping = callNameMap.find(function (m) { return m.base === base_4 && m.file === file; });
|
|
213
|
-
var importMapping = importNameMap.find(function (m) { return m.base === base_4 && m.file === file; });
|
|
142
|
+
const exportExpression = (compilerOptions) => {
|
|
143
|
+
return ({ file, content, ...rest }) => {
|
|
144
|
+
const sourceFile = ts.createSourceFile(file, content, ts.ScriptTarget.Latest, true);
|
|
145
|
+
const transformer = (context) => {
|
|
146
|
+
const { factory } = context;
|
|
147
|
+
const visitor = (node) => {
|
|
148
|
+
if (ts.isExportSpecifier(node)) {
|
|
149
|
+
if (ts.isIdentifier(node.name)) {
|
|
150
|
+
const base = node.name.text;
|
|
151
|
+
let new_name = null;
|
|
152
|
+
const mapping = callNameMap.find((m) => m.base === base && m.file === file);
|
|
153
|
+
const importMapping = importNameMap.find((m) => m.base === base && m.file === file);
|
|
214
154
|
if (mapping) {
|
|
215
155
|
exportNameMap.push({
|
|
216
|
-
base
|
|
156
|
+
base,
|
|
217
157
|
file: getFileKey(file),
|
|
218
158
|
newName: mapping.newName,
|
|
219
159
|
});
|
|
@@ -227,16 +167,16 @@ var exportExpression = function (callNameMap, importNameMap, exportNameMap, comp
|
|
|
227
167
|
}
|
|
228
168
|
}
|
|
229
169
|
}
|
|
230
|
-
else if (
|
|
231
|
-
|
|
232
|
-
if (
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
170
|
+
else if (ts.isExportAssignment(node)) {
|
|
171
|
+
const expr = node.expression;
|
|
172
|
+
if (ts.isIdentifier(expr)) {
|
|
173
|
+
const base = expr.text;
|
|
174
|
+
let new_name = null;
|
|
175
|
+
const mapping = callNameMap.find((m) => m.base === base && m.file === file);
|
|
176
|
+
const importMapping = importNameMap.find((m) => m.base === base && m.file === file);
|
|
237
177
|
if (mapping) {
|
|
238
178
|
exportNameMap.push({
|
|
239
|
-
base
|
|
179
|
+
base,
|
|
240
180
|
file: getFileKey(file),
|
|
241
181
|
newName: mapping.newName,
|
|
242
182
|
});
|
|
@@ -251,52 +191,41 @@ var exportExpression = function (callNameMap, importNameMap, exportNameMap, comp
|
|
|
251
191
|
}
|
|
252
192
|
}
|
|
253
193
|
/* ----------------------Returns for visitor function------------------------------- */
|
|
254
|
-
return
|
|
194
|
+
return ts.visitEachChild(node, visitor, context);
|
|
255
195
|
}; // visitor;
|
|
256
196
|
/* --------------------Returns for transformer function--------------------------------- */
|
|
257
|
-
return
|
|
197
|
+
return (rootNode) => ts.visitNode(rootNode, visitor);
|
|
258
198
|
}; // transformer;
|
|
259
199
|
/* --------------------Returns for main handler function--------------------------------- */
|
|
260
|
-
|
|
261
|
-
return { file
|
|
200
|
+
const _content = transformFunction(transformer, sourceFile, compilerOptions);
|
|
201
|
+
return { file, content: _content, ...rest };
|
|
262
202
|
}; // returns
|
|
263
203
|
};
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
var factory = context.factory;
|
|
277
|
-
var visitor = function (node) {
|
|
278
|
-
var _a, _b;
|
|
279
|
-
if (typescript_1.default.isImportDeclaration(node)) {
|
|
280
|
-
var moduleKey_1 = getModuleKeyFromSpecifier(node.moduleSpecifier, sourceFile, file);
|
|
281
|
-
var baseNames = [];
|
|
282
|
-
if (((_a = node.importClause) === null || _a === void 0 ? void 0 : _a.namedBindings) &&
|
|
283
|
-
typescript_1.default.isNamedImports(node.importClause.namedBindings)) {
|
|
284
|
-
baseNames = node.importClause.namedBindings.elements.map(function (el) {
|
|
285
|
-
return el.name.text.trim();
|
|
286
|
-
});
|
|
204
|
+
const importExpression = (compilerOptions) => {
|
|
205
|
+
return ({ file, content, ...rest }) => {
|
|
206
|
+
const sourceFile = ts.createSourceFile(file, content, ts.ScriptTarget.Latest, true);
|
|
207
|
+
const transformer = (context) => {
|
|
208
|
+
const { factory } = context;
|
|
209
|
+
const visitor = (node) => {
|
|
210
|
+
if (ts.isImportDeclaration(node)) {
|
|
211
|
+
const moduleKey = getModuleKeyFromSpecifier(node.moduleSpecifier, sourceFile, file);
|
|
212
|
+
let baseNames = [];
|
|
213
|
+
if (node.importClause?.namedBindings &&
|
|
214
|
+
ts.isNamedImports(node.importClause.namedBindings)) {
|
|
215
|
+
baseNames = node.importClause.namedBindings.elements.map((el) => el.name.text.trim());
|
|
287
216
|
}
|
|
288
217
|
// import default expression
|
|
289
|
-
if (
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
218
|
+
if (node.importClause?.name &&
|
|
219
|
+
ts.isIdentifier(node.importClause.name)) {
|
|
220
|
+
const base = node.importClause.name.text.trim();
|
|
221
|
+
const mapping = exportNameMap.find((m) => m.base === base && m.file === moduleKey);
|
|
293
222
|
if (mapping) {
|
|
294
223
|
importNameMap.push({
|
|
295
224
|
base: mapping.base,
|
|
296
|
-
file
|
|
225
|
+
file,
|
|
297
226
|
newName: mapping.newName,
|
|
298
227
|
});
|
|
299
|
-
|
|
228
|
+
const newImportClause = factory.updateImportClause(node.importClause, node.importClause.phaseModifier, factory.createIdentifier(mapping.newName), node.importClause.namedBindings);
|
|
300
229
|
return factory.updateImportDeclaration(node, node.modifiers, newImportClause, node.moduleSpecifier, node.attributes);
|
|
301
230
|
}
|
|
302
231
|
}
|
|
@@ -304,187 +233,163 @@ var importExpression = function (exportNameMap, importNameMap, compilerOptions)
|
|
|
304
233
|
if (baseNames.length > 0 &&
|
|
305
234
|
node.importClause &&
|
|
306
235
|
node.importClause.namedBindings &&
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
236
|
+
ts.isNamedImports(node.importClause.namedBindings)) {
|
|
237
|
+
const updatedElements = node.importClause.namedBindings.elements.map((el) => {
|
|
238
|
+
const mapping = exportNameMap.find((m) => m.base === el.name.text.trim() && m.file === moduleKey);
|
|
310
239
|
if (mapping) {
|
|
311
240
|
importNameMap.push({
|
|
312
241
|
base: mapping.base,
|
|
313
|
-
file
|
|
242
|
+
file,
|
|
314
243
|
newName: mapping.newName,
|
|
315
244
|
});
|
|
316
245
|
return factory.updateImportSpecifier(el, el.isTypeOnly, el.propertyName, factory.createIdentifier(mapping.newName));
|
|
317
246
|
}
|
|
318
247
|
return el;
|
|
319
248
|
});
|
|
320
|
-
|
|
321
|
-
|
|
249
|
+
const newNamedImports = factory.updateNamedImports(node.importClause.namedBindings, updatedElements);
|
|
250
|
+
const newImportClause = factory.updateImportClause(node.importClause, node.importClause.phaseModifier, node.importClause.name, newNamedImports);
|
|
322
251
|
return factory.updateImportDeclaration(node, node.modifiers, newImportClause, node.moduleSpecifier, node.attributes);
|
|
323
252
|
}
|
|
324
253
|
} //&&
|
|
325
254
|
/* ----------------------Returns for visitor function------------------------------- */
|
|
326
|
-
return
|
|
255
|
+
return ts.visitEachChild(node, visitor, context);
|
|
327
256
|
}; // visitor;
|
|
328
257
|
/* --------------------Returns for transformer function--------------------------------- */
|
|
329
|
-
return
|
|
258
|
+
return (rootNode) => ts.visitNode(rootNode, visitor);
|
|
330
259
|
}; // transformer;
|
|
331
260
|
/* --------------------Returns for main handler function--------------------------------- */
|
|
332
|
-
|
|
333
|
-
return { file
|
|
261
|
+
const _content = transformFunction(transformer, sourceFile, compilerOptions);
|
|
262
|
+
return { file, content: _content, ...rest };
|
|
334
263
|
}; // returns
|
|
335
264
|
};
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
* @param compilerOptions - The options for the TypeScript compiler.
|
|
342
|
-
* @return A new source file with collected information.
|
|
343
|
-
*/
|
|
344
|
-
var collector = function (namesMap, compilerOptions) {
|
|
345
|
-
return function (_a) {
|
|
346
|
-
var file = _a.file, content = _a.content;
|
|
347
|
-
var sourceFile = typescript_1.default.createSourceFile(file, content, typescript_1.default.ScriptTarget.Latest, true);
|
|
348
|
-
var transformer = function (context) {
|
|
349
|
-
function visitNode(node, isGlobalScope) {
|
|
350
|
-
var _a;
|
|
351
|
-
if (isGlobalScope === void 0) { isGlobalScope = true; }
|
|
265
|
+
const collector = (compilerOptions) => {
|
|
266
|
+
return ({ file, content, ...rest }) => {
|
|
267
|
+
const sourceFile = ts.createSourceFile(file, content, ts.ScriptTarget.Latest, true);
|
|
268
|
+
const transformer = (context) => {
|
|
269
|
+
function visitNode(node, isGlobalScope = true) {
|
|
352
270
|
// Global declarations များကိုသာ collect လုပ်မယ်
|
|
353
271
|
if (isGlobalScope) {
|
|
354
272
|
// Variable statements (const, let, var)
|
|
355
|
-
if (
|
|
356
|
-
node.declarationList.declarations.forEach(
|
|
357
|
-
if (
|
|
358
|
-
|
|
273
|
+
if (ts.isVariableStatement(node)) {
|
|
274
|
+
node.declarationList.declarations.forEach((decl) => {
|
|
275
|
+
if (ts.isIdentifier(decl.name)) {
|
|
276
|
+
const $name = decl.name.text;
|
|
359
277
|
if (!namesMap.has($name)) {
|
|
360
|
-
namesMap.set($name, new Set([{ file
|
|
278
|
+
namesMap.set($name, new Set([{ file }]));
|
|
361
279
|
}
|
|
362
280
|
else {
|
|
363
281
|
// biome-ignore lint/style/noNonNullAssertion : !namesMap.has($name) before
|
|
364
|
-
namesMap.get($name).add({ file
|
|
282
|
+
namesMap.get($name).add({ file });
|
|
365
283
|
}
|
|
366
284
|
}
|
|
367
285
|
});
|
|
368
286
|
}
|
|
369
287
|
// Function, Class, Enum, Interface, Type declarations
|
|
370
|
-
else if (
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
288
|
+
else if (ts.isFunctionDeclaration(node) ||
|
|
289
|
+
ts.isClassDeclaration(node) ||
|
|
290
|
+
ts.isEnumDeclaration(node) ||
|
|
291
|
+
ts.isInterfaceDeclaration(node) ||
|
|
292
|
+
ts.isTypeAliasDeclaration(node)) {
|
|
293
|
+
const $name = node.name?.text;
|
|
376
294
|
if ($name) {
|
|
377
295
|
if (!namesMap.has($name)) {
|
|
378
|
-
namesMap.set($name, new Set([{ file
|
|
296
|
+
namesMap.set($name, new Set([{ file }]));
|
|
379
297
|
}
|
|
380
298
|
else {
|
|
381
299
|
// biome-ignore lint/style/noNonNullAssertion : !namesMap.has($name) before
|
|
382
|
-
namesMap.get($name).add({ file
|
|
300
|
+
namesMap.get($name).add({ file });
|
|
383
301
|
}
|
|
384
302
|
}
|
|
385
303
|
}
|
|
386
304
|
}
|
|
387
305
|
// Local scope ထဲရောက်သွားတဲ့ node တွေအတွက် recursive visit
|
|
388
|
-
if (
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
306
|
+
if (ts.isBlock(node) ||
|
|
307
|
+
ts.isFunctionDeclaration(node) ||
|
|
308
|
+
ts.isFunctionExpression(node) ||
|
|
309
|
+
ts.isArrowFunction(node) ||
|
|
310
|
+
ts.isMethodDeclaration(node) ||
|
|
311
|
+
ts.isClassDeclaration(node)) {
|
|
394
312
|
// Local scope ထဲကို ဝင်သွားပြီဆိုတာနဲ့ isGlobalScope = false
|
|
395
|
-
if (
|
|
396
|
-
|
|
313
|
+
if (ts.isBlock(node)) {
|
|
314
|
+
ts.visitNodes(node.statements, (child) => visitNode(child, false));
|
|
397
315
|
}
|
|
398
316
|
else {
|
|
399
|
-
|
|
317
|
+
ts.forEachChild(node, (child) => {
|
|
400
318
|
visitNode(child, false);
|
|
401
319
|
});
|
|
402
320
|
}
|
|
403
321
|
}
|
|
404
322
|
else {
|
|
405
323
|
// Global scope ထဲဆက်ရှိနေတဲ့ node တွေအတွက်
|
|
406
|
-
return
|
|
324
|
+
return ts.visitEachChild(node, (child) => visitNode(child, isGlobalScope), context);
|
|
407
325
|
}
|
|
408
326
|
/* ----------------------Returns for visitNode function------------------------------- */
|
|
409
327
|
return node;
|
|
410
328
|
} // visitNode
|
|
411
329
|
/* --------------------Returns for transformer function--------------------------------- */
|
|
412
|
-
return
|
|
330
|
+
return (rootNode) => visitNode(rootNode, true);
|
|
413
331
|
}; // transformer;
|
|
414
332
|
/* --------------------Returns for main handler function--------------------------------- */
|
|
415
|
-
|
|
416
|
-
return { file
|
|
333
|
+
const _content = transformFunction(transformer, sourceFile, compilerOptions);
|
|
334
|
+
return { file, content: _content, ...rest };
|
|
417
335
|
}; // returns
|
|
418
336
|
};
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
return function (_a) {
|
|
430
|
-
var file = _a.file, content = _a.content;
|
|
431
|
-
var sourceFile = typescript_1.default.createSourceFile(file, content, typescript_1.default.ScriptTarget.Latest, true);
|
|
432
|
-
var transformer = function (context) {
|
|
433
|
-
var factory = context.factory;
|
|
434
|
-
var visitor = function (node) {
|
|
435
|
-
if (typescript_1.default.isVariableStatement(node)) {
|
|
436
|
-
var newDeclarations = node.declarationList.declarations.map(function (decl) {
|
|
437
|
-
if (typescript_1.default.isIdentifier(decl.name)) {
|
|
438
|
-
var base = decl.name.text;
|
|
337
|
+
const updater = (compilerOptions) => {
|
|
338
|
+
return ({ file, content, ...rest }) => {
|
|
339
|
+
const sourceFile = ts.createSourceFile(file, content, ts.ScriptTarget.Latest, true);
|
|
340
|
+
const transformer = (context) => {
|
|
341
|
+
const { factory } = context;
|
|
342
|
+
const visitor = (node) => {
|
|
343
|
+
if (ts.isVariableStatement(node)) {
|
|
344
|
+
const newDeclarations = node.declarationList.declarations.map((decl) => {
|
|
345
|
+
if (ts.isIdentifier(decl.name)) {
|
|
346
|
+
const base = decl.name.text;
|
|
439
347
|
// biome-ignore lint/style/noNonNullAssertion : namesMap.has(base) before that get just only size
|
|
440
348
|
if (namesMap.has(base) && namesMap.get(base).size > 1) {
|
|
441
|
-
|
|
442
|
-
callNameMap.push({ base
|
|
349
|
+
const newName = dupName.getName(base);
|
|
350
|
+
callNameMap.push({ base, file, newName });
|
|
443
351
|
return factory.updateVariableDeclaration(decl, factory.createIdentifier(newName), decl.exclamationToken, decl.type, decl.initializer);
|
|
444
352
|
}
|
|
445
353
|
}
|
|
446
354
|
return decl;
|
|
447
355
|
});
|
|
448
|
-
|
|
356
|
+
const newDeclList = factory.updateVariableDeclarationList(node.declarationList, newDeclarations);
|
|
449
357
|
return factory.updateVariableStatement(node, node.modifiers, newDeclList);
|
|
450
358
|
}
|
|
451
|
-
else if (
|
|
452
|
-
if (node.name &&
|
|
453
|
-
|
|
359
|
+
else if (ts.isFunctionDeclaration(node)) {
|
|
360
|
+
if (node.name && ts.isIdentifier(node.name)) {
|
|
361
|
+
const base = node.name.text;
|
|
454
362
|
// biome-ignore lint/style/noNonNullAssertion : namesMap.has(base) before that get just only size
|
|
455
363
|
if (namesMap.has(base) && namesMap.get(base).size > 1) {
|
|
456
|
-
|
|
457
|
-
callNameMap.push({ base
|
|
364
|
+
const newName = dupName.getName(base);
|
|
365
|
+
callNameMap.push({ base, file, newName });
|
|
458
366
|
return factory.updateFunctionDeclaration(node, node.modifiers, node.asteriskToken, factory.createIdentifier(newName), node.typeParameters, node.parameters, node.type, node.body);
|
|
459
367
|
}
|
|
460
368
|
}
|
|
461
369
|
}
|
|
462
|
-
else if (
|
|
463
|
-
if (node.name &&
|
|
464
|
-
|
|
370
|
+
else if (ts.isClassDeclaration(node)) {
|
|
371
|
+
if (node.name && ts.isIdentifier(node.name)) {
|
|
372
|
+
const base = node.name.text;
|
|
465
373
|
// biome-ignore lint/style/noNonNullAssertion : namesMap.has(base) before that get just only size
|
|
466
374
|
if (namesMap.has(base) && namesMap.get(base).size > 1) {
|
|
467
|
-
|
|
468
|
-
callNameMap.push({ base
|
|
375
|
+
const newName = dupName.getName(base);
|
|
376
|
+
callNameMap.push({ base, file, newName });
|
|
469
377
|
return factory.updateClassDeclaration(node, node.modifiers, factory.createIdentifier(newName), node.typeParameters, node.heritageClauses, node.members);
|
|
470
378
|
}
|
|
471
379
|
}
|
|
472
380
|
}
|
|
473
381
|
/* ----------------------Returns for visitor function------------------------------- */
|
|
474
|
-
return
|
|
382
|
+
return ts.visitEachChild(node, visitor, context);
|
|
475
383
|
}; // visitor;
|
|
476
384
|
/* --------------------Returns for transformer function--------------------------------- */
|
|
477
|
-
return
|
|
385
|
+
return (rootNode) => ts.visitNode(rootNode, visitor);
|
|
478
386
|
}; // transformer;
|
|
479
387
|
/* --------------------Returns for main handler function--------------------------------- */
|
|
480
|
-
|
|
481
|
-
return { file
|
|
388
|
+
const _content = transformFunction(transformer, sourceFile, compilerOptions);
|
|
389
|
+
return { file, content: _content, ...rest };
|
|
482
390
|
}; // returns
|
|
483
391
|
};
|
|
484
|
-
|
|
485
|
-
return new Promise(function (resolve) { return setTimeout(resolve, time); });
|
|
486
|
-
};
|
|
487
|
-
var duplicateHandlers = {
|
|
392
|
+
const duplicateHandlers = {
|
|
488
393
|
/**
|
|
489
394
|
* A bundle handler that takes a list of source files and transforms them into renamed source files.
|
|
490
395
|
* The transformation is done in a series of steps, each step transforms the source files based on the given maps.
|
|
@@ -497,43 +402,23 @@ var duplicateHandlers = {
|
|
|
497
402
|
* @param compilerOptions - The options for the TypeScript compiler.
|
|
498
403
|
* @returns A list of transformed source files.
|
|
499
404
|
*/
|
|
500
|
-
renamed:
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
[callExpression, callNameMap, importNameMap, compilerOptions],
|
|
518
|
-
[
|
|
519
|
-
exportExpression,
|
|
520
|
-
callNameMap,
|
|
521
|
-
importNameMap,
|
|
522
|
-
exportNameMap,
|
|
523
|
-
compilerOptions,
|
|
524
|
-
],
|
|
525
|
-
]);
|
|
526
|
-
return [4 /*yield*/, duplicates.concurrent()];
|
|
527
|
-
case 1:
|
|
528
|
-
duplicate = _a.sent();
|
|
529
|
-
for (_i = 0, duplicate_1 = duplicate; _i < duplicate_1.length; _i++) {
|
|
530
|
-
func = duplicate_1[_i];
|
|
531
|
-
deps = deps.map(func);
|
|
532
|
-
}
|
|
533
|
-
return [2 /*return*/, deps];
|
|
534
|
-
}
|
|
535
|
-
});
|
|
536
|
-
}); },
|
|
405
|
+
renamed: async (deps, compilerOptions) => {
|
|
406
|
+
// order is important here
|
|
407
|
+
const duplicates = resolves([
|
|
408
|
+
[collector, compilerOptions],
|
|
409
|
+
[updater, compilerOptions],
|
|
410
|
+
[callExpression, compilerOptions],
|
|
411
|
+
[exportExpression, compilerOptions],
|
|
412
|
+
[importExpression, compilerOptions],
|
|
413
|
+
[callExpression, compilerOptions],
|
|
414
|
+
[exportExpression, compilerOptions],
|
|
415
|
+
]);
|
|
416
|
+
const duplicate = await duplicates.concurrent();
|
|
417
|
+
for (const func of duplicate) {
|
|
418
|
+
deps = deps.map(func);
|
|
419
|
+
}
|
|
420
|
+
return deps;
|
|
421
|
+
},
|
|
537
422
|
/**
|
|
538
423
|
* A bundle handler that takes a list of source files and checks if they have been renamed correctly.
|
|
539
424
|
* If a source file has not been renamed, an error will be thrown.
|
|
@@ -542,38 +427,26 @@ var duplicateHandlers = {
|
|
|
542
427
|
* @param compilerOptions - The options for the TypeScript compiler.
|
|
543
428
|
* @returns A list of source files that have been renamed correctly.
|
|
544
429
|
*/
|
|
545
|
-
notRenamed:
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
case 2:
|
|
558
|
-
_a.sent();
|
|
559
|
-
namesMap.forEach(function (files, name) {
|
|
560
|
-
if (files.size > 1) {
|
|
561
|
-
_err = true;
|
|
562
|
-
console.warn("Name -> ".concat(name, " declared in multiple files :"));
|
|
563
|
-
// biome-ignore lint/suspicious/useIterableCallbackReturn : just log warn
|
|
564
|
-
files.forEach(function (f) { return console.warn(" - ".concat(f.file)); });
|
|
565
|
-
}
|
|
566
|
-
});
|
|
567
|
-
return [4 /*yield*/, wait(500)];
|
|
568
|
-
case 3:
|
|
569
|
-
_a.sent();
|
|
570
|
-
if (_err) {
|
|
571
|
-
process.exit(1);
|
|
572
|
-
}
|
|
573
|
-
return [2 /*return*/, deps];
|
|
430
|
+
notRenamed: async (deps, compilerOptions) => {
|
|
431
|
+
let _err = false;
|
|
432
|
+
const duplicates = resolves([[collector, namesMap, compilerOptions]]);
|
|
433
|
+
const duplicate = await duplicates.concurrent();
|
|
434
|
+
deps.map(duplicate[0]);
|
|
435
|
+
await utilities.wait(1000);
|
|
436
|
+
namesMap.forEach((files, name) => {
|
|
437
|
+
if (files.size > 1) {
|
|
438
|
+
_err = true;
|
|
439
|
+
console.warn(`Name -> ${name} declared in multiple files :`);
|
|
440
|
+
// biome-ignore lint/suspicious/useIterableCallbackReturn : just log warn
|
|
441
|
+
files.forEach((f) => console.warn(` - ${f.file}`));
|
|
574
442
|
}
|
|
575
443
|
});
|
|
576
|
-
|
|
444
|
+
await utilities.wait(500);
|
|
445
|
+
if (_err) {
|
|
446
|
+
process.exit(1);
|
|
447
|
+
}
|
|
448
|
+
return deps;
|
|
449
|
+
},
|
|
577
450
|
};
|
|
578
451
|
module.exports = duplicateHandlers;
|
|
579
|
-
//# sourceMappingURL=index.
|
|
452
|
+
//# sourceMappingURL=index.cjs.map
|