-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcreate-module-declaration.js
More file actions
565 lines (472 loc) · 14.6 KB
/
create-module-declaration.js
File metadata and controls
565 lines (472 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/** @import { Binding, Declaration, Mapping, Module, ModuleReference } from './types' */
import path from 'node:path';
import ts from 'typescript';
import * as tsu from 'ts-api-utils';
import MagicString from 'magic-string';
import {
clean_jsdoc,
get_dts,
is_declaration,
is_internal,
is_reference,
resolve_dts,
walk
} from './utils.js';
/**
* @param {string} id
* @param {string} entry
* @param {Record<string, string>} created
* @param {(file: string, specifier: string) => string | null} resolve
* @param {{ stripInternal?: boolean }} options
* @returns {{
* content: string;
* mappings: Map<string, Mapping>;
* ambient: ModuleReference[];
* }}
*/
export function create_module_declaration(id, entry, created, resolve, options) {
let content = '';
/** @type {Map<string, Mapping>} */
const mappings = new Map();
/** @type {ModuleReference[]} */
const ambient = [];
/** @type {Record<string, Record<string, Declaration>>} */
const external_imports = {};
/** @type {Record<string, Record<string, Declaration>>} */
const external_import_alls = {};
/** @type {Record<string, Record<string, Declaration>>} */
const external_export_from = {};
/** @type {Set<string>} */
const external_export_all_from = new Set();
/** @type {Map<string, Module>} */
const bundle = new Map();
/** @type {Map<string, Map<string, Declaration>>} */
const traced = new Map();
/** @type {Set<string>} */
const exports = new Set();
/** @type {Set<string>} */
const reserved = new Set(['default']);
/** @type {string[]} */
const export_specifiers = [];
// step 1 — discover which modules are included in the bundle
{
const included = new Set([entry]);
for (const file of included) {
const module = get_dts(file, created, resolve, options);
for (const name of module.globals) {
reserved.add(name);
}
for (const dep of module.dependencies) {
included.add(dep);
}
for (const dep of module.ambient_imports) {
ambient.push(dep);
}
for (const [name, binding] of module.imports) {
if (binding.external) {
(external_imports[binding.id] ??= {})[binding.name] ??= create_external_declaration(
binding,
name
);
}
}
for (const [name, binding] of module.import_all) {
if (binding.external) {
(external_import_alls[binding.id] ??= {})[binding.name] ??= create_external_declaration(
binding,
name
);
}
}
for (const [name, binding] of module.export_from) {
if (binding.external) {
(external_export_from[binding.id] ??= {})[binding.name] ??= create_external_declaration(
binding,
name
);
}
}
for (const binding of module.export_all.values()) {
if (binding.external) {
external_export_all_from.add(binding.id);
}
}
bundle.set(file, module);
traced.set(file, new Map());
}
/** @type {Set<Module>} */
const modules_to_export_all_from = new Set([/** @type {Module} */ (bundle.get(entry))]);
for (const module of modules_to_export_all_from) {
for (const exported of module.exports.keys()) {
exports.add(exported);
}
for (const exported of module.export_from.keys()) {
exports.add(exported);
}
for (const next of module.export_all) {
const m = bundle.get(next.id);
if (m) modules_to_export_all_from.add(m);
}
}
}
// step 2 - treeshaking
{
/** @type {Set<string>} */
const names = new Set(reserved);
/** @type {Set<Declaration>} */
const declarations = new Set();
/** @param {string} name */
function get_name(name) {
let i = 1;
while (names.has(name)) {
name = `${name}_${i++}`;
}
names.add(name);
return name;
}
/**
* @param {Declaration} declaration
*/
const mark = (declaration) => {
if (declaration.included) return;
declarations.add(declaration);
declaration.included = true;
for (const { module, name } of declaration.dependencies) {
const dependency = trace(module, name);
mark(dependency);
}
};
for (const name of exports) {
const declaration = trace_export(entry, name);
if (declaration) {
declaration.alias = get_name(reserved.has(name) ? declaration.name : name);
mark(declaration);
if (name === 'default') {
declaration.default = true;
} else if (declaration.alias !== name) {
export_specifiers.push(`${declaration.alias} as ${name}`);
} else {
declaration.export = true;
}
} else {
throw new Error('Something strange happened');
}
}
// provide a name for declarations that are included but not exported
for (const declaration of declarations) {
if (!declaration.alias) {
declaration.alias = get_name(declaration.preferred_alias || declaration.name);
}
}
}
// step 3 - generate code
{
content += `declare module '${id}' {`;
// inject imports from external modules
for (const id in external_imports) {
const specifiers = [];
for (const name in external_imports[id]) {
const declaration = external_imports[id][name];
if (declaration.included) {
specifiers.push(name === declaration.alias ? name : `${name} as ${declaration.alias}`);
}
}
if (specifiers.length > 0) {
content += `\n\timport type { ${specifiers.join(', ')} } from '${id}';`;
}
}
for (const id in external_import_alls) {
for (const name in external_import_alls[id]) {
content += `\n\timport * as ${name} from '${id}';`; // TODO could this have been aliased?
}
}
for (const id in external_export_from) {
const specifiers = Object.keys(external_export_from[id]).map((name) => {
// this is a bit of a hack, but it makes life easier
exports.delete(name);
const declaration = external_export_from[id][name];
return name === declaration.alias ? name : `${name} as ${declaration.alias}`;
});
content += `\n\texport { ${specifiers.join(', ')} } from '${id}';`;
}
// second pass — editing
for (const module of bundle.values()) {
const result = new MagicString(module.dts);
const index = module.dts.indexOf('//# sourceMappingURL=');
if (index !== -1) result.remove(index, module.dts.length);
if (module.import_all.size > 0) {
// remove the leading `Foo.` from references to `import * as Foo` namespace imports
// but only for internal imports, not external ones
walk(module.ast, (node) => {
if (is_reference(node) && ts.isQualifiedName(node.parent)) {
const binding = module.import_all.get(node.getText(module.ast));
if (binding && !binding.external) {
result.remove(node.pos, result.original.indexOf('.', node.end) + 1);
const declaration = bundle
.get(binding.id)
?.declarations.get(node.parent.right.getText(module.ast));
if (declaration?.alias) {
result.overwrite(node.parent.right.pos, node.parent.right.end, declaration.alias);
}
}
}
});
}
ts.forEachChild(module.ast, (node) => {
if (
ts.isImportDeclaration(node) ||
ts.isExportDeclaration(node) ||
ts.isExportAssignment(node)
) {
result.remove(node.pos, node.end);
return;
}
// remove `declare module 'foo'`
if (
ts.isModuleDeclaration(node) &&
!tsu.isNamespaceDeclaration(node) &&
node.modifiers?.some((modifier) => tsu.isDeclareKeyword(modifier))
) {
result.remove(node.pos, node.end);
return;
}
if (!is_declaration(node)) {
return;
}
if (is_internal(node) && options.stripInternal) {
result.remove(node.pos, node.end);
}
const identifier = /** @type {ts.DeclarationName} */ (
ts.isVariableStatement(node)
? ts.getNameOfDeclaration(node.declarationList.declarations[0])
: ts.getNameOfDeclaration(node)
);
const name = identifier.getText(module.ast);
const declaration = /** @type {Declaration} */ (module.declarations.get(name));
if (!declaration.included) {
result.remove(node.pos, node.end);
return;
}
// special case — TS turns `export default 42` into `const _default: 42; export default _default` —
// the `export default` assignment is already taken care of, we just need to remove the `const _default:`
if (declaration.default && ts.isVariableStatement(node)) {
result.remove(
node.getStart(),
/** @type {ts.TypeNode} */ (node.declarationList.declarations[0].type).getStart()
);
}
const modifiers = declaration.default
? 'export default '
: declaration.export
? 'export '
: '';
if (node.modifiers) {
let end = node.modifiers[node.modifiers.length - 1].end;
while (/\s/.test(result.original[end])) end += 1;
result.overwrite(node.getStart(), end, modifiers);
} else if (modifiers) {
result.prependRight(node.getStart(), modifiers);
}
const loc = module.locator(identifier.getStart(module.ast));
if (loc) {
// the sourcemaps generated by TypeScript are very inaccurate, borderline useless.
// we need to fix them up here. TODO is it only inaccurate in the JSDoc case?
const segments = module.source?.mappings?.[loc.line - 1];
if (module.source && segments) {
// find the segments immediately before and after the generated column
const index = segments.findIndex((segment) => segment[0] >= loc.column);
const a = segments[index - 1] ?? segments[0];
if (a) {
let l = /** @type {number} */ (a[2]);
const source_line = module.source.code.split('\n')[l];
const regex = new RegExp(`\\b${name}\\b`);
const match = regex.exec(source_line);
if (match) {
const mapping = {
source: path.resolve(path.dirname(module.file), module.source.map.sources[0]),
line: l + 1,
column: match.index
};
mappings.set(name, /** @type {Mapping} */ (mapping));
} else {
// TODO figure out how to repair sourcemaps in this case
}
} else {
// TODO how does this happen?
}
} else {
mappings.set(name, {
source: module.file,
line: loc.line,
column: loc.column
});
}
}
walk(node, (node) => {
if (ts.isPropertySignature(node) && is_internal(node) && options.stripInternal) {
result.remove(node.pos, node.end);
return false;
}
// We need to include the declarations because if references to them have changed, we need to update the declarations, too
if (is_reference(node, true)) {
const name = node.getText(module.ast);
const { alias } = trace(module.file, name);
if (alias !== name) {
result.overwrite(node.getStart(module.ast), node.getEnd(), alias);
}
}
// `import('./foo').Foo` -> `Foo`
if (
ts.isImportTypeNode(node) &&
ts.isLiteralTypeNode(node.argument) &&
ts.isStringLiteral(node.argument.literal) &&
node.argument.literal.text.startsWith('.')
) {
// follow import
const resolved = resolve_dts(path.dirname(module.file), node.argument.literal.text);
// included.add(resolved);
// remove the `import(...)`
if (node.qualifier) {
const name = node.qualifier.getText(module.ast);
const declaration = trace(resolved, name);
result.overwrite(node.getStart(module.ast), node.qualifier.end, declaration.alias);
} else {
throw new Error('TODO');
}
}
clean_jsdoc(node, result);
});
});
const mod = result
.trim()
.indent()
.toString()
.replace(/^( )+/gm, (match) => '\t'.repeat(match.length / 4));
if (mod) content += '\n' + mod;
}
// finally, export any bindings that are exported from external modules
for (const name of exports) {
const declaration = trace_export(entry, name);
if (declaration?.external) {
export_specifiers.push(declaration.alias);
}
}
// Always add an export { .. } statement, even if there are no exports. This ensures
// that only the public types are exposed to consumers of the declaration file. Due to some
// old TypeScript inconsistency, omitting the export statement would expose all types.
if (export_specifiers.length > 0) {
content += `\n\n\texport { ${export_specifiers.join(', ')} };`;
} else {
content += '\n\n\texport {};';
}
content += `\n}`;
}
/**
* @param {string} module_id
* @param {string} name
* @returns {Declaration | null}
*/
function trace_export(module_id, name) {
if (module_id === id) {
return trace_export(entry, name);
}
const module = bundle.get(module_id);
if (module) {
const local = module.exports.get(name);
if (local) {
return trace(module_id, local);
}
const binding = module.export_from.get(name);
if (binding) {
return trace_export(binding.id, binding.name);
}
for (const reference of module.export_all) {
const declaration = trace_export(reference.id, name);
if (declaration) return declaration;
}
} else {
const declaration =
external_imports[module_id]?.[name] ??
external_import_alls[module_id]?.[name] ??
external_export_from[module_id]?.[name];
if (declaration) return declaration;
}
return null;
}
/**
* @param {string} id
* @param {string} name
* @returns {Declaration}
*/
function trace(id, name) {
const cache = traced.get(id);
if (!cache) {
// this means we're dealing with an external module
return (
external_imports[id]?.[name] ??
external_import_alls[id]?.[name] ??
external_export_from[id]?.[name]
);
}
if (cache.has(name)) {
return /** @type {Declaration} */ (cache.get(name));
}
const module = bundle.get(id);
if (module) {
const declaration = module.declarations.get(name);
if (declaration) {
cache.set(name, declaration);
return declaration;
}
const binding = module.imports.get(name) ?? module.export_from.get(name);
if (binding) {
const declaration = trace_export(binding.id, binding.name);
if (declaration) return declaration;
}
for (const reference of module.export_all) {
const declaration = trace_export(reference.id, name);
if (declaration) {
cache.set(name, declaration);
return declaration;
}
}
// otherwise it's presumably a built-in
return {
module: '<builtin>',
external: false,
included: true,
export: false,
default: false,
name,
alias: name,
dependencies: [],
preferred_alias: ''
};
} else {
throw new Error('TODO external imports');
}
}
return {
content,
mappings,
ambient
};
}
/**
* @param {Binding} binding
* @param {string} alias
* @returns {Declaration}
*/
function create_external_declaration(binding, alias) {
return {
module: binding.id,
name: binding.name,
alias: '',
export: false,
default: false,
external: true,
included: false,
dependencies: [],
preferred_alias: alias
};
}