@typespec/emitter-framework 0.8.0-dev.1 → 0.8.0-dev.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/dist/src/typescript/components/enum-declaration.d.ts +1 -0
- package/dist/src/typescript/components/enum-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/enum-declaration.js +7 -0
- package/dist/src/typescript/components/function-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/function-declaration.js +8 -3
- package/dist/src/typescript/components/interface-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/interface-declaration.js +3 -0
- package/dist/src/typescript/components/interface-member.d.ts +3 -1
- package/dist/src/typescript/components/interface-member.d.ts.map +1 -1
- package/dist/src/typescript/components/interface-member.js +2 -0
- package/dist/src/typescript/components/interface-method.d.ts +3 -1
- package/dist/src/typescript/components/interface-method.d.ts.map +1 -1
- package/dist/src/typescript/components/interface-method.js +9 -2
- package/dist/src/typescript/components/type-alias-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/type-alias-declaration.js +4 -1
- package/dist/src/typescript/components/type-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/type-declaration.js +10 -0
- package/dist/src/typescript/components/union-declaration.d.ts +3 -1
- package/dist/src/typescript/components/union-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/union-declaration.js +4 -1
- package/dist/src/typescript/components/union-expression.d.ts.map +1 -1
- package/dist/src/typescript/components/union-expression.js +103 -10
- package/dist/src/typescript/utils/operation.d.ts.map +1 -1
- package/dist/src/typescript/utils/operation.js +5 -0
- package/dist/test/typescript/components/enum-declaration.test.js +79 -0
- package/dist/test/typescript/components/function-declaration.test.js +81 -0
- package/dist/test/typescript/components/interface-declaration.test.js +232 -9
- package/dist/test/typescript/components/type-alias-declaration.test.js +75 -0
- package/dist/test/typescript/components/union-declaration.test.js +358 -106
- package/package.json +6 -6
- package/src/typescript/components/enum-declaration.tsx +12 -1
- package/src/typescript/components/function-declaration.tsx +6 -1
- package/src/typescript/components/interface-declaration.tsx +3 -1
- package/src/typescript/components/interface-member.tsx +4 -0
- package/src/typescript/components/interface-method.tsx +7 -1
- package/src/typescript/components/type-alias-declaration.tsx +2 -1
- package/src/typescript/components/type-declaration.tsx +8 -6
- package/src/typescript/components/union-declaration.tsx +4 -1
- package/src/typescript/components/union-expression.tsx +100 -7
- package/src/typescript/utils/operation.ts +3 -0
- package/test/typescript/components/enum-declaration.test.tsx +72 -0
- package/test/typescript/components/function-declaration.test.tsx +78 -0
- package/test/typescript/components/interface-declaration.test.tsx +223 -9
- package/test/typescript/components/type-alias-declaration.test.tsx +72 -0
- package/test/typescript/components/union-declaration.test.tsx +330 -102
|
@@ -9,6 +9,200 @@ import { getProgram } from "../test-host.js";
|
|
|
9
9
|
|
|
10
10
|
describe("Typescript Interface", () => {
|
|
11
11
|
describe("Interface bound to Typespec Types", () => {
|
|
12
|
+
it("declares an interface with multi line docs, explicit docs passed", async () => {
|
|
13
|
+
const program = await getProgram(`
|
|
14
|
+
namespace DemoService;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* This is a test
|
|
18
|
+
* with multiple lines
|
|
19
|
+
*/
|
|
20
|
+
model Foo {
|
|
21
|
+
knownProp: string;
|
|
22
|
+
}
|
|
23
|
+
`);
|
|
24
|
+
|
|
25
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
26
|
+
const models = Array.from((namespace as Namespace).models.values());
|
|
27
|
+
|
|
28
|
+
const res = render(
|
|
29
|
+
<Output program={program}>
|
|
30
|
+
<SourceFile path="test.ts">
|
|
31
|
+
<List hardline>
|
|
32
|
+
{models.map((model) => (
|
|
33
|
+
<InterfaceDeclaration
|
|
34
|
+
export
|
|
35
|
+
type={model}
|
|
36
|
+
doc={["This is an overridden doc comment\nwith multiple lines"]}
|
|
37
|
+
/>
|
|
38
|
+
))}
|
|
39
|
+
</List>
|
|
40
|
+
</SourceFile>
|
|
41
|
+
</Output>,
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const testFile = res.contents.find((file) => file.path === "test.ts");
|
|
45
|
+
assert(testFile, "test.ts file not rendered");
|
|
46
|
+
const actualContent = await format(testFile.contents as string, { parser: "typescript" });
|
|
47
|
+
const expectedContent = await format(
|
|
48
|
+
`
|
|
49
|
+
/**
|
|
50
|
+
* This is an overridden doc comment
|
|
51
|
+
* with multiple lines
|
|
52
|
+
*/
|
|
53
|
+
export interface Foo {
|
|
54
|
+
knownProp: string;
|
|
55
|
+
}
|
|
56
|
+
`,
|
|
57
|
+
{
|
|
58
|
+
parser: "typescript",
|
|
59
|
+
},
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
expect(actualContent).toBe(expectedContent);
|
|
63
|
+
});
|
|
64
|
+
it("declares an interface with multi line docs", async () => {
|
|
65
|
+
const program = await getProgram(`
|
|
66
|
+
namespace DemoService;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* This is a test
|
|
70
|
+
* with multiple lines
|
|
71
|
+
*/
|
|
72
|
+
model Foo {
|
|
73
|
+
knownProp: string;
|
|
74
|
+
}
|
|
75
|
+
`);
|
|
76
|
+
|
|
77
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
78
|
+
const models = Array.from((namespace as Namespace).models.values());
|
|
79
|
+
|
|
80
|
+
const res = render(
|
|
81
|
+
<Output program={program}>
|
|
82
|
+
<SourceFile path="test.ts">
|
|
83
|
+
<List hardline>
|
|
84
|
+
{models.map((model) => (
|
|
85
|
+
<InterfaceDeclaration export type={model} />
|
|
86
|
+
))}
|
|
87
|
+
</List>
|
|
88
|
+
</SourceFile>
|
|
89
|
+
</Output>,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const testFile = res.contents.find((file) => file.path === "test.ts");
|
|
93
|
+
assert(testFile, "test.ts file not rendered");
|
|
94
|
+
const actualContent = await format(testFile.contents as string, { parser: "typescript" });
|
|
95
|
+
const expectedContent = await format(
|
|
96
|
+
`
|
|
97
|
+
/**
|
|
98
|
+
* This is a test
|
|
99
|
+
* with multiple lines
|
|
100
|
+
*/
|
|
101
|
+
export interface Foo {
|
|
102
|
+
knownProp: string;
|
|
103
|
+
}
|
|
104
|
+
`,
|
|
105
|
+
{
|
|
106
|
+
parser: "typescript",
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
expect(actualContent).toBe(expectedContent);
|
|
111
|
+
});
|
|
112
|
+
it("declares an interface with @doc", async () => {
|
|
113
|
+
const program = await getProgram(`
|
|
114
|
+
namespace DemoService;
|
|
115
|
+
|
|
116
|
+
@doc("This is a test")
|
|
117
|
+
model Foo {
|
|
118
|
+
knownProp: string;
|
|
119
|
+
}
|
|
120
|
+
`);
|
|
121
|
+
|
|
122
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
123
|
+
const models = Array.from((namespace as Namespace).models.values());
|
|
124
|
+
|
|
125
|
+
const res = render(
|
|
126
|
+
<Output program={program}>
|
|
127
|
+
<SourceFile path="test.ts">
|
|
128
|
+
<List hardline>
|
|
129
|
+
{models.map((model) => (
|
|
130
|
+
<InterfaceDeclaration export type={model} />
|
|
131
|
+
))}
|
|
132
|
+
</List>
|
|
133
|
+
</SourceFile>
|
|
134
|
+
</Output>,
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
const testFile = res.contents.find((file) => file.path === "test.ts");
|
|
138
|
+
assert(testFile, "test.ts file not rendered");
|
|
139
|
+
const actualContent = await format(testFile.contents as string, { parser: "typescript" });
|
|
140
|
+
const expectedContent = await format(
|
|
141
|
+
`
|
|
142
|
+
/**
|
|
143
|
+
* This is a test
|
|
144
|
+
*/
|
|
145
|
+
export interface Foo {
|
|
146
|
+
knownProp: string;
|
|
147
|
+
}
|
|
148
|
+
`,
|
|
149
|
+
{
|
|
150
|
+
parser: "typescript",
|
|
151
|
+
},
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
expect(actualContent).toBe(expectedContent);
|
|
155
|
+
});
|
|
156
|
+
it("declares an interface with doc", async () => {
|
|
157
|
+
const program = await getProgram(`
|
|
158
|
+
namespace DemoService;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* This is a test
|
|
162
|
+
*/
|
|
163
|
+
model Foo {
|
|
164
|
+
@doc("This is a known property")
|
|
165
|
+
knownProp: string;
|
|
166
|
+
}
|
|
167
|
+
`);
|
|
168
|
+
|
|
169
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
170
|
+
const models = Array.from((namespace as Namespace).models.values());
|
|
171
|
+
|
|
172
|
+
const res = render(
|
|
173
|
+
<Output program={program}>
|
|
174
|
+
<SourceFile path="test.ts">
|
|
175
|
+
<List hardline>
|
|
176
|
+
{models.map((model) => (
|
|
177
|
+
<InterfaceDeclaration export type={model} />
|
|
178
|
+
))}
|
|
179
|
+
</List>
|
|
180
|
+
</SourceFile>
|
|
181
|
+
</Output>,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
const testFile = res.contents.find((file) => file.path === "test.ts");
|
|
185
|
+
assert(testFile, "test.ts file not rendered");
|
|
186
|
+
const actualContent = await format(testFile.contents as string, { parser: "typescript" });
|
|
187
|
+
const expectedContent = await format(
|
|
188
|
+
`
|
|
189
|
+
/**
|
|
190
|
+
* This is a test
|
|
191
|
+
*/
|
|
192
|
+
export interface Foo {
|
|
193
|
+
/**
|
|
194
|
+
* This is a known property
|
|
195
|
+
*/
|
|
196
|
+
knownProp: string;
|
|
197
|
+
}
|
|
198
|
+
`,
|
|
199
|
+
{
|
|
200
|
+
parser: "typescript",
|
|
201
|
+
},
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
expect(actualContent).toBe(expectedContent);
|
|
205
|
+
});
|
|
12
206
|
describe("Bound to Model", () => {
|
|
13
207
|
it("creates an interface that extends a model for Record spread", async () => {
|
|
14
208
|
const program = await getProgram(`
|
|
@@ -623,8 +817,19 @@ describe("Typescript Interface", () => {
|
|
|
623
817
|
const program = await getProgram(`
|
|
624
818
|
namespace DemoService;
|
|
625
819
|
|
|
820
|
+
/**
|
|
821
|
+
* Operations for Widget
|
|
822
|
+
*/
|
|
626
823
|
interface WidgetOperations {
|
|
627
|
-
|
|
824
|
+
/**
|
|
825
|
+
* Get the name of the widget
|
|
826
|
+
*/
|
|
827
|
+
op getName(
|
|
828
|
+
/**
|
|
829
|
+
* The id of the widget
|
|
830
|
+
*/
|
|
831
|
+
id: string
|
|
832
|
+
): Widget;
|
|
628
833
|
}
|
|
629
834
|
|
|
630
835
|
model Widget {
|
|
@@ -653,14 +858,23 @@ describe("Typescript Interface", () => {
|
|
|
653
858
|
assert(testFile, "test.ts file not rendered");
|
|
654
859
|
const actualContent = await format(testFile.contents as string, { parser: "typescript" });
|
|
655
860
|
const expectedContent = await format(
|
|
656
|
-
`
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
861
|
+
`
|
|
862
|
+
/**
|
|
863
|
+
* Operations for Widget
|
|
864
|
+
*/
|
|
865
|
+
export interface WidgetOperations {
|
|
866
|
+
/**
|
|
867
|
+
* Get the name of the widget
|
|
868
|
+
*
|
|
869
|
+
* @param {string} id - The id of the widget
|
|
870
|
+
*/
|
|
871
|
+
getName(id: string): Widget;
|
|
872
|
+
}
|
|
873
|
+
export interface Widget {
|
|
874
|
+
id: string;
|
|
875
|
+
weight: number;
|
|
876
|
+
color: "blue" | "red";
|
|
877
|
+
}`,
|
|
664
878
|
{
|
|
665
879
|
parser: "typescript",
|
|
666
880
|
},
|
|
@@ -37,6 +37,78 @@ describe("Typescript Type Alias Declaration", () => {
|
|
|
37
37
|
expect(actualContent).toBe(expectedContent);
|
|
38
38
|
});
|
|
39
39
|
|
|
40
|
+
it("creates a type alias declaration with JSDoc", async () => {
|
|
41
|
+
const program = await getProgram(`
|
|
42
|
+
namespace DemoService;
|
|
43
|
+
/**
|
|
44
|
+
* Type to represent a date
|
|
45
|
+
*/
|
|
46
|
+
scalar MyDate extends utcDateTime;
|
|
47
|
+
`);
|
|
48
|
+
|
|
49
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
50
|
+
const scalar = Array.from((namespace as Namespace).scalars.values())[0];
|
|
51
|
+
|
|
52
|
+
const res = render(
|
|
53
|
+
<Output program={program}>
|
|
54
|
+
<SourceFile path="test.ts">
|
|
55
|
+
<TypeAliasDeclaration type={scalar} />
|
|
56
|
+
</SourceFile>
|
|
57
|
+
</Output>,
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const testFile = res.contents.find((file) => file.path === "test.ts");
|
|
61
|
+
assert(testFile, "test.ts file not rendered");
|
|
62
|
+
const actualContent = await format(testFile.contents as string, { parser: "typescript" });
|
|
63
|
+
const expectedContent = await format(
|
|
64
|
+
`
|
|
65
|
+
/**
|
|
66
|
+
* Type to represent a date
|
|
67
|
+
*/
|
|
68
|
+
type MyDate = Date;`,
|
|
69
|
+
{
|
|
70
|
+
parser: "typescript",
|
|
71
|
+
},
|
|
72
|
+
);
|
|
73
|
+
expect(actualContent).toBe(expectedContent);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("can override JSDoc", async () => {
|
|
77
|
+
const program = await getProgram(`
|
|
78
|
+
namespace DemoService;
|
|
79
|
+
/**
|
|
80
|
+
* Type to represent a date
|
|
81
|
+
*/
|
|
82
|
+
scalar MyDate extends utcDateTime;
|
|
83
|
+
`);
|
|
84
|
+
|
|
85
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
86
|
+
const scalar = Array.from((namespace as Namespace).scalars.values())[0];
|
|
87
|
+
|
|
88
|
+
const res = render(
|
|
89
|
+
<Output program={program}>
|
|
90
|
+
<SourceFile path="test.ts">
|
|
91
|
+
<TypeAliasDeclaration doc={"Overridden Doc"} type={scalar} />
|
|
92
|
+
</SourceFile>
|
|
93
|
+
</Output>,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const testFile = res.contents.find((file) => file.path === "test.ts");
|
|
97
|
+
assert(testFile, "test.ts file not rendered");
|
|
98
|
+
const actualContent = await format(testFile.contents as string, { parser: "typescript" });
|
|
99
|
+
const expectedContent = await format(
|
|
100
|
+
`
|
|
101
|
+
/**
|
|
102
|
+
* Overridden Doc
|
|
103
|
+
*/
|
|
104
|
+
type MyDate = Date;`,
|
|
105
|
+
{
|
|
106
|
+
parser: "typescript",
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
expect(actualContent).toBe(expectedContent);
|
|
110
|
+
});
|
|
111
|
+
|
|
40
112
|
it("creates a type alias declaration for a utcDateTime with unixTimeStamp encoding", async () => {
|
|
41
113
|
const program = await getProgram(`
|
|
42
114
|
namespace DemoService;
|