@typespec/emitter-framework 0.10.0-dev.2 → 0.10.0

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.
Files changed (29) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/src/core/write-output.js +10 -6
  3. package/dist/src/csharp/components/class/declaration.d.ts.map +1 -1
  4. package/dist/src/csharp/components/class/declaration.js +2 -1
  5. package/dist/src/typescript/components/interface-declaration.d.ts.map +1 -1
  6. package/dist/src/typescript/components/interface-declaration.js +2 -2
  7. package/dist/src/typescript/components/union/expression.test.js +3 -3
  8. package/dist/test/typescript/components/function-declaration.test.js +31 -112
  9. package/dist/test/typescript/components/interface-declaration.test.js +146 -308
  10. package/dist/test/typescript/components/type-alias-declaration.test.js +17 -75
  11. package/dist/test/typescript/components/type-transform.test.js +32 -83
  12. package/dist/test/typescript/components/value-expression.test.js +3 -6
  13. package/dist/test/typescript/utils.d.ts +7 -0
  14. package/dist/test/typescript/utils.d.ts.map +1 -0
  15. package/dist/test/typescript/utils.js +18 -0
  16. package/dist/test/utils.d.ts.map +1 -1
  17. package/dist/test/utils.js +2 -0
  18. package/package.json +14 -15
  19. package/src/core/write-output.ts +10 -6
  20. package/src/csharp/components/class/declaration.tsx +2 -1
  21. package/src/typescript/components/interface-declaration.tsx +3 -3
  22. package/src/typescript/components/union/expression.test.tsx +5 -5
  23. package/test/typescript/components/function-declaration.test.tsx +31 -121
  24. package/test/typescript/components/interface-declaration.test.tsx +123 -342
  25. package/test/typescript/components/type-alias-declaration.test.tsx +17 -76
  26. package/test/typescript/components/type-transform.test.tsx +32 -93
  27. package/test/typescript/components/value-expression.test.tsx +4 -12
  28. package/test/typescript/utils.tsx +12 -0
  29. package/test/utils.ts +2 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog - @typespec/emitter-framework
2
2
 
