This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
variable-declaration.ts
66 lines (60 loc) · 1.74 KB
/
variable-declaration.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
import * as ts from 'typescript';
import { IType } from '../collections';
import { any_type, ElementKind } from '../constants';
import {
create_element,
is_element,
IElement,
IElementOptions,
} from '../element';
import { transform } from '../transform';
import { add_declare_modifier_if_need } from '../utils';
export interface IVariableDeclarationOptions extends IElementOptions {
name:
| string
// for object member
| number;
type?: IType;
export?: boolean;
const?: boolean;
let?: boolean;
}
export interface IVariableDeclaration
extends IElement<ElementKind.VariableDeclaration>,
IVariableDeclarationOptions {}
export const create_variable_declaration = (
options: IVariableDeclarationOptions,
): IVariableDeclaration => ({
...create_element(ElementKind.VariableDeclaration),
...options,
});
export const is_variable_declaration = (
value: any,
): value is IVariableDeclaration =>
is_element(value) && value.kind === ElementKind.VariableDeclaration;
// tslint:disable:ter-indent
/**
* @hidden
*/
export const transform_variable_declaration = (
element: IVariableDeclaration,
path: IElement<any>[],
) => {
const type = transform(element.type || any_type, path) as ts.TypeNode;
return ts.createVariableStatement(
/* modifiers */ add_declare_modifier_if_need(
element.export === true
? [ts.createToken(ts.SyntaxKind.ExportKeyword)]
: undefined,
path,
),
/* declarationList */ ts.createVariableDeclarationList(
/* declarations */ [
ts.createVariableDeclaration(element.name.toString(), type),
],
/* flags */ element.const === true
? ts.NodeFlags.Const
: element.let === true ? ts.NodeFlags.Let : undefined,
),
);
};