-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
610 lines (550 loc) · 16.2 KB
/
index.ts
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
import ts from "typescript";
import { TypeStripError } from "./errors.ts";
const SyntaxKind = ts.SyntaxKind;
/**
* `Type-Strip` Options
*/
export type TypeStripOptions = {
/**
* Whether to strip comments
*/
removeComments?: boolean;
/**
* @deprecated Use pathRewriting instead
*/
tsToJsModuleSpecifiers?: boolean;
/**
* Whether to rewrite .ts module imports to .js imports
*/
pathRewriting?: boolean;
};
const defaults: Required<TypeStripOptions> = {
removeComments: false,
tsToJsModuleSpecifiers: false,
pathRewriting: false,
};
type StripItem = { start: number; end: number; trailing?: RegExp };
let sourceFile: ts.SourceFile;
let sourceCode: string;
let outputCode: string;
let removeComments: boolean;
let pathRewriting: boolean;
let strip: StripItem[] = [];
/**
* Takes a TypeScript input source file and outputs JavaScript with types stripped
*
* Throws if the code is not forward compatible with the TC39 type annotations proposal
*/
export default (
input: string,
options?: TypeStripOptions,
): string => {
sourceFile = ts.createSourceFile(
"input.ts",
input,
ts.ScriptTarget.Latest,
false, // setParentNodes
ts.ScriptKind.TS,
);
removeComments = options?.removeComments ?? defaults.removeComments;
pathRewriting = (options?.pathRewriting || options?.tsToJsModuleSpecifiers) ??
defaults.pathRewriting;
sourceCode = sourceFile.getFullText();
outputCode = "";
strip = [];
const statements = sourceFile.statements;
for (let i = 0; i < statements.length; i++) {
topLevelVisitor(statements[i]);
}
let currentIndex = 0;
for (let i = 0; i < strip.length; i++) {
const { start, end, trailing } = strip[i];
if (start !== undefined && end !== undefined) {
if (currentIndex < start) {
outputCode += sourceCode.slice(currentIndex, start);
currentIndex = end;
}
if (currentIndex < end) {
currentIndex = end;
}
}
if (trailing) {
const match = sourceCode.slice(currentIndex).match(trailing);
if (match) {
currentIndex += match[0].length;
}
}
}
if (currentIndex < sourceCode.length) {
outputCode += sourceCode.slice(currentIndex);
}
return outputCode;
};
const stripComments = (node: ts.Node, kind: "leading" | "trailing") => {
const commentRanges = kind === "leading"
? ts.getLeadingCommentRanges(sourceCode, node.pos)
: ts.getTrailingCommentRanges(sourceCode, node.end);
if (commentRanges) {
for (let i = 0; i < commentRanges.length; i++) {
const commentRange = commentRanges[i];
strip.push({
start: commentRange.pos,
end: commentRange.end,
trailing: commentRange.hasTrailingNewLine ? /\n?/ : undefined,
});
}
}
};
const topLevelVisitor = (node: ts.Node) => {
if (removeComments) {
stripComments(node, "leading");
}
try {
switch (node.kind) {
case SyntaxKind.ExportDeclaration:
return visitExportDeclaration(node as ts.ExportDeclaration);
case SyntaxKind.ImportDeclaration:
return visitImportDeclaration(node as ts.ImportDeclaration);
}
return visitor(node);
} finally {
if (removeComments) {
stripComments(node, "trailing");
}
}
};
const visitor = (node: ts.Node) => {
if (removeComments) {
stripComments(node, "leading");
}
try {
switch (node.kind) {
case SyntaxKind.Identifier:
return;
case SyntaxKind.VariableStatement:
return visitVariableStatement(node as ts.VariableStatement);
case SyntaxKind.VariableDeclaration:
return visitVariableDeclaration(node as ts.VariableDeclaration);
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.TypeAliasDeclaration:
strip.push({ start: node.pos, end: node.end });
return;
case SyntaxKind.NonNullExpression:
case SyntaxKind.AsExpression:
case SyntaxKind.SatisfiesExpression:
visitor((node as ts.AsExpression).expression);
strip.push({
start: (node as ts.AsExpression).expression.end,
end: node.end,
});
return;
case SyntaxKind.CallExpression:
if (
(node as ts.CallExpression).expression.kind ===
SyntaxKind.ImportKeyword
) {
return visitImportCallExpression(node as ts.CallExpression);
}
return visitCallOrNewExpression(node as ts.CallExpression);
case SyntaxKind.NewExpression:
return visitCallOrNewExpression(node as ts.CallExpression);
case SyntaxKind.ExpressionWithTypeArguments:
return visitExpressionWithTypeArguments(
node as ts.ExpressionWithTypeArguments,
);
case SyntaxKind.TaggedTemplateExpression:
return visitTaggedTemplateExpression(
node as ts.TaggedTemplateExpression,
);
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.Constructor:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction:
return visitFunctionLikeDeclaration(node as ts.FunctionLikeDeclaration);
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
return visitClassLike(node as ts.ClassLikeDeclaration);
// Unsupported features
case SyntaxKind.EnumDeclaration:
throw new TypeStripError("enum");
case SyntaxKind.ModuleDeclaration:
throw new TypeStripError("namespace");
case SyntaxKind.TypeAssertionExpression:
throw new TypeStripError("type-assertion-expression");
}
node.forEachChild((child) => {
if (!ts.isToken(child) && !ts.isIdentifier(child)) {
visitor(child);
}
});
} finally {
if (removeComments) {
stripComments(node, "trailing");
}
}
};
const visitExportDeclaration = (node: ts.ExportDeclaration) => {
if (node.isTypeOnly) {
strip.push({ start: node.pos, end: node.end });
} else if (node.exportClause && ts.isNamedExports(node.exportClause)) {
const exportsToSkip = node.exportClause.elements.map(visitExportSpecifier)
.filter(isNotUndefined);
if (exportsToSkip?.length === node.exportClause.elements.length) {
// skip the whole import declaration
strip.push({ start: node.pos, end: node.end });
} else {
for (let i = 0; i < exportsToSkip.length; i++) {
strip.push(exportsToSkip[i]);
}
}
}
const moduleSpecifier = node.moduleSpecifier;
if (pathRewriting && moduleSpecifier) {
if (ts.isStringLiteral(moduleSpecifier)) {
sourceCode = sourceCode.slice(0, moduleSpecifier.pos) +
` "${moduleSpecifier.text.replace(/\.ts$/, ".js")}"` +
sourceCode.slice(moduleSpecifier.end);
}
}
};
const visitExportSpecifier = (
node: ts.ExportSpecifier,
): StripItem | undefined => {
if (node.isTypeOnly) {
return { start: node.pos, end: node.end, trailing: /,/ };
}
};
const visitImportDeclaration = (node: ts.ImportDeclaration) => {
if (node.importClause?.isTypeOnly) {
strip.push({ start: node.pos, end: node.end });
} else if (node.importClause) {
const skipDeclaration = visitImportClause(node.importClause);
if (skipDeclaration) {
strip.push({ start: node.pos, end: node.end });
}
}
if (pathRewriting) {
if (ts.isStringLiteral(node.moduleSpecifier)) {
const moduleSpecifier = node.moduleSpecifier;
sourceCode = sourceCode.slice(0, moduleSpecifier.pos) +
` "${moduleSpecifier.text.replace(/\.ts$/, ".js")}"` +
sourceCode.slice(moduleSpecifier.end);
}
}
};
const visitImportCallExpression = (node: ts.CallExpression) => {
if (pathRewriting) {
if (ts.isStringLiteral(node.arguments[0])) {
const moduleSpecifier = node.arguments[0];
sourceCode = sourceCode.slice(0, moduleSpecifier.pos) +
`"${moduleSpecifier.text.replace(/\.ts$/, ".js")}"` +
sourceCode.slice(moduleSpecifier.end);
}
}
};
const visitImportClause = (node: ts.ImportClause) => {
if (node?.namedBindings && ts.isNamedImports(node.namedBindings)) {
const importsToSkip = node.namedBindings.elements.map(visitImportSpecifier)
.filter(isNotUndefined);
if (importsToSkip?.length === node.namedBindings?.elements.length) {
return true; // skip the whole import declaration
} else {
for (let i = 0; i < importsToSkip.length; i++) {
strip.push(importsToSkip[i]);
}
}
}
};
const visitImportSpecifier = (
node: ts.ImportSpecifier,
): StripItem | undefined => {
if (node.isTypeOnly) {
return { start: node.pos, end: node.end, trailing: /,/ };
}
};
const visitVariableStatement = (node: ts.VariableStatement) => {
if (
node.modifiers && hasModifier(node.modifiers, SyntaxKind.DeclareKeyword)
) {
throw new TypeStripError("declare");
}
const declarations = node.declarationList.declarations;
for (let i = 0; i < declarations.length; i++) {
visitVariableDeclaration(declarations[i]);
}
};
/**
* Handle variable declaration type annotation
* @example
* let x: string = "foo";
*/
const visitVariableDeclaration = (node: ts.VariableDeclaration) => {
visitor(node.name);
if (node.exclamationToken) {
strip.push({
start: node.exclamationToken.pos,
end: node.exclamationToken.end,
});
}
if (node.type) {
strip.push({
start: node.type.pos - 1,
end: node.type.end,
});
}
if (node.initializer) {
visitor(node.initializer);
}
};
const visitExpressionWithTypeArguments = (
node: ts.ExpressionWithTypeArguments,
) => {
if (node.typeArguments) {
strip.push({
start: node.typeArguments.pos - 1,
end: node.typeArguments.end + 1,
});
}
visitor(node.expression);
};
const visitTaggedTemplateExpression = (
node: ts.TaggedTemplateExpression,
) => {
visitor(node.tag);
if (node.typeArguments) {
strip.push({
start: node.typeArguments.pos - 1,
end: node.typeArguments.end + 1,
});
}
visitor(node.template);
};
const visitParameter = (node: ts.ParameterDeclaration) => {
if (
node.modifiers &&
hasModifier(node.modifiers, [
SyntaxKind.PublicKeyword,
SyntaxKind.PrivateKeyword,
SyntaxKind.ProtectedKeyword,
SyntaxKind.ReadonlyKeyword,
SyntaxKind.OverrideKeyword,
])
) {
throw new TypeStripError("parameter-property");
}
if (ts.isIdentifier(node.name) && node.name.escapedText === "this") {
strip.push({ start: node.pos, end: node.end, trailing: /,?\s*/ });
} else if (!ts.isIdentifier(node.name)) {
visitor(node.name);
}
if (node.questionToken) {
strip.push({ start: node.questionToken.pos, end: node.questionToken.end });
}
if (node.type) {
strip.push({ start: node.type.pos - 1, end: node.type.end });
}
if (node.initializer) {
visitor(node.initializer);
}
};
/**
* Handle function declaration return type annotation and type parameters
* @example
* function add<T>(x: T, y: T): T {
return x + y;
}
*/
const visitFunctionLikeDeclaration = (
node: ts.FunctionLikeDeclaration,
) => {
let hasAbstractModifier = false;
// Check if it has a declare modifier first
if (node.modifiers) {
hasAbstractModifier = visitModifiers(node.modifiers);
if (ts.isMethodDeclaration(node) && hasAbstractModifier) {
strip.push({
start: node.pos,
end: node.end,
});
}
}
if (node.name && !ts.isIdentifier(node.name)) {
visitor(node.name);
}
if (node.typeParameters) {
strip.push({
start: node.typeParameters.pos - 1, // <
end: node.typeParameters.end,
trailing: /,?\s*\>/,
});
}
if (node.questionToken) {
strip.push({ start: node.questionToken.pos, end: node.questionToken.end });
}
if (node.parameters) {
const parameters = node.parameters;
for (let i = 0; i < parameters.length; i++) {
visitParameter(parameters[i]);
}
}
if (node.type) {
strip.push({ start: node.type.pos - 1, end: node.type.end });
}
if (node.body) {
visitor(node.body);
} else if (!hasAbstractModifier) throw new TypeStripError("overload");
};
const visitCallOrNewExpression = (
node: ts.CallExpression | ts.NewExpression,
) => {
if (node.typeArguments) {
strip.push({
start: node.typeArguments.pos - 1,
end: node.typeArguments.end,
trailing: /\s*\>/,
});
}
if (node.expression) {
visitor(node.expression);
}
if (node.arguments) {
const args = node.arguments;
for (let i = 0; i < args.length; i++) {
const argument = args[i];
if (!ts.isIdentifier(argument)) {
visitor(argument);
}
}
}
};
/**
* Handle class like declarations and visits all members
*
* @example
* class Dog {};
*
* export class {};
*
* abstract class Thing {};
*/
const visitClassLike = (node: ts.ClassLikeDeclaration) => {
// Check if it has a declare modifier first
if (node.modifiers) {
visitModifiers(node.modifiers);
}
if (node.typeParameters) {
strip.push({
start: node.typeParameters.pos - 1,
end: node.typeParameters.end,
trailing: /,?\s*\>/,
});
}
if (node.heritageClauses) {
const heritageClauses = node.heritageClauses;
for (let i = 0; i < heritageClauses.length; i++) {
const heritageClause = heritageClauses[i];
if (heritageClause.token === SyntaxKind.ImplementsKeyword) {
strip.push({ start: heritageClause.pos, end: heritageClause.end });
} else {
const children = heritageClause.types;
for (let i = 0; i < children.length; i++) {
const child = children[i];
visitor(child);
}
}
}
}
if (node.members) {
const members = node.members;
for (let i = 0; i < members.length; i++) {
const member = members[i];
switch (member.kind) {
case SyntaxKind.IndexSignature:
strip.push({ start: member.pos, end: member.end });
break;
case SyntaxKind.PropertyDeclaration:
if (removeComments) {
stripComments(member, "leading");
}
visitPropertyDeclaration(member as ts.PropertyDeclaration);
if (removeComments) {
stripComments(member, "trailing");
}
break;
default:
visitor(member);
break;
}
}
}
};
const visitPropertyDeclaration = (node: ts.PropertyDeclaration) => {
if (node.modifiers) {
visitModifiers(node.modifiers);
}
if (!ts.isIdentifier(node.name)) {
visitor(node.name);
}
if (node.questionToken) {
strip.push({ start: node.questionToken.pos, end: node.questionToken.end });
}
if (node.exclamationToken) {
strip.push({
start: node.exclamationToken.pos,
end: node.exclamationToken.end,
});
}
if (node.type) {
strip.push({ start: node.type.pos - 1, end: node.type.end });
}
if (node.initializer) {
visitor(node.initializer);
}
};
const visitModifiers = (node: ts.NodeArray<ts.ModifierLike>) => {
let hasAbstractModifier = false;
for (let i = 0; i < node.length; i++) {
const modifier = node[i];
if (modifier.kind === SyntaxKind.DeclareKeyword) {
throw new TypeStripError("declare");
}
if (modifier.kind === SyntaxKind.AccessorKeyword) {
throw new TypeStripError("accessor-keyword");
}
if (modifier.kind === SyntaxKind.Decorator) {
throw new TypeStripError("decorator");
}
if (
modifier.kind === SyntaxKind.PublicKeyword ||
modifier.kind === SyntaxKind.PrivateKeyword ||
modifier.kind === SyntaxKind.ProtectedKeyword ||
modifier.kind === SyntaxKind.ReadonlyKeyword ||
modifier.kind === SyntaxKind.OverrideKeyword
) {
strip.push({ start: modifier.pos, end: modifier.end });
} else if (modifier.kind === SyntaxKind.AbstractKeyword) {
strip.push({ start: modifier.pos, end: modifier.end });
hasAbstractModifier = true;
}
}
return hasAbstractModifier;
};
/**
* Checks whether one of the modifiers matches against any searched kind
*/
const hasModifier = (
modifiers: ArrayLike<ts.ModifierLike>,
kind: ts.SyntaxKind | ts.SyntaxKind[],
) => {
const kindArray = Array.isArray(kind) ? kind : [kind];
for (let i = 0; i < modifiers.length; i++) {
if (kindArray.includes(modifiers[i].kind)) {
return true;
}
}
return false;
};
const isNotUndefined = <T>(x: T) => x !== undefined;