UNPKG

2.79 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.createMergeStatementsTransformer = void 0;
7const typescript_1 = __importDefault(require("typescript"));
8/* ****************************************************************************************************************** */
9// region: Utils
10/* ****************************************************************************************************************** */
11function createMergeStatementsTransformer(baseSourceFile, sourceFile) {
12 const replacements = new Map();
13 for (const node of sourceFile.statements) {
14 if (typescript_1.default.isVariableStatement(node)) {
15 const name = node.declarationList.declarations[0].name.text;
16 replacements.set(name, node);
17 }
18 else if (typescript_1.default.isFunctionDeclaration(node) && node.name) {
19 const name = node.name.text;
20 replacements.set(name, node);
21 }
22 }
23 return (context) => {
24 const { factory } = context;
25 return (node) => {
26 if (node.fileName !== baseSourceFile.fileName)
27 return node;
28 const transformedStatements = [];
29 node.statements.forEach((statement) => {
30 if (typescript_1.default.isVariableStatement(statement)) {
31 const name = statement.declarationList.declarations[0].name.text;
32 if (replacements.has(name)) {
33 transformedStatements.push(replacements.get(name));
34 replacements.delete(name);
35 }
36 else {
37 transformedStatements.push(statement);
38 }
39 }
40 else if (typescript_1.default.isFunctionDeclaration(statement) && statement.name) {
41 const name = statement.name.text;
42 if (replacements.has(name)) {
43 transformedStatements.push(replacements.get(name));
44 replacements.delete(name);
45 }
46 else {
47 transformedStatements.push(statement);
48 }
49 }
50 else {
51 transformedStatements.push(statement);
52 }
53 });
54 replacements.forEach((value) => transformedStatements.push(value));
55 return factory.updateSourceFile(node, transformedStatements);
56 };
57 };
58}
59exports.createMergeStatementsTransformer = createMergeStatementsTransformer;
60// endregion
61//# sourceMappingURL=merge-statements.js.map
\No newline at end of file