|
| 1 | +import ts from 'typescript'; |
| 2 | +import { evaluate, hasModifier } from './utils'; |
| 3 | + |
| 4 | +export default function(program: ts.Program, pluginOptions: unknown) { |
| 5 | + return (ctx: ts.TransformationContext) => { |
| 6 | + return (sourceFile: ts.SourceFile) => { |
| 7 | + const ambient = sourceFile.isDeclarationFile; |
| 8 | + |
| 9 | + return ts.visitEachChild(sourceFile, visitor, ctx); |
| 10 | + |
| 11 | + function visitor(node: ts.Node): ts.Node { |
| 12 | + if (!ts.isEnumDeclaration(node)) { |
| 13 | + return ts.visitEachChild(node, visitor, ctx); |
| 14 | + } |
| 15 | + if ( |
| 16 | + !hasModifier(node, ts.SyntaxKind.ExportKeyword) |
| 17 | + || !hasModifier(node, ts.SyntaxKind.ConstKeyword) |
| 18 | + ) { |
| 19 | + return node; |
| 20 | + } |
| 21 | + |
| 22 | + if (ambient) { |
| 23 | + return ts.visitEachChild(node, stripConstKeyword, ctx); |
| 24 | + } |
| 25 | + |
| 26 | + return transformEnum(node); |
| 27 | + } |
| 28 | + }; |
| 29 | + }; |
| 30 | + |
| 31 | + function stripConstKeyword(node: ts.Node) { |
| 32 | + return node.kind === ts.SyntaxKind.ConstKeyword ? undefined : node; |
| 33 | + } |
| 34 | + |
| 35 | + function transformEnum(node: ts.EnumDeclaration) { |
| 36 | + const members = node.members; |
| 37 | + const known = new Map<string, number | string>(); |
| 38 | + const ast = []; |
| 39 | + let lastValue: string | number = -1; |
| 40 | + |
| 41 | + for (const member of members) { |
| 42 | + if (!ts.isIdentifier(member.name)) { |
| 43 | + throw new Error('The name of enum member must be an identifier'); |
| 44 | + } |
| 45 | + const name = member.name.getText(); |
| 46 | + let value: string | number; |
| 47 | + |
| 48 | + if (member.initializer) { |
| 49 | + value = evaluate(member.initializer, known); |
| 50 | + let valueExpr: ts.Expression; |
| 51 | + if (typeof value === 'number') { |
| 52 | + valueExpr = ts.factory.createNumericLiteral(value); |
| 53 | + lastValue = value; |
| 54 | + } else if (typeof value === 'string') { |
| 55 | + valueExpr = ts.factory.createStringLiteral(value); |
| 56 | + lastValue = value; |
| 57 | + } else throw new Error('Unsupported member value'); |
| 58 | + |
| 59 | + ast.push(ts.factory.createPropertyAssignment(name, valueExpr)); |
| 60 | + } else { |
| 61 | + if (typeof lastValue === 'string') { |
| 62 | + throw new Error('Invalid enum'); |
| 63 | + } |
| 64 | + /** |
| 65 | + * last known value+1 |
| 66 | + * enum MyEnum { |
| 67 | + * A = 5, |
| 68 | + * B, // implicit 6 |
| 69 | + * C, // implicit 7, |
| 70 | + * D = 1000, |
| 71 | + * E, // implicit 1001 |
| 72 | + * } |
| 73 | + */ |
| 74 | + value = ++lastValue; |
| 75 | + ast.push( |
| 76 | + ts.factory.createPropertyAssignment( |
| 77 | + name, |
| 78 | + ts.factory.createNumericLiteral(value), |
| 79 | + ), |
| 80 | + ); |
| 81 | + } |
| 82 | + known.set(name, value); |
| 83 | + } |
| 84 | + |
| 85 | + return ts.factory.createVariableStatement( |
| 86 | + [ |
| 87 | + ts.factory.createModifier(ts.SyntaxKind.ExportKeyword), |
| 88 | + ts.factory.createModifier(ts.SyntaxKind.ConstKeyword), |
| 89 | + ], |
| 90 | + ts.factory.createVariableDeclarationList( |
| 91 | + [ |
| 92 | + ts.factory.createVariableDeclaration( |
| 93 | + node.name, |
| 94 | + undefined, |
| 95 | + undefined, |
| 96 | + ts.factory.createAsExpression( |
| 97 | + ts.factory.createObjectLiteralExpression(ast, true), |
| 98 | + ts.factory.createTypeReferenceNode( |
| 99 | + ts.factory.createIdentifier('const'), |
| 100 | + undefined, |
| 101 | + ), |
| 102 | + ), |
| 103 | + ), |
| 104 | + ], |
| 105 | + ts.NodeFlags.Const, |
| 106 | + ), |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
0 commit comments