@hexaijs/plugin-contracts-generator 0.1.1 → 0.2.0
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/README.md +5 -2
- package/dist/cli-CPg-O4OY.d.ts +214 -0
- package/dist/cli.d.ts +1 -35
- package/dist/cli.js +2479 -232
- package/dist/cli.js.map +1 -1
- package/dist/decorators/index.d.ts +12 -11
- package/dist/decorators/index.js +21 -116
- package/dist/decorators/index.js.map +1 -1
- package/dist/index.d.ts +533 -40
- package/dist/index.js +2738 -91
- package/dist/index.js.map +1 -1
- package/dist/runtime/index.d.ts +23 -2
- package/dist/runtime/index.js +38 -5
- package/dist/runtime/index.js.map +1 -1
- package/package.json +10 -14
- package/dist/ast-utils.d.ts +0 -6
- package/dist/ast-utils.d.ts.map +0 -1
- package/dist/ast-utils.js +0 -111
- package/dist/ast-utils.js.map +0 -1
- package/dist/class-analyzer.d.ts +0 -16
- package/dist/class-analyzer.d.ts.map +0 -1
- package/dist/class-analyzer.js +0 -155
- package/dist/class-analyzer.js.map +0 -1
- package/dist/cli.d.ts.map +0 -1
- package/dist/config-loader.d.ts +0 -34
- package/dist/config-loader.d.ts.map +0 -1
- package/dist/config-loader.js +0 -188
- package/dist/config-loader.js.map +0 -1
- package/dist/decorators/index.d.ts.map +0 -1
- package/dist/domain/index.d.ts +0 -2
- package/dist/domain/index.d.ts.map +0 -1
- package/dist/domain/index.js +0 -18
- package/dist/domain/index.js.map +0 -1
- package/dist/domain/types.d.ts +0 -182
- package/dist/domain/types.d.ts.map +0 -1
- package/dist/domain/types.js +0 -65
- package/dist/domain/types.js.map +0 -1
- package/dist/errors.d.ts +0 -79
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -138
- package/dist/errors.js.map +0 -1
- package/dist/file-copier.d.ts +0 -84
- package/dist/file-copier.d.ts.map +0 -1
- package/dist/file-copier.js +0 -758
- package/dist/file-copier.js.map +0 -1
- package/dist/file-graph-resolver.d.ts +0 -47
- package/dist/file-graph-resolver.d.ts.map +0 -1
- package/dist/file-graph-resolver.js +0 -230
- package/dist/file-graph-resolver.js.map +0 -1
- package/dist/file-system.d.ts +0 -26
- package/dist/file-system.d.ts.map +0 -1
- package/dist/file-system.js +0 -34
- package/dist/file-system.js.map +0 -1
- package/dist/hexai-plugin.d.ts +0 -16
- package/dist/hexai-plugin.d.ts.map +0 -1
- package/dist/hexai-plugin.js +0 -59
- package/dist/hexai-plugin.js.map +0 -1
- package/dist/import-analyzer.d.ts +0 -6
- package/dist/import-analyzer.d.ts.map +0 -1
- package/dist/import-analyzer.js +0 -77
- package/dist/import-analyzer.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/logger.d.ts +0 -21
- package/dist/logger.d.ts.map +0 -1
- package/dist/logger.js +0 -47
- package/dist/logger.js.map +0 -1
- package/dist/parser.d.ts +0 -32
- package/dist/parser.d.ts.map +0 -1
- package/dist/parser.js +0 -209
- package/dist/parser.js.map +0 -1
- package/dist/pipeline.d.ts +0 -56
- package/dist/pipeline.d.ts.map +0 -1
- package/dist/pipeline.js +0 -152
- package/dist/pipeline.js.map +0 -1
- package/dist/reexport-generator.d.ts +0 -81
- package/dist/reexport-generator.d.ts.map +0 -1
- package/dist/reexport-generator.js +0 -208
- package/dist/reexport-generator.js.map +0 -1
- package/dist/registry-generator.d.ts +0 -27
- package/dist/registry-generator.d.ts.map +0 -1
- package/dist/registry-generator.js +0 -108
- package/dist/registry-generator.js.map +0 -1
- package/dist/runtime/index.d.ts.map +0 -1
- package/dist/runtime/message-registry.d.ts +0 -23
- package/dist/runtime/message-registry.d.ts.map +0 -1
- package/dist/runtime/message-registry.js +0 -39
- package/dist/runtime/message-registry.js.map +0 -1
- package/dist/scanner.d.ts +0 -21
- package/dist/scanner.d.ts.map +0 -1
- package/dist/scanner.js +0 -53
- package/dist/scanner.js.map +0 -1
- package/dist/test-utils.d.ts +0 -23
- package/dist/test-utils.d.ts.map +0 -1
- package/dist/test-utils.js +0 -198
- package/dist/test-utils.js.map +0 -1
- package/dist/tsconfig-loader.d.ts +0 -8
- package/dist/tsconfig-loader.d.ts.map +0 -1
- package/dist/tsconfig-loader.js +0 -64
- package/dist/tsconfig-loader.js.map +0 -1
package/dist/file-copier.js
DELETED
|
@@ -1,758 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.FileCopier = void 0;
|
|
37
|
-
const path = __importStar(require("path"));
|
|
38
|
-
const ts = __importStar(require("typescript"));
|
|
39
|
-
const errors_1 = require("./errors");
|
|
40
|
-
const file_system_1 = require("./file-system");
|
|
41
|
-
const domain_1 = require("./domain");
|
|
42
|
-
const class_analyzer_1 = require("./class-analyzer");
|
|
43
|
-
const CONTRACTS_GENERATOR_MODULE = "@hexaijs/plugin-contracts-generator";
|
|
44
|
-
const CONTRACT_DECORATORS = new Set([
|
|
45
|
-
"PublicCommand",
|
|
46
|
-
"PublicEvent",
|
|
47
|
-
"PublicQuery",
|
|
48
|
-
]);
|
|
49
|
-
const TS_EXTENSION_PATTERN = /\.ts$/;
|
|
50
|
-
const TS_EXTENSION = ".ts";
|
|
51
|
-
const REQUEST_SUFFIX = "Request";
|
|
52
|
-
const QUERY_SUFFIX = "Query";
|
|
53
|
-
class FileCopier {
|
|
54
|
-
fs;
|
|
55
|
-
constructor(options = {}) {
|
|
56
|
-
this.fs = options.fileSystem ?? file_system_1.nodeFileSystem;
|
|
57
|
-
}
|
|
58
|
-
async copyFiles(options) {
|
|
59
|
-
const { sourceRoot, outputDir, fileGraph, pathAliasRewrites, removeDecorators, responseTypesToExport, messageTypes, decoratorNames, } = options;
|
|
60
|
-
const copiedFiles = [];
|
|
61
|
-
const rewrittenImports = new Map();
|
|
62
|
-
const { entryContents, usedLocalImports } = await this.preprocessEntryFiles(fileGraph, messageTypes, decoratorNames, sourceRoot);
|
|
63
|
-
this.expandTransitiveDependencies(usedLocalImports, fileGraph);
|
|
64
|
-
for (const node of fileGraph.nodes.values()) {
|
|
65
|
-
const content = await this.resolveNodeContent(node, entryContents, usedLocalImports, messageTypes);
|
|
66
|
-
if (content === null) {
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
const transformedContent = this.applyTransformations(content, node, fileGraph, sourceRoot, removeDecorators, responseTypesToExport, pathAliasRewrites);
|
|
70
|
-
const outputPath = await this.writeOutputFile(outputDir, node.relativePath, transformedContent.content);
|
|
71
|
-
copiedFiles.push(outputPath);
|
|
72
|
-
if (transformedContent.rewrites.length > 0) {
|
|
73
|
-
rewrittenImports.set(outputPath, transformedContent.rewrites);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return { copiedFiles, rewrittenImports };
|
|
77
|
-
}
|
|
78
|
-
async preprocessEntryFiles(fileGraph, messageTypes, decoratorNames, sourceRoot) {
|
|
79
|
-
const entryContents = new Map();
|
|
80
|
-
const usedLocalImports = new Set();
|
|
81
|
-
if (!messageTypes?.length) {
|
|
82
|
-
return { entryContents, usedLocalImports };
|
|
83
|
-
}
|
|
84
|
-
for (const node of fileGraph.nodes.values()) {
|
|
85
|
-
if (!node.isEntryPoint) {
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
const rawContent = await this.readFileContent(node.absolutePath);
|
|
89
|
-
const { content: extractedContent, usedModuleSpecifiers } = this.extractSymbolsFromEntry(rawContent, node.absolutePath, messageTypes, decoratorNames);
|
|
90
|
-
entryContents.set(node.absolutePath, extractedContent);
|
|
91
|
-
for (const specifier of usedModuleSpecifiers) {
|
|
92
|
-
const importInfo = node.imports.find((i) => i.moduleSpecifier === specifier);
|
|
93
|
-
if (importInfo?.resolvedPath && !importInfo.isExternal) {
|
|
94
|
-
usedLocalImports.add(importInfo.resolvedPath);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return { entryContents, usedLocalImports };
|
|
99
|
-
}
|
|
100
|
-
expandTransitiveDependencies(usedLocalImports, fileGraph) {
|
|
101
|
-
let changed = true;
|
|
102
|
-
while (changed) {
|
|
103
|
-
changed = false;
|
|
104
|
-
for (const importPath of usedLocalImports) {
|
|
105
|
-
const node = fileGraph.nodes.get(importPath);
|
|
106
|
-
if (!node)
|
|
107
|
-
continue;
|
|
108
|
-
for (const imp of node.imports) {
|
|
109
|
-
const isUnusedLocalImport = imp.resolvedPath &&
|
|
110
|
-
!imp.isExternal &&
|
|
111
|
-
!usedLocalImports.has(imp.resolvedPath);
|
|
112
|
-
if (isUnusedLocalImport) {
|
|
113
|
-
usedLocalImports.add(imp.resolvedPath);
|
|
114
|
-
changed = true;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
async resolveNodeContent(node, entryContents, usedLocalImports, messageTypes) {
|
|
121
|
-
if (node.isEntryPoint && messageTypes?.length) {
|
|
122
|
-
return (entryContents.get(node.absolutePath) ??
|
|
123
|
-
(await this.readFileContent(node.absolutePath)));
|
|
124
|
-
}
|
|
125
|
-
const isUnusedDependency = messageTypes?.length && !usedLocalImports.has(node.absolutePath);
|
|
126
|
-
if (isUnusedDependency) {
|
|
127
|
-
return null;
|
|
128
|
-
}
|
|
129
|
-
return await this.readFileContent(node.absolutePath);
|
|
130
|
-
}
|
|
131
|
-
applyTransformations(content, node, fileGraph, sourceRoot, removeDecorators, responseTypesToExport, pathAliasRewrites) {
|
|
132
|
-
const rewrites = [];
|
|
133
|
-
let transformedContent = content;
|
|
134
|
-
transformedContent = this.processExcludedImports(transformedContent, node, fileGraph.excludedPaths, rewrites);
|
|
135
|
-
transformedContent = this.processDecoratorRemoval(transformedContent, node.absolutePath, removeDecorators, rewrites);
|
|
136
|
-
transformedContent = this.processTypeExports(transformedContent, node.absolutePath, responseTypesToExport?.get(node.absolutePath), rewrites);
|
|
137
|
-
transformedContent = this.processInternalPathAliases(transformedContent, node, fileGraph, sourceRoot, rewrites);
|
|
138
|
-
transformedContent = this.processExternalPathAliases(transformedContent, pathAliasRewrites, rewrites);
|
|
139
|
-
return { content: transformedContent, rewrites };
|
|
140
|
-
}
|
|
141
|
-
generateBarrelExport(copiedFiles, outputDir) {
|
|
142
|
-
const lines = [];
|
|
143
|
-
for (const filePath of copiedFiles) {
|
|
144
|
-
const relativePath = path.relative(outputDir, filePath);
|
|
145
|
-
lines.push(this.createExportStatement(relativePath));
|
|
146
|
-
}
|
|
147
|
-
return lines.join("\n");
|
|
148
|
-
}
|
|
149
|
-
async readFileContent(absolutePath) {
|
|
150
|
-
try {
|
|
151
|
-
return await this.fs.readFile(absolutePath);
|
|
152
|
-
}
|
|
153
|
-
catch (error) {
|
|
154
|
-
throw new errors_1.FileReadError(absolutePath, { cause: error });
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
async writeOutputFile(outputDir, relativePath, content) {
|
|
158
|
-
const outputPath = path.join(outputDir, relativePath);
|
|
159
|
-
const outputDirPath = path.dirname(outputPath);
|
|
160
|
-
await this.fs.mkdir(outputDirPath, { recursive: true });
|
|
161
|
-
try {
|
|
162
|
-
await this.fs.writeFile(outputPath, content);
|
|
163
|
-
}
|
|
164
|
-
catch (error) {
|
|
165
|
-
throw new errors_1.FileWriteError(outputPath, { cause: error });
|
|
166
|
-
}
|
|
167
|
-
return outputPath;
|
|
168
|
-
}
|
|
169
|
-
processExcludedImports(content, node, excludedPaths, rewrites) {
|
|
170
|
-
if (excludedPaths.size === 0) {
|
|
171
|
-
return content;
|
|
172
|
-
}
|
|
173
|
-
const excludedResult = this.removeExcludedImports(content, node, excludedPaths);
|
|
174
|
-
rewrites.push(...excludedResult.changes);
|
|
175
|
-
return excludedResult.content;
|
|
176
|
-
}
|
|
177
|
-
processDecoratorRemoval(content, filePath, removeDecorators, rewrites) {
|
|
178
|
-
if (!removeDecorators) {
|
|
179
|
-
return content;
|
|
180
|
-
}
|
|
181
|
-
const decoratorResult = this.removeContractDecorators(content, filePath);
|
|
182
|
-
rewrites.push(...decoratorResult.changes);
|
|
183
|
-
return decoratorResult.content;
|
|
184
|
-
}
|
|
185
|
-
processTypeExports(content, filePath, typesToExport, rewrites) {
|
|
186
|
-
if (!typesToExport || typesToExport.length === 0) {
|
|
187
|
-
return content;
|
|
188
|
-
}
|
|
189
|
-
const exportResult = this.addExportToTypes(content, filePath, typesToExport);
|
|
190
|
-
rewrites.push(...exportResult.changes);
|
|
191
|
-
return exportResult.content;
|
|
192
|
-
}
|
|
193
|
-
processInternalPathAliases(content, node, fileGraph, sourceRoot, rewrites) {
|
|
194
|
-
const internalResult = this.rewriteInternalPathAliases(content, node, fileGraph, sourceRoot);
|
|
195
|
-
rewrites.push(...internalResult.rewrites);
|
|
196
|
-
return internalResult.content;
|
|
197
|
-
}
|
|
198
|
-
processExternalPathAliases(content, pathAliasRewrites, rewrites) {
|
|
199
|
-
if (!pathAliasRewrites) {
|
|
200
|
-
return content;
|
|
201
|
-
}
|
|
202
|
-
const aliasResult = this.applyPathAliasRewrites(content, pathAliasRewrites);
|
|
203
|
-
rewrites.push(...aliasResult.rewrites);
|
|
204
|
-
return aliasResult.content;
|
|
205
|
-
}
|
|
206
|
-
createExportStatement(relativePath) {
|
|
207
|
-
const exportPath = "./" + relativePath.replace(TS_EXTENSION_PATTERN, "");
|
|
208
|
-
return `export * from '${exportPath}'`;
|
|
209
|
-
}
|
|
210
|
-
rewriteInternalPathAliases(content, node, fileGraph, sourceRoot) {
|
|
211
|
-
let transformedContent = content;
|
|
212
|
-
const appliedRewrites = [];
|
|
213
|
-
for (const importInfo of node.imports) {
|
|
214
|
-
if (this.isExternalOrRelativeImport(importInfo)) {
|
|
215
|
-
continue;
|
|
216
|
-
}
|
|
217
|
-
const targetNode = this.resolveInternalImport(importInfo, fileGraph);
|
|
218
|
-
if (!targetNode) {
|
|
219
|
-
continue;
|
|
220
|
-
}
|
|
221
|
-
const relativePath = this.computeRelativePath(node.relativePath, targetNode.relativePath);
|
|
222
|
-
const rewriteResult = this.rewriteModuleSpecifier(transformedContent, importInfo.moduleSpecifier, relativePath);
|
|
223
|
-
if (rewriteResult.wasRewritten) {
|
|
224
|
-
transformedContent = rewriteResult.content;
|
|
225
|
-
appliedRewrites.push(`${importInfo.moduleSpecifier} → ${relativePath}`);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return { content: transformedContent, rewrites: appliedRewrites };
|
|
229
|
-
}
|
|
230
|
-
isExternalOrRelativeImport(importInfo) {
|
|
231
|
-
return (importInfo.isExternal || importInfo.moduleSpecifier.startsWith("."));
|
|
232
|
-
}
|
|
233
|
-
resolveInternalImport(importInfo, fileGraph) {
|
|
234
|
-
if (!importInfo.resolvedPath ||
|
|
235
|
-
!fileGraph.nodes.has(importInfo.resolvedPath)) {
|
|
236
|
-
return undefined;
|
|
237
|
-
}
|
|
238
|
-
return fileGraph.nodes.get(importInfo.resolvedPath);
|
|
239
|
-
}
|
|
240
|
-
rewriteModuleSpecifier(content, originalSpecifier, newSpecifier) {
|
|
241
|
-
const importPattern = new RegExp(`(from\\s+['"])${this.escapeRegex(originalSpecifier)}(['"])`, "g");
|
|
242
|
-
if (!importPattern.test(content)) {
|
|
243
|
-
return { content, wasRewritten: false };
|
|
244
|
-
}
|
|
245
|
-
const rewrittenContent = content.replace(importPattern, `$1${newSpecifier}$2`);
|
|
246
|
-
return { content: rewrittenContent, wasRewritten: true };
|
|
247
|
-
}
|
|
248
|
-
computeRelativePath(fromRelative, toRelative) {
|
|
249
|
-
const fromDir = path.dirname(fromRelative);
|
|
250
|
-
let relativePath = path.relative(fromDir, toRelative);
|
|
251
|
-
relativePath = relativePath.replace(TS_EXTENSION_PATTERN, "");
|
|
252
|
-
if (!relativePath.startsWith(".")) {
|
|
253
|
-
relativePath = "./" + relativePath;
|
|
254
|
-
}
|
|
255
|
-
return relativePath;
|
|
256
|
-
}
|
|
257
|
-
applyPathAliasRewrites(content, rewrites) {
|
|
258
|
-
let transformedContent = content;
|
|
259
|
-
const appliedRewrites = [];
|
|
260
|
-
for (const [from, to] of rewrites) {
|
|
261
|
-
if (transformedContent.includes(from)) {
|
|
262
|
-
transformedContent = transformedContent.replace(new RegExp(this.escapeRegex(from), "g"), to);
|
|
263
|
-
appliedRewrites.push(`${from} → ${to}`);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return { content: transformedContent, rewrites: appliedRewrites };
|
|
267
|
-
}
|
|
268
|
-
escapeRegex(str) {
|
|
269
|
-
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
270
|
-
}
|
|
271
|
-
removeExcludedImports(content, node, excludedPaths) {
|
|
272
|
-
const excludedModuleSpecifiers = this.findExcludedModuleSpecifiers(node, excludedPaths);
|
|
273
|
-
if (excludedModuleSpecifiers.size === 0) {
|
|
274
|
-
return { content, changes: [] };
|
|
275
|
-
}
|
|
276
|
-
const changes = Array.from(excludedModuleSpecifiers).map((specifier) => `removed import: ${specifier}`);
|
|
277
|
-
const visitorFactory = this.createExcludedImportsVisitor(excludedModuleSpecifiers);
|
|
278
|
-
const transformedContent = this.transformSourceFile(content, node.absolutePath, visitorFactory);
|
|
279
|
-
return { content: transformedContent, changes };
|
|
280
|
-
}
|
|
281
|
-
findExcludedModuleSpecifiers(node, excludedPaths) {
|
|
282
|
-
const excludedModuleSpecifiers = new Set();
|
|
283
|
-
for (const importInfo of node.imports) {
|
|
284
|
-
if (importInfo.resolvedPath &&
|
|
285
|
-
excludedPaths.has(importInfo.resolvedPath)) {
|
|
286
|
-
excludedModuleSpecifiers.add(importInfo.moduleSpecifier);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return excludedModuleSpecifiers;
|
|
290
|
-
}
|
|
291
|
-
createExcludedImportsVisitor(excludedModuleSpecifiers) {
|
|
292
|
-
return (context) => {
|
|
293
|
-
const visit = (node) => {
|
|
294
|
-
if (this.isImportFromExcludedModule(node, excludedModuleSpecifiers)) {
|
|
295
|
-
return undefined;
|
|
296
|
-
}
|
|
297
|
-
if (this.isExportFromExcludedModule(node, excludedModuleSpecifiers)) {
|
|
298
|
-
return undefined;
|
|
299
|
-
}
|
|
300
|
-
return ts.visitEachChild(node, visit, context);
|
|
301
|
-
};
|
|
302
|
-
return visit;
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
isImportFromExcludedModule(node, excludedModules) {
|
|
306
|
-
if (!ts.isImportDeclaration(node)) {
|
|
307
|
-
return false;
|
|
308
|
-
}
|
|
309
|
-
const moduleSpecifier = node.moduleSpecifier.text;
|
|
310
|
-
return excludedModules.has(moduleSpecifier);
|
|
311
|
-
}
|
|
312
|
-
isExportFromExcludedModule(node, excludedModules) {
|
|
313
|
-
if (!ts.isExportDeclaration(node) || !node.moduleSpecifier) {
|
|
314
|
-
return false;
|
|
315
|
-
}
|
|
316
|
-
const moduleSpecifier = node.moduleSpecifier.text;
|
|
317
|
-
return excludedModules.has(moduleSpecifier);
|
|
318
|
-
}
|
|
319
|
-
removeContractDecorators(content, filePath) {
|
|
320
|
-
const changes = [];
|
|
321
|
-
const visitorFactory = this.createDecoratorRemovalVisitor(changes);
|
|
322
|
-
const transformedContent = this.transformSourceFile(content, filePath, visitorFactory);
|
|
323
|
-
return { content: transformedContent, changes };
|
|
324
|
-
}
|
|
325
|
-
createDecoratorRemovalVisitor(changes) {
|
|
326
|
-
return (context) => {
|
|
327
|
-
const visit = (node) => {
|
|
328
|
-
if (ts.isImportDeclaration(node)) {
|
|
329
|
-
return this.processContractGeneratorImport(node, changes);
|
|
330
|
-
}
|
|
331
|
-
if (ts.isClassDeclaration(node) && node.modifiers) {
|
|
332
|
-
return this.removeContractDecoratorsFromClass(node, changes);
|
|
333
|
-
}
|
|
334
|
-
return ts.visitEachChild(node, visit, context);
|
|
335
|
-
};
|
|
336
|
-
return visit;
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
processContractGeneratorImport(node, changes) {
|
|
340
|
-
const moduleSpecifier = node.moduleSpecifier.text;
|
|
341
|
-
const isContractGeneratorModule = moduleSpecifier === CONTRACTS_GENERATOR_MODULE ||
|
|
342
|
-
moduleSpecifier.startsWith(CONTRACTS_GENERATOR_MODULE + "/");
|
|
343
|
-
if (!isContractGeneratorModule) {
|
|
344
|
-
return node;
|
|
345
|
-
}
|
|
346
|
-
const namedBindings = node.importClause?.namedBindings;
|
|
347
|
-
if (!namedBindings || !ts.isNamedImports(namedBindings)) {
|
|
348
|
-
return node;
|
|
349
|
-
}
|
|
350
|
-
const remainingElements = namedBindings.elements.filter((el) => !CONTRACT_DECORATORS.has(el.name.text));
|
|
351
|
-
if (remainingElements.length === 0) {
|
|
352
|
-
changes.push(`removed import: ${moduleSpecifier}`);
|
|
353
|
-
return undefined;
|
|
354
|
-
}
|
|
355
|
-
if (remainingElements.length < namedBindings.elements.length) {
|
|
356
|
-
changes.push(`removed decorators from import: ${moduleSpecifier}`);
|
|
357
|
-
return this.createImportWithFilteredBindings(node, remainingElements);
|
|
358
|
-
}
|
|
359
|
-
return node;
|
|
360
|
-
}
|
|
361
|
-
createImportWithFilteredBindings(originalImport, remainingElements) {
|
|
362
|
-
const newNamedImports = ts.factory.createNamedImports(remainingElements);
|
|
363
|
-
const newImportClause = ts.factory.createImportClause(originalImport.importClause.isTypeOnly, originalImport.importClause.name, newNamedImports);
|
|
364
|
-
return ts.factory.createImportDeclaration(originalImport.modifiers, newImportClause, originalImport.moduleSpecifier, originalImport.attributes);
|
|
365
|
-
}
|
|
366
|
-
removeContractDecoratorsFromClass(node, changes) {
|
|
367
|
-
const filteredModifiers = node.modifiers.filter((modifier) => {
|
|
368
|
-
if (!ts.isDecorator(modifier)) {
|
|
369
|
-
return true;
|
|
370
|
-
}
|
|
371
|
-
const decoratorName = this.extractDecoratorName(modifier);
|
|
372
|
-
if (decoratorName && CONTRACT_DECORATORS.has(decoratorName)) {
|
|
373
|
-
const decoratorSuffix = this.isDecoratorCallExpression(modifier)
|
|
374
|
-
? "()"
|
|
375
|
-
: "";
|
|
376
|
-
changes.push(`removed decorator: @${decoratorName}${decoratorSuffix}`);
|
|
377
|
-
return false;
|
|
378
|
-
}
|
|
379
|
-
return true;
|
|
380
|
-
});
|
|
381
|
-
if (filteredModifiers.length === node.modifiers.length) {
|
|
382
|
-
return node;
|
|
383
|
-
}
|
|
384
|
-
return ts.factory.createClassDeclaration(filteredModifiers, node.name, node.typeParameters, node.heritageClauses, node.members);
|
|
385
|
-
}
|
|
386
|
-
extractDecoratorName(decorator) {
|
|
387
|
-
const expression = decorator.expression;
|
|
388
|
-
if (ts.isCallExpression(expression) &&
|
|
389
|
-
ts.isIdentifier(expression.expression)) {
|
|
390
|
-
return expression.expression.text;
|
|
391
|
-
}
|
|
392
|
-
if (ts.isIdentifier(expression)) {
|
|
393
|
-
return expression.text;
|
|
394
|
-
}
|
|
395
|
-
return undefined;
|
|
396
|
-
}
|
|
397
|
-
isDecoratorCallExpression(decorator) {
|
|
398
|
-
return ts.isCallExpression(decorator.expression);
|
|
399
|
-
}
|
|
400
|
-
hasExportModifier(node) {
|
|
401
|
-
return (node.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) ?? false);
|
|
402
|
-
}
|
|
403
|
-
prependExportModifier(modifiers) {
|
|
404
|
-
const exportModifier = ts.factory.createModifier(ts.SyntaxKind.ExportKeyword);
|
|
405
|
-
return modifiers ? [exportModifier, ...modifiers] : [exportModifier];
|
|
406
|
-
}
|
|
407
|
-
addExportToTypes(content, filePath, typeNames) {
|
|
408
|
-
const changes = [];
|
|
409
|
-
const typeNamesSet = new Set(typeNames);
|
|
410
|
-
const visitorFactory = this.createExportAdditionVisitor(typeNamesSet, changes);
|
|
411
|
-
const transformedContent = this.transformSourceFile(content, filePath, visitorFactory);
|
|
412
|
-
return { content: transformedContent, changes };
|
|
413
|
-
}
|
|
414
|
-
createExportAdditionVisitor(typeNamesSet, changes) {
|
|
415
|
-
return (context) => {
|
|
416
|
-
const visit = (node) => {
|
|
417
|
-
if (ts.isTypeAliasDeclaration(node)) {
|
|
418
|
-
return this.addExportToTypeAlias(node, typeNamesSet, changes);
|
|
419
|
-
}
|
|
420
|
-
if (ts.isInterfaceDeclaration(node)) {
|
|
421
|
-
return this.addExportToInterface(node, typeNamesSet, changes);
|
|
422
|
-
}
|
|
423
|
-
return ts.visitEachChild(node, visit, context);
|
|
424
|
-
};
|
|
425
|
-
return visit;
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
addExportToTypeAlias(node, typeNamesSet, changes) {
|
|
429
|
-
const typeName = node.name.text;
|
|
430
|
-
if (!typeNamesSet.has(typeName) || this.hasExportModifier(node)) {
|
|
431
|
-
return node;
|
|
432
|
-
}
|
|
433
|
-
changes.push(`added export: type ${typeName}`);
|
|
434
|
-
return ts.factory.createTypeAliasDeclaration(this.prependExportModifier(node.modifiers), node.name, node.typeParameters, node.type);
|
|
435
|
-
}
|
|
436
|
-
addExportToInterface(node, typeNamesSet, changes) {
|
|
437
|
-
const typeName = node.name.text;
|
|
438
|
-
if (!typeNamesSet.has(typeName) || this.hasExportModifier(node)) {
|
|
439
|
-
return node;
|
|
440
|
-
}
|
|
441
|
-
changes.push(`added export: interface ${typeName}`);
|
|
442
|
-
return ts.factory.createInterfaceDeclaration(this.prependExportModifier(node.modifiers), node.name, node.typeParameters, node.heritageClauses, node.members);
|
|
443
|
-
}
|
|
444
|
-
transformSourceFile(content, filePath, visitorFactory) {
|
|
445
|
-
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
446
|
-
const transformer = (context) => {
|
|
447
|
-
return (sf) => {
|
|
448
|
-
const visitor = visitorFactory(context);
|
|
449
|
-
return ts.visitNode(sf, visitor);
|
|
450
|
-
};
|
|
451
|
-
};
|
|
452
|
-
const result = ts.transform(sourceFile, [transformer]);
|
|
453
|
-
const transformedSourceFile = result.transformed[0];
|
|
454
|
-
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
|
|
455
|
-
const transformedContent = printer.printFile(transformedSourceFile);
|
|
456
|
-
result.dispose();
|
|
457
|
-
return transformedContent;
|
|
458
|
-
}
|
|
459
|
-
extractSymbolsFromEntry(content, filePath, messageTypes, decoratorNames) {
|
|
460
|
-
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
461
|
-
const decoratorToMessageType = this.buildDecoratorToMessageTypeMap(decoratorNames);
|
|
462
|
-
const context = {
|
|
463
|
-
sourceFile,
|
|
464
|
-
content,
|
|
465
|
-
messageTypes,
|
|
466
|
-
decoratorToMessageType,
|
|
467
|
-
};
|
|
468
|
-
const { targetClassNames, targetClasses } = this.findTargetClasses(context);
|
|
469
|
-
if (targetClasses.length === 0) {
|
|
470
|
-
return { content, usedModuleSpecifiers: new Set() };
|
|
471
|
-
}
|
|
472
|
-
const relatedTypeNames = this.computeRelatedTypeNames(targetClassNames);
|
|
473
|
-
const usedIdentifiers = this.collectUsedIdentifiers(targetClasses);
|
|
474
|
-
const localTypeDeclarations = this.collectLocalTypeDeclarations(sourceFile);
|
|
475
|
-
const includedLocalTypes = this.resolveIncludedLocalTypes(usedIdentifiers, relatedTypeNames, localTypeDeclarations);
|
|
476
|
-
const extractedSymbols = {
|
|
477
|
-
targetClassNames,
|
|
478
|
-
targetClasses,
|
|
479
|
-
usedIdentifiers,
|
|
480
|
-
includedLocalTypes,
|
|
481
|
-
localTypeDeclarations,
|
|
482
|
-
};
|
|
483
|
-
return this.generateExtractedOutput(context, extractedSymbols);
|
|
484
|
-
}
|
|
485
|
-
buildDecoratorToMessageTypeMap(decoratorNames) {
|
|
486
|
-
const names = { ...domain_1.DEFAULT_DECORATOR_NAMES, ...decoratorNames };
|
|
487
|
-
return {
|
|
488
|
-
[names.event]: "event",
|
|
489
|
-
[names.command]: "command",
|
|
490
|
-
[names.query]: "query",
|
|
491
|
-
};
|
|
492
|
-
}
|
|
493
|
-
findTargetClasses(context) {
|
|
494
|
-
const targetClassNames = new Set();
|
|
495
|
-
const targetClasses = [];
|
|
496
|
-
const findClasses = (node) => {
|
|
497
|
-
if (ts.isClassDeclaration(node) && node.name) {
|
|
498
|
-
for (const [decoratorName, messageType] of Object.entries(context.decoratorToMessageType)) {
|
|
499
|
-
if ((0, class_analyzer_1.hasDecorator)(node, decoratorName) &&
|
|
500
|
-
context.messageTypes.includes(messageType)) {
|
|
501
|
-
targetClassNames.add(node.name.text);
|
|
502
|
-
targetClasses.push(node);
|
|
503
|
-
break;
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
ts.forEachChild(node, findClasses);
|
|
508
|
-
};
|
|
509
|
-
findClasses(context.sourceFile);
|
|
510
|
-
return { targetClassNames, targetClasses };
|
|
511
|
-
}
|
|
512
|
-
computeRelatedTypeNames(targetClassNames) {
|
|
513
|
-
const relatedTypeNames = new Set();
|
|
514
|
-
for (const className of targetClassNames) {
|
|
515
|
-
if (className.endsWith(REQUEST_SUFFIX)) {
|
|
516
|
-
const baseName = className.slice(0, -REQUEST_SUFFIX.length);
|
|
517
|
-
relatedTypeNames.add(baseName + "Response");
|
|
518
|
-
relatedTypeNames.add(baseName + "Payload");
|
|
519
|
-
}
|
|
520
|
-
else if (className.endsWith(QUERY_SUFFIX)) {
|
|
521
|
-
const baseName = className.slice(0, -QUERY_SUFFIX.length);
|
|
522
|
-
relatedTypeNames.add(baseName + "QueryResult");
|
|
523
|
-
relatedTypeNames.add(baseName + "Payload");
|
|
524
|
-
}
|
|
525
|
-
relatedTypeNames.add(className + "Response");
|
|
526
|
-
relatedTypeNames.add(className + "Payload");
|
|
527
|
-
}
|
|
528
|
-
return relatedTypeNames;
|
|
529
|
-
}
|
|
530
|
-
collectUsedIdentifiers(targetClasses) {
|
|
531
|
-
const usedIdentifiers = new Set();
|
|
532
|
-
const collectIdentifiers = (node) => {
|
|
533
|
-
if (ts.isTypeReferenceNode(node) &&
|
|
534
|
-
ts.isIdentifier(node.typeName)) {
|
|
535
|
-
usedIdentifiers.add(node.typeName.text);
|
|
536
|
-
}
|
|
537
|
-
if (ts.isExpressionWithTypeArguments(node) &&
|
|
538
|
-
ts.isIdentifier(node.expression)) {
|
|
539
|
-
usedIdentifiers.add(node.expression.text);
|
|
540
|
-
}
|
|
541
|
-
if (ts.isIdentifier(node) && node.parent) {
|
|
542
|
-
const isHeritageOrTypeRef = ts.isHeritageClause(node.parent.parent) ||
|
|
543
|
-
ts.isTypeReferenceNode(node.parent);
|
|
544
|
-
if (isHeritageOrTypeRef) {
|
|
545
|
-
usedIdentifiers.add(node.text);
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
if (ts.isDecorator(node)) {
|
|
549
|
-
this.collectDecoratorIdentifier(node, usedIdentifiers);
|
|
550
|
-
}
|
|
551
|
-
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
|
|
552
|
-
usedIdentifiers.add(node.expression.text);
|
|
553
|
-
}
|
|
554
|
-
ts.forEachChild(node, collectIdentifiers);
|
|
555
|
-
};
|
|
556
|
-
for (const cls of targetClasses) {
|
|
557
|
-
collectIdentifiers(cls);
|
|
558
|
-
}
|
|
559
|
-
return usedIdentifiers;
|
|
560
|
-
}
|
|
561
|
-
collectDecoratorIdentifier(decorator, usedIdentifiers) {
|
|
562
|
-
const expr = decorator.expression;
|
|
563
|
-
if (ts.isIdentifier(expr)) {
|
|
564
|
-
usedIdentifiers.add(expr.text);
|
|
565
|
-
}
|
|
566
|
-
else if (ts.isCallExpression(expr) &&
|
|
567
|
-
ts.isIdentifier(expr.expression)) {
|
|
568
|
-
usedIdentifiers.add(expr.expression.text);
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
collectLocalTypeDeclarations(sourceFile) {
|
|
572
|
-
const localTypeDeclarations = new Map();
|
|
573
|
-
const collectLocalTypes = (node) => {
|
|
574
|
-
if (ts.isInterfaceDeclaration(node) && node.name) {
|
|
575
|
-
this.addToDeclarationMap(localTypeDeclarations, node.name.text, node);
|
|
576
|
-
}
|
|
577
|
-
if (ts.isTypeAliasDeclaration(node) && node.name) {
|
|
578
|
-
this.addToDeclarationMap(localTypeDeclarations, node.name.text, node);
|
|
579
|
-
}
|
|
580
|
-
if (ts.isVariableStatement(node)) {
|
|
581
|
-
for (const decl of node.declarationList.declarations) {
|
|
582
|
-
if (ts.isIdentifier(decl.name)) {
|
|
583
|
-
this.addToDeclarationMap(localTypeDeclarations, decl.name.text, node);
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
ts.forEachChild(node, collectLocalTypes);
|
|
588
|
-
};
|
|
589
|
-
collectLocalTypes(sourceFile);
|
|
590
|
-
return localTypeDeclarations;
|
|
591
|
-
}
|
|
592
|
-
addToDeclarationMap(map, name, node) {
|
|
593
|
-
const existing = map.get(name) ?? [];
|
|
594
|
-
existing.push(node);
|
|
595
|
-
map.set(name, existing);
|
|
596
|
-
}
|
|
597
|
-
resolveIncludedLocalTypes(usedIdentifiers, relatedTypeNames, localTypeDeclarations) {
|
|
598
|
-
const includedLocalTypes = new Set();
|
|
599
|
-
const queue = [...usedIdentifiers, ...relatedTypeNames];
|
|
600
|
-
while (queue.length > 0) {
|
|
601
|
-
const identifier = queue.shift();
|
|
602
|
-
if (includedLocalTypes.has(identifier))
|
|
603
|
-
continue;
|
|
604
|
-
const typeNodes = localTypeDeclarations.get(identifier);
|
|
605
|
-
if (!typeNodes || typeNodes.length === 0)
|
|
606
|
-
continue;
|
|
607
|
-
includedLocalTypes.add(identifier);
|
|
608
|
-
const typeIdentifiers = this.collectTypeIdentifiersFromNodes(typeNodes);
|
|
609
|
-
for (const id of typeIdentifiers) {
|
|
610
|
-
if (!includedLocalTypes.has(id)) {
|
|
611
|
-
queue.push(id);
|
|
612
|
-
usedIdentifiers.add(id);
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
return includedLocalTypes;
|
|
617
|
-
}
|
|
618
|
-
collectTypeIdentifiersFromNodes(nodes) {
|
|
619
|
-
const typeIdentifiers = new Set();
|
|
620
|
-
const collectFromType = (node) => {
|
|
621
|
-
if (ts.isTypeReferenceNode(node) &&
|
|
622
|
-
ts.isIdentifier(node.typeName)) {
|
|
623
|
-
typeIdentifiers.add(node.typeName.text);
|
|
624
|
-
}
|
|
625
|
-
if (ts.isExpressionWithTypeArguments(node) &&
|
|
626
|
-
ts.isIdentifier(node.expression)) {
|
|
627
|
-
typeIdentifiers.add(node.expression.text);
|
|
628
|
-
}
|
|
629
|
-
if (ts.isComputedPropertyName(node)) {
|
|
630
|
-
this.collectComputedPropertyIdentifier(node, typeIdentifiers);
|
|
631
|
-
}
|
|
632
|
-
ts.forEachChild(node, collectFromType);
|
|
633
|
-
};
|
|
634
|
-
for (const typeNode of nodes) {
|
|
635
|
-
collectFromType(typeNode);
|
|
636
|
-
}
|
|
637
|
-
return typeIdentifiers;
|
|
638
|
-
}
|
|
639
|
-
collectComputedPropertyIdentifier(node, typeIdentifiers) {
|
|
640
|
-
const expr = node.expression;
|
|
641
|
-
if (ts.isPropertyAccessExpression(expr) &&
|
|
642
|
-
ts.isIdentifier(expr.expression)) {
|
|
643
|
-
typeIdentifiers.add(expr.expression.text);
|
|
644
|
-
}
|
|
645
|
-
if (ts.isIdentifier(expr)) {
|
|
646
|
-
typeIdentifiers.add(expr.text);
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
generateExtractedOutput(context, symbols) {
|
|
650
|
-
const output = [];
|
|
651
|
-
const importMap = this.buildImportMap(context.sourceFile);
|
|
652
|
-
const filteredImports = this.filterImports(symbols.usedIdentifiers, importMap, symbols.includedLocalTypes);
|
|
653
|
-
this.appendImportStatements(output, filteredImports);
|
|
654
|
-
this.appendLocalTypeDeclarations(output, context, symbols);
|
|
655
|
-
this.appendTargetClasses(output, context, symbols.targetClasses);
|
|
656
|
-
const usedModuleSpecifiers = new Set(filteredImports.keys());
|
|
657
|
-
return {
|
|
658
|
-
content: output.join("\n"),
|
|
659
|
-
usedModuleSpecifiers,
|
|
660
|
-
};
|
|
661
|
-
}
|
|
662
|
-
buildImportMap(sourceFile) {
|
|
663
|
-
const importMap = new Map();
|
|
664
|
-
const collectImports = (node) => {
|
|
665
|
-
if (ts.isImportDeclaration(node)) {
|
|
666
|
-
const moduleSpecifier = node.moduleSpecifier.text;
|
|
667
|
-
const namedBindings = node.importClause?.namedBindings;
|
|
668
|
-
const isTypeOnly = node.importClause?.isTypeOnly ?? false;
|
|
669
|
-
if (namedBindings && ts.isNamedImports(namedBindings)) {
|
|
670
|
-
for (const element of namedBindings.elements) {
|
|
671
|
-
importMap.set(element.name.text, {
|
|
672
|
-
moduleSpecifier,
|
|
673
|
-
isTypeOnly,
|
|
674
|
-
});
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
ts.forEachChild(node, collectImports);
|
|
679
|
-
};
|
|
680
|
-
collectImports(sourceFile);
|
|
681
|
-
return importMap;
|
|
682
|
-
}
|
|
683
|
-
filterImports(usedIdentifiers, importMap, includedLocalTypes) {
|
|
684
|
-
const filteredImports = new Map();
|
|
685
|
-
for (const identifier of usedIdentifiers) {
|
|
686
|
-
const importInfo = importMap.get(identifier);
|
|
687
|
-
if (!importInfo || includedLocalTypes.has(identifier)) {
|
|
688
|
-
continue;
|
|
689
|
-
}
|
|
690
|
-
const existing = filteredImports.get(importInfo.moduleSpecifier);
|
|
691
|
-
if (existing) {
|
|
692
|
-
existing.identifiers.add(identifier);
|
|
693
|
-
}
|
|
694
|
-
else {
|
|
695
|
-
filteredImports.set(importInfo.moduleSpecifier, {
|
|
696
|
-
identifiers: new Set([identifier]),
|
|
697
|
-
isTypeOnly: importInfo.isTypeOnly,
|
|
698
|
-
});
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
return filteredImports;
|
|
702
|
-
}
|
|
703
|
-
appendImportStatements(output, filteredImports) {
|
|
704
|
-
for (const [moduleSpecifier, info] of filteredImports) {
|
|
705
|
-
const identifiers = [...info.identifiers].sort().join(", ");
|
|
706
|
-
const typeOnlyPrefix = info.isTypeOnly ? "type " : "";
|
|
707
|
-
output.push(`import ${typeOnlyPrefix}{ ${identifiers} } from "${moduleSpecifier}";`);
|
|
708
|
-
}
|
|
709
|
-
if (output.length > 0) {
|
|
710
|
-
output.push("");
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
appendLocalTypeDeclarations(output, context, symbols) {
|
|
714
|
-
const outputNodes = new Set();
|
|
715
|
-
for (const typeName of symbols.includedLocalTypes) {
|
|
716
|
-
const typeNodes = symbols.localTypeDeclarations.get(typeName);
|
|
717
|
-
if (!typeNodes)
|
|
718
|
-
continue;
|
|
719
|
-
for (const typeNode of typeNodes) {
|
|
720
|
-
if (outputNodes.has(typeNode))
|
|
721
|
-
continue;
|
|
722
|
-
outputNodes.add(typeNode);
|
|
723
|
-
this.appendNodeWithExport(output, context, typeNode);
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
appendTargetClasses(output, context, targetClasses) {
|
|
728
|
-
for (const cls of targetClasses) {
|
|
729
|
-
this.appendNodeWithExport(output, context, cls);
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
appendNodeWithExport(output, context, node) {
|
|
733
|
-
const nodeText = context.content
|
|
734
|
-
.substring(node.getStart(context.sourceFile), node.end)
|
|
735
|
-
.trim();
|
|
736
|
-
const hasExport = this.nodeHasExportKeyword(node);
|
|
737
|
-
if (hasExport) {
|
|
738
|
-
output.push(nodeText);
|
|
739
|
-
}
|
|
740
|
-
else {
|
|
741
|
-
output.push("export " + nodeText);
|
|
742
|
-
}
|
|
743
|
-
output.push("");
|
|
744
|
-
}
|
|
745
|
-
nodeHasExportKeyword(node) {
|
|
746
|
-
const isExportableDeclaration = ts.isInterfaceDeclaration(node) ||
|
|
747
|
-
ts.isTypeAliasDeclaration(node) ||
|
|
748
|
-
ts.isVariableStatement(node) ||
|
|
749
|
-
ts.isClassDeclaration(node);
|
|
750
|
-
if (!isExportableDeclaration) {
|
|
751
|
-
return false;
|
|
752
|
-
}
|
|
753
|
-
const declarationNode = node;
|
|
754
|
-
return (declarationNode.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) ?? false);
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
exports.FileCopier = FileCopier;
|
|
758
|
-
//# sourceMappingURL=file-copier.js.map
|