@ssets/dts 1.0.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 +88 -0
- package/dist/bundle-generator.cjs +2836 -0
- package/dist/bundle-generator.d.cts +81 -0
- package/dist/bundle-generator.d.ts +81 -0
- package/dist/bundle-generator.js +2801 -0
- package/package.json +32 -0
|
@@ -0,0 +1,2836 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/bundle-generator.ts
|
|
31
|
+
var bundle_generator_exports = {};
|
|
32
|
+
__export(bundle_generator_exports, {
|
|
33
|
+
generateDtsBundle: () => generateDtsBundle
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(bundle_generator_exports);
|
|
36
|
+
var ts9 = __toESM(require("typescript"), 1);
|
|
37
|
+
|
|
38
|
+
// src/compile-dts.ts
|
|
39
|
+
var ts3 = __toESM(require("typescript"), 1);
|
|
40
|
+
|
|
41
|
+
// src/logger.ts
|
|
42
|
+
function verboseLog(message) {
|
|
43
|
+
logMessage(message, 0 /* Verbose */);
|
|
44
|
+
}
|
|
45
|
+
function normalLog(message) {
|
|
46
|
+
logMessage(message, 1 /* Normal */);
|
|
47
|
+
}
|
|
48
|
+
function warnLog(message) {
|
|
49
|
+
logMessage(message, 2 /* Warning */);
|
|
50
|
+
}
|
|
51
|
+
function errorLog(message) {
|
|
52
|
+
logMessage(message, 3 /* Error */);
|
|
53
|
+
}
|
|
54
|
+
var currentLogLevel = 3 /* Error */;
|
|
55
|
+
function logMessage(message, level = 0 /* Verbose */) {
|
|
56
|
+
if (level < currentLogLevel) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
switch (level) {
|
|
60
|
+
case 3 /* Error */:
|
|
61
|
+
console.error(`\x1B[0;31m${message}\x1B[0m`);
|
|
62
|
+
break;
|
|
63
|
+
case 2 /* Warning */:
|
|
64
|
+
console.warn(`\x1B[1;33m${message}\x1B[0m`);
|
|
65
|
+
break;
|
|
66
|
+
case 1 /* Normal */:
|
|
67
|
+
case 0 /* Verbose */:
|
|
68
|
+
console.log(message);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/get-compiler-options.ts
|
|
73
|
+
var ts2 = __toESM(require("typescript"), 1);
|
|
74
|
+
var path2 = __toESM(require("path"), 1);
|
|
75
|
+
|
|
76
|
+
// src/helpers/get-absolute-path.ts
|
|
77
|
+
var path = __toESM(require("path"), 1);
|
|
78
|
+
var process2 = __toESM(require("process"), 1);
|
|
79
|
+
|
|
80
|
+
// src/helpers/fix-path.ts
|
|
81
|
+
function fixPath(path5) {
|
|
82
|
+
return path5.replace(/\\/g, "/");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/helpers/get-absolute-path.ts
|
|
86
|
+
function getAbsolutePath(fileName, cwd2) {
|
|
87
|
+
if (!path.isAbsolute(fileName)) {
|
|
88
|
+
fileName = path.join(cwd2 !== void 0 ? cwd2 : process2.cwd(), fileName);
|
|
89
|
+
}
|
|
90
|
+
return fixPath(fileName);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/helpers/check-diagnostics-errors.ts
|
|
94
|
+
var ts = __toESM(require("typescript"), 1);
|
|
95
|
+
var formatDiagnosticsHost = {
|
|
96
|
+
getCanonicalFileName: (fileName) => ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(),
|
|
97
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
98
|
+
getNewLine: () => ts.sys.newLine
|
|
99
|
+
};
|
|
100
|
+
function checkProgramDiagnosticsErrors(program) {
|
|
101
|
+
if (!program.getCompilerOptions().declaration) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Something went wrong - the program doesn't have declaration option enabled`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
checkDiagnosticsErrors(
|
|
107
|
+
ts.getPreEmitDiagnostics(program),
|
|
108
|
+
"Compiled with errors"
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
function checkDiagnosticsErrors(diagnostics, failMessage) {
|
|
112
|
+
if (diagnostics.length === 0) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
errorLog(ts.formatDiagnostics(diagnostics, formatDiagnosticsHost).trim());
|
|
116
|
+
throw new Error(failMessage);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/get-compiler-options.ts
|
|
120
|
+
var parseConfigHost = {
|
|
121
|
+
useCaseSensitiveFileNames: ts2.sys.useCaseSensitiveFileNames,
|
|
122
|
+
readDirectory: ts2.sys.readDirectory,
|
|
123
|
+
fileExists: ts2.sys.fileExists,
|
|
124
|
+
readFile: ts2.sys.readFile
|
|
125
|
+
};
|
|
126
|
+
function getCompilerOptions(inputFileNames, preferredConfigPath) {
|
|
127
|
+
const configFileName = preferredConfigPath !== void 0 ? preferredConfigPath : findConfig(inputFileNames);
|
|
128
|
+
verboseLog(`Using config: ${configFileName}`);
|
|
129
|
+
const configParseResult = ts2.readConfigFile(configFileName, ts2.sys.readFile);
|
|
130
|
+
checkDiagnosticsErrors(
|
|
131
|
+
configParseResult.error !== void 0 ? [configParseResult.error] : [],
|
|
132
|
+
"Error while processing tsconfig file"
|
|
133
|
+
);
|
|
134
|
+
const compilerOptionsParseResult = ts2.parseJsonConfigFileContent(
|
|
135
|
+
configParseResult.config,
|
|
136
|
+
parseConfigHost,
|
|
137
|
+
path2.resolve(path2.dirname(configFileName)),
|
|
138
|
+
void 0,
|
|
139
|
+
getAbsolutePath(configFileName)
|
|
140
|
+
);
|
|
141
|
+
const diagnostics = compilerOptionsParseResult.errors.filter(
|
|
142
|
+
(d) => d.code !== 18003 /* NoInputsWereFoundDiagnosticCode */
|
|
143
|
+
);
|
|
144
|
+
checkDiagnosticsErrors(
|
|
145
|
+
diagnostics,
|
|
146
|
+
"Error while processing tsconfig compiler options"
|
|
147
|
+
);
|
|
148
|
+
return compilerOptionsParseResult.options;
|
|
149
|
+
}
|
|
150
|
+
function findConfig(inputFiles) {
|
|
151
|
+
if (inputFiles.length !== 1) {
|
|
152
|
+
throw new Error(
|
|
153
|
+
"Cannot find tsconfig for multiple files. Please specify preferred tsconfig file"
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
const searchPath = getAbsolutePath(inputFiles[0]);
|
|
157
|
+
const configFileName = ts2.findConfigFile(searchPath, ts2.sys.fileExists);
|
|
158
|
+
if (!configFileName) {
|
|
159
|
+
throw new Error(`Cannot find config file for file ${searchPath}`);
|
|
160
|
+
}
|
|
161
|
+
return configFileName;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// src/compile-dts.ts
|
|
165
|
+
var declarationExtsRemapping = {
|
|
166
|
+
[ts3.Extension.Js]: ts3.Extension.Js,
|
|
167
|
+
[ts3.Extension.Jsx]: ts3.Extension.Jsx,
|
|
168
|
+
[ts3.Extension.Json]: ts3.Extension.Json,
|
|
169
|
+
[ts3.Extension.TsBuildInfo]: ts3.Extension.TsBuildInfo,
|
|
170
|
+
[ts3.Extension.Mjs]: ts3.Extension.Mjs,
|
|
171
|
+
[ts3.Extension.Cjs]: ts3.Extension.Cjs,
|
|
172
|
+
[ts3.Extension.Ts]: ts3.Extension.Dts,
|
|
173
|
+
[ts3.Extension.Tsx]: ts3.Extension.Dts,
|
|
174
|
+
[ts3.Extension.Dts]: ts3.Extension.Dts,
|
|
175
|
+
[ts3.Extension.Mts]: ts3.Extension.Dmts,
|
|
176
|
+
[ts3.Extension.Dmts]: ts3.Extension.Dmts,
|
|
177
|
+
[ts3.Extension.Cts]: ts3.Extension.Dcts,
|
|
178
|
+
[ts3.Extension.Dcts]: ts3.Extension.Dcts
|
|
179
|
+
};
|
|
180
|
+
function compileDts(rootFiles, preferredConfigPath, followSymlinks = true) {
|
|
181
|
+
const compilerOptions = getCompilerOptions(rootFiles, preferredConfigPath);
|
|
182
|
+
compilerOptions.outDir = void 0;
|
|
183
|
+
compilerOptions.incremental = void 0;
|
|
184
|
+
compilerOptions.tsBuildInfoFile = void 0;
|
|
185
|
+
compilerOptions.declarationDir = void 0;
|
|
186
|
+
compilerOptions.declaration = true;
|
|
187
|
+
if (compilerOptions.composite) {
|
|
188
|
+
warnLog(
|
|
189
|
+
`Composite projects aren't supported at the time. Prefer to use non-composite project to generate declarations instead or just ignore this message if everything works fine. See https://github.com/timocov/dts-bundle-generator/issues/93`
|
|
190
|
+
);
|
|
191
|
+
compilerOptions.composite = void 0;
|
|
192
|
+
}
|
|
193
|
+
const host = createCachingCompilerHost(compilerOptions);
|
|
194
|
+
const dtsFiles = getDeclarationFiles(rootFiles, compilerOptions, host);
|
|
195
|
+
if (!followSymlinks) {
|
|
196
|
+
host.realpath = (p) => p;
|
|
197
|
+
}
|
|
198
|
+
const moduleResolutionCache = ts3.createModuleResolutionCache(
|
|
199
|
+
host.getCurrentDirectory(),
|
|
200
|
+
host.getCanonicalFileName,
|
|
201
|
+
compilerOptions
|
|
202
|
+
);
|
|
203
|
+
host.resolveModuleNameLiterals = (moduleLiterals, containingFile) => {
|
|
204
|
+
return moduleLiterals.map(
|
|
205
|
+
(moduleLiteral) => {
|
|
206
|
+
const resolvedModule = ts3.resolveModuleName(
|
|
207
|
+
moduleLiteral.text,
|
|
208
|
+
containingFile,
|
|
209
|
+
compilerOptions,
|
|
210
|
+
host,
|
|
211
|
+
moduleResolutionCache
|
|
212
|
+
).resolvedModule;
|
|
213
|
+
if (resolvedModule && !resolvedModule.isExternalLibraryImport) {
|
|
214
|
+
const newExt = declarationExtsRemapping[resolvedModule.extension];
|
|
215
|
+
if (newExt === void 0) {
|
|
216
|
+
verboseLog(
|
|
217
|
+
`Skipping module ${resolvedModule.resolvedFileName} because it has unsupported extension "${resolvedModule.extension}"`
|
|
218
|
+
);
|
|
219
|
+
return { resolvedModule };
|
|
220
|
+
}
|
|
221
|
+
if (newExt !== resolvedModule.extension) {
|
|
222
|
+
verboseLog(
|
|
223
|
+
`Changing module from ${resolvedModule.extension} to ${newExt} for ${resolvedModule.resolvedFileName}`
|
|
224
|
+
);
|
|
225
|
+
resolvedModule.extension = newExt;
|
|
226
|
+
resolvedModule.resolvedFileName = changeExtensionToDts(
|
|
227
|
+
resolvedModule.resolvedFileName
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return { resolvedModule };
|
|
232
|
+
}
|
|
233
|
+
);
|
|
234
|
+
};
|
|
235
|
+
const originalGetSourceFile = host.getSourceFile;
|
|
236
|
+
host.getSourceFile = (fileName, languageVersion, onError) => {
|
|
237
|
+
const storedValue = dtsFiles.get(host.getCanonicalFileName(fileName));
|
|
238
|
+
if (storedValue !== void 0) {
|
|
239
|
+
return ts3.createSourceFile(fileName, storedValue, languageVersion);
|
|
240
|
+
}
|
|
241
|
+
return originalGetSourceFile(fileName, languageVersion, onError);
|
|
242
|
+
};
|
|
243
|
+
const rootFilesRemapping = /* @__PURE__ */ new Map();
|
|
244
|
+
const inputFiles = rootFiles.map((rootFile) => {
|
|
245
|
+
const rootDtsFile = changeExtensionToDts(rootFile);
|
|
246
|
+
rootFilesRemapping.set(rootFile, rootDtsFile);
|
|
247
|
+
return rootDtsFile;
|
|
248
|
+
});
|
|
249
|
+
const program = ts3.createProgram(inputFiles, compilerOptions, host);
|
|
250
|
+
checkProgramDiagnosticsErrors(program);
|
|
251
|
+
warnAboutTypeScriptFilesInProgram(program);
|
|
252
|
+
return { program, rootFilesRemapping };
|
|
253
|
+
}
|
|
254
|
+
function createCachingCompilerHost(compilerOptions) {
|
|
255
|
+
const host = ts3.createIncrementalCompilerHost(compilerOptions);
|
|
256
|
+
const sourceFilesCache = /* @__PURE__ */ new Map();
|
|
257
|
+
const originalGetSourceFile = host.getSourceFile;
|
|
258
|
+
host.getSourceFile = (fileName, languageVersion, onError) => {
|
|
259
|
+
const key = host.getCanonicalFileName(fileName);
|
|
260
|
+
let cacheValue = sourceFilesCache.get(key);
|
|
261
|
+
if (cacheValue === void 0) {
|
|
262
|
+
cacheValue = originalGetSourceFile(fileName, languageVersion, onError);
|
|
263
|
+
sourceFilesCache.set(key, cacheValue);
|
|
264
|
+
}
|
|
265
|
+
return cacheValue;
|
|
266
|
+
};
|
|
267
|
+
return host;
|
|
268
|
+
}
|
|
269
|
+
function changeExtensionToDts(fileName) {
|
|
270
|
+
let ext;
|
|
271
|
+
if (fileName.endsWith(ts3.Extension.Dts)) {
|
|
272
|
+
return fileName;
|
|
273
|
+
}
|
|
274
|
+
if (fileName.endsWith(ts3.Extension.Cts)) {
|
|
275
|
+
ext = ts3.Extension.Cts;
|
|
276
|
+
} else if (fileName.endsWith(ts3.Extension.Mts)) {
|
|
277
|
+
ext = ts3.Extension.Mts;
|
|
278
|
+
} else if (fileName.endsWith(ts3.Extension.Ts)) {
|
|
279
|
+
ext = ts3.Extension.Ts;
|
|
280
|
+
} else if (fileName.endsWith(ts3.Extension.Tsx)) {
|
|
281
|
+
ext = ts3.Extension.Tsx;
|
|
282
|
+
}
|
|
283
|
+
if (ext === void 0) {
|
|
284
|
+
return fileName;
|
|
285
|
+
}
|
|
286
|
+
return fileName.slice(0, -ext.length) + declarationExtsRemapping[ext];
|
|
287
|
+
}
|
|
288
|
+
function getDeclarationFiles(rootFiles, compilerOptions, host) {
|
|
289
|
+
compilerOptions = {
|
|
290
|
+
...compilerOptions,
|
|
291
|
+
noEmit: false,
|
|
292
|
+
declaration: true,
|
|
293
|
+
emitDeclarationOnly: true
|
|
294
|
+
};
|
|
295
|
+
const program = ts3.createProgram(rootFiles, compilerOptions, host);
|
|
296
|
+
const allFilesAreDeclarations = program.getSourceFiles().every((s) => s.isDeclarationFile);
|
|
297
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
298
|
+
if (allFilesAreDeclarations) {
|
|
299
|
+
verboseLog(
|
|
300
|
+
"Skipping compiling the project to generate d.ts because all files in it are d.ts already"
|
|
301
|
+
);
|
|
302
|
+
return declarations;
|
|
303
|
+
}
|
|
304
|
+
checkProgramDiagnosticsErrors(program);
|
|
305
|
+
const emitResult = program.emit(
|
|
306
|
+
void 0,
|
|
307
|
+
(fileName, data) => declarations.set(host.getCanonicalFileName(fileName), data),
|
|
308
|
+
void 0,
|
|
309
|
+
true
|
|
310
|
+
);
|
|
311
|
+
checkDiagnosticsErrors(
|
|
312
|
+
emitResult.diagnostics,
|
|
313
|
+
"Errors while emitting declarations"
|
|
314
|
+
);
|
|
315
|
+
return declarations;
|
|
316
|
+
}
|
|
317
|
+
function warnAboutTypeScriptFilesInProgram(program) {
|
|
318
|
+
const nonDeclarationFiles = program.getSourceFiles().filter((file) => !file.isDeclarationFile);
|
|
319
|
+
if (nonDeclarationFiles.length !== 0) {
|
|
320
|
+
warnLog(`WARNING: It seems that some files in the compilation still are not declaration files.
|
|
321
|
+
For more information see https://github.com/timocov/dts-bundle-generator/issues/53.
|
|
322
|
+
If you think this is a mistake, feel free to open new issue or just ignore this warning.
|
|
323
|
+
${nonDeclarationFiles.map((file) => file.fileName).join("\n ")}
|
|
324
|
+
`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// src/types-usage-evaluator.ts
|
|
329
|
+
var ts5 = __toESM(require("typescript"), 1);
|
|
330
|
+
|
|
331
|
+
// src/helpers/typescript.ts
|
|
332
|
+
var ts4 = __toESM(require("typescript"), 1);
|
|
333
|
+
var namedDeclarationKinds = [
|
|
334
|
+
ts4.SyntaxKind.InterfaceDeclaration,
|
|
335
|
+
ts4.SyntaxKind.ClassDeclaration,
|
|
336
|
+
ts4.SyntaxKind.EnumDeclaration,
|
|
337
|
+
ts4.SyntaxKind.TypeAliasDeclaration,
|
|
338
|
+
ts4.SyntaxKind.ModuleDeclaration,
|
|
339
|
+
ts4.SyntaxKind.FunctionDeclaration,
|
|
340
|
+
ts4.SyntaxKind.VariableDeclaration,
|
|
341
|
+
ts4.SyntaxKind.PropertySignature,
|
|
342
|
+
ts4.SyntaxKind.NamespaceExport,
|
|
343
|
+
ts4.SyntaxKind.NamespaceImport,
|
|
344
|
+
ts4.SyntaxKind.ExportSpecifier,
|
|
345
|
+
ts4.SyntaxKind.BindingElement
|
|
346
|
+
];
|
|
347
|
+
function isNodeNamedDeclaration(node) {
|
|
348
|
+
return namedDeclarationKinds.indexOf(node.kind) !== -1;
|
|
349
|
+
}
|
|
350
|
+
function hasNodeModifier(node, modifier) {
|
|
351
|
+
const modifiers = getModifiers2(node);
|
|
352
|
+
return Boolean(
|
|
353
|
+
modifiers && modifiers.some(
|
|
354
|
+
(nodeModifier) => nodeModifier.kind === modifier
|
|
355
|
+
)
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
function getNodeName(node) {
|
|
359
|
+
const nodeName = node.name;
|
|
360
|
+
if (nodeName === void 0) {
|
|
361
|
+
const modifiers = getModifiers2(node);
|
|
362
|
+
const defaultModifier = modifiers?.find(
|
|
363
|
+
(mod) => mod.kind === ts4.SyntaxKind.DefaultKeyword
|
|
364
|
+
);
|
|
365
|
+
if (defaultModifier !== void 0) {
|
|
366
|
+
return defaultModifier;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return nodeName;
|
|
370
|
+
}
|
|
371
|
+
function getActualSymbol(symbol, typeChecker) {
|
|
372
|
+
if (symbol.flags & ts4.SymbolFlags.Alias) {
|
|
373
|
+
symbol = typeChecker.getAliasedSymbol(symbol);
|
|
374
|
+
}
|
|
375
|
+
return typeChecker.getMergedSymbol(symbol);
|
|
376
|
+
}
|
|
377
|
+
function getDeclarationNameSymbol(name, typeChecker) {
|
|
378
|
+
const symbol = typeChecker.getSymbolAtLocation(name);
|
|
379
|
+
if (symbol === void 0) {
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
return getActualSymbol(symbol, typeChecker);
|
|
383
|
+
}
|
|
384
|
+
function splitTransientSymbol(symbol, typeChecker) {
|
|
385
|
+
if ((symbol.flags & ts4.SymbolFlags.Transient) === 0) {
|
|
386
|
+
return /* @__PURE__ */ new Set([symbol]);
|
|
387
|
+
}
|
|
388
|
+
const declarations = getDeclarationsForSymbol(symbol);
|
|
389
|
+
const result = /* @__PURE__ */ new Set();
|
|
390
|
+
for (const declaration of declarations) {
|
|
391
|
+
if (!isNodeNamedDeclaration(declaration) || declaration.name === void 0) {
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
const sym = typeChecker.getSymbolAtLocation(declaration.name);
|
|
395
|
+
if (sym === void 0) {
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
result.add(getActualSymbol(sym, typeChecker));
|
|
399
|
+
}
|
|
400
|
+
return result;
|
|
401
|
+
}
|
|
402
|
+
function isGlobalScopeAugmentation(module2) {
|
|
403
|
+
return Boolean(module2.flags & ts4.NodeFlags.GlobalAugmentation);
|
|
404
|
+
}
|
|
405
|
+
function isAmbientModule(node) {
|
|
406
|
+
return ts4.isModuleDeclaration(node) && (node.name.kind === ts4.SyntaxKind.StringLiteral || isGlobalScopeAugmentation(node));
|
|
407
|
+
}
|
|
408
|
+
function isDeclareModule(node) {
|
|
409
|
+
return ts4.isModuleDeclaration(node) && !(node.flags & ts4.NodeFlags.Namespace) && !isGlobalScopeAugmentation(node);
|
|
410
|
+
}
|
|
411
|
+
function isDeclareGlobalStatement(statement) {
|
|
412
|
+
return ts4.isModuleDeclaration(statement) && isGlobalScopeAugmentation(statement);
|
|
413
|
+
}
|
|
414
|
+
function getDeclarationsForSymbol(symbol) {
|
|
415
|
+
const result = [];
|
|
416
|
+
if (symbol.declarations !== void 0) {
|
|
417
|
+
result.push(...symbol.declarations);
|
|
418
|
+
}
|
|
419
|
+
if (symbol.valueDeclaration !== void 0) {
|
|
420
|
+
if (!result.includes(symbol.valueDeclaration)) {
|
|
421
|
+
result.push(symbol.valueDeclaration);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return result;
|
|
425
|
+
}
|
|
426
|
+
function getExportsForSourceFile(typeChecker, sourceFileSymbol) {
|
|
427
|
+
if (sourceFileSymbol.exports !== void 0) {
|
|
428
|
+
const commonJsExport = sourceFileSymbol.exports.get(
|
|
429
|
+
ts4.InternalSymbolName.ExportEquals
|
|
430
|
+
);
|
|
431
|
+
if (commonJsExport !== void 0) {
|
|
432
|
+
const symbol = getActualSymbol(commonJsExport, typeChecker);
|
|
433
|
+
return [
|
|
434
|
+
{
|
|
435
|
+
symbol,
|
|
436
|
+
originalSymbol: commonJsExport,
|
|
437
|
+
type: 0 /* CommonJS */,
|
|
438
|
+
exportedName: ""
|
|
439
|
+
}
|
|
440
|
+
];
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
const result = typeChecker.getExportsOfModule(sourceFileSymbol).map((symbol) => ({
|
|
444
|
+
symbol,
|
|
445
|
+
originalSymbol: symbol,
|
|
446
|
+
exportedName: symbol.name,
|
|
447
|
+
type: 1 /* ES6Named */
|
|
448
|
+
}));
|
|
449
|
+
if (sourceFileSymbol.exports !== void 0) {
|
|
450
|
+
const defaultExportSymbol = sourceFileSymbol.exports.get(
|
|
451
|
+
ts4.InternalSymbolName.Default
|
|
452
|
+
);
|
|
453
|
+
if (defaultExportSymbol !== void 0) {
|
|
454
|
+
const defaultExport = result.find(
|
|
455
|
+
(exp) => exp.symbol === defaultExportSymbol
|
|
456
|
+
);
|
|
457
|
+
if (defaultExport !== void 0) {
|
|
458
|
+
defaultExport.type = 2 /* ES6Default */;
|
|
459
|
+
} else {
|
|
460
|
+
result.push({
|
|
461
|
+
symbol: defaultExportSymbol,
|
|
462
|
+
originalSymbol: defaultExportSymbol,
|
|
463
|
+
type: 2 /* ES6Default */,
|
|
464
|
+
exportedName: "default"
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
const symbolsMergingResolvedExports = [];
|
|
470
|
+
result.forEach((exp) => {
|
|
471
|
+
exp.symbol = getActualSymbol(exp.symbol, typeChecker);
|
|
472
|
+
const symbolsDeclarations = getDeclarationsForSymbol(exp.symbol);
|
|
473
|
+
const importSpecifierDeclaration = symbolsDeclarations.find(
|
|
474
|
+
ts4.isImportSpecifier
|
|
475
|
+
);
|
|
476
|
+
if (symbolsDeclarations.length > 1 && importSpecifierDeclaration !== void 0) {
|
|
477
|
+
const referencedModule = resolveReferencedModule(
|
|
478
|
+
importSpecifierDeclaration.parent.parent.parent,
|
|
479
|
+
typeChecker
|
|
480
|
+
);
|
|
481
|
+
if (referencedModule !== null) {
|
|
482
|
+
const referencedModuleSymbol = getNodeSymbol(
|
|
483
|
+
referencedModule,
|
|
484
|
+
typeChecker
|
|
485
|
+
);
|
|
486
|
+
if (referencedModuleSymbol !== null) {
|
|
487
|
+
const importedName = (importSpecifierDeclaration.propertyName ?? importSpecifierDeclaration.name).getText();
|
|
488
|
+
const exportedItemSymbol = typeChecker.getExportsOfModule(referencedModuleSymbol).find(
|
|
489
|
+
(exportSymbol) => exportSymbol.getName() === importedName
|
|
490
|
+
);
|
|
491
|
+
if (exportedItemSymbol !== void 0) {
|
|
492
|
+
symbolsMergingResolvedExports.push({
|
|
493
|
+
...exp,
|
|
494
|
+
symbol: getActualSymbol(exportedItemSymbol, typeChecker)
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
return [...result, ...symbolsMergingResolvedExports];
|
|
502
|
+
}
|
|
503
|
+
function getExportsForStatement(exportedSymbols, typeChecker, statement) {
|
|
504
|
+
if (ts4.isVariableStatement(statement)) {
|
|
505
|
+
if (statement.declarationList.declarations.length === 0) {
|
|
506
|
+
return [];
|
|
507
|
+
}
|
|
508
|
+
const firstDeclarationExports = getExportsForName(
|
|
509
|
+
exportedSymbols,
|
|
510
|
+
typeChecker,
|
|
511
|
+
statement.declarationList.declarations[0].name
|
|
512
|
+
);
|
|
513
|
+
const allDeclarationsHaveSameExportType = statement.declarationList.declarations.every(
|
|
514
|
+
(variableDecl) => {
|
|
515
|
+
return getExportsForName(
|
|
516
|
+
exportedSymbols,
|
|
517
|
+
typeChecker,
|
|
518
|
+
variableDecl.name
|
|
519
|
+
)[0]?.type === firstDeclarationExports[0]?.type;
|
|
520
|
+
}
|
|
521
|
+
);
|
|
522
|
+
if (!allDeclarationsHaveSameExportType) {
|
|
523
|
+
return [];
|
|
524
|
+
}
|
|
525
|
+
return firstDeclarationExports;
|
|
526
|
+
}
|
|
527
|
+
const nodeName = getNodeName(statement);
|
|
528
|
+
if (nodeName === void 0) {
|
|
529
|
+
return [];
|
|
530
|
+
}
|
|
531
|
+
return getExportsForName(exportedSymbols, typeChecker, nodeName);
|
|
532
|
+
}
|
|
533
|
+
function getExportsForName(exportedSymbols, typeChecker, name) {
|
|
534
|
+
if (ts4.isArrayBindingPattern(name) || ts4.isObjectBindingPattern(name)) {
|
|
535
|
+
return [];
|
|
536
|
+
}
|
|
537
|
+
const declarationSymbol = typeChecker.getSymbolAtLocation(name);
|
|
538
|
+
return exportedSymbols.filter(
|
|
539
|
+
(rootExport) => rootExport.symbol === declarationSymbol
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
var modifiersPriority = {
|
|
543
|
+
[ts4.SyntaxKind.ExportKeyword]: 4,
|
|
544
|
+
[ts4.SyntaxKind.DefaultKeyword]: 3,
|
|
545
|
+
[ts4.SyntaxKind.DeclareKeyword]: 2,
|
|
546
|
+
[ts4.SyntaxKind.AsyncKeyword]: 1,
|
|
547
|
+
[ts4.SyntaxKind.ConstKeyword]: 1
|
|
548
|
+
};
|
|
549
|
+
function modifiersToMap(modifiers) {
|
|
550
|
+
modifiers = modifiers || [];
|
|
551
|
+
return modifiers.reduce(
|
|
552
|
+
(result, modifier) => {
|
|
553
|
+
result[modifier.kind] = true;
|
|
554
|
+
return result;
|
|
555
|
+
},
|
|
556
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
557
|
+
{}
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
function modifiersMapToArray(modifiersMap) {
|
|
561
|
+
return Object.entries(modifiersMap).filter(([kind, include]) => include).map(([kind]) => {
|
|
562
|
+
return ts4.factory.createModifier(Number(kind));
|
|
563
|
+
}).sort((a, b) => {
|
|
564
|
+
const aValue = modifiersPriority[a.kind] || 0;
|
|
565
|
+
const bValue = modifiersPriority[b.kind] || 0;
|
|
566
|
+
return bValue - aValue;
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
function recreateRootLevelNodeWithModifiers(node, modifiersMap, newName, keepComments = true) {
|
|
570
|
+
const newNode = recreateRootLevelNodeWithModifiersImpl(
|
|
571
|
+
node,
|
|
572
|
+
modifiersMap,
|
|
573
|
+
newName
|
|
574
|
+
);
|
|
575
|
+
if (keepComments) {
|
|
576
|
+
ts4.setCommentRange(newNode, ts4.getCommentRange(node));
|
|
577
|
+
}
|
|
578
|
+
return newNode;
|
|
579
|
+
}
|
|
580
|
+
function recreateRootLevelNodeWithModifiersImpl(node, modifiersMap, newName) {
|
|
581
|
+
const modifiers = modifiersMapToArray(modifiersMap);
|
|
582
|
+
if (ts4.isArrowFunction(node)) {
|
|
583
|
+
return ts4.factory.createArrowFunction(
|
|
584
|
+
modifiers,
|
|
585
|
+
node.typeParameters,
|
|
586
|
+
node.parameters,
|
|
587
|
+
node.type,
|
|
588
|
+
node.equalsGreaterThanToken,
|
|
589
|
+
node.body
|
|
590
|
+
);
|
|
591
|
+
}
|
|
592
|
+
if (ts4.isClassDeclaration(node)) {
|
|
593
|
+
return ts4.factory.createClassDeclaration(
|
|
594
|
+
modifiers,
|
|
595
|
+
newName || node.name,
|
|
596
|
+
node.typeParameters,
|
|
597
|
+
node.heritageClauses,
|
|
598
|
+
node.members
|
|
599
|
+
);
|
|
600
|
+
}
|
|
601
|
+
if (ts4.isClassExpression(node)) {
|
|
602
|
+
return ts4.factory.createClassExpression(
|
|
603
|
+
modifiers,
|
|
604
|
+
newName || node.name,
|
|
605
|
+
node.typeParameters,
|
|
606
|
+
node.heritageClauses,
|
|
607
|
+
node.members
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
if (ts4.isEnumDeclaration(node)) {
|
|
611
|
+
return ts4.factory.createEnumDeclaration(
|
|
612
|
+
modifiers,
|
|
613
|
+
newName || node.name,
|
|
614
|
+
node.members
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
if (ts4.isExportAssignment(node)) {
|
|
618
|
+
return ts4.factory.createExportAssignment(
|
|
619
|
+
modifiers,
|
|
620
|
+
node.isExportEquals,
|
|
621
|
+
node.expression
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
if (ts4.isExportDeclaration(node)) {
|
|
625
|
+
return ts4.factory.createExportDeclaration(
|
|
626
|
+
modifiers,
|
|
627
|
+
node.isTypeOnly,
|
|
628
|
+
node.exportClause,
|
|
629
|
+
node.moduleSpecifier,
|
|
630
|
+
// eslint-disable-next-line deprecation/deprecation
|
|
631
|
+
node.attributes || node.assertClause
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
if (ts4.isFunctionDeclaration(node)) {
|
|
635
|
+
return ts4.factory.createFunctionDeclaration(
|
|
636
|
+
modifiers,
|
|
637
|
+
node.asteriskToken,
|
|
638
|
+
newName || node.name,
|
|
639
|
+
node.typeParameters,
|
|
640
|
+
node.parameters,
|
|
641
|
+
node.type,
|
|
642
|
+
node.body
|
|
643
|
+
);
|
|
644
|
+
}
|
|
645
|
+
if (ts4.isFunctionExpression(node)) {
|
|
646
|
+
return ts4.factory.createFunctionExpression(
|
|
647
|
+
modifiers,
|
|
648
|
+
node.asteriskToken,
|
|
649
|
+
newName || node.name,
|
|
650
|
+
node.typeParameters,
|
|
651
|
+
node.parameters,
|
|
652
|
+
node.type,
|
|
653
|
+
node.body
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
if (ts4.isImportDeclaration(node)) {
|
|
657
|
+
return ts4.factory.createImportDeclaration(
|
|
658
|
+
modifiers,
|
|
659
|
+
node.importClause,
|
|
660
|
+
node.moduleSpecifier,
|
|
661
|
+
// eslint-disable-next-line deprecation/deprecation
|
|
662
|
+
node.attributes || node.assertClause
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
if (ts4.isImportEqualsDeclaration(node)) {
|
|
666
|
+
return ts4.factory.createImportEqualsDeclaration(
|
|
667
|
+
modifiers,
|
|
668
|
+
node.isTypeOnly,
|
|
669
|
+
newName || node.name,
|
|
670
|
+
node.moduleReference
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
if (ts4.isInterfaceDeclaration(node)) {
|
|
674
|
+
return ts4.factory.createInterfaceDeclaration(
|
|
675
|
+
modifiers,
|
|
676
|
+
newName || node.name,
|
|
677
|
+
node.typeParameters,
|
|
678
|
+
node.heritageClauses,
|
|
679
|
+
node.members
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
if (ts4.isModuleDeclaration(node)) {
|
|
683
|
+
return ts4.factory.createModuleDeclaration(
|
|
684
|
+
modifiers,
|
|
685
|
+
node.name,
|
|
686
|
+
node.body,
|
|
687
|
+
node.flags
|
|
688
|
+
);
|
|
689
|
+
}
|
|
690
|
+
if (ts4.isTypeAliasDeclaration(node)) {
|
|
691
|
+
return ts4.factory.createTypeAliasDeclaration(
|
|
692
|
+
modifiers,
|
|
693
|
+
newName || node.name,
|
|
694
|
+
node.typeParameters,
|
|
695
|
+
node.type
|
|
696
|
+
);
|
|
697
|
+
}
|
|
698
|
+
if (ts4.isVariableStatement(node)) {
|
|
699
|
+
return ts4.factory.createVariableStatement(modifiers, node.declarationList);
|
|
700
|
+
}
|
|
701
|
+
throw new Error(`Unknown top-level node kind (with modifiers): ${ts4.SyntaxKind[node.kind]}.
|
|
702
|
+
If you're seeing this error, please report a bug on https://github.com/timocov/dts-bundle-generator/issues`);
|
|
703
|
+
}
|
|
704
|
+
function getModifiers2(node) {
|
|
705
|
+
if (!ts4.canHaveModifiers(node)) {
|
|
706
|
+
return void 0;
|
|
707
|
+
}
|
|
708
|
+
return ts4.getModifiers(node);
|
|
709
|
+
}
|
|
710
|
+
function getRootSourceFile(program, rootFileName) {
|
|
711
|
+
if (program.getRootFileNames().indexOf(rootFileName) === -1) {
|
|
712
|
+
throw new Error(`There is no such root file ${rootFileName}`);
|
|
713
|
+
}
|
|
714
|
+
const sourceFile = program.getSourceFile(rootFileName);
|
|
715
|
+
if (sourceFile === void 0) {
|
|
716
|
+
throw new Error(`Cannot get source file for root file ${rootFileName}`);
|
|
717
|
+
}
|
|
718
|
+
return sourceFile;
|
|
719
|
+
}
|
|
720
|
+
function getNodeOwnSymbol(node, typeChecker) {
|
|
721
|
+
const nodeSymbol = typeChecker.getSymbolAtLocation(node);
|
|
722
|
+
if (nodeSymbol === void 0) {
|
|
723
|
+
throw new Error(
|
|
724
|
+
`Cannot find symbol for node "${node.getText()}" in "${node.parent.getText()}" from "${node.getSourceFile().fileName}"`
|
|
725
|
+
);
|
|
726
|
+
}
|
|
727
|
+
return nodeSymbol;
|
|
728
|
+
}
|
|
729
|
+
function getNodeSymbol(node, typeChecker) {
|
|
730
|
+
if (ts4.isSourceFile(node)) {
|
|
731
|
+
const fileSymbol = typeChecker.getSymbolAtLocation(node);
|
|
732
|
+
if (fileSymbol === void 0) {
|
|
733
|
+
return null;
|
|
734
|
+
}
|
|
735
|
+
return getActualSymbol(fileSymbol, typeChecker);
|
|
736
|
+
}
|
|
737
|
+
const nodeName = getNodeName(node);
|
|
738
|
+
if (nodeName === void 0) {
|
|
739
|
+
return null;
|
|
740
|
+
}
|
|
741
|
+
return getDeclarationNameSymbol(nodeName, typeChecker);
|
|
742
|
+
}
|
|
743
|
+
function getClosestModuleLikeNode(node) {
|
|
744
|
+
while (!ts4.isModuleBlock(node) && !ts4.isSourceFile(node)) {
|
|
745
|
+
node = node.parent;
|
|
746
|
+
}
|
|
747
|
+
return ts4.isSourceFile(node) ? node : node.parent;
|
|
748
|
+
}
|
|
749
|
+
function getClosestSourceFileLikeNode(node) {
|
|
750
|
+
while (!(ts4.isModuleBlock(node) && ts4.isStringLiteral(node.parent.name)) && !ts4.isSourceFile(node)) {
|
|
751
|
+
node = node.parent;
|
|
752
|
+
}
|
|
753
|
+
return ts4.isSourceFile(node) ? node : node.parent;
|
|
754
|
+
}
|
|
755
|
+
function resolveReferencedModule(node, typeChecker) {
|
|
756
|
+
let moduleName;
|
|
757
|
+
if (ts4.isExportDeclaration(node) || ts4.isImportDeclaration(node)) {
|
|
758
|
+
moduleName = node.moduleSpecifier;
|
|
759
|
+
} else if (ts4.isModuleDeclaration(node)) {
|
|
760
|
+
moduleName = node.name;
|
|
761
|
+
} else if (ts4.isImportEqualsDeclaration(node)) {
|
|
762
|
+
if (ts4.isExternalModuleReference(node.moduleReference)) {
|
|
763
|
+
moduleName = node.moduleReference.expression;
|
|
764
|
+
}
|
|
765
|
+
} else if (ts4.isLiteralTypeNode(node.argument) && ts4.isStringLiteral(node.argument.literal)) {
|
|
766
|
+
moduleName = node.argument.literal;
|
|
767
|
+
}
|
|
768
|
+
if (moduleName === void 0) {
|
|
769
|
+
return null;
|
|
770
|
+
}
|
|
771
|
+
const moduleSymbol = typeChecker.getSymbolAtLocation(moduleName);
|
|
772
|
+
if (moduleSymbol === void 0) {
|
|
773
|
+
return null;
|
|
774
|
+
}
|
|
775
|
+
const symbol = getActualSymbol(moduleSymbol, typeChecker);
|
|
776
|
+
if (symbol.valueDeclaration === void 0) {
|
|
777
|
+
return null;
|
|
778
|
+
}
|
|
779
|
+
return ts4.isSourceFile(symbol.valueDeclaration) || ts4.isModuleDeclaration(symbol.valueDeclaration) ? symbol.valueDeclaration : null;
|
|
780
|
+
}
|
|
781
|
+
function getImportModuleName(imp) {
|
|
782
|
+
if (ts4.isImportDeclaration(imp)) {
|
|
783
|
+
return imp.importClause === void 0 ? null : imp.moduleSpecifier.text;
|
|
784
|
+
}
|
|
785
|
+
if (ts4.isExportDeclaration(imp)) {
|
|
786
|
+
return imp.moduleSpecifier === void 0 ? null : imp.moduleSpecifier.text;
|
|
787
|
+
}
|
|
788
|
+
if (ts4.isExternalModuleReference(imp.moduleReference)) {
|
|
789
|
+
if (!ts4.isStringLiteral(imp.moduleReference.expression)) {
|
|
790
|
+
warnLog(
|
|
791
|
+
`Cannot handle non string-literal-like import expression: ${imp.moduleReference.expression.getText()}`
|
|
792
|
+
);
|
|
793
|
+
return null;
|
|
794
|
+
}
|
|
795
|
+
return imp.moduleReference.expression.text;
|
|
796
|
+
}
|
|
797
|
+
return null;
|
|
798
|
+
}
|
|
799
|
+
function getImportExportReferencedSymbol(importExportSpecifier, typeChecker) {
|
|
800
|
+
return importExportSpecifier.propertyName !== void 0 ? (
|
|
801
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
802
|
+
typeChecker.getSymbolAtLocation(importExportSpecifier.propertyName)
|
|
803
|
+
) : (
|
|
804
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
805
|
+
typeChecker.getImmediateAliasedSymbol(
|
|
806
|
+
typeChecker.getSymbolAtLocation(importExportSpecifier.name)
|
|
807
|
+
)
|
|
808
|
+
);
|
|
809
|
+
}
|
|
810
|
+
function getSymbolExportStarDeclarations(symbol) {
|
|
811
|
+
if (symbol.escapedName !== ts4.InternalSymbolName.ExportStar) {
|
|
812
|
+
throw new Error(
|
|
813
|
+
`Only ExportStar symbol can have export star declaration, but got ${symbol.escapedName}`
|
|
814
|
+
);
|
|
815
|
+
}
|
|
816
|
+
return getDeclarationsForSymbol(symbol).filter(
|
|
817
|
+
(decl) => ts4.isExportDeclaration(decl) && decl.moduleSpecifier !== void 0
|
|
818
|
+
);
|
|
819
|
+
}
|
|
820
|
+
function getDeclarationsForExportedValues(exp, typeChecker) {
|
|
821
|
+
const nodeForSymbol = ts4.isExportAssignment(exp) ? exp.expression : exp.moduleSpecifier;
|
|
822
|
+
if (nodeForSymbol === void 0) {
|
|
823
|
+
return [];
|
|
824
|
+
}
|
|
825
|
+
const symbolForExpression = typeChecker.getSymbolAtLocation(nodeForSymbol);
|
|
826
|
+
if (symbolForExpression === void 0) {
|
|
827
|
+
return [];
|
|
828
|
+
}
|
|
829
|
+
const symbol = getActualSymbol(symbolForExpression, typeChecker);
|
|
830
|
+
return getDeclarationsForSymbol(symbol);
|
|
831
|
+
}
|
|
832
|
+
function resolveGlobalName(typeChecker, name) {
|
|
833
|
+
const tsSymbolFlagsAll = (
|
|
834
|
+
/* ts.SymbolFlags.All */
|
|
835
|
+
-1
|
|
836
|
+
);
|
|
837
|
+
return typeChecker.resolveName(
|
|
838
|
+
name,
|
|
839
|
+
void 0,
|
|
840
|
+
tsSymbolFlagsAll,
|
|
841
|
+
false
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
// src/types-usage-evaluator.ts
|
|
846
|
+
var TypesUsageEvaluator = class {
|
|
847
|
+
typeChecker;
|
|
848
|
+
nodesParentsMap = /* @__PURE__ */ new Map();
|
|
849
|
+
usageResultCache = /* @__PURE__ */ new Map();
|
|
850
|
+
constructor(files, typeChecker) {
|
|
851
|
+
this.typeChecker = typeChecker;
|
|
852
|
+
this.computeUsages(files);
|
|
853
|
+
}
|
|
854
|
+
isSymbolUsedBySymbol(symbol, by) {
|
|
855
|
+
return this.isSymbolUsedBySymbolImpl(
|
|
856
|
+
this.getActualSymbol(symbol),
|
|
857
|
+
this.getActualSymbol(by),
|
|
858
|
+
/* @__PURE__ */ new Set()
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
getSymbolsUsingSymbol(symbol) {
|
|
862
|
+
return this.nodesParentsMap.get(this.getActualSymbol(symbol)) || null;
|
|
863
|
+
}
|
|
864
|
+
isSymbolUsedBySymbolImpl(fromSymbol, toSymbol, visitedSymbols) {
|
|
865
|
+
if (fromSymbol === toSymbol) {
|
|
866
|
+
return this.setUsageCacheValue(fromSymbol, toSymbol, true);
|
|
867
|
+
}
|
|
868
|
+
const cacheResult = this.usageResultCache.get(fromSymbol)?.get(toSymbol);
|
|
869
|
+
if (cacheResult !== void 0) {
|
|
870
|
+
return cacheResult;
|
|
871
|
+
}
|
|
872
|
+
const reachableNodes = this.nodesParentsMap.get(fromSymbol);
|
|
873
|
+
if (reachableNodes !== void 0) {
|
|
874
|
+
for (const symbol of reachableNodes) {
|
|
875
|
+
if (visitedSymbols.has(symbol)) {
|
|
876
|
+
continue;
|
|
877
|
+
}
|
|
878
|
+
visitedSymbols.add(symbol);
|
|
879
|
+
if (this.isSymbolUsedBySymbolImpl(symbol, toSymbol, visitedSymbols)) {
|
|
880
|
+
return this.setUsageCacheValue(fromSymbol, toSymbol, true);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
visitedSymbols.add(fromSymbol);
|
|
885
|
+
return false;
|
|
886
|
+
}
|
|
887
|
+
setUsageCacheValue(fromSymbol, toSymbol, value) {
|
|
888
|
+
let fromSymbolCacheMap = this.usageResultCache.get(fromSymbol);
|
|
889
|
+
if (fromSymbolCacheMap === void 0) {
|
|
890
|
+
fromSymbolCacheMap = /* @__PURE__ */ new Map();
|
|
891
|
+
this.usageResultCache.set(fromSymbol, fromSymbolCacheMap);
|
|
892
|
+
}
|
|
893
|
+
fromSymbolCacheMap.set(toSymbol, value);
|
|
894
|
+
return value;
|
|
895
|
+
}
|
|
896
|
+
computeUsages(files) {
|
|
897
|
+
this.nodesParentsMap.clear();
|
|
898
|
+
for (const file of files) {
|
|
899
|
+
ts5.forEachChild(file, this.computeUsageForNode.bind(this));
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
// eslint-disable-next-line complexity
|
|
903
|
+
computeUsageForNode(node) {
|
|
904
|
+
if (isDeclareModule(node) && node.body !== void 0 && ts5.isModuleBlock(node.body)) {
|
|
905
|
+
const moduleSymbol = this.getSymbol(node.name);
|
|
906
|
+
for (const statement of node.body.statements) {
|
|
907
|
+
this.computeUsageForNode(statement);
|
|
908
|
+
if (isNodeNamedDeclaration(statement)) {
|
|
909
|
+
const nodeName = getNodeName(statement);
|
|
910
|
+
if (nodeName !== void 0) {
|
|
911
|
+
const statementSymbol = this.getSymbol(nodeName);
|
|
912
|
+
this.addUsages(statementSymbol, moduleSymbol);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
if (isNodeNamedDeclaration(node)) {
|
|
918
|
+
const nodeName = getNodeName(node);
|
|
919
|
+
if (nodeName !== void 0) {
|
|
920
|
+
if (ts5.isObjectBindingPattern(nodeName) || ts5.isArrayBindingPattern(nodeName)) {
|
|
921
|
+
for (const element of nodeName.elements) {
|
|
922
|
+
this.computeUsageForNode(element);
|
|
923
|
+
}
|
|
924
|
+
} else {
|
|
925
|
+
const childSymbol = this.getSymbol(nodeName);
|
|
926
|
+
if (childSymbol !== null) {
|
|
927
|
+
this.computeUsagesRecursively(node, childSymbol);
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
if (ts5.isVariableStatement(node)) {
|
|
933
|
+
for (const varDeclaration of node.declarationList.declarations) {
|
|
934
|
+
this.computeUsageForNode(varDeclaration);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
if (ts5.isExportDeclaration(node) && node.moduleSpecifier !== void 0 && node.exportClause !== void 0 && ts5.isNamespaceExport(node.exportClause)) {
|
|
938
|
+
this.addUsagesForNamespacedModule(
|
|
939
|
+
node.exportClause,
|
|
940
|
+
node.moduleSpecifier
|
|
941
|
+
);
|
|
942
|
+
}
|
|
943
|
+
if (ts5.isImportDeclaration(node) && node.moduleSpecifier !== void 0 && node.importClause?.namedBindings !== void 0 && ts5.isNamespaceImport(node.importClause.namedBindings)) {
|
|
944
|
+
this.addUsagesForNamespacedModule(
|
|
945
|
+
node.importClause.namedBindings,
|
|
946
|
+
node.moduleSpecifier,
|
|
947
|
+
false
|
|
948
|
+
);
|
|
949
|
+
}
|
|
950
|
+
if (ts5.isExportDeclaration(node) && node.exportClause !== void 0 && ts5.isNamedExports(node.exportClause)) {
|
|
951
|
+
for (const exportElement of node.exportClause.elements) {
|
|
952
|
+
const exportElementSymbol = getImportExportReferencedSymbol(
|
|
953
|
+
exportElement,
|
|
954
|
+
this.typeChecker
|
|
955
|
+
);
|
|
956
|
+
const namespaceImportForElement = getDeclarationsForSymbol(
|
|
957
|
+
exportElementSymbol
|
|
958
|
+
).find(ts5.isNamespaceImport);
|
|
959
|
+
if (namespaceImportForElement !== void 0) {
|
|
960
|
+
this.addUsagesForNamespacedModule(
|
|
961
|
+
namespaceImportForElement,
|
|
962
|
+
namespaceImportForElement.parent.parent.moduleSpecifier
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
const exportElementOwnSymbol = this.getNodeOwnSymbol(
|
|
966
|
+
exportElement.name
|
|
967
|
+
);
|
|
968
|
+
this.addUsages(exportElementSymbol, exportElementOwnSymbol);
|
|
969
|
+
this.addUsages(
|
|
970
|
+
this.getActualSymbol(exportElementSymbol),
|
|
971
|
+
exportElementOwnSymbol
|
|
972
|
+
);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
if (ts5.isExportAssignment(node) && node.isExportEquals) {
|
|
976
|
+
this.addUsagesForExportAssignment(node);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
addUsagesForExportAssignment(exportAssignment) {
|
|
980
|
+
for (const declaration of getDeclarationsForExportedValues(
|
|
981
|
+
exportAssignment,
|
|
982
|
+
this.typeChecker
|
|
983
|
+
)) {
|
|
984
|
+
if (ts5.isModuleDeclaration(declaration) && ts5.isIdentifier(declaration.name) && declaration.body !== void 0 && ts5.isModuleBlock(declaration.body)) {
|
|
985
|
+
const moduleSymbol = this.getSymbol(declaration.name);
|
|
986
|
+
for (const statement of declaration.body.statements) {
|
|
987
|
+
if (isNodeNamedDeclaration(statement) && statement.name !== void 0) {
|
|
988
|
+
const statementSymbol = this.getSymbol(statement.name);
|
|
989
|
+
if (statementSymbol !== null) {
|
|
990
|
+
this.addUsages(moduleSymbol, statementSymbol);
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
addUsagesForNamespacedModule(namespaceNode, moduleSpecifier, includeExports = true) {
|
|
998
|
+
const namespaceSymbol = this.getNodeOwnSymbol(namespaceNode.name);
|
|
999
|
+
const referencedSourceFileSymbol = this.getSymbol(moduleSpecifier);
|
|
1000
|
+
this.addUsages(referencedSourceFileSymbol, namespaceSymbol);
|
|
1001
|
+
const resolvedNamespaceSymbol = this.getSymbol(namespaceNode.name);
|
|
1002
|
+
this.addUsages(resolvedNamespaceSymbol, namespaceSymbol);
|
|
1003
|
+
if (includeExports) {
|
|
1004
|
+
this.addExportsToSymbol(
|
|
1005
|
+
referencedSourceFileSymbol.exports,
|
|
1006
|
+
referencedSourceFileSymbol
|
|
1007
|
+
);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
addExportsToSymbol(exports2, parentSymbol, visitedSymbols = /* @__PURE__ */ new Set()) {
|
|
1011
|
+
exports2?.forEach((moduleExportedSymbol, name) => {
|
|
1012
|
+
if (name === ts5.InternalSymbolName.ExportStar) {
|
|
1013
|
+
for (const exportStarDeclaration of getSymbolExportStarDeclarations(
|
|
1014
|
+
moduleExportedSymbol
|
|
1015
|
+
)) {
|
|
1016
|
+
if (exportStarDeclaration.moduleSpecifier === void 0) {
|
|
1017
|
+
throw new Error(
|
|
1018
|
+
`Export star declaration does not have a module specifier '${exportStarDeclaration.getText()}'`
|
|
1019
|
+
);
|
|
1020
|
+
}
|
|
1021
|
+
const referencedSourceFileSymbol = this.getSymbol(
|
|
1022
|
+
exportStarDeclaration.moduleSpecifier
|
|
1023
|
+
);
|
|
1024
|
+
if (visitedSymbols.has(referencedSourceFileSymbol)) {
|
|
1025
|
+
continue;
|
|
1026
|
+
}
|
|
1027
|
+
visitedSymbols.add(referencedSourceFileSymbol);
|
|
1028
|
+
this.addExportsToSymbol(
|
|
1029
|
+
referencedSourceFileSymbol.exports,
|
|
1030
|
+
parentSymbol,
|
|
1031
|
+
visitedSymbols
|
|
1032
|
+
);
|
|
1033
|
+
}
|
|
1034
|
+
return;
|
|
1035
|
+
}
|
|
1036
|
+
this.addUsages(moduleExportedSymbol, parentSymbol);
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
computeUsagesRecursively(parent, parentSymbol) {
|
|
1040
|
+
ts5.forEachChild(parent, (child) => {
|
|
1041
|
+
if (child.kind === ts5.SyntaxKind.JSDoc) {
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1044
|
+
this.computeUsagesRecursively(child, parentSymbol);
|
|
1045
|
+
if (ts5.isIdentifier(child) || child.kind === ts5.SyntaxKind.DefaultKeyword) {
|
|
1046
|
+
if (ts5.isNamedTupleMember(child.parent) && child.parent.name === child) {
|
|
1047
|
+
return;
|
|
1048
|
+
}
|
|
1049
|
+
if (ts5.isBindingElement(child.parent) && child.parent.propertyName === child) {
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
this.addUsages(this.getSymbol(child), parentSymbol);
|
|
1053
|
+
if (!ts5.isQualifiedName(child.parent)) {
|
|
1054
|
+
const childOwnSymbol = this.getNodeOwnSymbol(child);
|
|
1055
|
+
const namespaceImport = getDeclarationsForSymbol(childOwnSymbol).find(
|
|
1056
|
+
ts5.isNamespaceImport
|
|
1057
|
+
);
|
|
1058
|
+
if (namespaceImport !== void 0) {
|
|
1059
|
+
this.addUsagesForNamespacedModule(
|
|
1060
|
+
namespaceImport,
|
|
1061
|
+
namespaceImport.parent.parent.moduleSpecifier,
|
|
1062
|
+
true
|
|
1063
|
+
);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
addUsages(childSymbol, parentSymbol) {
|
|
1070
|
+
const childSymbols = splitTransientSymbol(childSymbol, this.typeChecker);
|
|
1071
|
+
for (const childSplitSymbol of childSymbols) {
|
|
1072
|
+
let symbols = this.nodesParentsMap.get(childSplitSymbol);
|
|
1073
|
+
if (symbols === void 0) {
|
|
1074
|
+
symbols = /* @__PURE__ */ new Set();
|
|
1075
|
+
this.nodesParentsMap.set(childSplitSymbol, symbols);
|
|
1076
|
+
}
|
|
1077
|
+
if (childSplitSymbol !== parentSymbol) {
|
|
1078
|
+
symbols.add(parentSymbol);
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
getSymbol(node) {
|
|
1083
|
+
return this.getActualSymbol(this.getNodeOwnSymbol(node));
|
|
1084
|
+
}
|
|
1085
|
+
getNodeOwnSymbol(node) {
|
|
1086
|
+
return getNodeOwnSymbol(node, this.typeChecker);
|
|
1087
|
+
}
|
|
1088
|
+
getActualSymbol(symbol) {
|
|
1089
|
+
return getActualSymbol(symbol, this.typeChecker);
|
|
1090
|
+
}
|
|
1091
|
+
};
|
|
1092
|
+
|
|
1093
|
+
// src/module-info.ts
|
|
1094
|
+
var path3 = __toESM(require("path"), 1);
|
|
1095
|
+
var ts6 = __toESM(require("typescript"), 1);
|
|
1096
|
+
|
|
1097
|
+
// src/helpers/node-modules.ts
|
|
1098
|
+
var nodeModulesFolderName = "node_modules/";
|
|
1099
|
+
var libraryNameRegex = /node_modules\/((?:(?=@)[^/]+\/[^/]+|[^/]+))\//;
|
|
1100
|
+
function getLibraryName(fileName) {
|
|
1101
|
+
const lastNodeModulesIndex = fileName.lastIndexOf(nodeModulesFolderName);
|
|
1102
|
+
if (lastNodeModulesIndex === -1) {
|
|
1103
|
+
return null;
|
|
1104
|
+
}
|
|
1105
|
+
const match = libraryNameRegex.exec(fileName.slice(lastNodeModulesIndex));
|
|
1106
|
+
if (match === null) {
|
|
1107
|
+
return null;
|
|
1108
|
+
}
|
|
1109
|
+
return match[1];
|
|
1110
|
+
}
|
|
1111
|
+
function getTypesLibraryName(path5) {
|
|
1112
|
+
const libraryName = getLibraryName(path5);
|
|
1113
|
+
if (libraryName === null) {
|
|
1114
|
+
return null;
|
|
1115
|
+
}
|
|
1116
|
+
const typesFolderPrefix = "@types/";
|
|
1117
|
+
if (!libraryName.startsWith(typesFolderPrefix)) {
|
|
1118
|
+
return null;
|
|
1119
|
+
}
|
|
1120
|
+
return libraryName.substring(typesFolderPrefix.length);
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
// src/module-info.ts
|
|
1124
|
+
function getFileModuleInfo(fileName, criteria) {
|
|
1125
|
+
return getModuleInfoImpl(fileName, fileName, criteria);
|
|
1126
|
+
}
|
|
1127
|
+
function getReferencedModuleInfo(moduleDecl, criteria, typeChecker) {
|
|
1128
|
+
const referencedModule = resolveReferencedModule(moduleDecl, typeChecker);
|
|
1129
|
+
if (referencedModule === null) {
|
|
1130
|
+
return null;
|
|
1131
|
+
}
|
|
1132
|
+
const moduleFilePath = ts6.isSourceFile(referencedModule) ? referencedModule.fileName : resolveModuleFileName(
|
|
1133
|
+
referencedModule.getSourceFile().fileName,
|
|
1134
|
+
referencedModule.name.text
|
|
1135
|
+
);
|
|
1136
|
+
return getFileModuleInfo(moduleFilePath, criteria);
|
|
1137
|
+
}
|
|
1138
|
+
function getModuleLikeModuleInfo(moduleLike, criteria, typeChecker) {
|
|
1139
|
+
const resolvedModuleLike = ts6.isSourceFile(moduleLike) ? moduleLike : resolveReferencedModule(moduleLike, typeChecker) ?? moduleLike;
|
|
1140
|
+
const fileName = ts6.isSourceFile(resolvedModuleLike) ? resolvedModuleLike.fileName : resolveModuleFileName(
|
|
1141
|
+
resolvedModuleLike.getSourceFile().fileName,
|
|
1142
|
+
resolvedModuleLike.name.text
|
|
1143
|
+
);
|
|
1144
|
+
return getFileModuleInfo(fileName, criteria);
|
|
1145
|
+
}
|
|
1146
|
+
function resolveModuleFileName(currentFileName, moduleName) {
|
|
1147
|
+
return moduleName.startsWith(".") ? fixPath(path3.join(currentFileName, "..", moduleName)) : `node_modules/${moduleName}/`;
|
|
1148
|
+
}
|
|
1149
|
+
function getModuleInfoImpl(currentFilePath, originalFileName, criteria) {
|
|
1150
|
+
const npmLibraryName = getLibraryName(currentFilePath);
|
|
1151
|
+
if (npmLibraryName === null) {
|
|
1152
|
+
if (criteria.typeRoots !== void 0) {
|
|
1153
|
+
for (const root of criteria.typeRoots) {
|
|
1154
|
+
const relativePath = fixPath(path3.relative(root, originalFileName));
|
|
1155
|
+
if (!relativePath.startsWith("../")) {
|
|
1156
|
+
return getModuleInfoImpl(
|
|
1157
|
+
remapToTypesFromNodeModules(relativePath),
|
|
1158
|
+
originalFileName,
|
|
1159
|
+
criteria
|
|
1160
|
+
);
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
return {
|
|
1165
|
+
type: 0 /* ShouldBeInlined */,
|
|
1166
|
+
fileName: originalFileName,
|
|
1167
|
+
isExternal: false
|
|
1168
|
+
};
|
|
1169
|
+
}
|
|
1170
|
+
const typesLibraryName = getTypesLibraryName(currentFilePath);
|
|
1171
|
+
if (shouldLibraryBeInlined(
|
|
1172
|
+
npmLibraryName,
|
|
1173
|
+
typesLibraryName,
|
|
1174
|
+
criteria.inlinedLibraries
|
|
1175
|
+
)) {
|
|
1176
|
+
return {
|
|
1177
|
+
type: 0 /* ShouldBeInlined */,
|
|
1178
|
+
fileName: originalFileName,
|
|
1179
|
+
isExternal: true
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1182
|
+
if (shouldLibraryBeImported(
|
|
1183
|
+
npmLibraryName,
|
|
1184
|
+
typesLibraryName,
|
|
1185
|
+
criteria.importedLibraries,
|
|
1186
|
+
criteria.allowedTypesLibraries
|
|
1187
|
+
)) {
|
|
1188
|
+
return {
|
|
1189
|
+
type: 1 /* ShouldBeImported */,
|
|
1190
|
+
fileName: originalFileName,
|
|
1191
|
+
isExternal: true
|
|
1192
|
+
};
|
|
1193
|
+
}
|
|
1194
|
+
if (typesLibraryName !== null && isLibraryAllowed(typesLibraryName, criteria.allowedTypesLibraries)) {
|
|
1195
|
+
return {
|
|
1196
|
+
type: 2 /* ShouldBeReferencedAsTypes */,
|
|
1197
|
+
fileName: originalFileName,
|
|
1198
|
+
typesLibraryName,
|
|
1199
|
+
isExternal: true
|
|
1200
|
+
};
|
|
1201
|
+
}
|
|
1202
|
+
return {
|
|
1203
|
+
type: 3 /* ShouldBeUsedForModulesOnly */,
|
|
1204
|
+
fileName: originalFileName,
|
|
1205
|
+
isExternal: true
|
|
1206
|
+
};
|
|
1207
|
+
}
|
|
1208
|
+
function shouldLibraryBeInlined(npmLibraryName, typesLibraryName, inlinedLibraries) {
|
|
1209
|
+
return isLibraryAllowed(npmLibraryName, inlinedLibraries) || typesLibraryName !== null && isLibraryAllowed(typesLibraryName, inlinedLibraries);
|
|
1210
|
+
}
|
|
1211
|
+
function shouldLibraryBeImported(npmLibraryName, typesLibraryName, importedLibraries, allowedTypesLibraries) {
|
|
1212
|
+
if (typesLibraryName === null) {
|
|
1213
|
+
return isLibraryAllowed(npmLibraryName, importedLibraries);
|
|
1214
|
+
}
|
|
1215
|
+
if (allowedTypesLibraries === void 0 || !isLibraryAllowed(typesLibraryName, allowedTypesLibraries)) {
|
|
1216
|
+
return isLibraryAllowed(typesLibraryName, importedLibraries);
|
|
1217
|
+
}
|
|
1218
|
+
return false;
|
|
1219
|
+
}
|
|
1220
|
+
function isLibraryAllowed(libraryName, allowedArray) {
|
|
1221
|
+
return allowedArray === void 0 || allowedArray.indexOf(libraryName) !== -1;
|
|
1222
|
+
}
|
|
1223
|
+
function remapToTypesFromNodeModules(pathRelativeToTypesRoot) {
|
|
1224
|
+
return `node_modules/@types/${pathRelativeToTypesRoot}`;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
// src/generate-output.ts
|
|
1228
|
+
var ts7 = __toESM(require("typescript"), 1);
|
|
1229
|
+
|
|
1230
|
+
// src/helpers/package-info.ts
|
|
1231
|
+
var fs = __toESM(require("fs"), 1);
|
|
1232
|
+
var path4 = __toESM(require("path"), 1);
|
|
1233
|
+
function getPackageInfo() {
|
|
1234
|
+
let dirName = process.cwd();
|
|
1235
|
+
while (dirName.length !== 0) {
|
|
1236
|
+
const packageJsonFilePath = path4.join(dirName, "package.json");
|
|
1237
|
+
if (fs.existsSync(packageJsonFilePath)) {
|
|
1238
|
+
const pkgContent = fs.readFileSync(packageJsonFilePath, "utf8");
|
|
1239
|
+
const pkg = JSON.parse(pkgContent);
|
|
1240
|
+
let authorName = void 0;
|
|
1241
|
+
if (pkg.author) {
|
|
1242
|
+
authorName = typeof pkg.author === "string" ? pkg.author : pkg.author.name;
|
|
1243
|
+
}
|
|
1244
|
+
return {
|
|
1245
|
+
name: pkg.name || "@sse-ui/locale",
|
|
1246
|
+
version: pkg.version,
|
|
1247
|
+
license: pkg.license || "MIT",
|
|
1248
|
+
author: authorName
|
|
1249
|
+
};
|
|
1250
|
+
}
|
|
1251
|
+
const parentDir = path4.join(dirName, "..");
|
|
1252
|
+
if (parentDir === dirName) {
|
|
1253
|
+
break;
|
|
1254
|
+
}
|
|
1255
|
+
dirName = parentDir;
|
|
1256
|
+
}
|
|
1257
|
+
throw new Error(`Cannot find up package.json in ${__dirname}`);
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
// src/generate-output.ts
|
|
1261
|
+
function generateOutput(params, options = {}) {
|
|
1262
|
+
const resultOutputParts = [];
|
|
1263
|
+
if (!options.noBanner) {
|
|
1264
|
+
const pkg = getPackageInfo();
|
|
1265
|
+
const authorLine = pkg.author ? `
|
|
1266
|
+
* @author ${pkg.author}` : "";
|
|
1267
|
+
resultOutputParts.push(`/**
|
|
1268
|
+
* ${pkg.name} v${pkg.version}${authorLine}
|
|
1269
|
+
*
|
|
1270
|
+
* @license ${pkg.license}
|
|
1271
|
+
* This source code is licensed under the ${pkg.license} license found in the
|
|
1272
|
+
* LICENSE file in the root directory of this source tree.
|
|
1273
|
+
*/`);
|
|
1274
|
+
}
|
|
1275
|
+
if (params.typesReferences.size !== 0) {
|
|
1276
|
+
const header = generateReferenceTypesDirective(
|
|
1277
|
+
Array.from(params.typesReferences)
|
|
1278
|
+
);
|
|
1279
|
+
resultOutputParts.push(header);
|
|
1280
|
+
}
|
|
1281
|
+
if (params.imports.size !== 0) {
|
|
1282
|
+
const sortedEntries = Array.from(params.imports.entries()).sort(
|
|
1283
|
+
(firstEntry, secondEntry) => {
|
|
1284
|
+
return firstEntry[0].localeCompare(secondEntry[0]);
|
|
1285
|
+
}
|
|
1286
|
+
);
|
|
1287
|
+
const importsArray = [];
|
|
1288
|
+
for (const [libraryName, libraryImports] of sortedEntries) {
|
|
1289
|
+
importsArray.push(...generateImports(libraryName, libraryImports));
|
|
1290
|
+
}
|
|
1291
|
+
if (importsArray.length !== 0) {
|
|
1292
|
+
resultOutputParts.push(importsArray.join("\n"));
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
const statements = params.statements.map(
|
|
1296
|
+
(statement) => getStatementText(statement, Boolean(options.sortStatements), params)
|
|
1297
|
+
);
|
|
1298
|
+
if (options.sortStatements) {
|
|
1299
|
+
statements.sort(compareStatementText);
|
|
1300
|
+
}
|
|
1301
|
+
if (statements.length !== 0) {
|
|
1302
|
+
resultOutputParts.push(statementsTextToString(statements));
|
|
1303
|
+
}
|
|
1304
|
+
if (params.wrappedNamespaces.size !== 0) {
|
|
1305
|
+
resultOutputParts.push(
|
|
1306
|
+
Array.from(params.wrappedNamespaces.entries()).map(
|
|
1307
|
+
([namespaceName, exportedNames]) => {
|
|
1308
|
+
return `declare namespace ${namespaceName} {
|
|
1309
|
+
export { ${Array.from(
|
|
1310
|
+
exportedNames.entries()
|
|
1311
|
+
).map(
|
|
1312
|
+
([exportedName, localName]) => renamedExportValue(exportedName, localName)
|
|
1313
|
+
).sort().join(", ")} };
|
|
1314
|
+
}`;
|
|
1315
|
+
}
|
|
1316
|
+
).join("\n")
|
|
1317
|
+
);
|
|
1318
|
+
}
|
|
1319
|
+
if (params.renamedExports.size !== 0) {
|
|
1320
|
+
resultOutputParts.push(
|
|
1321
|
+
`export {
|
|
1322
|
+
${Array.from(params.renamedExports.entries()).map(
|
|
1323
|
+
([exportedName, localName]) => renamedExportValue(exportedName, localName)
|
|
1324
|
+
).sort().join(",\n ")},
|
|
1325
|
+
};`
|
|
1326
|
+
);
|
|
1327
|
+
}
|
|
1328
|
+
if (options.umdModuleName !== void 0) {
|
|
1329
|
+
resultOutputParts.push(`export as namespace ${options.umdModuleName};`);
|
|
1330
|
+
}
|
|
1331
|
+
resultOutputParts.push(`export {};
|
|
1332
|
+
`);
|
|
1333
|
+
return resultOutputParts.join("\n\n");
|
|
1334
|
+
}
|
|
1335
|
+
function statementsTextToString(statements) {
|
|
1336
|
+
const statementsText = statements.map((statement) => statement.text).join("\n");
|
|
1337
|
+
return spacesToTabs(prettifyStatementsText(statementsText));
|
|
1338
|
+
}
|
|
1339
|
+
function renamedExportValue(exportedName, localName) {
|
|
1340
|
+
return exportedName !== localName ? `${localName} as ${exportedName}` : exportedName;
|
|
1341
|
+
}
|
|
1342
|
+
function renamedImportValue(importedName, localName) {
|
|
1343
|
+
return importedName !== localName ? `${importedName} as ${localName}` : importedName;
|
|
1344
|
+
}
|
|
1345
|
+
function prettifyStatementsText(statementsText) {
|
|
1346
|
+
const sourceFile = ts7.createSourceFile(
|
|
1347
|
+
"output.d.ts",
|
|
1348
|
+
statementsText,
|
|
1349
|
+
ts7.ScriptTarget.Latest,
|
|
1350
|
+
false,
|
|
1351
|
+
ts7.ScriptKind.TS
|
|
1352
|
+
);
|
|
1353
|
+
const printer = ts7.createPrinter({
|
|
1354
|
+
newLine: ts7.NewLineKind.LineFeed,
|
|
1355
|
+
removeComments: false
|
|
1356
|
+
});
|
|
1357
|
+
return printer.printFile(sourceFile).trim();
|
|
1358
|
+
}
|
|
1359
|
+
function compareStatementText(a, b) {
|
|
1360
|
+
if (a.sortingValue > b.sortingValue) {
|
|
1361
|
+
return 1;
|
|
1362
|
+
} else if (a.sortingValue < b.sortingValue) {
|
|
1363
|
+
return -1;
|
|
1364
|
+
}
|
|
1365
|
+
return 0;
|
|
1366
|
+
}
|
|
1367
|
+
function recreateEntityName(node, helpers) {
|
|
1368
|
+
const resolvedName = helpers.resolveIdentifierName(node);
|
|
1369
|
+
if (resolvedName !== null && resolvedName !== node.getText()) {
|
|
1370
|
+
const identifiers = resolvedName.split(".");
|
|
1371
|
+
let result = ts7.factory.createIdentifier(identifiers[0]);
|
|
1372
|
+
for (let index = 1; index < identifiers.length; index += 1) {
|
|
1373
|
+
result = ts7.factory.createQualifiedName(
|
|
1374
|
+
result,
|
|
1375
|
+
ts7.factory.createIdentifier(identifiers[index])
|
|
1376
|
+
);
|
|
1377
|
+
}
|
|
1378
|
+
return result;
|
|
1379
|
+
}
|
|
1380
|
+
return node;
|
|
1381
|
+
}
|
|
1382
|
+
function getStatementText(statement, includeSortingValue, helpers) {
|
|
1383
|
+
const { shouldHaveExportKeyword, shouldHaveJSDoc } = helpers.getStatementSettings(statement);
|
|
1384
|
+
const needResolveIdentifiers = !ts7.isExportDeclaration(statement) || statement.moduleSpecifier === void 0;
|
|
1385
|
+
const printer = ts7.createPrinter(
|
|
1386
|
+
{
|
|
1387
|
+
newLine: ts7.NewLineKind.LineFeed,
|
|
1388
|
+
removeComments: false
|
|
1389
|
+
},
|
|
1390
|
+
{
|
|
1391
|
+
// eslint-disable-next-line complexity
|
|
1392
|
+
substituteNode: (hint, node) => {
|
|
1393
|
+
if (node.parent === void 0) {
|
|
1394
|
+
return node;
|
|
1395
|
+
}
|
|
1396
|
+
if (needResolveIdentifiers) {
|
|
1397
|
+
if (ts7.isPropertyAccessExpression(node)) {
|
|
1398
|
+
const resolvedName = helpers.resolveIdentifierName(
|
|
1399
|
+
node
|
|
1400
|
+
);
|
|
1401
|
+
if (resolvedName !== null && resolvedName !== node.getText()) {
|
|
1402
|
+
const identifiers = resolvedName.split(".");
|
|
1403
|
+
let result = ts7.factory.createIdentifier(identifiers[0]);
|
|
1404
|
+
for (let index = 1; index < identifiers.length; index += 1) {
|
|
1405
|
+
result = ts7.factory.createPropertyAccessExpression(
|
|
1406
|
+
result,
|
|
1407
|
+
ts7.factory.createIdentifier(identifiers[index])
|
|
1408
|
+
);
|
|
1409
|
+
}
|
|
1410
|
+
return result;
|
|
1411
|
+
}
|
|
1412
|
+
return node;
|
|
1413
|
+
}
|
|
1414
|
+
if (ts7.isIdentifier(node) || ts7.isQualifiedName(node)) {
|
|
1415
|
+
if (ts7.isIdentifier(node) && (ts7.isQualifiedName(node.parent) || ts7.isPropertyAccessExpression(node.parent))) {
|
|
1416
|
+
return node;
|
|
1417
|
+
}
|
|
1418
|
+
if (ts7.isIdentifier(node) && ts7.isImportTypeNode(node.parent) && node.parent.qualifier === node) {
|
|
1419
|
+
return node;
|
|
1420
|
+
}
|
|
1421
|
+
return recreateEntityName(node, helpers);
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
if (ts7.isImportTypeNode(node) && node.qualifier !== void 0 && helpers.needStripImportFromImportTypeNode(node)) {
|
|
1425
|
+
const newQualifier = recreateEntityName(node.qualifier, helpers);
|
|
1426
|
+
if (node.isTypeOf) {
|
|
1427
|
+
return ts7.factory.createTypeQueryNode(newQualifier);
|
|
1428
|
+
}
|
|
1429
|
+
return ts7.factory.createTypeReferenceNode(
|
|
1430
|
+
newQualifier,
|
|
1431
|
+
node.typeArguments
|
|
1432
|
+
);
|
|
1433
|
+
}
|
|
1434
|
+
if (node !== statement) {
|
|
1435
|
+
return node;
|
|
1436
|
+
}
|
|
1437
|
+
const modifiersMap = modifiersToMap(getModifiers2(node));
|
|
1438
|
+
if (ts7.isEnumDeclaration(node) && modifiersMap[ts7.SyntaxKind.ConstKeyword] && helpers.needStripConstFromConstEnum(node)) {
|
|
1439
|
+
modifiersMap[ts7.SyntaxKind.ConstKeyword] = false;
|
|
1440
|
+
}
|
|
1441
|
+
const nodeName = getNodeName(node);
|
|
1442
|
+
const resolvedStatementName = nodeName !== void 0 ? helpers.resolveIdentifierName(nodeName) || void 0 : void 0;
|
|
1443
|
+
if (modifiersMap[ts7.SyntaxKind.DefaultKeyword]) {
|
|
1444
|
+
modifiersMap[ts7.SyntaxKind.DefaultKeyword] = false;
|
|
1445
|
+
if (ts7.isClassDeclaration(node)) {
|
|
1446
|
+
modifiersMap[ts7.SyntaxKind.DeclareKeyword] = true;
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
if (!shouldHaveExportKeyword) {
|
|
1450
|
+
modifiersMap[ts7.SyntaxKind.ExportKeyword] = false;
|
|
1451
|
+
} else {
|
|
1452
|
+
modifiersMap[ts7.SyntaxKind.ExportKeyword] = true;
|
|
1453
|
+
}
|
|
1454
|
+
if (!modifiersMap[ts7.SyntaxKind.ExportKeyword] && (ts7.isClassDeclaration(node) || ts7.isFunctionDeclaration(node) || ts7.isVariableStatement(node) || ts7.isEnumDeclaration(node) || ts7.isModuleDeclaration(node))) {
|
|
1455
|
+
modifiersMap[ts7.SyntaxKind.DeclareKeyword] = true;
|
|
1456
|
+
}
|
|
1457
|
+
return recreateRootLevelNodeWithModifiers(
|
|
1458
|
+
node,
|
|
1459
|
+
modifiersMap,
|
|
1460
|
+
resolvedStatementName,
|
|
1461
|
+
shouldHaveJSDoc
|
|
1462
|
+
);
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
);
|
|
1466
|
+
const statementText = printer.printNode(ts7.EmitHint.Unspecified, statement, statement.getSourceFile()).trim();
|
|
1467
|
+
let sortingValue = "";
|
|
1468
|
+
if (includeSortingValue) {
|
|
1469
|
+
const tempSourceFile = ts7.createSourceFile(
|
|
1470
|
+
"temp.d.ts",
|
|
1471
|
+
statementText,
|
|
1472
|
+
ts7.ScriptTarget.ESNext
|
|
1473
|
+
);
|
|
1474
|
+
sortingValue = tempSourceFile.getChildren()[0].getText();
|
|
1475
|
+
}
|
|
1476
|
+
return { text: statementText, sortingValue };
|
|
1477
|
+
}
|
|
1478
|
+
function generateImports(libraryName, imports) {
|
|
1479
|
+
const fromEnding = `from '${libraryName}';`;
|
|
1480
|
+
const result = [];
|
|
1481
|
+
if (imports.nsImport !== null) {
|
|
1482
|
+
result.push(`import * as ${imports.nsImport} ${fromEnding}`);
|
|
1483
|
+
}
|
|
1484
|
+
Array.from(imports.requireImports).sort().forEach(
|
|
1485
|
+
(importName) => result.push(`import ${importName} = require('${libraryName}');`)
|
|
1486
|
+
);
|
|
1487
|
+
Array.from(imports.defaultImports).sort().forEach(
|
|
1488
|
+
(importName) => result.push(`import ${importName} ${fromEnding}`)
|
|
1489
|
+
);
|
|
1490
|
+
if (imports.namedImports.size !== 0) {
|
|
1491
|
+
result.push(
|
|
1492
|
+
`import { ${Array.from(imports.namedImports.entries()).map(
|
|
1493
|
+
([localName, importedName]) => renamedImportValue(importedName, localName)
|
|
1494
|
+
).sort().join(", ")} } ${fromEnding}`
|
|
1495
|
+
);
|
|
1496
|
+
}
|
|
1497
|
+
if (imports.reExports.size !== 0) {
|
|
1498
|
+
result.push(
|
|
1499
|
+
`export { ${Array.from(imports.reExports.entries()).map(
|
|
1500
|
+
([localName, importedName]) => renamedImportValue(importedName, localName)
|
|
1501
|
+
).sort().join(", ")} } ${fromEnding}`
|
|
1502
|
+
);
|
|
1503
|
+
}
|
|
1504
|
+
return result;
|
|
1505
|
+
}
|
|
1506
|
+
function generateReferenceTypesDirective(libraries) {
|
|
1507
|
+
return libraries.sort().map((library) => {
|
|
1508
|
+
return `/// <reference types="${library}" />`;
|
|
1509
|
+
}).join("\n");
|
|
1510
|
+
}
|
|
1511
|
+
function spacesToTabs(text) {
|
|
1512
|
+
return text.replace(/^( )+/gm, (substring) => {
|
|
1513
|
+
return " ".repeat(substring.length / 4);
|
|
1514
|
+
});
|
|
1515
|
+
}
|
|
1516
|
+
|
|
1517
|
+
// src/collisions-resolver.ts
|
|
1518
|
+
var ts8 = __toESM(require("typescript"), 1);
|
|
1519
|
+
var renamingSupportedSymbols = [
|
|
1520
|
+
ts8.SymbolFlags.Alias,
|
|
1521
|
+
ts8.SymbolFlags.Variable,
|
|
1522
|
+
ts8.SymbolFlags.Class,
|
|
1523
|
+
ts8.SymbolFlags.Enum,
|
|
1524
|
+
ts8.SymbolFlags.Function,
|
|
1525
|
+
ts8.SymbolFlags.Interface,
|
|
1526
|
+
ts8.SymbolFlags.NamespaceModule,
|
|
1527
|
+
ts8.SymbolFlags.TypeAlias,
|
|
1528
|
+
ts8.SymbolFlags.ValueModule
|
|
1529
|
+
];
|
|
1530
|
+
var CollisionsResolver = class {
|
|
1531
|
+
typeChecker;
|
|
1532
|
+
collisionsMap = /* @__PURE__ */ new Map();
|
|
1533
|
+
generatedNames = /* @__PURE__ */ new Map();
|
|
1534
|
+
constructor(typeChecker) {
|
|
1535
|
+
this.typeChecker = typeChecker;
|
|
1536
|
+
}
|
|
1537
|
+
/**
|
|
1538
|
+
* Adds (or "registers") a top-level {@link identifier} (which takes a top-level scope name to use).
|
|
1539
|
+
*/
|
|
1540
|
+
addTopLevelIdentifier(identifier) {
|
|
1541
|
+
const symbol = getDeclarationNameSymbol(identifier, this.typeChecker);
|
|
1542
|
+
if (symbol === null) {
|
|
1543
|
+
throw new Error(`Something went wrong - cannot find a symbol for top-level identifier ${identifier.getText()} (from ${identifier.parent.parent.getText()})`);
|
|
1544
|
+
}
|
|
1545
|
+
const newLocalName = this.registerSymbol(symbol, identifier.getText());
|
|
1546
|
+
if (newLocalName === null) {
|
|
1547
|
+
throw new Error(`Something went wrong - a symbol ${symbol.name} for top-level identifier ${identifier.getText()} cannot be renamed`);
|
|
1548
|
+
}
|
|
1549
|
+
return newLocalName;
|
|
1550
|
+
}
|
|
1551
|
+
/**
|
|
1552
|
+
* Returns a set of all already registered names for a given {@link symbol}.
|
|
1553
|
+
*/
|
|
1554
|
+
namesForSymbol(symbol) {
|
|
1555
|
+
return this.generatedNames.get(getActualSymbol(symbol, this.typeChecker)) || /* @__PURE__ */ new Set();
|
|
1556
|
+
}
|
|
1557
|
+
/**
|
|
1558
|
+
* Resolves given {@link referencedIdentifier} to a name.
|
|
1559
|
+
* It assumes that a symbol for this identifier has been registered before by calling {@link addTopLevelIdentifier} method.
|
|
1560
|
+
* Otherwise it will return `null`.
|
|
1561
|
+
*
|
|
1562
|
+
* Note that a returned value might be of a different type of the identifier (e.g. {@link ts.QualifiedName} for a given {@link ts.Identifier})
|
|
1563
|
+
*/
|
|
1564
|
+
resolveReferencedIdentifier(referencedIdentifier) {
|
|
1565
|
+
const identifierSymbol = getDeclarationNameSymbol(referencedIdentifier, this.typeChecker);
|
|
1566
|
+
if (identifierSymbol === null) {
|
|
1567
|
+
return null;
|
|
1568
|
+
}
|
|
1569
|
+
const symbolScopePath = this.getSymbolScope(identifierSymbol);
|
|
1570
|
+
const currentIdentifierScope = this.getNodeScope(referencedIdentifier);
|
|
1571
|
+
if (symbolScopePath.length > 0 && currentIdentifierScope.length > 0 && symbolScopePath[0] === currentIdentifierScope[0]) {
|
|
1572
|
+
return referencedIdentifier.getText();
|
|
1573
|
+
}
|
|
1574
|
+
const topLevelIdentifierSymbol = symbolScopePath.length === 0 ? identifierSymbol : symbolScopePath[0];
|
|
1575
|
+
const namesForTopLevelSymbol = this.namesForSymbol(topLevelIdentifierSymbol);
|
|
1576
|
+
if (namesForTopLevelSymbol.size === 0) {
|
|
1577
|
+
return null;
|
|
1578
|
+
}
|
|
1579
|
+
let topLevelName = symbolScopePath.length === 0 ? referencedIdentifier.getText() : topLevelIdentifierSymbol.getName();
|
|
1580
|
+
if (!namesForTopLevelSymbol.has(topLevelName)) {
|
|
1581
|
+
const topLevelNamesArray = Array.from(namesForTopLevelSymbol);
|
|
1582
|
+
let suitableTopLevelName = topLevelNamesArray[0];
|
|
1583
|
+
for (const name of topLevelNamesArray) {
|
|
1584
|
+
if (name.startsWith(`${topLevelName}$`)) {
|
|
1585
|
+
suitableTopLevelName = name;
|
|
1586
|
+
break;
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
topLevelName = suitableTopLevelName;
|
|
1590
|
+
}
|
|
1591
|
+
const newIdentifierParts = [
|
|
1592
|
+
...symbolScopePath.map((symbol) => symbol.getName()),
|
|
1593
|
+
referencedIdentifier.getText()
|
|
1594
|
+
];
|
|
1595
|
+
newIdentifierParts[0] = topLevelName;
|
|
1596
|
+
return newIdentifierParts.join(".");
|
|
1597
|
+
}
|
|
1598
|
+
/**
|
|
1599
|
+
* Similar to {@link resolveReferencedIdentifier}, but works with qualified names (Ns.Ns1.Interface).
|
|
1600
|
+
* The main point of this resolver is that it might change the first part of the qualifier only (as it drives uniqueness of a name).
|
|
1601
|
+
*/
|
|
1602
|
+
resolveReferencedQualifiedName(referencedIdentifier) {
|
|
1603
|
+
let topLevelIdentifier = referencedIdentifier;
|
|
1604
|
+
if (ts8.isQualifiedName(topLevelIdentifier) || ts8.isPropertyAccessExpression(topLevelIdentifier)) {
|
|
1605
|
+
let leftmostIdentifier = ts8.isQualifiedName(topLevelIdentifier) ? topLevelIdentifier.left : topLevelIdentifier.expression;
|
|
1606
|
+
while (ts8.isQualifiedName(leftmostIdentifier) || ts8.isPropertyAccessExpression(leftmostIdentifier)) {
|
|
1607
|
+
leftmostIdentifier = ts8.isQualifiedName(leftmostIdentifier) ? leftmostIdentifier.left : leftmostIdentifier.expression;
|
|
1608
|
+
}
|
|
1609
|
+
topLevelIdentifier = leftmostIdentifier;
|
|
1610
|
+
}
|
|
1611
|
+
const topLevelName = this.resolveReferencedIdentifier(topLevelIdentifier);
|
|
1612
|
+
if (topLevelName === null) {
|
|
1613
|
+
const identifierSymbol = getDeclarationNameSymbol(referencedIdentifier, this.typeChecker);
|
|
1614
|
+
if (identifierSymbol === null) {
|
|
1615
|
+
return null;
|
|
1616
|
+
}
|
|
1617
|
+
const namesForSymbol = this.namesForSymbol(identifierSymbol);
|
|
1618
|
+
if (namesForSymbol.size !== 0) {
|
|
1619
|
+
return Array.from(namesForSymbol)[0];
|
|
1620
|
+
}
|
|
1621
|
+
return null;
|
|
1622
|
+
}
|
|
1623
|
+
const identifierParts = referencedIdentifier.getText().split(".");
|
|
1624
|
+
identifierParts[0] = topLevelName;
|
|
1625
|
+
return identifierParts.join(".");
|
|
1626
|
+
}
|
|
1627
|
+
getSymbolScope(identifierSymbol) {
|
|
1628
|
+
const identifierDeclarations = getDeclarationsForSymbol(identifierSymbol);
|
|
1629
|
+
if (identifierDeclarations.length === 0) {
|
|
1630
|
+
return [];
|
|
1631
|
+
}
|
|
1632
|
+
return this.getNodeScope(identifierDeclarations[0]);
|
|
1633
|
+
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Returns a node's scope where it is located in terms of namespaces/modules.
|
|
1636
|
+
* E.g. A scope for `Opt` in `declare module foo { type Opt = number; }` is `[Symbol(foo)]`
|
|
1637
|
+
*/
|
|
1638
|
+
getNodeScope(node) {
|
|
1639
|
+
const scopeIdentifiersPath = [];
|
|
1640
|
+
let currentNode = getClosestModuleLikeNode(node);
|
|
1641
|
+
while (ts8.isModuleDeclaration(currentNode) && ts8.isIdentifier(currentNode.name)) {
|
|
1642
|
+
const nameSymbol = getDeclarationNameSymbol(currentNode.name, this.typeChecker);
|
|
1643
|
+
if (nameSymbol === null) {
|
|
1644
|
+
throw new Error(`Cannot find symbol for identifier '${currentNode.name.getText()}'`);
|
|
1645
|
+
}
|
|
1646
|
+
scopeIdentifiersPath.push(nameSymbol);
|
|
1647
|
+
currentNode = getClosestModuleLikeNode(currentNode.parent);
|
|
1648
|
+
}
|
|
1649
|
+
return scopeIdentifiersPath.reverse();
|
|
1650
|
+
}
|
|
1651
|
+
registerSymbol(identifierSymbol, preferredName) {
|
|
1652
|
+
if (!renamingSupportedSymbols.some((flag) => identifierSymbol.flags & flag)) {
|
|
1653
|
+
verboseLog(`Symbol ${identifierSymbol.name} cannot be renamed because its flag (${identifierSymbol.flags}) isn't supported`);
|
|
1654
|
+
return null;
|
|
1655
|
+
}
|
|
1656
|
+
if (identifierSymbol.flags & ts8.SymbolFlags.NamespaceModule && identifierSymbol.escapedName === ts8.InternalSymbolName.Global) {
|
|
1657
|
+
return null;
|
|
1658
|
+
}
|
|
1659
|
+
let symbolName = preferredName;
|
|
1660
|
+
if (symbolName === "default") {
|
|
1661
|
+
symbolName = "_default";
|
|
1662
|
+
}
|
|
1663
|
+
const collisionsKey = symbolName;
|
|
1664
|
+
let collisionSymbols = this.collisionsMap.get(collisionsKey);
|
|
1665
|
+
if (collisionSymbols === void 0) {
|
|
1666
|
+
collisionSymbols = /* @__PURE__ */ new Map();
|
|
1667
|
+
this.collisionsMap.set(collisionsKey, collisionSymbols);
|
|
1668
|
+
}
|
|
1669
|
+
const storedSymbolName = collisionSymbols.get(identifierSymbol);
|
|
1670
|
+
if (storedSymbolName !== void 0) {
|
|
1671
|
+
return storedSymbolName;
|
|
1672
|
+
}
|
|
1673
|
+
let nameIndex = collisionSymbols.size;
|
|
1674
|
+
let newName = collisionSymbols.size === 0 ? symbolName : `${symbolName}$${nameIndex}`;
|
|
1675
|
+
let resolvedGlobalSymbol = resolveGlobalName(this.typeChecker, newName);
|
|
1676
|
+
while (resolvedGlobalSymbol !== void 0 && resolvedGlobalSymbol !== identifierSymbol) {
|
|
1677
|
+
nameIndex += 1;
|
|
1678
|
+
newName = `${symbolName}$${nameIndex}`;
|
|
1679
|
+
resolvedGlobalSymbol = resolveGlobalName(this.typeChecker, newName);
|
|
1680
|
+
}
|
|
1681
|
+
collisionSymbols.set(identifierSymbol, newName);
|
|
1682
|
+
let symbolNames = this.generatedNames.get(identifierSymbol);
|
|
1683
|
+
if (symbolNames === void 0) {
|
|
1684
|
+
symbolNames = /* @__PURE__ */ new Set();
|
|
1685
|
+
this.generatedNames.set(identifierSymbol, symbolNames);
|
|
1686
|
+
}
|
|
1687
|
+
symbolNames.add(newName);
|
|
1688
|
+
return newName;
|
|
1689
|
+
}
|
|
1690
|
+
};
|
|
1691
|
+
|
|
1692
|
+
// src/bundle-generator.ts
|
|
1693
|
+
function generateDtsBundle(entries, options = {}) {
|
|
1694
|
+
normalLog("Compiling input files...");
|
|
1695
|
+
const { program, rootFilesRemapping } = compileDts(
|
|
1696
|
+
entries.map((entry) => entry.filePath),
|
|
1697
|
+
options.preferredConfigPath,
|
|
1698
|
+
options.followSymlinks
|
|
1699
|
+
);
|
|
1700
|
+
const typeChecker = program.getTypeChecker();
|
|
1701
|
+
const typeRoots = ts9.getEffectiveTypeRoots(program.getCompilerOptions(), {});
|
|
1702
|
+
const sourceFiles = program.getSourceFiles().filter((file) => {
|
|
1703
|
+
return !program.isSourceFileDefaultLibrary(file);
|
|
1704
|
+
});
|
|
1705
|
+
const typesUsageEvaluator = new TypesUsageEvaluator(sourceFiles, typeChecker);
|
|
1706
|
+
return entries.map((entryConfig) => {
|
|
1707
|
+
normalLog(`Processing ${entryConfig.filePath}`);
|
|
1708
|
+
const newRootFilePath = rootFilesRemapping.get(entryConfig.filePath);
|
|
1709
|
+
if (newRootFilePath === void 0) {
|
|
1710
|
+
throw new Error(`Cannot remap root source file ${entryConfig.filePath}`);
|
|
1711
|
+
}
|
|
1712
|
+
const rootSourceFile = getRootSourceFile(program, newRootFilePath);
|
|
1713
|
+
const rootSourceFileSymbol = typeChecker.getSymbolAtLocation(rootSourceFile);
|
|
1714
|
+
if (rootSourceFileSymbol === void 0) {
|
|
1715
|
+
throw new Error(
|
|
1716
|
+
`Symbol for root source file ${newRootFilePath} not found`
|
|
1717
|
+
);
|
|
1718
|
+
}
|
|
1719
|
+
const librariesOptions = entryConfig.libraries || {};
|
|
1720
|
+
const criteria = {
|
|
1721
|
+
allowedTypesLibraries: librariesOptions.allowedTypesLibraries,
|
|
1722
|
+
importedLibraries: librariesOptions.importedLibraries,
|
|
1723
|
+
inlinedLibraries: librariesOptions.inlinedLibraries || [],
|
|
1724
|
+
typeRoots
|
|
1725
|
+
};
|
|
1726
|
+
const rootFileExports = getExportsForSourceFile(
|
|
1727
|
+
typeChecker,
|
|
1728
|
+
rootSourceFileSymbol
|
|
1729
|
+
);
|
|
1730
|
+
const rootFileExportSymbols = rootFileExports.map(
|
|
1731
|
+
(exp) => exp.symbol
|
|
1732
|
+
);
|
|
1733
|
+
const collectionResult = {
|
|
1734
|
+
typesReferences: /* @__PURE__ */ new Set(),
|
|
1735
|
+
imports: /* @__PURE__ */ new Map(),
|
|
1736
|
+
statements: [],
|
|
1737
|
+
renamedExports: /* @__PURE__ */ new Map(),
|
|
1738
|
+
wrappedNamespaces: /* @__PURE__ */ new Map()
|
|
1739
|
+
};
|
|
1740
|
+
const outputOptions = entryConfig.output || {};
|
|
1741
|
+
const inlineDeclareGlobals = Boolean(outputOptions.inlineDeclareGlobals);
|
|
1742
|
+
const inlineDeclareExternals = Boolean(
|
|
1743
|
+
outputOptions.inlineDeclareExternals
|
|
1744
|
+
);
|
|
1745
|
+
const collisionsResolver = new CollisionsResolver(typeChecker);
|
|
1746
|
+
function updateResultForAnyModule(statements, currentModule) {
|
|
1747
|
+
const visitedModules = /* @__PURE__ */ new Set();
|
|
1748
|
+
function updateResultForExternalExport(exportAssignment) {
|
|
1749
|
+
for (const declaration of getDeclarationsForExportedValues(
|
|
1750
|
+
exportAssignment,
|
|
1751
|
+
typeChecker
|
|
1752
|
+
)) {
|
|
1753
|
+
if (ts9.isVariableDeclaration(declaration)) {
|
|
1754
|
+
continue;
|
|
1755
|
+
}
|
|
1756
|
+
let exportedDeclarations = [];
|
|
1757
|
+
if (ts9.isExportDeclaration(exportAssignment) && ts9.isSourceFile(declaration)) {
|
|
1758
|
+
const referencedModule = getReferencedModuleInfo(
|
|
1759
|
+
exportAssignment,
|
|
1760
|
+
criteria,
|
|
1761
|
+
typeChecker
|
|
1762
|
+
);
|
|
1763
|
+
if (referencedModule !== null) {
|
|
1764
|
+
if (visitedModules.has(referencedModule.fileName)) {
|
|
1765
|
+
continue;
|
|
1766
|
+
}
|
|
1767
|
+
visitedModules.add(referencedModule.fileName);
|
|
1768
|
+
}
|
|
1769
|
+
exportedDeclarations = declaration.statements;
|
|
1770
|
+
} else if (ts9.isModuleDeclaration(declaration)) {
|
|
1771
|
+
if (declaration.body !== void 0 && ts9.isModuleBlock(declaration.body)) {
|
|
1772
|
+
const referencedModule = getReferencedModuleInfo(
|
|
1773
|
+
declaration,
|
|
1774
|
+
criteria,
|
|
1775
|
+
typeChecker
|
|
1776
|
+
);
|
|
1777
|
+
if (referencedModule !== null) {
|
|
1778
|
+
if (visitedModules.has(referencedModule.fileName)) {
|
|
1779
|
+
continue;
|
|
1780
|
+
}
|
|
1781
|
+
visitedModules.add(referencedModule.fileName);
|
|
1782
|
+
}
|
|
1783
|
+
exportedDeclarations = declaration.body.statements;
|
|
1784
|
+
}
|
|
1785
|
+
} else {
|
|
1786
|
+
exportedDeclarations = [declaration];
|
|
1787
|
+
}
|
|
1788
|
+
updateResultImpl(exportedDeclarations);
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
function updateResultImpl(statementsToProcess) {
|
|
1792
|
+
for (const statement of statementsToProcess) {
|
|
1793
|
+
if (statement.kind === ts9.SyntaxKind.ImportDeclaration || statement.kind === ts9.SyntaxKind.ImportEqualsDeclaration) {
|
|
1794
|
+
continue;
|
|
1795
|
+
}
|
|
1796
|
+
if (isDeclareModule(statement)) {
|
|
1797
|
+
updateResultForModuleDeclaration(statement, currentModule);
|
|
1798
|
+
if (ts9.isStringLiteral(statement.name)) {
|
|
1799
|
+
continue;
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
if (currentModule.type === 3 /* ShouldBeUsedForModulesOnly */) {
|
|
1803
|
+
continue;
|
|
1804
|
+
}
|
|
1805
|
+
if (isDeclareGlobalStatement(statement) && inlineDeclareGlobals && currentModule.type === 0 /* ShouldBeInlined */) {
|
|
1806
|
+
collectionResult.statements.push(statement);
|
|
1807
|
+
continue;
|
|
1808
|
+
}
|
|
1809
|
+
if (ts9.isExportDeclaration(statement)) {
|
|
1810
|
+
if (currentModule.type === 0 /* ShouldBeInlined */) {
|
|
1811
|
+
continue;
|
|
1812
|
+
}
|
|
1813
|
+
if (statement.exportClause === void 0) {
|
|
1814
|
+
updateResultForExternalExport(statement);
|
|
1815
|
+
continue;
|
|
1816
|
+
}
|
|
1817
|
+
if (ts9.isNamedExports(statement.exportClause) && currentModule.type === 1 /* ShouldBeImported */) {
|
|
1818
|
+
updateImportsForStatement(statement);
|
|
1819
|
+
continue;
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
if (ts9.isExportAssignment(statement) && statement.isExportEquals && currentModule.type !== 0 /* ShouldBeInlined */) {
|
|
1823
|
+
updateResultForExternalExport(statement);
|
|
1824
|
+
continue;
|
|
1825
|
+
}
|
|
1826
|
+
if (!isNodeUsed(statement)) {
|
|
1827
|
+
continue;
|
|
1828
|
+
}
|
|
1829
|
+
switch (currentModule.type) {
|
|
1830
|
+
case 2 /* ShouldBeReferencedAsTypes */:
|
|
1831
|
+
forEachNodeThatShouldBeImported(
|
|
1832
|
+
statement,
|
|
1833
|
+
() => addTypesReference(currentModule.typesLibraryName)
|
|
1834
|
+
);
|
|
1835
|
+
break;
|
|
1836
|
+
case 1 /* ShouldBeImported */:
|
|
1837
|
+
updateImportsForStatement(statement);
|
|
1838
|
+
break;
|
|
1839
|
+
case 0 /* ShouldBeInlined */:
|
|
1840
|
+
if (ts9.isVariableStatement(statement)) {
|
|
1841
|
+
for (const variableDeclaration of statement.declarationList.declarations) {
|
|
1842
|
+
if (ts9.isIdentifier(variableDeclaration.name)) {
|
|
1843
|
+
collisionsResolver.addTopLevelIdentifier(
|
|
1844
|
+
variableDeclaration.name
|
|
1845
|
+
);
|
|
1846
|
+
continue;
|
|
1847
|
+
}
|
|
1848
|
+
for (const element of variableDeclaration.name.elements) {
|
|
1849
|
+
if (!ts9.isOmittedExpression(element) && ts9.isIdentifier(element.name)) {
|
|
1850
|
+
collisionsResolver.addTopLevelIdentifier(element.name);
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
} else if (isNodeNamedDeclaration(statement)) {
|
|
1855
|
+
const statementName = getNodeName(statement);
|
|
1856
|
+
if (statementName !== void 0) {
|
|
1857
|
+
collisionsResolver.addTopLevelIdentifier(
|
|
1858
|
+
statementName
|
|
1859
|
+
);
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
collectionResult.statements.push(statement);
|
|
1863
|
+
break;
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
}
|
|
1867
|
+
updateResultImpl(statements);
|
|
1868
|
+
}
|
|
1869
|
+
function isReferencedModuleImportable(statement) {
|
|
1870
|
+
return getReferencedModuleInfo(statement, criteria, typeChecker)?.type === 1 /* ShouldBeImported */;
|
|
1871
|
+
}
|
|
1872
|
+
function handleExportDeclarationFromRootModule(exportDeclaration) {
|
|
1873
|
+
function handleExportStarStatement(exportStarStatement, visitedSymbols = /* @__PURE__ */ new Set()) {
|
|
1874
|
+
if (exportStarStatement.moduleSpecifier === void 0 || exportStarStatement.exportClause !== void 0) {
|
|
1875
|
+
throw new Error(
|
|
1876
|
+
`Invalid export-star declaration statement provided, ${exportStarStatement.getText()}`
|
|
1877
|
+
);
|
|
1878
|
+
}
|
|
1879
|
+
const importModuleSpecifier = getImportModuleName(exportStarStatement);
|
|
1880
|
+
if (importModuleSpecifier === null) {
|
|
1881
|
+
return;
|
|
1882
|
+
}
|
|
1883
|
+
const referencedModuleInfo = getReferencedModuleInfo(
|
|
1884
|
+
exportStarStatement,
|
|
1885
|
+
criteria,
|
|
1886
|
+
typeChecker
|
|
1887
|
+
);
|
|
1888
|
+
if (referencedModuleInfo === null) {
|
|
1889
|
+
return;
|
|
1890
|
+
}
|
|
1891
|
+
switch (referencedModuleInfo.type) {
|
|
1892
|
+
case 0 /* ShouldBeInlined */: {
|
|
1893
|
+
const referencedModuleSymbol = getNodeOwnSymbol(
|
|
1894
|
+
exportStarStatement.moduleSpecifier,
|
|
1895
|
+
typeChecker
|
|
1896
|
+
);
|
|
1897
|
+
const referencedSourceFileExportStarSymbol = referencedModuleSymbol.exports?.get(
|
|
1898
|
+
ts9.InternalSymbolName.ExportStar
|
|
1899
|
+
);
|
|
1900
|
+
if (referencedSourceFileExportStarSymbol !== void 0) {
|
|
1901
|
+
if (visitedSymbols.has(referencedSourceFileExportStarSymbol)) {
|
|
1902
|
+
return;
|
|
1903
|
+
}
|
|
1904
|
+
visitedSymbols.add(referencedSourceFileExportStarSymbol);
|
|
1905
|
+
for (const exportDecl of getSymbolExportStarDeclarations(
|
|
1906
|
+
referencedSourceFileExportStarSymbol
|
|
1907
|
+
)) {
|
|
1908
|
+
handleExportStarStatement(exportDecl, visitedSymbols);
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
break;
|
|
1912
|
+
}
|
|
1913
|
+
case 1 /* ShouldBeImported */: {
|
|
1914
|
+
collectionResult.statements.push(exportStarStatement);
|
|
1915
|
+
break;
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1919
|
+
function findExportingExportStarExportFromImportableModule(referencedModuleSymbol, nodeSymbol) {
|
|
1920
|
+
function findResultRecursively(referencedModuleSym, exportedNodeSym, visitedSymbols) {
|
|
1921
|
+
if (visitedSymbols.has(referencedModuleSym)) {
|
|
1922
|
+
return null;
|
|
1923
|
+
}
|
|
1924
|
+
visitedSymbols.add(referencedModuleSym);
|
|
1925
|
+
const exportStarExport = referencedModuleSym.exports?.get(
|
|
1926
|
+
ts9.InternalSymbolName.ExportStar
|
|
1927
|
+
);
|
|
1928
|
+
if (exportStarExport === void 0) {
|
|
1929
|
+
return null;
|
|
1930
|
+
}
|
|
1931
|
+
for (const exportStarDeclaration of getDeclarationsForSymbol(
|
|
1932
|
+
exportStarExport
|
|
1933
|
+
).filter(ts9.isExportDeclaration)) {
|
|
1934
|
+
if (exportStarDeclaration.moduleSpecifier === void 0) {
|
|
1935
|
+
continue;
|
|
1936
|
+
}
|
|
1937
|
+
const exportStarModuleSymbol = getNodeOwnSymbol(
|
|
1938
|
+
exportStarDeclaration.moduleSpecifier,
|
|
1939
|
+
typeChecker
|
|
1940
|
+
);
|
|
1941
|
+
if (exportStarModuleSymbol.exports === void 0) {
|
|
1942
|
+
continue;
|
|
1943
|
+
}
|
|
1944
|
+
if (isReferencedModuleImportable(exportStarDeclaration)) {
|
|
1945
|
+
const referencedModuleExports = typeChecker.getExportsOfModule(
|
|
1946
|
+
exportStarModuleSymbol
|
|
1947
|
+
);
|
|
1948
|
+
const exportedNodeSymbol = referencedModuleExports.find(
|
|
1949
|
+
(exp) => getActualSymbol(exp, typeChecker) === nodeSymbol
|
|
1950
|
+
);
|
|
1951
|
+
if (exportedNodeSymbol !== void 0) {
|
|
1952
|
+
return { exportStarDeclaration, exportedNodeSymbol };
|
|
1953
|
+
}
|
|
1954
|
+
continue;
|
|
1955
|
+
}
|
|
1956
|
+
const result = findResultRecursively(
|
|
1957
|
+
exportStarModuleSymbol,
|
|
1958
|
+
exportedNodeSym,
|
|
1959
|
+
visitedSymbols
|
|
1960
|
+
);
|
|
1961
|
+
if (result !== null) {
|
|
1962
|
+
return result;
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
return null;
|
|
1966
|
+
}
|
|
1967
|
+
if (referencedModuleSymbol.exports === void 0) {
|
|
1968
|
+
throw new Error(
|
|
1969
|
+
`No exports found for "${referencedModuleSymbol.getName()}" symbol`
|
|
1970
|
+
);
|
|
1971
|
+
}
|
|
1972
|
+
const hasExplicitExportOfSymbol = Array.from(
|
|
1973
|
+
referencedModuleSymbol.exports.values()
|
|
1974
|
+
).some((exp) => {
|
|
1975
|
+
if (exp.escapedName === ts9.InternalSymbolName.ExportStar) {
|
|
1976
|
+
return false;
|
|
1977
|
+
}
|
|
1978
|
+
return getActualSymbol(exp, typeChecker) === nodeSymbol;
|
|
1979
|
+
});
|
|
1980
|
+
if (hasExplicitExportOfSymbol) {
|
|
1981
|
+
return null;
|
|
1982
|
+
}
|
|
1983
|
+
return findResultRecursively(
|
|
1984
|
+
referencedModuleSymbol,
|
|
1985
|
+
nodeSymbol,
|
|
1986
|
+
/* @__PURE__ */ new Set()
|
|
1987
|
+
);
|
|
1988
|
+
}
|
|
1989
|
+
if (exportDeclaration.exportClause === void 0) {
|
|
1990
|
+
handleExportStarStatement(exportDeclaration);
|
|
1991
|
+
return;
|
|
1992
|
+
}
|
|
1993
|
+
if (exportDeclaration.exportClause !== void 0 && ts9.isNamedExports(exportDeclaration.exportClause)) {
|
|
1994
|
+
if (exportDeclaration.moduleSpecifier === void 0) {
|
|
1995
|
+
for (const exportElement of exportDeclaration.exportClause.elements) {
|
|
1996
|
+
const exportElementSymbol = getImportExportReferencedSymbol(
|
|
1997
|
+
exportElement,
|
|
1998
|
+
typeChecker
|
|
1999
|
+
);
|
|
2000
|
+
const namespaceImportFromImportableModule = getDeclarationsForSymbol(exportElementSymbol).find(
|
|
2001
|
+
(importDecl) => {
|
|
2002
|
+
return ts9.isNamespaceImport(importDecl) && isReferencedModuleImportable(
|
|
2003
|
+
importDecl.parent.parent
|
|
2004
|
+
);
|
|
2005
|
+
}
|
|
2006
|
+
);
|
|
2007
|
+
if (namespaceImportFromImportableModule !== void 0) {
|
|
2008
|
+
const importModuleSpecifier = getImportModuleName(
|
|
2009
|
+
namespaceImportFromImportableModule.parent.parent
|
|
2010
|
+
);
|
|
2011
|
+
if (importModuleSpecifier === null) {
|
|
2012
|
+
throw new Error(
|
|
2013
|
+
`Cannot get import module name from '${namespaceImportFromImportableModule.parent.parent.getText()}'`
|
|
2014
|
+
);
|
|
2015
|
+
}
|
|
2016
|
+
addNsImport(
|
|
2017
|
+
getImportItem(importModuleSpecifier),
|
|
2018
|
+
namespaceImportFromImportableModule.name
|
|
2019
|
+
);
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
return;
|
|
2023
|
+
}
|
|
2024
|
+
if (exportDeclaration.moduleSpecifier !== void 0) {
|
|
2025
|
+
const referencedModuleSymbol = getNodeOwnSymbol(
|
|
2026
|
+
exportDeclaration.moduleSpecifier,
|
|
2027
|
+
typeChecker
|
|
2028
|
+
);
|
|
2029
|
+
for (const exportElement of exportDeclaration.exportClause.elements) {
|
|
2030
|
+
const exportedNodeSymbol = getActualSymbol(
|
|
2031
|
+
getImportExportReferencedSymbol(exportElement, typeChecker),
|
|
2032
|
+
typeChecker
|
|
2033
|
+
);
|
|
2034
|
+
const exportingExportStarResult = findExportingExportStarExportFromImportableModule(
|
|
2035
|
+
referencedModuleSymbol,
|
|
2036
|
+
exportedNodeSymbol
|
|
2037
|
+
);
|
|
2038
|
+
if (exportingExportStarResult === null) {
|
|
2039
|
+
continue;
|
|
2040
|
+
}
|
|
2041
|
+
const importModuleSpecifier = getImportModuleName(
|
|
2042
|
+
exportingExportStarResult.exportStarDeclaration
|
|
2043
|
+
);
|
|
2044
|
+
if (importModuleSpecifier === null) {
|
|
2045
|
+
throw new Error(
|
|
2046
|
+
`Cannot get import module name from '${exportingExportStarResult.exportStarDeclaration.getText()}'`
|
|
2047
|
+
);
|
|
2048
|
+
}
|
|
2049
|
+
addReExport(
|
|
2050
|
+
getImportItem(importModuleSpecifier),
|
|
2051
|
+
exportingExportStarResult.exportedNodeSymbol.getName(),
|
|
2052
|
+
exportElement.name.text
|
|
2053
|
+
);
|
|
2054
|
+
}
|
|
2055
|
+
return;
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
function updateResultForRootModule(statements, currentModule) {
|
|
2060
|
+
updateResultForAnyModule(statements, currentModule);
|
|
2061
|
+
for (const statement of statements) {
|
|
2062
|
+
if (ts9.isExportDeclaration(statement)) {
|
|
2063
|
+
handleExportDeclarationFromRootModule(statement);
|
|
2064
|
+
continue;
|
|
2065
|
+
}
|
|
2066
|
+
if (ts9.isExportAssignment(statement)) {
|
|
2067
|
+
if (statement.isExportEquals || !ts9.isIdentifier(statement.expression)) {
|
|
2068
|
+
collectionResult.statements.push(statement);
|
|
2069
|
+
}
|
|
2070
|
+
continue;
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
function updateResultForModuleDeclaration(moduleDecl, currentModule) {
|
|
2075
|
+
if (moduleDecl.body === void 0 || !ts9.isModuleBlock(moduleDecl.body)) {
|
|
2076
|
+
return;
|
|
2077
|
+
}
|
|
2078
|
+
const referencedModuleInfo = getModuleLikeModuleInfo(
|
|
2079
|
+
moduleDecl,
|
|
2080
|
+
criteria,
|
|
2081
|
+
typeChecker
|
|
2082
|
+
);
|
|
2083
|
+
if (referencedModuleInfo === null) {
|
|
2084
|
+
return;
|
|
2085
|
+
}
|
|
2086
|
+
if (!currentModule.isExternal && referencedModuleInfo.isExternal) {
|
|
2087
|
+
if (inlineDeclareExternals) {
|
|
2088
|
+
collectionResult.statements.push(moduleDecl);
|
|
2089
|
+
}
|
|
2090
|
+
return;
|
|
2091
|
+
}
|
|
2092
|
+
updateResultForAnyModule(
|
|
2093
|
+
moduleDecl.body.statements,
|
|
2094
|
+
referencedModuleInfo
|
|
2095
|
+
);
|
|
2096
|
+
}
|
|
2097
|
+
function addTypesReference(library) {
|
|
2098
|
+
if (!collectionResult.typesReferences.has(library)) {
|
|
2099
|
+
normalLog(`Library "${library}" will be added via reference directive`);
|
|
2100
|
+
collectionResult.typesReferences.add(library);
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
function forEachNodeThatShouldBeImported(statement, callback) {
|
|
2104
|
+
const statementsToImport = ts9.isVariableStatement(statement) ? statement.declarationList.declarations : ts9.isExportDeclaration(statement) && statement.exportClause !== void 0 ? ts9.isNamespaceExport(statement.exportClause) ? [statement.exportClause] : statement.exportClause.elements : [statement];
|
|
2105
|
+
for (const statementToImport of statementsToImport) {
|
|
2106
|
+
if (shouldNodeBeImported(statementToImport)) {
|
|
2107
|
+
callback(statementToImport);
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
}
|
|
2111
|
+
function updateImportsForStatement(statement) {
|
|
2112
|
+
forEachNodeThatShouldBeImported(
|
|
2113
|
+
statement,
|
|
2114
|
+
(statementToImport) => {
|
|
2115
|
+
addImport(statementToImport);
|
|
2116
|
+
const sourceFile = statementToImport.getSourceFile();
|
|
2117
|
+
const moduleInfo = getFileModuleInfo(sourceFile.fileName, criteria);
|
|
2118
|
+
if (moduleInfo.type === 2 /* ShouldBeReferencedAsTypes */) {
|
|
2119
|
+
addTypesReference(moduleInfo.typesLibraryName);
|
|
2120
|
+
}
|
|
2121
|
+
}
|
|
2122
|
+
);
|
|
2123
|
+
}
|
|
2124
|
+
function getDeclarationUsagesSourceFiles(declaration) {
|
|
2125
|
+
return new Set(
|
|
2126
|
+
getExportedSymbolsUsingStatement(declaration).map((symbol) => getDeclarationsForSymbol(symbol)).reduce(
|
|
2127
|
+
(acc, val) => acc.concat(val),
|
|
2128
|
+
[]
|
|
2129
|
+
).map(getClosestModuleLikeNode)
|
|
2130
|
+
);
|
|
2131
|
+
}
|
|
2132
|
+
function getImportItem(importModuleSpecifier) {
|
|
2133
|
+
let importItem = collectionResult.imports.get(importModuleSpecifier);
|
|
2134
|
+
if (importItem === void 0) {
|
|
2135
|
+
importItem = {
|
|
2136
|
+
defaultImports: /* @__PURE__ */ new Set(),
|
|
2137
|
+
namedImports: /* @__PURE__ */ new Map(),
|
|
2138
|
+
nsImport: null,
|
|
2139
|
+
requireImports: /* @__PURE__ */ new Set(),
|
|
2140
|
+
reExports: /* @__PURE__ */ new Map()
|
|
2141
|
+
};
|
|
2142
|
+
collectionResult.imports.set(importModuleSpecifier, importItem);
|
|
2143
|
+
}
|
|
2144
|
+
return importItem;
|
|
2145
|
+
}
|
|
2146
|
+
function addRequireImport(importItem, preferredLocalName) {
|
|
2147
|
+
importItem.requireImports.add(
|
|
2148
|
+
collisionsResolver.addTopLevelIdentifier(preferredLocalName)
|
|
2149
|
+
);
|
|
2150
|
+
}
|
|
2151
|
+
function addNamedImport(importItem, preferredLocalName, importedIdentifier) {
|
|
2152
|
+
const newLocalName = collisionsResolver.addTopLevelIdentifier(preferredLocalName);
|
|
2153
|
+
const importedName = importedIdentifier.text;
|
|
2154
|
+
importItem.namedImports.set(newLocalName, importedName);
|
|
2155
|
+
}
|
|
2156
|
+
function addReExport(importItem, moduleExportedName, reExportedName) {
|
|
2157
|
+
importItem.reExports.set(reExportedName, moduleExportedName);
|
|
2158
|
+
}
|
|
2159
|
+
function addNsImport(importItem, preferredLocalName) {
|
|
2160
|
+
if (importItem.nsImport === null) {
|
|
2161
|
+
importItem.nsImport = collisionsResolver.addTopLevelIdentifier(preferredLocalName);
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
function addDefaultImport(importItem, preferredLocalName) {
|
|
2165
|
+
importItem.defaultImports.add(
|
|
2166
|
+
collisionsResolver.addTopLevelIdentifier(preferredLocalName)
|
|
2167
|
+
);
|
|
2168
|
+
}
|
|
2169
|
+
function addImport(statement) {
|
|
2170
|
+
forEachImportOfStatement(
|
|
2171
|
+
statement,
|
|
2172
|
+
(imp, referencedModuleInfo, importModuleSpecifier) => {
|
|
2173
|
+
if (referencedModuleInfo.type !== 1 /* ShouldBeImported */) {
|
|
2174
|
+
return;
|
|
2175
|
+
}
|
|
2176
|
+
const importItem = getImportItem(importModuleSpecifier);
|
|
2177
|
+
if (ts9.isImportEqualsDeclaration(imp)) {
|
|
2178
|
+
addRequireImport(importItem, imp.name);
|
|
2179
|
+
return;
|
|
2180
|
+
}
|
|
2181
|
+
if (ts9.isExportSpecifier(imp)) {
|
|
2182
|
+
addNamedImport(
|
|
2183
|
+
importItem,
|
|
2184
|
+
imp.name,
|
|
2185
|
+
imp.propertyName || imp.name
|
|
2186
|
+
);
|
|
2187
|
+
return;
|
|
2188
|
+
}
|
|
2189
|
+
if (ts9.isNamespaceExport(imp)) {
|
|
2190
|
+
addNsImport(importItem, imp.name);
|
|
2191
|
+
return;
|
|
2192
|
+
}
|
|
2193
|
+
if (ts9.isImportClause(imp) && imp.name !== void 0) {
|
|
2194
|
+
addDefaultImport(importItem, imp.name);
|
|
2195
|
+
return;
|
|
2196
|
+
}
|
|
2197
|
+
if (ts9.isImportSpecifier(imp)) {
|
|
2198
|
+
addNamedImport(
|
|
2199
|
+
importItem,
|
|
2200
|
+
imp.name,
|
|
2201
|
+
imp.propertyName || imp.name
|
|
2202
|
+
);
|
|
2203
|
+
return;
|
|
2204
|
+
}
|
|
2205
|
+
if (ts9.isNamespaceImport(imp)) {
|
|
2206
|
+
addNsImport(importItem, imp.name);
|
|
2207
|
+
return;
|
|
2208
|
+
}
|
|
2209
|
+
}
|
|
2210
|
+
);
|
|
2211
|
+
}
|
|
2212
|
+
function forEachImportOfStatement(statement, callback) {
|
|
2213
|
+
if (!ts9.isSourceFile(statement) && statement.name === void 0) {
|
|
2214
|
+
throw new Error(
|
|
2215
|
+
`Import/usage unnamed declaration: ${statement.getText()}`
|
|
2216
|
+
);
|
|
2217
|
+
}
|
|
2218
|
+
getDeclarationUsagesSourceFiles(statement).forEach(
|
|
2219
|
+
(sourceFile) => {
|
|
2220
|
+
if (getModuleLikeModuleInfo(sourceFile, criteria, typeChecker).type !== 0 /* ShouldBeInlined */) {
|
|
2221
|
+
return;
|
|
2222
|
+
}
|
|
2223
|
+
const sourceFileStatements = ts9.isSourceFile(
|
|
2224
|
+
sourceFile
|
|
2225
|
+
) ? sourceFile.statements : sourceFile.body !== void 0 && ts9.isModuleBlock(sourceFile.body) ? sourceFile.body.statements : [];
|
|
2226
|
+
sourceFileStatements.forEach((st) => {
|
|
2227
|
+
if (!ts9.isImportEqualsDeclaration(st) && !ts9.isImportDeclaration(st) && !ts9.isExportDeclaration(st)) {
|
|
2228
|
+
return;
|
|
2229
|
+
}
|
|
2230
|
+
const importModuleSpecifier = getImportModuleName(st);
|
|
2231
|
+
if (importModuleSpecifier === null) {
|
|
2232
|
+
return;
|
|
2233
|
+
}
|
|
2234
|
+
const referencedModuleInfo = getReferencedModuleInfo(
|
|
2235
|
+
st,
|
|
2236
|
+
criteria,
|
|
2237
|
+
typeChecker
|
|
2238
|
+
);
|
|
2239
|
+
if (referencedModuleInfo === null) {
|
|
2240
|
+
return;
|
|
2241
|
+
}
|
|
2242
|
+
if (ts9.isImportEqualsDeclaration(st)) {
|
|
2243
|
+
if (areDeclarationSame(statement, st)) {
|
|
2244
|
+
callback(st, referencedModuleInfo, importModuleSpecifier);
|
|
2245
|
+
}
|
|
2246
|
+
return;
|
|
2247
|
+
}
|
|
2248
|
+
if (ts9.isExportDeclaration(st) && st.exportClause !== void 0) {
|
|
2249
|
+
if (ts9.isNamedExports(st.exportClause)) {
|
|
2250
|
+
st.exportClause.elements.filter(areDeclarationSame.bind(null, statement)).forEach((specifier) => {
|
|
2251
|
+
callback(
|
|
2252
|
+
specifier,
|
|
2253
|
+
referencedModuleInfo,
|
|
2254
|
+
importModuleSpecifier
|
|
2255
|
+
);
|
|
2256
|
+
});
|
|
2257
|
+
} else {
|
|
2258
|
+
if (isNodeUsed(st.exportClause)) {
|
|
2259
|
+
callback(
|
|
2260
|
+
st.exportClause,
|
|
2261
|
+
referencedModuleInfo,
|
|
2262
|
+
importModuleSpecifier
|
|
2263
|
+
);
|
|
2264
|
+
}
|
|
2265
|
+
}
|
|
2266
|
+
} else if (ts9.isImportDeclaration(st) && st.importClause !== void 0) {
|
|
2267
|
+
if (st.importClause.name !== void 0 && areDeclarationSame(statement, st.importClause)) {
|
|
2268
|
+
callback(
|
|
2269
|
+
st.importClause,
|
|
2270
|
+
referencedModuleInfo,
|
|
2271
|
+
importModuleSpecifier
|
|
2272
|
+
);
|
|
2273
|
+
}
|
|
2274
|
+
if (st.importClause.namedBindings !== void 0) {
|
|
2275
|
+
if (ts9.isNamedImports(st.importClause.namedBindings)) {
|
|
2276
|
+
st.importClause.namedBindings.elements.filter(areDeclarationSame.bind(null, statement)).forEach((specifier) => {
|
|
2277
|
+
callback(
|
|
2278
|
+
specifier,
|
|
2279
|
+
referencedModuleInfo,
|
|
2280
|
+
importModuleSpecifier
|
|
2281
|
+
);
|
|
2282
|
+
});
|
|
2283
|
+
} else {
|
|
2284
|
+
if (isNodeUsed(st.importClause)) {
|
|
2285
|
+
callback(
|
|
2286
|
+
st.importClause.namedBindings,
|
|
2287
|
+
referencedModuleInfo,
|
|
2288
|
+
importModuleSpecifier
|
|
2289
|
+
);
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
});
|
|
2295
|
+
}
|
|
2296
|
+
);
|
|
2297
|
+
}
|
|
2298
|
+
function getInlinedSymbolsUsingSymbol(symbol, predicate) {
|
|
2299
|
+
return Array.from(
|
|
2300
|
+
typesUsageEvaluator.getSymbolsUsingSymbol(symbol) ?? []
|
|
2301
|
+
).filter((usedInSymbol) => {
|
|
2302
|
+
if (!predicate(usedInSymbol)) {
|
|
2303
|
+
return false;
|
|
2304
|
+
}
|
|
2305
|
+
return getDeclarationsForSymbol(usedInSymbol).some(
|
|
2306
|
+
(decl) => {
|
|
2307
|
+
const closestModuleLike = getClosestSourceFileLikeNode(decl);
|
|
2308
|
+
const moduleInfo = getModuleLikeModuleInfo(
|
|
2309
|
+
closestModuleLike,
|
|
2310
|
+
criteria,
|
|
2311
|
+
typeChecker
|
|
2312
|
+
);
|
|
2313
|
+
return moduleInfo.type === 0 /* ShouldBeInlined */;
|
|
2314
|
+
}
|
|
2315
|
+
);
|
|
2316
|
+
});
|
|
2317
|
+
}
|
|
2318
|
+
function isSymbolUsedByInlinedSymbols(symbol, predicate, visitedSymbols = /* @__PURE__ */ new Set()) {
|
|
2319
|
+
if (visitedSymbols.has(symbol)) {
|
|
2320
|
+
return false;
|
|
2321
|
+
}
|
|
2322
|
+
visitedSymbols.add(symbol);
|
|
2323
|
+
return Array.from(
|
|
2324
|
+
typesUsageEvaluator.getSymbolsUsingSymbol(symbol) ?? []
|
|
2325
|
+
).some((usedInSymbol) => {
|
|
2326
|
+
if (!predicate(usedInSymbol)) {
|
|
2327
|
+
return isSymbolUsedByInlinedSymbols(
|
|
2328
|
+
usedInSymbol,
|
|
2329
|
+
predicate,
|
|
2330
|
+
visitedSymbols
|
|
2331
|
+
);
|
|
2332
|
+
}
|
|
2333
|
+
const usedByThisSymbol = getDeclarationsForSymbol(usedInSymbol).some(
|
|
2334
|
+
(decl) => {
|
|
2335
|
+
const closestModuleLike = getClosestSourceFileLikeNode(decl);
|
|
2336
|
+
const moduleInfo = getModuleLikeModuleInfo(
|
|
2337
|
+
closestModuleLike,
|
|
2338
|
+
criteria,
|
|
2339
|
+
typeChecker
|
|
2340
|
+
);
|
|
2341
|
+
return moduleInfo.type === 0 /* ShouldBeInlined */;
|
|
2342
|
+
}
|
|
2343
|
+
);
|
|
2344
|
+
if (usedByThisSymbol) {
|
|
2345
|
+
return true;
|
|
2346
|
+
}
|
|
2347
|
+
return isSymbolUsedByInlinedSymbols(
|
|
2348
|
+
usedInSymbol,
|
|
2349
|
+
predicate,
|
|
2350
|
+
visitedSymbols
|
|
2351
|
+
);
|
|
2352
|
+
});
|
|
2353
|
+
}
|
|
2354
|
+
function isSymbolUsedByRootFileExports(symbol) {
|
|
2355
|
+
return rootFileExportSymbols.some(
|
|
2356
|
+
(rootSymbol) => typesUsageEvaluator.isSymbolUsedBySymbol(symbol, rootSymbol)
|
|
2357
|
+
);
|
|
2358
|
+
}
|
|
2359
|
+
function isSymbolForGlobalDeclaration(symbol) {
|
|
2360
|
+
return symbol.escapedName === ts9.InternalSymbolName.Global;
|
|
2361
|
+
}
|
|
2362
|
+
function isSymbolForDeclareModuleDeclaration(symbol) {
|
|
2363
|
+
return getDeclarationsForSymbol(symbol).some(isDeclareModule);
|
|
2364
|
+
}
|
|
2365
|
+
function isNodeUsed(node) {
|
|
2366
|
+
if (isNodeNamedDeclaration(node) || ts9.isSourceFile(node)) {
|
|
2367
|
+
const nodeSymbol = getNodeSymbol(node, typeChecker);
|
|
2368
|
+
if (nodeSymbol === null) {
|
|
2369
|
+
return false;
|
|
2370
|
+
}
|
|
2371
|
+
const nodeUsedByDirectExports = isSymbolUsedByRootFileExports(nodeSymbol);
|
|
2372
|
+
if (nodeUsedByDirectExports) {
|
|
2373
|
+
return true;
|
|
2374
|
+
}
|
|
2375
|
+
if (inlineDeclareGlobals && isSymbolUsedByInlinedSymbols(nodeSymbol, isSymbolForGlobalDeclaration)) {
|
|
2376
|
+
return true;
|
|
2377
|
+
}
|
|
2378
|
+
if (inlineDeclareExternals && isSymbolUsedByInlinedSymbols(
|
|
2379
|
+
nodeSymbol,
|
|
2380
|
+
isSymbolForDeclareModuleDeclaration
|
|
2381
|
+
)) {
|
|
2382
|
+
return true;
|
|
2383
|
+
}
|
|
2384
|
+
return false;
|
|
2385
|
+
}
|
|
2386
|
+
if (ts9.isVariableStatement(node)) {
|
|
2387
|
+
return node.declarationList.declarations.some(
|
|
2388
|
+
(declaration) => {
|
|
2389
|
+
if (ts9.isObjectBindingPattern(declaration.name) || ts9.isArrayBindingPattern(declaration.name)) {
|
|
2390
|
+
return declaration.name.elements.some(isNodeUsed);
|
|
2391
|
+
}
|
|
2392
|
+
return isNodeUsed(declaration);
|
|
2393
|
+
}
|
|
2394
|
+
);
|
|
2395
|
+
}
|
|
2396
|
+
if (ts9.isExportDeclaration(node) && node.exportClause !== void 0 && ts9.isNamespaceExport(node.exportClause)) {
|
|
2397
|
+
return isNodeUsed(node.exportClause);
|
|
2398
|
+
}
|
|
2399
|
+
if (ts9.isImportClause(node) && node.namedBindings !== void 0) {
|
|
2400
|
+
return isNodeUsed(node.namedBindings);
|
|
2401
|
+
}
|
|
2402
|
+
return false;
|
|
2403
|
+
}
|
|
2404
|
+
function shouldNodeBeImported(node) {
|
|
2405
|
+
const nodeSymbol = getNodeSymbol(node, typeChecker);
|
|
2406
|
+
if (nodeSymbol === null) {
|
|
2407
|
+
return false;
|
|
2408
|
+
}
|
|
2409
|
+
return shouldSymbolBeImported(nodeSymbol);
|
|
2410
|
+
}
|
|
2411
|
+
function shouldSymbolBeImported(nodeSymbol) {
|
|
2412
|
+
const isSymbolDeclaredInDefaultLibrary = getDeclarationsForSymbol(
|
|
2413
|
+
nodeSymbol
|
|
2414
|
+
).some(
|
|
2415
|
+
(declaration) => program.isSourceFileDefaultLibrary(declaration.getSourceFile())
|
|
2416
|
+
);
|
|
2417
|
+
if (isSymbolDeclaredInDefaultLibrary) {
|
|
2418
|
+
return false;
|
|
2419
|
+
}
|
|
2420
|
+
const symbolsDeclarations = getDeclarationsForSymbol(nodeSymbol);
|
|
2421
|
+
const shouldSymbolBeInlined = symbolsDeclarations.every(
|
|
2422
|
+
(decl) => getModuleLikeModuleInfo(
|
|
2423
|
+
getClosestSourceFileLikeNode(decl),
|
|
2424
|
+
criteria,
|
|
2425
|
+
typeChecker
|
|
2426
|
+
).type === 0 /* ShouldBeInlined */
|
|
2427
|
+
);
|
|
2428
|
+
if (shouldSymbolBeInlined) {
|
|
2429
|
+
return false;
|
|
2430
|
+
}
|
|
2431
|
+
return getExportedSymbolsUsingSymbol(nodeSymbol).length !== 0;
|
|
2432
|
+
}
|
|
2433
|
+
function getExportedSymbolsUsingStatement(node) {
|
|
2434
|
+
const nodeSymbol = getNodeSymbol(node, typeChecker);
|
|
2435
|
+
if (nodeSymbol === null) {
|
|
2436
|
+
return [];
|
|
2437
|
+
}
|
|
2438
|
+
return getExportedSymbolsUsingSymbol(nodeSymbol);
|
|
2439
|
+
}
|
|
2440
|
+
function getExportedSymbolsUsingSymbol(nodeSymbol) {
|
|
2441
|
+
const symbolsUsingNode = typesUsageEvaluator.getSymbolsUsingSymbol(nodeSymbol);
|
|
2442
|
+
if (symbolsUsingNode === null) {
|
|
2443
|
+
throw new Error(
|
|
2444
|
+
`Something went wrong - getSymbolsUsingSymbol returned null but expected to be a set of symbols (symbol=${nodeSymbol.name})`
|
|
2445
|
+
);
|
|
2446
|
+
}
|
|
2447
|
+
return [
|
|
2448
|
+
...rootFileExportSymbols.includes(nodeSymbol) ? [nodeSymbol] : [],
|
|
2449
|
+
// symbols which are used in types directly
|
|
2450
|
+
...getInlinedSymbolsUsingSymbol(
|
|
2451
|
+
nodeSymbol,
|
|
2452
|
+
isSymbolUsedByRootFileExports
|
|
2453
|
+
),
|
|
2454
|
+
// symbols which are used in global types i.e. in `declare global`s
|
|
2455
|
+
...inlineDeclareGlobals ? getInlinedSymbolsUsingSymbol(
|
|
2456
|
+
nodeSymbol,
|
|
2457
|
+
isSymbolForGlobalDeclaration
|
|
2458
|
+
) : [],
|
|
2459
|
+
// symbols which are used in "declare module" types
|
|
2460
|
+
...inlineDeclareExternals ? getInlinedSymbolsUsingSymbol(
|
|
2461
|
+
nodeSymbol,
|
|
2462
|
+
isSymbolForDeclareModuleDeclaration
|
|
2463
|
+
) : []
|
|
2464
|
+
];
|
|
2465
|
+
}
|
|
2466
|
+
function areDeclarationSame(left, right) {
|
|
2467
|
+
const leftSymbols = splitTransientSymbol(
|
|
2468
|
+
getNodeSymbol(left, typeChecker),
|
|
2469
|
+
typeChecker
|
|
2470
|
+
);
|
|
2471
|
+
const rightSymbols = splitTransientSymbol(
|
|
2472
|
+
getNodeSymbol(right, typeChecker),
|
|
2473
|
+
typeChecker
|
|
2474
|
+
);
|
|
2475
|
+
for (const leftSymbol of leftSymbols) {
|
|
2476
|
+
if (rightSymbols.has(leftSymbol)) {
|
|
2477
|
+
return true;
|
|
2478
|
+
}
|
|
2479
|
+
}
|
|
2480
|
+
return false;
|
|
2481
|
+
}
|
|
2482
|
+
function createNamespaceForExports(exports2, namespaceSymbol) {
|
|
2483
|
+
function addSymbolToNamespaceExports(namespaceExports2, symbol) {
|
|
2484
|
+
if (!typesUsageEvaluator.isSymbolUsedBySymbol(symbol, namespaceSymbol)) {
|
|
2485
|
+
return;
|
|
2486
|
+
}
|
|
2487
|
+
const symbolKnownNames = collisionsResolver.namesForSymbol(symbol);
|
|
2488
|
+
if (symbolKnownNames.size === 0) {
|
|
2489
|
+
throw new Error(
|
|
2490
|
+
`Cannot get local names for symbol '${symbol.getName()}' while generating namespaced export`
|
|
2491
|
+
);
|
|
2492
|
+
}
|
|
2493
|
+
namespaceExports2.set(symbol.getName(), Array.from(symbolKnownNames)[0]);
|
|
2494
|
+
}
|
|
2495
|
+
function handleNamespacedImportOrExport(namespacedImportOrExport, namespaceExports2, symbol) {
|
|
2496
|
+
if (namespacedImportOrExport.moduleSpecifier === void 0) {
|
|
2497
|
+
return;
|
|
2498
|
+
}
|
|
2499
|
+
if (isReferencedModuleImportable(namespacedImportOrExport)) {
|
|
2500
|
+
addSymbolToNamespaceExports(namespaceExports2, symbol);
|
|
2501
|
+
return;
|
|
2502
|
+
}
|
|
2503
|
+
const referencedSourceFileSymbol = getNodeOwnSymbol(
|
|
2504
|
+
namespacedImportOrExport.moduleSpecifier,
|
|
2505
|
+
typeChecker
|
|
2506
|
+
);
|
|
2507
|
+
if (referencedSourceFileSymbol.exports === void 0) {
|
|
2508
|
+
return;
|
|
2509
|
+
}
|
|
2510
|
+
if (ts9.isImportDeclaration(namespacedImportOrExport) && referencedSourceFileSymbol.exports.has(
|
|
2511
|
+
ts9.InternalSymbolName.ExportEquals
|
|
2512
|
+
)) {
|
|
2513
|
+
return;
|
|
2514
|
+
}
|
|
2515
|
+
const localNamespaceName = createNamespaceForExports(
|
|
2516
|
+
referencedSourceFileSymbol.exports,
|
|
2517
|
+
symbol
|
|
2518
|
+
);
|
|
2519
|
+
if (localNamespaceName !== null) {
|
|
2520
|
+
namespaceExports2.set(symbol.getName(), localNamespaceName);
|
|
2521
|
+
}
|
|
2522
|
+
}
|
|
2523
|
+
function processExportSymbol(namespaceExports2, symbol) {
|
|
2524
|
+
if (symbol.escapedName === ts9.InternalSymbolName.ExportStar) {
|
|
2525
|
+
for (const exportStarDeclaration of getSymbolExportStarDeclarations(
|
|
2526
|
+
symbol
|
|
2527
|
+
)) {
|
|
2528
|
+
if (exportStarDeclaration.moduleSpecifier === void 0) {
|
|
2529
|
+
throw new Error(
|
|
2530
|
+
`Export star declaration does not have a module specifier '${exportStarDeclaration.getText()}'`
|
|
2531
|
+
);
|
|
2532
|
+
}
|
|
2533
|
+
if (isReferencedModuleImportable(exportStarDeclaration)) {
|
|
2534
|
+
throw new Error(
|
|
2535
|
+
`Having a re-export from an importable module as a part of namespaced export is not supported yet.`
|
|
2536
|
+
);
|
|
2537
|
+
}
|
|
2538
|
+
const referencedSourceFileSymbol = getNodeOwnSymbol(
|
|
2539
|
+
exportStarDeclaration.moduleSpecifier,
|
|
2540
|
+
typeChecker
|
|
2541
|
+
);
|
|
2542
|
+
referencedSourceFileSymbol.exports?.forEach(
|
|
2543
|
+
processExportSymbol.bind(null, namespaceExports2)
|
|
2544
|
+
);
|
|
2545
|
+
}
|
|
2546
|
+
return;
|
|
2547
|
+
}
|
|
2548
|
+
symbol.declarations?.forEach((decl) => {
|
|
2549
|
+
if (ts9.isNamespaceExport(decl) && decl.parent.moduleSpecifier !== void 0) {
|
|
2550
|
+
handleNamespacedImportOrExport(
|
|
2551
|
+
decl.parent,
|
|
2552
|
+
namespaceExports2,
|
|
2553
|
+
symbol
|
|
2554
|
+
);
|
|
2555
|
+
return;
|
|
2556
|
+
}
|
|
2557
|
+
if (ts9.isExportSpecifier(decl)) {
|
|
2558
|
+
const exportElementSymbol = getImportExportReferencedSymbol(
|
|
2559
|
+
decl,
|
|
2560
|
+
typeChecker
|
|
2561
|
+
);
|
|
2562
|
+
const namespaceImport = getDeclarationsForSymbol(
|
|
2563
|
+
exportElementSymbol
|
|
2564
|
+
).find(ts9.isNamespaceImport);
|
|
2565
|
+
if (namespaceImport !== void 0) {
|
|
2566
|
+
handleNamespacedImportOrExport(
|
|
2567
|
+
namespaceImport.parent.parent,
|
|
2568
|
+
namespaceExports2,
|
|
2569
|
+
symbol
|
|
2570
|
+
);
|
|
2571
|
+
}
|
|
2572
|
+
return;
|
|
2573
|
+
}
|
|
2574
|
+
});
|
|
2575
|
+
addSymbolToNamespaceExports(namespaceExports2, symbol);
|
|
2576
|
+
}
|
|
2577
|
+
function getIdentifierOfNamespaceImportFromInlinedModule(nsSymbol) {
|
|
2578
|
+
for (const decl of getDeclarationsForSymbol(nsSymbol)) {
|
|
2579
|
+
if (!ts9.isNamespaceExport(decl) && !ts9.isExportSpecifier(decl) && !ts9.isNamespaceImport(decl)) {
|
|
2580
|
+
continue;
|
|
2581
|
+
}
|
|
2582
|
+
if (ts9.isNamespaceExport(decl) && !isReferencedModuleImportable(decl.parent)) {
|
|
2583
|
+
return decl.name;
|
|
2584
|
+
}
|
|
2585
|
+
if (ts9.isNamespaceImport(decl) && !isReferencedModuleImportable(
|
|
2586
|
+
decl.parent.parent
|
|
2587
|
+
)) {
|
|
2588
|
+
return decl.name;
|
|
2589
|
+
}
|
|
2590
|
+
if (ts9.isExportSpecifier(decl)) {
|
|
2591
|
+
if (decl.parent.parent.moduleSpecifier !== void 0) {
|
|
2592
|
+
if (isReferencedModuleImportable(decl.parent.parent)) {
|
|
2593
|
+
continue;
|
|
2594
|
+
}
|
|
2595
|
+
if (getIdentifierOfNamespaceImportFromInlinedModule(
|
|
2596
|
+
getImportExportReferencedSymbol(decl, typeChecker)
|
|
2597
|
+
)) {
|
|
2598
|
+
return decl.name;
|
|
2599
|
+
}
|
|
2600
|
+
}
|
|
2601
|
+
const result = getDeclarationsForSymbol(
|
|
2602
|
+
getImportExportReferencedSymbol(decl, typeChecker)
|
|
2603
|
+
).some((importDecl) => {
|
|
2604
|
+
if (ts9.isNamespaceImport(importDecl)) {
|
|
2605
|
+
return !isReferencedModuleImportable(
|
|
2606
|
+
importDecl.parent.parent
|
|
2607
|
+
);
|
|
2608
|
+
}
|
|
2609
|
+
if (ts9.isImportSpecifier(importDecl)) {
|
|
2610
|
+
return getIdentifierOfNamespaceImportFromInlinedModule(
|
|
2611
|
+
getImportExportReferencedSymbol(importDecl, typeChecker)
|
|
2612
|
+
);
|
|
2613
|
+
}
|
|
2614
|
+
return false;
|
|
2615
|
+
});
|
|
2616
|
+
if (result) {
|
|
2617
|
+
return decl.name;
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
}
|
|
2621
|
+
return null;
|
|
2622
|
+
}
|
|
2623
|
+
const namespaceNameIdentifier = getIdentifierOfNamespaceImportFromInlinedModule(namespaceSymbol);
|
|
2624
|
+
if (namespaceNameIdentifier === null) {
|
|
2625
|
+
return null;
|
|
2626
|
+
}
|
|
2627
|
+
const namespaceExports = /* @__PURE__ */ new Map();
|
|
2628
|
+
exports2.forEach(processExportSymbol.bind(null, namespaceExports));
|
|
2629
|
+
if (namespaceExports.size !== 0) {
|
|
2630
|
+
const namespaceLocalName = collisionsResolver.addTopLevelIdentifier(
|
|
2631
|
+
namespaceNameIdentifier
|
|
2632
|
+
);
|
|
2633
|
+
collectionResult.wrappedNamespaces.set(
|
|
2634
|
+
namespaceLocalName,
|
|
2635
|
+
namespaceExports
|
|
2636
|
+
);
|
|
2637
|
+
return namespaceLocalName;
|
|
2638
|
+
}
|
|
2639
|
+
return null;
|
|
2640
|
+
}
|
|
2641
|
+
function syncExports() {
|
|
2642
|
+
for (const exp of rootFileExports) {
|
|
2643
|
+
if (exp.type === 0 /* CommonJS */) {
|
|
2644
|
+
continue;
|
|
2645
|
+
}
|
|
2646
|
+
const namespaceLocalName = exp.symbol.flags & ts9.SymbolFlags.ValueModule && exp.symbol.exports !== void 0 ? createNamespaceForExports(exp.symbol.exports, exp.originalSymbol) : null;
|
|
2647
|
+
if (namespaceLocalName !== null) {
|
|
2648
|
+
collectionResult.renamedExports.set(
|
|
2649
|
+
exp.exportedName,
|
|
2650
|
+
namespaceLocalName
|
|
2651
|
+
);
|
|
2652
|
+
}
|
|
2653
|
+
const symbolKnownNames = collisionsResolver.namesForSymbol(exp.symbol);
|
|
2654
|
+
if (symbolKnownNames.size === 0) {
|
|
2655
|
+
continue;
|
|
2656
|
+
}
|
|
2657
|
+
if (symbolKnownNames.has(exp.exportedName)) {
|
|
2658
|
+
if (shouldSymbolBeImported(exp.symbol)) {
|
|
2659
|
+
collectionResult.renamedExports.set(
|
|
2660
|
+
exp.exportedName,
|
|
2661
|
+
exp.exportedName
|
|
2662
|
+
);
|
|
2663
|
+
}
|
|
2664
|
+
continue;
|
|
2665
|
+
}
|
|
2666
|
+
collectionResult.renamedExports.set(
|
|
2667
|
+
exp.exportedName,
|
|
2668
|
+
Array.from(symbolKnownNames)[0]
|
|
2669
|
+
);
|
|
2670
|
+
}
|
|
2671
|
+
}
|
|
2672
|
+
for (const sourceFile of sourceFiles) {
|
|
2673
|
+
verboseLog(`======= Processing ${sourceFile.fileName} =======`);
|
|
2674
|
+
const updateFn = sourceFile === rootSourceFile ? updateResultForRootModule : updateResultForAnyModule;
|
|
2675
|
+
const currentModule = getFileModuleInfo(sourceFile.fileName, criteria);
|
|
2676
|
+
updateFn(sourceFile.statements, currentModule);
|
|
2677
|
+
if (isNodeUsed(sourceFile)) {
|
|
2678
|
+
switch (currentModule.type) {
|
|
2679
|
+
case 1 /* ShouldBeImported */: {
|
|
2680
|
+
updateImportsForStatement(sourceFile);
|
|
2681
|
+
break;
|
|
2682
|
+
}
|
|
2683
|
+
case 0 /* ShouldBeInlined */: {
|
|
2684
|
+
const sourceFileSymbol = getNodeSymbol(sourceFile, typeChecker);
|
|
2685
|
+
if (sourceFileSymbol === null || sourceFileSymbol.exports === void 0) {
|
|
2686
|
+
throw new Error(
|
|
2687
|
+
`Cannot find symbol or exports for source file ${sourceFile.fileName}`
|
|
2688
|
+
);
|
|
2689
|
+
}
|
|
2690
|
+
let namespaceIdentifier = null;
|
|
2691
|
+
forEachImportOfStatement(sourceFile, (imp) => {
|
|
2692
|
+
if (ts9.isNamespaceExport(imp) || ts9.isNamespaceImport(imp)) {
|
|
2693
|
+
namespaceIdentifier = imp.name;
|
|
2694
|
+
}
|
|
2695
|
+
});
|
|
2696
|
+
if (namespaceIdentifier === null) {
|
|
2697
|
+
break;
|
|
2698
|
+
}
|
|
2699
|
+
createNamespaceForExports(
|
|
2700
|
+
sourceFileSymbol.exports,
|
|
2701
|
+
getNodeOwnSymbol(namespaceIdentifier, typeChecker)
|
|
2702
|
+
);
|
|
2703
|
+
break;
|
|
2704
|
+
}
|
|
2705
|
+
}
|
|
2706
|
+
}
|
|
2707
|
+
}
|
|
2708
|
+
if (entryConfig.failOnClass) {
|
|
2709
|
+
const classes = collectionResult.statements.filter(ts9.isClassDeclaration);
|
|
2710
|
+
if (classes.length !== 0) {
|
|
2711
|
+
const classesNames = classes.map(
|
|
2712
|
+
(c) => c.name === void 0 ? "anonymous class" : c.name.text
|
|
2713
|
+
);
|
|
2714
|
+
throw new Error(
|
|
2715
|
+
`${classes.length} class statement(s) are found in generated dts: ${classesNames.join(", ")}`
|
|
2716
|
+
);
|
|
2717
|
+
}
|
|
2718
|
+
}
|
|
2719
|
+
syncExports();
|
|
2720
|
+
const exportReferencedTypes = outputOptions.exportReferencedTypes !== false;
|
|
2721
|
+
function isExportedWithLocalName(namedDeclaration, exportedName) {
|
|
2722
|
+
const nodeName = getNodeName(namedDeclaration);
|
|
2723
|
+
if (nodeName === void 0) {
|
|
2724
|
+
throw new Error(`Cannot find node name ${namedDeclaration.getText()}`);
|
|
2725
|
+
}
|
|
2726
|
+
return collisionsResolver.resolveReferencedIdentifier(
|
|
2727
|
+
nodeName
|
|
2728
|
+
) === exportedName;
|
|
2729
|
+
}
|
|
2730
|
+
const renamedAndNotExplicitlyExportedTypes = [];
|
|
2731
|
+
const output = generateOutput(
|
|
2732
|
+
{
|
|
2733
|
+
...collectionResult,
|
|
2734
|
+
resolveIdentifierName: (identifier) => {
|
|
2735
|
+
if (ts9.isPropertyAccessOrQualifiedName(identifier)) {
|
|
2736
|
+
return collisionsResolver.resolveReferencedQualifiedName(
|
|
2737
|
+
identifier
|
|
2738
|
+
);
|
|
2739
|
+
} else {
|
|
2740
|
+
return collisionsResolver.resolveReferencedIdentifier(identifier);
|
|
2741
|
+
}
|
|
2742
|
+
},
|
|
2743
|
+
// eslint-disable-next-line complexity
|
|
2744
|
+
getStatementSettings: (statement) => {
|
|
2745
|
+
if (isAmbientModule(statement) || ts9.isExportDeclaration(statement)) {
|
|
2746
|
+
return { shouldHaveExportKeyword: false, shouldHaveJSDoc: true };
|
|
2747
|
+
}
|
|
2748
|
+
const statementExports = getExportsForStatement(
|
|
2749
|
+
rootFileExports,
|
|
2750
|
+
typeChecker,
|
|
2751
|
+
statement
|
|
2752
|
+
);
|
|
2753
|
+
const isExplicitlyExportedWithOriginalName = statementExports.find((exp) => {
|
|
2754
|
+
if (ts9.isVariableStatement(statement)) {
|
|
2755
|
+
for (const variableDeclaration of statement.declarationList.declarations) {
|
|
2756
|
+
if (ts9.isIdentifier(variableDeclaration.name)) {
|
|
2757
|
+
const resolvedName = collisionsResolver.resolveReferencedIdentifier(
|
|
2758
|
+
variableDeclaration.name
|
|
2759
|
+
);
|
|
2760
|
+
if (exp.exportedName === resolvedName) {
|
|
2761
|
+
return true;
|
|
2762
|
+
}
|
|
2763
|
+
continue;
|
|
2764
|
+
}
|
|
2765
|
+
warnLog(
|
|
2766
|
+
`Unhandled variable identifier type detected (${ts9.SyntaxKind[variableDeclaration.name.kind]}). Please report this issue to https://github.com/timocov/dts-bundle-generator`
|
|
2767
|
+
);
|
|
2768
|
+
}
|
|
2769
|
+
return false;
|
|
2770
|
+
}
|
|
2771
|
+
return isNodeNamedDeclaration(statement) && isExportedWithLocalName(statement, exp.exportedName);
|
|
2772
|
+
}) !== void 0;
|
|
2773
|
+
const onlyExplicitlyExportedShouldBeExported = !exportReferencedTypes || ts9.isClassDeclaration(statement) || ts9.isEnumDeclaration(statement) && !hasNodeModifier(statement, ts9.SyntaxKind.ConstKeyword) || ts9.isFunctionDeclaration(statement) || ts9.isVariableStatement(statement) || ts9.isModuleDeclaration(statement);
|
|
2774
|
+
if (onlyExplicitlyExportedShouldBeExported) {
|
|
2775
|
+
return {
|
|
2776
|
+
shouldHaveExportKeyword: isExplicitlyExportedWithOriginalName,
|
|
2777
|
+
shouldHaveJSDoc: statementExports.length !== 0
|
|
2778
|
+
};
|
|
2779
|
+
}
|
|
2780
|
+
if (isNodeNamedDeclaration(statement) && !isExportedWithLocalName(
|
|
2781
|
+
statement,
|
|
2782
|
+
getNodeName(statement).getText()
|
|
2783
|
+
)) {
|
|
2784
|
+
renamedAndNotExplicitlyExportedTypes.push(statement);
|
|
2785
|
+
return {
|
|
2786
|
+
shouldHaveExportKeyword: false,
|
|
2787
|
+
shouldHaveJSDoc: statementExports.length !== 0
|
|
2788
|
+
};
|
|
2789
|
+
}
|
|
2790
|
+
return {
|
|
2791
|
+
shouldHaveExportKeyword: isExplicitlyExportedWithOriginalName || statementExports.length === 0,
|
|
2792
|
+
shouldHaveJSDoc: true
|
|
2793
|
+
};
|
|
2794
|
+
},
|
|
2795
|
+
needStripConstFromConstEnum: (constEnum) => {
|
|
2796
|
+
if (!program.getCompilerOptions().preserveConstEnums || !outputOptions.respectPreserveConstEnum) {
|
|
2797
|
+
return false;
|
|
2798
|
+
}
|
|
2799
|
+
const enumSymbol = getNodeSymbol(constEnum, typeChecker);
|
|
2800
|
+
if (enumSymbol === null) {
|
|
2801
|
+
return false;
|
|
2802
|
+
}
|
|
2803
|
+
return rootFileExportSymbols.includes(enumSymbol);
|
|
2804
|
+
},
|
|
2805
|
+
needStripImportFromImportTypeNode: (node) => {
|
|
2806
|
+
if (node.qualifier === void 0) {
|
|
2807
|
+
return false;
|
|
2808
|
+
}
|
|
2809
|
+
if (!ts9.isLiteralTypeNode(node.argument) || !ts9.isStringLiteral(node.argument.literal)) {
|
|
2810
|
+
return false;
|
|
2811
|
+
}
|
|
2812
|
+
return getReferencedModuleInfo(node, criteria, typeChecker)?.type === 0 /* ShouldBeInlined */;
|
|
2813
|
+
}
|
|
2814
|
+
},
|
|
2815
|
+
{
|
|
2816
|
+
sortStatements: outputOptions.sortNodes,
|
|
2817
|
+
umdModuleName: outputOptions.umdModuleName,
|
|
2818
|
+
noBanner: outputOptions.noBanner
|
|
2819
|
+
}
|
|
2820
|
+
);
|
|
2821
|
+
if (renamedAndNotExplicitlyExportedTypes.length !== 0) {
|
|
2822
|
+
warnLog(
|
|
2823
|
+
`The following type nodes were renamed because of the name collisions and will not be exported from the generated bundle:
|
|
2824
|
+
- ${// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
2825
|
+
renamedAndNotExplicitlyExportedTypes.map(
|
|
2826
|
+
(node) => `${getNodeName(node).getText()} (from ${node.getSourceFile().fileName})`
|
|
2827
|
+
).join("\n- ")}${"\n"}This might lead to unpredictable and unexpected output, and possible breaking changes to your API.${"\n"}Consider either (re-)exporting them explicitly from the entry point, or disable --export-referenced-types option ('output.exportReferencedTypes' in the config).`
|
|
2828
|
+
);
|
|
2829
|
+
}
|
|
2830
|
+
return output;
|
|
2831
|
+
});
|
|
2832
|
+
}
|
|
2833
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
2834
|
+
0 && (module.exports = {
|
|
2835
|
+
generateDtsBundle
|
|
2836
|
+
});
|