File: generateTypes_classes.ts

package info (click to toggle)
node-typescript 3.3.3333-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 324,548 kB
  • sloc: makefile: 6; sh: 3
file content (149 lines) | stat: -rw-r--r-- 3,329 bytes parent folder | download
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
/// <reference path="fourslash.ts" />

////dummy text

verify.generateTypes(
{
    value: class {},
    output:
`export = example;
declare class example {
}
`,
},

{
    value: class {
        constructor(x) {
            (this as any).x = x;
            // Code inside this function should be ignored
            function f(this: any) {
                this.y = 0;
            }
            // Same for this class
            class Other { constructor() { (this as any).z = 0; } }
        }
    },
    output:
`export = example;
declare class example {
    constructor(x: any);
    x: any;
}
`,
},
{
    value: { x: 0, export: 0 },
    output: `export const x: number;
`,
},
{
    value: (() => {
        class Super {
            superField = 0; // TODO: climb to prototype.constructor and get instance fields?
            superMethod() {}
            static superStaticMethod() {}
        }
        class C extends Super {
            constructor() {
                super();
                (this as any)._privateField = 0;
                (this as any).field = 0;
            }

            _privateMethod() {}
            method(_p) {
                (this as any).otherField = 0; // TODO: include this in output?
            }

            static _privateStatic() {}
            static staticMethod(_s: any) {}
            static staticMethodWithNoNamespaceMembers(_p: any) {}

            static _privateStaticField = 0;
            static staticField = 0;
            static innerClass = class {};
        }
        (C.prototype as any).prototypeNonFunction = 0; // ignored
        (C.staticMethod as any).staticMethodProperty = 0;
        (C.staticMethod as any)._staticFieldPrivateMember = 0;
        (C.prototype.method as any).methodMember = 0; // ignored
        // Non-identifier names should be ignored.
        (C as any)["&"] = function() {};
        (C.prototype as any)["&"] = function() {};
        return C;
    })(),
    output:
`export = example;
declare class example {
    static staticField: number;
    static staticMethodWithNoNamespaceMembers(_p: any): void;
    static superStaticMethod(): void;
    field: any;
    method(_p: any): void;
    superMethod(): void;
}
declare namespace example {
    class innerClass {
    }
    function staticMethod(_s: any): void;
    namespace staticMethod {
        const staticMethodProperty: number;
    }
}
`,
},

{
    value: (() => {
        function F() { this.x = 0; }
        (F as any).staticMethod = function() {}
        F.prototype.method = function() { }
        return F;
    })(),
    output:
`export = example;
declare class example {
    static staticMethod(): void;
    x: any;
    method(): void;
}
`,
},

{
    // No duplicate instance members
    value: (() => {
        class C {
            constructor() {
                (this as any).x = 0;
                (this as any).x = 1;
                (this as any).m = 0;
            }
            m() {}
        }
        return C;
    })(),
    output:
`export = example;
declare class example {
    x: any;
    m(): void;
}
`,
},

{
    // nontrivial prototype marks something as an instance
    value: (() => {
        const obj = Object.create({});
        obj.m = function() { this.x = 0; }
        return { obj };
    })(),
    output:
`export const obj: {
    m: Function;
};
`,
},
);