@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
|
@@ -40,6 +40,87 @@ describe("Typescript Function Declaration", () => {
|
|
|
40
40
|
});
|
|
41
41
|
expect(actualContent).toBe(expectedContent);
|
|
42
42
|
});
|
|
43
|
+
it("creates a function with JSDoc", async () => {
|
|
44
|
+
const program = await getProgram(`
|
|
45
|
+
namespace DemoService;
|
|
46
|
+
/**
|
|
47
|
+
* This is a test function
|
|
48
|
+
*/
|
|
49
|
+
op getName(
|
|
50
|
+
@doc("This is the id")
|
|
51
|
+
id: string, name: string): string;
|
|
52
|
+
`);
|
|
53
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
54
|
+
const operation = Array.from(namespace.operations.values())[0];
|
|
55
|
+
const res = render(_$createComponent(Output, {
|
|
56
|
+
program: program,
|
|
57
|
+
get children() {
|
|
58
|
+
return _$createComponent(SourceFile, {
|
|
59
|
+
path: "test.ts",
|
|
60
|
+
get children() {
|
|
61
|
+
return _$createComponent(FunctionDeclaration, {
|
|
62
|
+
type: operation
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}));
|
|
68
|
+
const testFile = res.contents.find(file => file.path === "test.ts");
|
|
69
|
+
assert(testFile, "test.ts file not rendered");
|
|
70
|
+
const actualContent = await format(testFile.contents, {
|
|
71
|
+
parser: "typescript"
|
|
72
|
+
});
|
|
73
|
+
const expectedContent = await format(`
|
|
74
|
+
/**
|
|
75
|
+
* This is a test function
|
|
76
|
+
*
|
|
77
|
+
* @param {string} id - This is the id
|
|
78
|
+
* @param {string} name
|
|
79
|
+
*/
|
|
80
|
+
function getName(id: string, name: string): string{}`, {
|
|
81
|
+
parser: "typescript"
|
|
82
|
+
});
|
|
83
|
+
expect(actualContent).toBe(expectedContent);
|
|
84
|
+
});
|
|
85
|
+
it("creates a function with overridden JSDoc", async () => {
|
|
86
|
+
const program = await getProgram(`
|
|
87
|
+
namespace DemoService;
|
|
88
|
+
/**
|
|
89
|
+
* This is a test function
|
|
90
|
+
*/
|
|
91
|
+
op getName(id: string): string;`);
|
|
92
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
93
|
+
const operation = Array.from(namespace.operations.values())[0];
|
|
94
|
+
const res = render(_$createComponent(Output, {
|
|
95
|
+
program: program,
|
|
96
|
+
get children() {
|
|
97
|
+
return _$createComponent(SourceFile, {
|
|
98
|
+
path: "test.ts",
|
|
99
|
+
get children() {
|
|
100
|
+
return _$createComponent(FunctionDeclaration, {
|
|
101
|
+
doc: ["This is a custom description"],
|
|
102
|
+
type: operation
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}));
|
|
108
|
+
const testFile = res.contents.find(file => file.path === "test.ts");
|
|
109
|
+
assert(testFile, "test.ts file not rendered");
|
|
110
|
+
const actualContent = await format(testFile.contents, {
|
|
111
|
+
parser: "typescript"
|
|
112
|
+
});
|
|
113
|
+
const expectedContent = await format(`
|
|
114
|
+
/**
|
|
115
|
+
* This is a custom description
|
|
116
|
+
*
|
|
117
|
+
* @param {string} id
|
|
118
|
+
*/
|
|
119
|
+
function getName(id: string): string{}`, {
|
|
120
|
+
parser: "typescript"
|
|
121
|
+
});
|
|
122
|
+
expect(actualContent).toBe(expectedContent);
|
|
123
|
+
});
|
|
43
124
|
it("creates an async function", async () => {
|
|
44
125
|
const program = await getProgram(`
|
|
45
126
|
namespace DemoService;
|
|
@@ -8,6 +8,209 @@ import { InterfaceDeclaration } from "../../../src/typescript/components/interfa
|
|
|
8
8
|
import { getProgram } from "../test-host.js";
|
|
9
9
|
describe("Typescript Interface", () => {
|
|
10
10
|
describe("Interface bound to Typespec Types", () => {
|
|
11
|
+
it("declares an interface with multi line docs, explicit docs passed", async () => {
|
|
12
|
+
const program = await getProgram(`
|
|
13
|
+
namespace DemoService;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* This is a test
|
|
17
|
+
* with multiple lines
|
|
18
|
+
*/
|
|
19
|
+
model Foo {
|
|
20
|
+
knownProp: string;
|
|
21
|
+
}
|
|
22
|
+
`);
|
|
23
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
24
|
+
const models = Array.from(namespace.models.values());
|
|
25
|
+
const res = render(_$createComponent(Output, {
|
|
26
|
+
program: program,
|
|
27
|
+
get children() {
|
|
28
|
+
return _$createComponent(SourceFile, {
|
|
29
|
+
path: "test.ts",
|
|
30
|
+
get children() {
|
|
31
|
+
return _$createComponent(List, {
|
|
32
|
+
hardline: true,
|
|
33
|
+
get children() {
|
|
34
|
+
return models.map(model => _$createComponent(InterfaceDeclaration, {
|
|
35
|
+
"export": true,
|
|
36
|
+
type: model,
|
|
37
|
+
doc: ["This is an overridden doc comment\nwith multiple lines"]
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}));
|
|
45
|
+
const testFile = res.contents.find(file => file.path === "test.ts");
|
|
46
|
+
assert(testFile, "test.ts file not rendered");
|
|
47
|
+
const actualContent = await format(testFile.contents, {
|
|
48
|
+
parser: "typescript"
|
|
49
|
+
});
|
|
50
|
+
const expectedContent = await format(`
|
|
51
|
+
/**
|
|
52
|
+
* This is an overridden doc comment
|
|
53
|
+
* with multiple lines
|
|
54
|
+
*/
|
|
55
|
+
export interface Foo {
|
|
56
|
+
knownProp: string;
|
|
57
|
+
}
|
|
58
|
+
`, {
|
|
59
|
+
parser: "typescript"
|
|
60
|
+
});
|
|
61
|
+
expect(actualContent).toBe(expectedContent);
|
|
62
|
+
});
|
|
63
|
+
it("declares an interface with multi line docs", async () => {
|
|
64
|
+
const program = await getProgram(`
|
|
65
|
+
namespace DemoService;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* This is a test
|
|
69
|
+
* with multiple lines
|
|
70
|
+
*/
|
|
71
|
+
model Foo {
|
|
72
|
+
knownProp: string;
|
|
73
|
+
}
|
|
74
|
+
`);
|
|
75
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
76
|
+
const models = Array.from(namespace.models.values());
|
|
77
|
+
const res = render(_$createComponent(Output, {
|
|
78
|
+
program: program,
|
|
79
|
+
get children() {
|
|
80
|
+
return _$createComponent(SourceFile, {
|
|
81
|
+
path: "test.ts",
|
|
82
|
+
get children() {
|
|
83
|
+
return _$createComponent(List, {
|
|
84
|
+
hardline: true,
|
|
85
|
+
get children() {
|
|
86
|
+
return models.map(model => _$createComponent(InterfaceDeclaration, {
|
|
87
|
+
"export": true,
|
|
88
|
+
type: model
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
});
|
|
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, {
|
|
99
|
+
parser: "typescript"
|
|
100
|
+
});
|
|
101
|
+
const expectedContent = await format(`
|
|
102
|
+
/**
|
|
103
|
+
* This is a test
|
|
104
|
+
* with multiple lines
|
|
105
|
+
*/
|
|
106
|
+
export interface Foo {
|
|
107
|
+
knownProp: string;
|
|
108
|
+
}
|
|
109
|
+
`, {
|
|
110
|
+
parser: "typescript"
|
|
111
|
+
});
|
|
112
|
+
expect(actualContent).toBe(expectedContent);
|
|
113
|
+
});
|
|
114
|
+
it("declares an interface with @doc", async () => {
|
|
115
|
+
const program = await getProgram(`
|
|
116
|
+
namespace DemoService;
|
|
117
|
+
|
|
118
|
+
@doc("This is a test")
|
|
119
|
+
model Foo {
|
|
120
|
+
knownProp: string;
|
|
121
|
+
}
|
|
122
|
+
`);
|
|
123
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
124
|
+
const models = Array.from(namespace.models.values());
|
|
125
|
+
const res = render(_$createComponent(Output, {
|
|
126
|
+
program: program,
|
|
127
|
+
get children() {
|
|
128
|
+
return _$createComponent(SourceFile, {
|
|
129
|
+
path: "test.ts",
|
|
130
|
+
get children() {
|
|
131
|
+
return _$createComponent(List, {
|
|
132
|
+
hardline: true,
|
|
133
|
+
get children() {
|
|
134
|
+
return models.map(model => _$createComponent(InterfaceDeclaration, {
|
|
135
|
+
"export": true,
|
|
136
|
+
type: model
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}));
|
|
144
|
+
const testFile = res.contents.find(file => file.path === "test.ts");
|
|
145
|
+
assert(testFile, "test.ts file not rendered");
|
|
146
|
+
const actualContent = await format(testFile.contents, {
|
|
147
|
+
parser: "typescript"
|
|
148
|
+
});
|
|
149
|
+
const expectedContent = await format(`
|
|
150
|
+
/**
|
|
151
|
+
* This is a test
|
|
152
|
+
*/
|
|
153
|
+
export interface Foo {
|
|
154
|
+
knownProp: string;
|
|
155
|
+
}
|
|
156
|
+
`, {
|
|
157
|
+
parser: "typescript"
|
|
158
|
+
});
|
|
159
|
+
expect(actualContent).toBe(expectedContent);
|
|
160
|
+
});
|
|
161
|
+
it("declares an interface with doc", async () => {
|
|
162
|
+
const program = await getProgram(`
|
|
163
|
+
namespace DemoService;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* This is a test
|
|
167
|
+
*/
|
|
168
|
+
model Foo {
|
|
169
|
+
@doc("This is a known property")
|
|
170
|
+
knownProp: string;
|
|
171
|
+
}
|
|
172
|
+
`);
|
|
173
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
174
|
+
const models = Array.from(namespace.models.values());
|
|
175
|
+
const res = render(_$createComponent(Output, {
|
|
176
|
+
program: program,
|
|
177
|
+
get children() {
|
|
178
|
+
return _$createComponent(SourceFile, {
|
|
179
|
+
path: "test.ts",
|
|
180
|
+
get children() {
|
|
181
|
+
return _$createComponent(List, {
|
|
182
|
+
hardline: true,
|
|
183
|
+
get children() {
|
|
184
|
+
return models.map(model => _$createComponent(InterfaceDeclaration, {
|
|
185
|
+
"export": true,
|
|
186
|
+
type: model
|
|
187
|
+
}));
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}));
|
|
194
|
+
const testFile = res.contents.find(file => file.path === "test.ts");
|
|
195
|
+
assert(testFile, "test.ts file not rendered");
|
|
196
|
+
const actualContent = await format(testFile.contents, {
|
|
197
|
+
parser: "typescript"
|
|
198
|
+
});
|
|
199
|
+
const expectedContent = await format(`
|
|
200
|
+
/**
|
|
201
|
+
* This is a test
|
|
202
|
+
*/
|
|
203
|
+
export interface Foo {
|
|
204
|
+
/**
|
|
205
|
+
* This is a known property
|
|
206
|
+
*/
|
|
207
|
+
knownProp: string;
|
|
208
|
+
}
|
|
209
|
+
`, {
|
|
210
|
+
parser: "typescript"
|
|
211
|
+
});
|
|
212
|
+
expect(actualContent).toBe(expectedContent);
|
|
213
|
+
});
|
|
11
214
|
describe("Bound to Model", () => {
|
|
12
215
|
it("creates an interface that extends a model for Record spread", async () => {
|
|
13
216
|
const program = await getProgram(`
|
|
@@ -681,8 +884,19 @@ describe("Typescript Interface", () => {
|
|
|
681
884
|
const program = await getProgram(`
|
|
682
885
|
namespace DemoService;
|
|
683
886
|
|
|
887
|
+
/**
|
|
888
|
+
* Operations for Widget
|
|
889
|
+
*/
|
|
684
890
|
interface WidgetOperations {
|
|
685
|
-
|
|
891
|
+
/**
|
|
892
|
+
* Get the name of the widget
|
|
893
|
+
*/
|
|
894
|
+
op getName(
|
|
895
|
+
/**
|
|
896
|
+
* The id of the widget
|
|
897
|
+
*/
|
|
898
|
+
id: string
|
|
899
|
+
): Widget;
|
|
686
900
|
}
|
|
687
901
|
|
|
688
902
|
model Widget {
|
|
@@ -718,14 +932,23 @@ describe("Typescript Interface", () => {
|
|
|
718
932
|
const actualContent = await format(testFile.contents, {
|
|
719
933
|
parser: "typescript"
|
|
720
934
|
});
|
|
721
|
-
const expectedContent = await format(`
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
935
|
+
const expectedContent = await format(`
|
|
936
|
+
/**
|
|
937
|
+
* Operations for Widget
|
|
938
|
+
*/
|
|
939
|
+
export interface WidgetOperations {
|
|
940
|
+
/**
|
|
941
|
+
* Get the name of the widget
|
|
942
|
+
*
|
|
943
|
+
* @param {string} id - The id of the widget
|
|
944
|
+
*/
|
|
945
|
+
getName(id: string): Widget;
|
|
946
|
+
}
|
|
947
|
+
export interface Widget {
|
|
948
|
+
id: string;
|
|
949
|
+
weight: number;
|
|
950
|
+
color: "blue" | "red";
|
|
951
|
+
}`, {
|
|
729
952
|
parser: "typescript"
|
|
730
953
|
});
|
|
731
954
|
expect(actualContent).toBe(expectedContent);
|
|
@@ -40,6 +40,81 @@ describe("Typescript Type Alias Declaration", () => {
|
|
|
40
40
|
});
|
|
41
41
|
expect(actualContent).toBe(expectedContent);
|
|
42
42
|
});
|
|
43
|
+
it("creates a type alias declaration with JSDoc", async () => {
|
|
44
|
+
const program = await getProgram(`
|
|
45
|
+
namespace DemoService;
|
|
46
|
+
/**
|
|
47
|
+
* Type to represent a date
|
|
48
|
+
*/
|
|
49
|
+
scalar MyDate extends utcDateTime;
|
|
50
|
+
`);
|
|
51
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
52
|
+
const scalar = Array.from(namespace.scalars.values())[0];
|
|
53
|
+
const res = render(_$createComponent(Output, {
|
|
54
|
+
program: program,
|
|
55
|
+
get children() {
|
|
56
|
+
return _$createComponent(SourceFile, {
|
|
57
|
+
path: "test.ts",
|
|
58
|
+
get children() {
|
|
59
|
+
return _$createComponent(TypeAliasDeclaration, {
|
|
60
|
+
type: scalar
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}));
|
|
66
|
+
const testFile = res.contents.find(file => file.path === "test.ts");
|
|
67
|
+
assert(testFile, "test.ts file not rendered");
|
|
68
|
+
const actualContent = await format(testFile.contents, {
|
|
69
|
+
parser: "typescript"
|
|
70
|
+
});
|
|
71
|
+
const expectedContent = await format(`
|
|
72
|
+
/**
|
|
73
|
+
* Type to represent a date
|
|
74
|
+
*/
|
|
75
|
+
type MyDate = Date;`, {
|
|
76
|
+
parser: "typescript"
|
|
77
|
+
});
|
|
78
|
+
expect(actualContent).toBe(expectedContent);
|
|
79
|
+
});
|
|
80
|
+
it("can override JSDoc", async () => {
|
|
81
|
+
const program = await getProgram(`
|
|
82
|
+
namespace DemoService;
|
|
83
|
+
/**
|
|
84
|
+
* Type to represent a date
|
|
85
|
+
*/
|
|
86
|
+
scalar MyDate extends utcDateTime;
|
|
87
|
+
`);
|
|
88
|
+
const [namespace] = program.resolveTypeReference("DemoService");
|
|
89
|
+
const scalar = Array.from(namespace.scalars.values())[0];
|
|
90
|
+
const res = render(_$createComponent(Output, {
|
|
91
|
+
program: program,
|
|
92
|
+
get children() {
|
|
93
|
+
return _$createComponent(SourceFile, {
|
|
94
|
+
path: "test.ts",
|
|
95
|
+
get children() {
|
|
96
|
+
return _$createComponent(TypeAliasDeclaration, {
|
|
97
|
+
doc: "Overridden Doc",
|
|
98
|
+
type: scalar
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}));
|
|
104
|
+
const testFile = res.contents.find(file => file.path === "test.ts");
|
|
105
|
+
assert(testFile, "test.ts file not rendered");
|
|
106
|
+
const actualContent = await format(testFile.contents, {
|
|
107
|
+
parser: "typescript"
|
|
108
|
+
});
|
|
109
|
+
const expectedContent = await format(`
|
|
110
|
+
/**
|
|
111
|
+
* Overridden Doc
|
|
112
|
+
*/
|
|
113
|
+
type MyDate = Date;`, {
|
|
114
|
+
parser: "typescript"
|
|
115
|
+
});
|
|
116
|
+
expect(actualContent).toBe(expectedContent);
|
|
117
|
+
});
|
|
43
118
|
it("creates a type alias declaration for a utcDateTime with unixTimeStamp encoding", async () => {
|
|
44
119
|
const program = await getProgram(`
|
|
45
120
|
namespace DemoService;
|