3
+ ## 0.10.0
4
+
5
+ ### Bump dependencies
6
+
7
+ - [#8050](https://github.com/microsoft/typespec/pull/8050) Upgrade alloy 0.19
8
+ - [#7978](https://github.com/microsoft/typespec/pull/7978) Upgrade dependencies
9
+
10
+
3
11
  ## 0.9.0
4
12
 
5
13
  ### Features
@@ -6,13 +6,17 @@ export async function writeOutput(program, rootComponent, emitterOutputDir) {
6
6
  }
7
7
  async function writeOutputDirectory(program, dir, emitterOutputDir) {
8
8
  for (const sub of dir.contents) {
9
- if (Array.isArray(sub.contents)) {
10
- await writeOutputDirectory(program, sub, emitterOutputDir);
9
+ if ("contents" in sub) {
10
+ if (Array.isArray(sub.contents)) {
11
+ await writeOutputDirectory(program, sub, emitterOutputDir);
12
+ } else {
13
+ await emitFile(program, {
14
+ content: sub.contents,
15
+ path: joinPaths(emitterOutputDir, sub.path)
16
+ });
17
+ }
11
18
  } else {
12
- await emitFile(program, {
13
- content: sub.contents,
14
- path: joinPaths(emitterOutputDir, sub.path)
15
- });
19
+ // TODO: support copy file
16
20
  }
17
21
  }
18
22
  }
@@ -1 +1 @@
1
- {"version":3,"file":"declaration.d.ts","sourceRoot":"","sources":["../../../../../src/csharp/components/class/declaration.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAO,MAAM,gBAAgB,CAAC;AACpD,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAO3D,MAAM,WAAW,qBAAsB,SAAQ,IAAI,CAAC,EAAE,CAAC,qBAAqB,EAAE,MAAM,CAAC;IACnF,iFAAiF;IACjF,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,wCAAwC;IACxC,IAAI,EAAE,KAAK,GAAG,SAAS,CAAC;IACxB,8FAA8F;IAC9F,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAYD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,GAAG,QAAQ,CAuBvE"}
1
+ {"version":3,"file":"declaration.d.ts","sourceRoot":"","sources":["../../../../../src/csharp/components/class/declaration.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAO,MAAM,gBAAgB,CAAC;AACpD,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEvC,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAO3D,MAAM,WAAW,qBAAsB,SAAQ,IAAI,CAAC,EAAE,CAAC,qBAAqB,EAAE,MAAM,CAAC;IACnF,iFAAiF;IACjF,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,wCAAwC;IACxC,IAAI,EAAE,KAAK,GAAG,SAAS,CAAC;IACxB,8FAA8F;IAC9F,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAYD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,GAAG,QAAQ,CAuBvE"}
@@ -1,6 +1,7 @@
1
1
  import { memo as _$memo, mergeProps as _$mergeProps, createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
2
  import { For } from "@alloy-js/core";
3
3
  import * as cs from "@alloy-js/csharp";
4
+ import { Method } from "@alloy-js/csharp";
4
5
  import { useTsp } from "../../../core/index.js";
5
6
  import { Property } from "../property/property.js";
6
7
  import { TypeExpression } from "../type-expression.js";
@@ -57,7 +58,7 @@ function ClassMethods(props) {
57
58
  const namePolicy = cs.useCSharpNamePolicy();
58
59
  const abstractMethods = [];
59
60
  for (const [name, method] of props.type.operations) {
60
- abstractMethods.push(_$createComponent(cs.ClassMethod, {
61
+ abstractMethods.push(_$createComponent(Method, {
61
62
  get name() {
62
63
  return namePolicy.getName(name, "class-method");
63
64
  },
@@ -1 +1 @@
1
- {"version":3,"file":"interface-declaration.d.ts","sourceRoot":"","sources":["../../../../src/typescript/components/interface-declaration.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,QAAQ,EAAgB,MAAM,gBAAgB,CAAC;AACpE,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC3C,OAAO,EACL,KAAK,SAAS,EAEd,KAAK,KAAK,EAIX,MAAM,oBAAoB,CAAC;AAQ5B,MAAM,WAAW,8BAA+B,SAAQ,IAAI,CAAC,EAAE,CAAC,yBAAyB,EAAE,MAAM,CAAC;IAChG,IAAI,EAAE,KAAK,GAAG,SAAS,CAAC;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,MAAM,yBAAyB,GACjC,8BAA8B,GAC9B,EAAE,CAAC,yBAAyB,CAAC;AAEjC,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,yBAAyB,YAmCpE;AAQD,MAAM,WAAW,wBAAyB,SAAQ,EAAE,CAAC,wBAAwB;IAC3E,IAAI,EAAE,KAAK,GAAG,SAAS,CAAC;CACzB;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,wBAAwB,YAMlE"}
1
+ {"version":3,"file":"interface-declaration.d.ts","sourceRoot":"","sources":["../../../../src/typescript/components/interface-declaration.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAgB,MAAM,gBAAgB,CAAC;AAC7D,OAAO,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC3C,OAAO,EACL,KAAK,SAAS,EAEd,KAAK,KAAK,EAIX,MAAM,oBAAoB,CAAC;AAQ5B,MAAM,WAAW,8BAA+B,SAAQ,IAAI,CAAC,EAAE,CAAC,yBAAyB,EAAE,MAAM,CAAC;IAChG,IAAI,EAAE,KAAK,GAAG,SAAS,CAAC;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,MAAM,yBAAyB,GACjC,8BAA8B,GAC9B,EAAE,CAAC,yBAAyB,CAAC;AAEjC,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,yBAAyB,YAmCpE;AAQD,MAAM,WAAW,wBAAyB,SAAQ,EAAE,CAAC,wBAAwB;IAC3E,IAAI,EAAE,KAAK,GAAG,SAAS,CAAC;CACzB;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,wBAAwB,YAMlE"}
@@ -1,5 +1,5 @@
1
1
  import { createComponent as _$createComponent, mergeProps as _$mergeProps, memo as _$memo } from "@alloy-js/core/jsx-runtime";
2
- import { Block, For, mapJoin } from "@alloy-js/core";
2
+ import { For, mapJoin } from "@alloy-js/core";
3
3
  import * as ts from "@alloy-js/typescript";
4
4
  import { isNeverType } from "@typespec/compiler";
5
5
  import { createRekeyableMap } from "@typespec/compiler/utils";
@@ -50,7 +50,7 @@ function isTypedInterfaceDeclarationProps(props) {
50
50
  return "type" in props;
51
51
  }
52
52
  export function InterfaceExpression(props) {
53
- return _$createComponent(Block, {
53
+ return _$createComponent(ts.InterfaceExpression, {
54
54
  get children() {
55
55
  return _$createComponent(InterfaceBody, props);
56
56
  }
@@ -1,6 +1,6 @@
1
1
  import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
- import { Output } from "#core/index.js";
3
2
  import { Tester } from "#test/test-host.js";
3
+ import { TestFile } from "#test/typescript/utils.js";
4
4
  import { t } from "@typespec/compiler/testing";
5
5
  import { expect, it } from "vitest";
6
6
  import { UnionExpression } from "./expression.js";
@@ -14,7 +14,7 @@ it("renders a union expression", async () => {
14
14
  two: "two"
15
15
  }
16
16
  `);
17
- expect(_$createComponent(Output, {
17
+ expect(_$createComponent(TestFile, {
18
18
  program: program,
19
19
  get children() {
20
20
  return _$createComponent(UnionExpression, {
@@ -33,7 +33,7 @@ it("renders a union expression without conflicting names", async () => {
33
33
  {common: "one", two: "two"}
34
34
  }
35
35
  `);
36
- expect(_$createComponent(Output, {
36
+ expect(_$createComponent(TestFile, {
37
37
  program: program,
38
38
  get children() {
39
39
  return _$createComponent(UnionExpression, {
@@ -1,9 +1,8 @@
1
1
  import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
- import { render } from "@alloy-js/core";
2
+ import { code } from "@alloy-js/core";
3
3
  import { d } from "@alloy-js/core/testing";
4
4
  import { SourceFile } from "@alloy-js/typescript";
5
- import { format } from "prettier";
6
- import { assert, describe, expect, it } from "vitest";
5
+ import { describe, expect, it } from "vitest";
7
6
  import { Output } from "../../../src/core/components/output.js";
8
7
  import { FunctionDeclaration } from "../../../src/typescript/components/function-declaration.js";
9
8
  import { getProgram } from "../test-host.js";
@@ -17,7 +16,7 @@ describe("Typescript Function Declaration", () => {
17
16
  `);
18
17
  const [namespace] = program.resolveTypeReference("DemoService");
19
18
  const operation = Array.from(namespace.operations.values())[0];
20
- const res = render(_$createComponent(Output, {
19
+ expect(_$createComponent(Output, {
21
20
  program: program,
22
21
  get children() {
23
22
  return _$createComponent(SourceFile, {
@@ -29,16 +28,7 @@ describe("Typescript Function Declaration", () => {
29
28
  }
30
29
  });
31
30
  }
32
- }));
33
- const testFile = res.contents.find(file => file.path === "test.ts");
34
- assert(testFile, "test.ts file not rendered");
35
- const actualContent = await format(testFile.contents, {
36
- parser: "typescript"
37
- });
38
- const expectedContent = await format(`function getName(id: string): string{}`, {
39
- parser: "typescript"
40
- });
41
- expect(actualContent).toBe(expectedContent);
31
+ })).toRenderTo(`function getName(id: string): string {}`);
42
32
  });
43
33
  it("creates a function with JSDoc", async () => {
44
34
  const program = await getProgram(`
@@ -52,7 +42,7 @@ describe("Typescript Function Declaration", () => {
52
42
  `);
53
43
  const [namespace] = program.resolveTypeReference("DemoService");
54
44
  const operation = Array.from(namespace.operations.values())[0];
55
- const res = render(_$createComponent(Output, {
45
+ expect(_$createComponent(Output, {
56
46
  program: program,
57
47
  get children() {
58
48
  return _$createComponent(SourceFile, {
@@ -64,23 +54,14 @@ describe("Typescript Function Declaration", () => {
64
54
  }
65
55
  });
66
56
  }
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(`
57
+ })).toRenderTo(`
74
58
  /**
75
59
  * This is a test function
76
- *
60
+ *
77
61
  * @param {string} id - This is the id
78
62
  * @param {string} name
79
63
  */
80
- function getName(id: string, name: string): string{}`, {
81
- parser: "typescript"
82
- });
83
- expect(actualContent).toBe(expectedContent);
64
+ function getName(id: string, name: string): string {}`);
84
65
  });
85
66
  it("creates a function with overridden JSDoc", async () => {
86
67
  const program = await getProgram(`
@@ -91,7 +72,7 @@ describe("Typescript Function Declaration", () => {
91
72
  op getName(id: string): string;`);
92
73
  const [namespace] = program.resolveTypeReference("DemoService");
93
74
  const operation = Array.from(namespace.operations.values())[0];
94
- const res = render(_$createComponent(Output, {
75
+ expect(_$createComponent(Output, {
95
76
  program: program,
96
77
  get children() {
97
78
  return _$createComponent(SourceFile, {
@@ -104,22 +85,13 @@ describe("Typescript Function Declaration", () => {
104
85
  }
105
86
  });
106
87
  }
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(`
88
+ })).toRenderTo(`
114
89
  /**
115
90
  * This is a custom description
116
91
  *
117
92
  * @param {string} id
118
93
  */
119
- function getName(id: string): string{}`, {
120
- parser: "typescript"
121
- });
122
- expect(actualContent).toBe(expectedContent);
94
+ function getName(id: string): string {}`);
123
95
  });
124
96
  it("creates an async function", async () => {
125
97
  const program = await getProgram(`
@@ -128,7 +100,7 @@ describe("Typescript Function Declaration", () => {
128
100
  `);
129
101
  const [namespace] = program.resolveTypeReference("DemoService");
130
102
  const operation = Array.from(namespace.operations.values())[0];
131
- const res = render(_$createComponent(Output, {
103
+ expect(_$createComponent(Output, {
132
104
  program: program,
133
105
  get children() {
134
106
  return _$createComponent(SourceFile, {
@@ -141,19 +113,7 @@ describe("Typescript Function Declaration", () => {
141
113
  }
142
114
  });
143
115
  }
144
- }));
145
- const testFile = res.contents.find(file => file.path === "test.ts");
146
- assert(testFile, "test.ts file not rendered");
147
- const actualContent = await format(testFile.contents, {
148
- parser: "typescript"
149
- });
150
- const expectedContent = await format(d`async function getName(id: string): Promise<string> {
151
-
152
-
153
- }`, {
154
- parser: "typescript"
155
- });
156
- expect(actualContent).toBe(expectedContent);
116
+ })).toRenderTo(d`async function getName(id: string): Promise<string> {}`);
157
117
  });
158
118
  it("exports a function", async () => {
159
119
  const program = await getProgram(`
@@ -162,7 +122,7 @@ describe("Typescript Function Declaration", () => {
162
122
  `);
163
123
  const [namespace] = program.resolveTypeReference("DemoService");
164
124
  const operation = Array.from(namespace.operations.values())[0];
165
- const res = render(_$createComponent(Output, {
125
+ expect(_$createComponent(Output, {
166
126
  program: program,
167
127
  get children() {
168
128
  return _$createComponent(SourceFile, {
@@ -175,16 +135,7 @@ describe("Typescript Function Declaration", () => {
175
135
  }
176
136
  });
177
137
  }
178
- }));
179
- const testFile = res.contents.find(file => file.path === "test.ts");
180
- assert(testFile, "test.ts file not rendered");
181
- const actualContent = await format(testFile.contents, {
182
- parser: "typescript"
183
- });
184
- const expectedContent = await format(`export function getName(id: string): string{}`, {
185
- parser: "typescript"
186
- });
187
- expect(actualContent).toBe(expectedContent);
138
+ })).toRenderTo(`export function getName(id: string): string {}`);
188
139
  });
189
140
  it("can override name", async () => {
190
141
  const program = await getProgram(`
@@ -193,7 +144,7 @@ describe("Typescript Function Declaration", () => {
193
144
  `);
194
145
  const [namespace] = program.resolveTypeReference("DemoService");
195
146
  const operation = Array.from(namespace.operations.values())[0];
196
- const res = render(_$createComponent(Output, {
147
+ expect(_$createComponent(Output, {
197
148
  program: program,
198
149
  get children() {
199
150
  return _$createComponent(SourceFile, {
@@ -206,16 +157,7 @@ describe("Typescript Function Declaration", () => {
206
157
  }
207
158
  });
208
159
  }
209
- }));
210
- const testFile = res.contents.find(file => file.path === "test.ts");
211
- assert(testFile, "test.ts file not rendered");
212
- const actualContent = await format(testFile.contents, {
213
- parser: "typescript"
214
- });
215
- const expectedContent = await format(`function newName(id: string): string{}`, {
216
- parser: "typescript"
217
- });
218
- expect(actualContent).toBe(expectedContent);
160
+ })).toRenderTo(`function newName(id: string): string {}`);
219
161
  });
220
162
  it("can append extra parameters with raw params provided", async () => {
221
163
  const program = await getProgram(`
@@ -224,7 +166,7 @@ describe("Typescript Function Declaration", () => {
224
166
  `);
225
167
  const [namespace] = program.resolveTypeReference("DemoService");
226
168
  const operation = Array.from(namespace.operations.values())[0];
227
- const res = render(_$createComponent(Output, {
169
+ expect(_$createComponent(Output, {
228
170
  program: program,
229
171
  get children() {
230
172
  return _$createComponent(SourceFile, {
@@ -243,16 +185,7 @@ describe("Typescript Function Declaration", () => {
243
185
  }
244
186
  });
245
187
  }
246
- }));
247
- const testFile = res.contents.find(file => file.path === "test.ts");
248
- assert(testFile, "test.ts file not rendered");
249
- const actualContent = await format(testFile.contents, {
250
- parser: "typescript"
251
- });
252
- const expectedContent = await format(`function createPerson(name: string, age: number, id: string): string{}`, {
253
- parser: "typescript"
254
- });
255
- expect(actualContent).toBe(expectedContent);
188
+ })).toRenderTo(`function createPerson(name: string, age: number, id: string): string {}`);
256
189
  });
257
190
  it.skip("can override parameters with an array of ModelProperties", async () => {
258
191
  const program = await getProgram(`
@@ -267,7 +200,7 @@ describe("Typescript Function Declaration", () => {
267
200
  const [namespace] = program.resolveTypeReference("DemoService");
268
201
  const operation = Array.from(namespace.operations.values())[0];
269
202
  const model = Array.from(namespace.models.values())[0];
270
- const res = render(_$createComponent(Output, {
203
+ expect(_$createComponent(Output, {
271
204
  program: program,
272
205
  get children() {
273
206
  return _$createComponent(SourceFile, {
@@ -284,16 +217,7 @@ describe("Typescript Function Declaration", () => {
284
217
  }
285
218
  });
286
219
  }
287
- }));
288
- const testFile = res.contents.find(file => file.path === "test.ts");
289
- assert(testFile, "test.ts file not rendered");
290
- const actualContent = await format(testFile.contents, {
291
- parser: "typescript"
292
- });
293
- const expectedContent = await format(`function createPerson(name: string, age: number): string{}`, {
294
- parser: "typescript"
295
- });
296
- expect(actualContent).toBe(expectedContent);
220
+ })).toRenderTo(`function createPerson(name: string, age: number): string {}`);
297
221
  });
298
222
  it("can render function body", async () => {
299
223
  const program = await getProgram(`
@@ -302,7 +226,7 @@ describe("Typescript Function Declaration", () => {
302
226
  `);
303
227
  const [namespace] = program.resolveTypeReference("DemoService");
304
228
  const operation = Array.from(namespace.operations.values())[0];
305
- const res = render(_$createComponent(Output, {
229
+ expect(_$createComponent(Output, {
306
230
  program: program,
307
231
  get children() {
308
232
  return _$createComponent(SourceFile, {
@@ -311,24 +235,19 @@ describe("Typescript Function Declaration", () => {
311
235
  return _$createComponent(FunctionDeclaration, {
312
236
  "export": true,
313
237
  type: operation,
314
- children: "const message = \"Hello World!\"; console.log(message);"
238
+ children: code`
239
+ const message = "Hello World!";
240
+ console.log(message);
241
+ `
315
242
  });
316
243
  }
317
244
  });
318
245
  }
319
- }));
320
- const testFile = res.contents.find(file => file.path === "test.ts");
321
- assert(testFile, "test.ts file not rendered");
322
- const actualContent = await format(testFile.contents, {
323
- parser: "typescript"
324
- });
325
- const expectedContent = await format(`export function createPerson(id: string): string {
326
- const message = "Hello World!";
327
- console.log(message);
328
- }`, {
329
- parser: "typescript"
330
- });
331
- expect(actualContent).toBe(expectedContent);
246
+ })).toRenderTo(`
247
+ export function createPerson(id: string): string {
248
+ const message = "Hello World!";
249
+ console.log(message);
250
+ }`);
332
251
  });
333
252
  });
334
253
  });