@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 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
- var typescript_1 = __importDefault(require("typescript"));
44
- var node_path_1 = __importDefault(require("node:path"));
45
- var transformer_1 = __importDefault(require("@suseejs/transformer"));
46
- var resolves_1 = __importDefault(require("@phothinmaung/resolves"));
47
- /**
48
- * Returns an object with methods to generate unique names based on a prefix.
49
- * The `setPrefix` method sets a prefix for the generated names.
50
- * The `getName` method generates a unique name based on the prefix and an input string.
51
- * The `getPrefix` method returns the prefix associated with a key.
52
- * If a prefix is set multiple times, an error will be thrown.
53
- */
54
- function uniqueName() {
55
- var storedPrefix = new Map();
56
- var obj = {
57
- setPrefix: function (_a) {
58
- var key = _a.key, value = _a.value;
59
- var names = [];
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("".concat(key, " already exist"));
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
- var length = names.length;
71
- var _name = _fix
72
- ? "".concat(_fix).concat(input, "_").concat(length + 1)
73
- : "$nyein".concat(input, "_").concat(length + 1);
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: getName };
36
+ return { getName };
78
37
  },
79
- getPrefix: function (key) {
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
- var dupName = uniqueName().setPrefix({
46
+ const dupName = __uniqueName().setPrefix({
88
47
  key: "DuplicatesNames",
89
- value: "d_",
48
+ value: "__duplicatesNames__",
90
49
  });
91
- var normalizePathKey = function (filePath) {
92
- var parsed = node_path_1.default.parse(filePath);
93
- var noExt = node_path_1.default.join(parsed.dir, parsed.name);
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 node_path_1.default.normalize(noExt);
56
+ return path.normalize(noExt);
98
57
  };
99
- var getFileKey = function (filePath) { return normalizePathKey(filePath); };
100
- var getModuleKeyFromSpecifier = function (moduleSpecifier, sourceFile, containingFile) {
101
- var spec = "";
102
- if (typescript_1.default.isStringLiteral(moduleSpecifier)) {
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
- var baseDir = node_path_1.default.dirname(containingFile);
110
- return normalizePathKey(node_path_1.default.resolve(baseDir, spec));
68
+ const baseDir = path.dirname(containingFile);
69
+ return normalizePathKey(path.resolve(baseDir, spec));
111
70
  }
112
71
  return spec;
113
72
  };
114
- /**
115
- * A bundle handler that transforms call expression in a given source file.
116
- * @param callNameMap - A mapping of base names to new names for call expressions.
117
- * @param importNameMap - A mapping of base names to new names for import expressions.
118
- * @param compilerOptions - The compiler options for the TypeScript compiler.
119
- * @return A new source file with transformed call expressions.
120
- */
121
- var callExpression = function (callNameMap, importNameMap, compilerOptions) {
122
- return function (_a) {
123
- var file = _a.file, content = _a.content;
124
- var sourceFile = typescript_1.default.createSourceFile(file, content, typescript_1.default.ScriptTarget.Latest, true);
125
- var transformer = function (context) {
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 (typescript_1.default.isPropertyAccessExpression(node)) {
147
- if (typescript_1.default.isIdentifier(node.expression)) {
148
- var base_2 = node.expression.text;
149
- var new_name = null;
150
- var mapping = callNameMap.find(function (m) { return m.base === base_2 && m.file === file; });
151
- var importMapping = importNameMap.find(function (m) { return m.base === base_2 && m.file === file; });
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 (typescript_1.default.isNewExpression(node)) {
164
- if (typescript_1.default.isIdentifier(node.expression)) {
165
- var base_3 = node.expression.text;
166
- var new_name = null;
167
- var mapping = callNameMap.find(function (m) { return m.base === base_3 && m.file === file; });
168
- var importMapping = importNameMap.find(function (m) { return m.base === base_3 && m.file === file; });
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 typescript_1.default.visitEachChild(node, visitor, context);
132
+ return ts.visitEachChild(node, visitor, context);
182
133
  }; // visitor;
183
134
  /* --------------------Returns for transformer function--------------------------------- */
184
- return function (rootNode) { return typescript_1.default.visitNode(rootNode, visitor); };
135
+ return (rootNode) => ts.visitNode(rootNode, visitor);
185
136
  }; // transformer;
186
137
  /* --------------------Returns for main handler function--------------------------------- */
187
- var _content = (0, transformer_1.default)(transformer, sourceFile, compilerOptions);
188
- return { file: file, content: _content };
138
+ const _content = transformFunction(transformer, sourceFile, compilerOptions);
139
+ return { file, content: _content, ...rest };
189
140
  }; // returns
190
141
  };
191
- /**
192
- * A bundle handler that renames the exported expression according to the given maps.
193
- * It will traverse the given source file and rename the exported expression if a mapping is found.
194
- *
195
- * @param callNameMap - A map of base names to new names for function calls.
196
- * @param importNameMap - A map of base names to new names for import expressions.
197
- * @param exportNameMap - A map of base names to new names for export expressions.
198
- * @param compilerOptions - The options for the TypeScript compiler.
199
- * @returns A bundle handler that takes a source file and returns a new source file with the renamed exported expressions.
200
- */
201
- var exportExpression = function (callNameMap, importNameMap, exportNameMap, compilerOptions) {
202
- return function (_a) {
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: base_4,
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 (typescript_1.default.isExportAssignment(node)) {
231
- var expr = node.expression;
232
- if (typescript_1.default.isIdentifier(expr)) {
233
- var base_5 = expr.text;
234
- var new_name = null;
235
- var mapping = callNameMap.find(function (m) { return m.base === base_5 && m.file === file; });
236
- var importMapping = importNameMap.find(function (m) { return m.base === base_5 && m.file === file; });
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: base_5,
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 typescript_1.default.visitEachChild(node, visitor, context);
194
+ return ts.visitEachChild(node, visitor, context);
255
195
  }; // visitor;
256
196
  /* --------------------Returns for transformer function--------------------------------- */
257
- return function (rootNode) { return typescript_1.default.visitNode(rootNode, visitor); };
197
+ return (rootNode) => ts.visitNode(rootNode, visitor);
258
198
  }; // transformer;
259
199
  /* --------------------Returns for main handler function--------------------------------- */
260
- var _content = (0, transformer_1.default)(transformer, sourceFile, compilerOptions);
261
- return { file: file, content: _content };
200
+ const _content = transformFunction(transformer, sourceFile, compilerOptions);
201
+ return { file, content: _content, ...rest };
262
202
  }; // returns
263
203
  };
264
- /**
265
- * A bundle handler that transforms import expressions in a given source file.
266
- * @param exportNameMap - A mapping of base names to new names for export expressions.
267
- * @param importNameMap - A mapping of base names to new names for import expressions.
268
- * @param compilerOptions - The compiler options for the TypeScript compiler.
269
- * @return A new source file with transformed import expressions.
270
- */
271
- var importExpression = function (exportNameMap, importNameMap, compilerOptions) {
272
- return function (_a) {
273
- var file = _a.file, content = _a.content;
274
- var sourceFile = typescript_1.default.createSourceFile(file, content, typescript_1.default.ScriptTarget.Latest, true);
275
- var transformer = function (context) {
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 (((_b = node.importClause) === null || _b === void 0 ? void 0 : _b.name) &&
290
- typescript_1.default.isIdentifier(node.importClause.name)) {
291
- var base_6 = node.importClause.name.text.trim();
292
- var mapping = exportNameMap.find(function (m) { return m.base === base_6 && m.file === moduleKey_1; });
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: file,
225
+ file,
297
226
  newName: mapping.newName,
298
227
  });
299
- var newImportClause = factory.updateImportClause(node.importClause, node.importClause.phaseModifier, factory.createIdentifier(mapping.newName), node.importClause.namedBindings);
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
- typescript_1.default.isNamedImports(node.importClause.namedBindings)) {
308
- var updatedElements = node.importClause.namedBindings.elements.map(function (el) {
309
- var mapping = exportNameMap.find(function (m) { return m.base === el.name.text.trim() && m.file === moduleKey_1; });
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: 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
- var newNamedImports = factory.updateNamedImports(node.importClause.namedBindings, updatedElements);
321
- var newImportClause = factory.updateImportClause(node.importClause, node.importClause.phaseModifier, node.importClause.name, newNamedImports);
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 typescript_1.default.visitEachChild(node, visitor, context);
255
+ return ts.visitEachChild(node, visitor, context);
327
256
  }; // visitor;
328
257
  /* --------------------Returns for transformer function--------------------------------- */
329
- return function (rootNode) { return typescript_1.default.visitNode(rootNode, visitor); };
258
+ return (rootNode) => ts.visitNode(rootNode, visitor);
330
259
  }; // transformer;
331
260
  /* --------------------Returns for main handler function--------------------------------- */
332
- var _content = (0, transformer_1.default)(transformer, sourceFile, compilerOptions);
333
- return { file: file, content: _content };
261
+ const _content = transformFunction(transformer, sourceFile, compilerOptions);
262
+ return { file, content: _content, ...rest };
334
263
  }; // returns
335
264
  };
336
- /**
337
- * A bundle handler that collects information about the usage of given names in a given source file.
338
- * It will traverse the given source file and collect information about the usage of given names.
339
- * The information will be stored in a namesMap.
340
- * @param namesMap - A map of base names to new names for function calls, import expressions, and export expressions.
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 (typescript_1.default.isVariableStatement(node)) {
356
- node.declarationList.declarations.forEach(function (decl) {
357
- if (typescript_1.default.isIdentifier(decl.name)) {
358
- var $name = decl.name.text;
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: 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: 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 (typescript_1.default.isFunctionDeclaration(node) ||
371
- typescript_1.default.isClassDeclaration(node) ||
372
- typescript_1.default.isEnumDeclaration(node) ||
373
- typescript_1.default.isInterfaceDeclaration(node) ||
374
- typescript_1.default.isTypeAliasDeclaration(node)) {
375
- var $name = (_a = node.name) === null || _a === void 0 ? void 0 : _a.text;
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: 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: file });
300
+ namesMap.get($name).add({ file });
383
301
  }
384
302
  }
385
303
  }
386
304
  }
387
305
  // Local scope ထဲရောက်သွားတဲ့ node တွေအတွက် recursive visit
388
- if (typescript_1.default.isBlock(node) ||
389
- typescript_1.default.isFunctionDeclaration(node) ||
390
- typescript_1.default.isFunctionExpression(node) ||
391
- typescript_1.default.isArrowFunction(node) ||
392
- typescript_1.default.isMethodDeclaration(node) ||
393
- typescript_1.default.isClassDeclaration(node)) {
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 (typescript_1.default.isBlock(node)) {
396
- typescript_1.default.visitNodes(node.statements, function (child) { return visitNode(child, false); });
313
+ if (ts.isBlock(node)) {
314
+ ts.visitNodes(node.statements, (child) => visitNode(child, false));
397
315
  }
398
316
  else {
399
- typescript_1.default.forEachChild(node, function (child) {
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 typescript_1.default.visitEachChild(node, function (child) { return visitNode(child, isGlobalScope); }, context);
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 function (rootNode) { return visitNode(rootNode, true); };
330
+ return (rootNode) => visitNode(rootNode, true);
413
331
  }; // transformer;
414
332
  /* --------------------Returns for main handler function--------------------------------- */
415
- var _content = (0, transformer_1.default)(transformer, sourceFile, compilerOptions);
416
- return { file: file, content: _content };
333
+ const _content = transformFunction(transformer, sourceFile, compilerOptions);
334
+ return { file, content: _content, ...rest };
417
335
  }; // returns
418
336
  };
419
- /**
420
- * A bundle handler that updates the given source file based on the given namesMap and callNameMap.
421
- * It will traverse the given source file and update the names of the exported expressions, call expressions, and import expressions if a mapping is found in the callNameMap.
422
- * The updated source file will be returned.
423
- * @param namesMap - A map of base names to new names for function calls, import expressions, and export expressions.
424
- * @param callNameMap - A map of base names to new names for call expressions.
425
- * @param compilerOptions - The options for the TypeScript compiler.
426
- * @returns A new source file with updated names.
427
- */
428
- var updater = function (namesMap, callNameMap, compilerOptions) {
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
- var newName = dupName.getName(base);
442
- callNameMap.push({ base: base, file: file, newName: newName });
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
- var newDeclList = factory.updateVariableDeclarationList(node.declarationList, newDeclarations);
356
+ const newDeclList = factory.updateVariableDeclarationList(node.declarationList, newDeclarations);
449
357
  return factory.updateVariableStatement(node, node.modifiers, newDeclList);
450
358
  }
451
- else if (typescript_1.default.isFunctionDeclaration(node)) {
452
- if (node.name && typescript_1.default.isIdentifier(node.name)) {
453
- var base = node.name.text;
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
- var newName = dupName.getName(base);
457
- callNameMap.push({ base: base, file: file, newName: newName });
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 (typescript_1.default.isClassDeclaration(node)) {
463
- if (node.name && typescript_1.default.isIdentifier(node.name)) {
464
- var base = node.name.text;
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
- var newName = dupName.getName(base);
468
- callNameMap.push({ base: base, file: file, newName: newName });
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 typescript_1.default.visitEachChild(node, visitor, context);
382
+ return ts.visitEachChild(node, visitor, context);
475
383
  }; // visitor;
476
384
  /* --------------------Returns for transformer function--------------------------------- */
477
- return function (rootNode) { return typescript_1.default.visitNode(rootNode, visitor); };
385
+ return (rootNode) => ts.visitNode(rootNode, visitor);
478
386
  }; // transformer;
479
387
  /* --------------------Returns for main handler function--------------------------------- */
480
- var _content = (0, transformer_1.default)(transformer, sourceFile, compilerOptions);
481
- return { file: file, content: _content };
388
+ const _content = transformFunction(transformer, sourceFile, compilerOptions);
389
+ return { file, content: _content, ...rest };
482
390
  }; // returns
483
391
  };
484
- var wait = function (time) {
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: function (deps, namesMap, callNameMap, importNameMap, exportNameMap, compilerOptions) { return __awaiter(void 0, void 0, void 0, function () {
501
- var duplicates, duplicate, _i, duplicate_1, func;
502
- return __generator(this, function (_a) {
503
- switch (_a.label) {
504
- case 0:
505
- duplicates = (0, resolves_1.default)([
506
- [collector, namesMap, compilerOptions],
507
- [updater, namesMap, callNameMap, compilerOptions],
508
- [callExpression, callNameMap, importNameMap, compilerOptions],
509
- [
510
- exportExpression,
511
- callNameMap,
512
- importNameMap,
513
- exportNameMap,
514
- compilerOptions,
515
- ],
516
- [importExpression, exportNameMap, importNameMap, compilerOptions],
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: function (deps, namesMap, compilerOptions) { return __awaiter(void 0, void 0, void 0, function () {
546
- var _err, duplicates, duplicate;
547
- return __generator(this, function (_a) {
548
- switch (_a.label) {
549
- case 0:
550
- _err = false;
551
- duplicates = (0, resolves_1.default)([[collector, namesMap, compilerOptions]]);
552
- return [4 /*yield*/, duplicates.concurrent()];
553
- case 1:
554
- duplicate = _a.sent();
555
- deps.map(duplicate[0]);
556
- return [4 /*yield*/, wait(1000)];
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.js.map
452
+ //# sourceMappingURL=index.cjs.map