/
convert-expression.ts
34 lines (31 loc) · 1.1 KB
/
convert-expression.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
import * as ts from 'typescript';
import * as _ts from '../ts-internal';
/**
* Return the default value of the given node.
*
* @param node The TypeScript node whose default value should be extracted.
* @returns The default value as a string.
*/
export function convertDefaultValue(node: ts.VariableDeclaration|ts.ParameterDeclaration|ts.EnumMember): string | undefined {
if (node.initializer) {
return convertExpression(node.initializer);
} else {
return undefined;
}
}
export function convertExpression(expression: ts.Expression): string {
switch (expression.kind) {
case ts.SyntaxKind.StringLiteral:
return '"' + (<ts.LiteralExpression> expression).text + '"';
case ts.SyntaxKind.NumericLiteral:
return (<ts.LiteralExpression> expression).text;
case ts.SyntaxKind.TrueKeyword:
return 'true';
case ts.SyntaxKind.FalseKeyword:
return 'false';
case ts.SyntaxKind.NullKeyword:
return 'null';
default:
return expression.getText(expression.getSourceFile());
}
}