@typespec/emitter-framework 0.6.0-dev.2 → 0.6.0-dev.5

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 (67) hide show
  1. package/dist/src/typescript/components/arrow-function.d.ts +17 -0
  2. package/dist/src/typescript/components/arrow-function.d.ts.map +1 -0
  3. package/dist/src/typescript/components/arrow-function.js +31 -0
  4. package/dist/src/typescript/components/arrow-function.js.map +1 -0
  5. package/dist/src/typescript/components/function-declaration.d.ts +5 -0
  6. package/dist/src/typescript/components/function-declaration.d.ts.map +1 -1
  7. package/dist/src/typescript/components/function-declaration.js +5 -1
  8. package/dist/src/typescript/components/function-declaration.js.map +1 -1
  9. package/dist/src/typescript/components/function-expression.d.ts +17 -0
  10. package/dist/src/typescript/components/function-expression.d.ts.map +1 -0
  11. package/dist/src/typescript/components/function-expression.js +31 -0
  12. package/dist/src/typescript/components/function-expression.js.map +1 -0
  13. package/dist/src/typescript/components/function-type.d.ts +17 -0
  14. package/dist/src/typescript/components/function-type.d.ts.map +1 -0
  15. package/dist/src/typescript/components/function-type.js +31 -0
  16. package/dist/src/typescript/components/function-type.js.map +1 -0
  17. package/dist/src/typescript/components/index.d.ts +6 -0
  18. package/dist/src/typescript/components/index.d.ts.map +1 -1
  19. package/dist/src/typescript/components/index.js +6 -0
  20. package/dist/src/typescript/components/index.js.map +1 -1
  21. package/dist/src/typescript/components/interface-member.d.ts +1 -2
  22. package/dist/src/typescript/components/interface-member.d.ts.map +1 -1
  23. package/dist/src/typescript/components/interface-member.js +3 -19
  24. package/dist/src/typescript/components/interface-member.js.map +1 -1
  25. package/dist/src/typescript/components/interface-method.d.ts +15 -0
  26. package/dist/src/typescript/components/interface-method.d.ts.map +1 -0
  27. package/dist/src/typescript/components/interface-method.js +34 -0
  28. package/dist/src/typescript/components/interface-method.js.map +1 -0
  29. package/dist/src/typescript/components/type-alias-declaration.d.ts +6 -2
  30. package/dist/src/typescript/components/type-alias-declaration.d.ts.map +1 -1
  31. package/dist/src/typescript/components/type-alias-declaration.js +7 -2
  32. package/dist/src/typescript/components/type-alias-declaration.js.map +1 -1
  33. package/dist/src/typescript/components/type-declaration.d.ts.map +1 -1
  34. package/dist/src/typescript/components/type-declaration.js +4 -0
  35. package/dist/src/typescript/components/type-declaration.js.map +1 -1
  36. package/dist/src/typescript/components/type-expression.d.ts +6 -0
  37. package/dist/src/typescript/components/type-expression.d.ts.map +1 -1
  38. package/dist/src/typescript/components/type-expression.js +6 -1
  39. package/dist/src/typescript/components/type-expression.js.map +1 -1
  40. package/dist/test/typescript/components/arrow-function.test.d.ts +2 -0
  41. package/dist/test/typescript/components/arrow-function.test.d.ts.map +1 -0
  42. package/dist/test/typescript/components/function-expression.test.d.ts +2 -0
  43. package/dist/test/typescript/components/function-expression.test.d.ts.map +1 -0
  44. package/dist/test/typescript/components/function-type.test.d.ts +2 -0
  45. package/dist/test/typescript/components/function-type.test.d.ts.map +1 -0
  46. package/dist/test/typescript/components/interface-method.test.d.ts +2 -0
  47. package/dist/test/typescript/components/interface-method.test.d.ts.map +1 -0
  48. package/dist/test/utils.d.ts +3 -2
  49. package/dist/test/utils.d.ts.map +1 -1
  50. package/package.json +5 -5
  51. package/src/typescript/components/arrow-function.tsx +39 -0
  52. package/src/typescript/components/function-declaration.tsx +5 -2
  53. package/src/typescript/components/function-expression.tsx +41 -0
  54. package/src/typescript/components/function-type.tsx +38 -0
  55. package/src/typescript/components/index.ts +6 -0
  56. package/src/typescript/components/interface-member.tsx +2 -19
  57. package/src/typescript/components/interface-method.tsx +48 -0
  58. package/src/typescript/components/type-alias-declaration.tsx +10 -4
  59. package/src/typescript/components/type-declaration.tsx +2 -0
  60. package/src/typescript/components/type-expression.tsx +11 -2
  61. package/test/typescript/components/arrow-function.test.tsx +85 -0
  62. package/test/typescript/components/function-expression.test.tsx +85 -0
  63. package/test/typescript/components/function-type.test.tsx +80 -0
  64. package/test/typescript/components/interface-declaration.test.tsx +6 -6
  65. package/test/typescript/components/interface-method.test.tsx +234 -0
  66. package/test/typescript/components/type-alias-declaration.test.tsx +20 -2
  67. package/test/utils.ts +8 -1
@@ -564,7 +564,7 @@ describe("Typescript Interface", () => {
564
564
  const actualContent = await format(testFile.contents as string, { parser: "typescript" });
565
565
  const expectedContent = await format(
566
566
  `export interface WidgetOperations {
567
- getName: (id: string) => string;
567
+ getName(id: string): string;
568
568
  }`,
569
569
  {
570
570
  parser: "typescript",
@@ -605,8 +605,8 @@ describe("Typescript Interface", () => {
605
605
  const actualContent = await format(testFile.contents as string, { parser: "typescript" });
606
606
  const expectedContent = await format(
607
607
  `export interface WidgetOperations {
608
- getName: (foo: Foo) => string;
609
- getOtherName: (name: string) => string
608
+ getName(foo: Foo): string;
609
+ getOtherName(name: string): string
610
610
  }
611
611
  export interface Foo {
612
612
  name: string;
@@ -654,7 +654,7 @@ describe("Typescript Interface", () => {
654
654
  const actualContent = await format(testFile.contents as string, { parser: "typescript" });
655
655
  const expectedContent = await format(
656
656
  `export interface WidgetOperations {
657
- getName: (id: string) => Widget;
657
+ getName(id: string): Widget;
658
658
  }
659
659
  export interface Widget {
660
660
  id: string;
@@ -707,8 +707,8 @@ describe("Typescript Interface", () => {
707
707
  const actualContent = await format(testFile.contents as string, { parser: "typescript" });
708
708
  const expectedContent = await format(
709
709
  `export interface WidgetOperationsExtended {
710
- getName: (id: string) => Widget;
711
- delete: (id: string) => void;
710
+ getName(id: string): Widget;
711
+ delete(id: string): void;
712
712
  }
713
713
  export interface Widget {
714
714
  id: string;
@@ -0,0 +1,234 @@
1
+ import { Output, render } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { InterfaceDeclaration, SourceFile } from "@alloy-js/typescript";
4
+ import { Operation } from "@typespec/compiler";
5
+ import { BasicTestRunner } from "@typespec/compiler/testing";
6
+ import { beforeEach, describe, it } from "vitest";
7
+ import { InterfaceMethod } from "../../../src/typescript/components/interface-method.jsx";
8
+ import { assertFileContents } from "../../utils.js";
9
+ import { createEmitterFrameworkTestRunner } from "../test-host.js";
10
+
11
+ describe("interface methods with a `type` prop", () => {
12
+ let runner: BasicTestRunner;
13
+
14
+ beforeEach(async () => {
15
+ runner = await createEmitterFrameworkTestRunner();
16
+ });
17
+
18
+ it("creates a interface method", async () => {
19
+ const { getName } = (await runner.compile(`
20
+ @test op getName(id: string): string;
21
+ `)) as { getName: Operation };
22
+
23
+ const res = render(
24
+ <Output>
25
+ <SourceFile path="test.ts">
26
+ <InterfaceDeclaration name="basicInterface">
27
+ <InterfaceMethod type={getName} name={getName.name} />
28
+ </InterfaceDeclaration>
29
+ </SourceFile>
30
+ </Output>,
31
+ );
32
+
33
+ assertFileContents(
34
+ res,
35
+ d`
36
+ interface basicInterface {
37
+ getName(id: string): string
38
+ }
39
+ `,
40
+ );
41
+ });
42
+
43
+ it("creates an async interface function", async () => {
44
+ const { getName } = (await runner.compile(`
45
+ @test op getName(id: string): string;
46
+ `)) as { getName: Operation };
47
+
48
+ const res = render(
49
+ <Output>
50
+ <SourceFile path="test.ts">
51
+ <InterfaceDeclaration name="basicInterface">
52
+ <InterfaceMethod async type={getName} />
53
+ </InterfaceDeclaration>
54
+ </SourceFile>
55
+ </Output>,
56
+ );
57
+
58
+ assertFileContents(
59
+ res,
60
+ d`
61
+ interface basicInterface {
62
+ getName(id: string): Promise<string>
63
+ }
64
+ `,
65
+ );
66
+ });
67
+
68
+ it("can append extra parameters with raw params provided", async () => {
69
+ const { getName } = (await runner.compile(`
70
+ @test op getName(id: string): string;
71
+ `)) as { getName: Operation };
72
+
73
+ const res = render(
74
+ <Output>
75
+ <SourceFile path="test.ts">
76
+ <InterfaceDeclaration name="basicInterface">
77
+ <InterfaceMethod
78
+ type={getName}
79
+ parametersMode="append"
80
+ parameters={[{ name: "foo", type: "string" }]}
81
+ />
82
+ </InterfaceDeclaration>
83
+ </SourceFile>
84
+ </Output>,
85
+ );
86
+
87
+ assertFileContents(
88
+ res,
89
+ d`
90
+ interface basicInterface {
91
+ getName(id: string, foo: string): string
92
+ }
93
+ `,
94
+ );
95
+ });
96
+
97
+ it("can prepend extra parameters with raw params provided", async () => {
98
+ const { getName } = (await runner.compile(`
99
+ @test op getName(id: string): string;
100
+ `)) as { getName: Operation };
101
+
102
+ const res = render(
103
+ <Output>
104
+ <SourceFile path="test.ts">
105
+ <InterfaceDeclaration name="basicInterface">
106
+ <InterfaceMethod
107
+ type={getName}
108
+ parametersMode="prepend"
109
+ parameters={[{ name: "foo", type: "string" }]}
110
+ />
111
+ </InterfaceDeclaration>
112
+ </SourceFile>
113
+ </Output>,
114
+ );
115
+
116
+ assertFileContents(
117
+ res,
118
+ d`
119
+ interface basicInterface {
120
+ getName(foo: string, id: string): string
121
+ }
122
+ `,
123
+ );
124
+ });
125
+
126
+ it("can replace parameters with raw params provided", async () => {
127
+ const { getName } = (await runner.compile(`
128
+ @test op getName(id: string): string;
129
+ `)) as { getName: Operation };
130
+
131
+ const res = render(
132
+ <Output>
133
+ <SourceFile path="test.ts">
134
+ <InterfaceDeclaration name="basicInterface">
135
+ <InterfaceMethod
136
+ type={getName}
137
+ parametersMode="replace"
138
+ parameters={[
139
+ { name: "foo", type: "string" },
140
+ { name: "bar", type: "number" },
141
+ ]}
142
+ />
143
+ </InterfaceDeclaration>
144
+ </SourceFile>
145
+ </Output>,
146
+ );
147
+
148
+ assertFileContents(
149
+ res,
150
+ d`
151
+ interface basicInterface {
152
+ getName(foo: string, bar: number): string
153
+ }
154
+ `,
155
+ );
156
+ });
157
+
158
+ it("can override return type", async () => {
159
+ const { getName } = (await runner.compile(`
160
+ @test op getName(id: string): string;
161
+ `)) as { getName: Operation };
162
+
163
+ const res = render(
164
+ <Output>
165
+ <SourceFile path="test.ts">
166
+ <InterfaceDeclaration name="basicInterface">
167
+ <InterfaceMethod type={getName} returnType="Promise<Record<string, unknown>>" />
168
+ </InterfaceDeclaration>
169
+ </SourceFile>
170
+ </Output>,
171
+ );
172
+
173
+ assertFileContents(
174
+ res,
175
+ d`
176
+ interface basicInterface {
177
+ getName(id: string): Promise<Record<string, unknown>>
178
+ }
179
+ `,
180
+ );
181
+ });
182
+
183
+ it("can override method name", async () => {
184
+ const { getName } = (await runner.compile(`
185
+ @test op getName(id: string): string;
186
+ `)) as { getName: Operation };
187
+
188
+ const res = render(
189
+ <Output>
190
+ <SourceFile path="test.ts">
191
+ <InterfaceDeclaration name="basicInterface">
192
+ <InterfaceMethod type={getName} name="getNameCustom" />
193
+ </InterfaceDeclaration>
194
+ </SourceFile>
195
+ </Output>,
196
+ );
197
+
198
+ assertFileContents(
199
+ res,
200
+ d`
201
+ interface basicInterface {
202
+ getNameCustom(id: string): string
203
+ }
204
+ `,
205
+ );
206
+ });
207
+ });
208
+
209
+ describe("interface methods without a `type` prop", () => {
210
+ it("renders a plain interface method", async () => {
211
+ const res = render(
212
+ <Output>
213
+ <SourceFile path="test.ts">
214
+ <InterfaceDeclaration name="basicInterface">
215
+ <InterfaceMethod
216
+ name="plainMethod"
217
+ parameters={[{ name: "param1", type: "string" }]}
218
+ returnType="number"
219
+ />
220
+ </InterfaceDeclaration>
221
+ </SourceFile>
222
+ </Output>,
223
+ );
224
+
225
+ assertFileContents(
226
+ res,
227
+ d`
228
+ interface basicInterface {
229
+ plainMethod(param1: string): number
230
+ }
231
+ `,
232
+ );
233
+ });
234
+ });
@@ -1,10 +1,11 @@
1
1
  import { Output, render } from "@alloy-js/core";
2
2
  import { SourceFile } from "@alloy-js/typescript";
3
- import { Namespace } from "@typespec/compiler";
3
+ import { Namespace, Operation } from "@typespec/compiler";
4
4
  import { format } from "prettier";
5
5
  import { assert, describe, expect, it } from "vitest";
6
6
  import { TypeAliasDeclaration } from "../../../src/typescript/components/type-alias-declaration.jsx";
7
- import { getProgram } from "../test-host.js";
7
+ import { assertFileContents } from "../../utils.js";
8
+ import { createEmitterFrameworkTestRunner, getProgram } from "../test-host.js";
8
9
 
9
10
  describe("Typescript Type Alias Declaration", () => {
10
11
  describe("Type Alias bound to Typespec Scalar", () => {
@@ -117,4 +118,21 @@ describe("Typescript Type Alias Declaration", () => {
117
118
  });
118
119
  });
119
120
  });
121
+
122
+ it("creates a type alias of a function", async () => {
123
+ const runner = await createEmitterFrameworkTestRunner();
124
+ const { getName } = (await runner.compile(`
125
+ @test op getName(id: string): string;
126
+ `)) as { getName: Operation };
127
+
128
+ const res = render(
129
+ <Output>
130
+ <SourceFile path="test.ts">
131
+ <TypeAliasDeclaration type={getName} />
132
+ </SourceFile>
133
+ </Output>,
134
+ );
135
+
136
+ assertFileContents(res, "type getName = (id: string) => string;");
137
+ });
120
138
  });
package/test/utils.ts CHANGED
@@ -1,7 +1,8 @@
1
- import { Children, render } from "@alloy-js/core";
1
+ import { Children, OutputDirectory, render } from "@alloy-js/core";
2
2
  import { Output } from "@alloy-js/core/stc";
3
3
  import { SourceFile } from "@alloy-js/typescript/stc";
4
4
  import { Program } from "@typespec/compiler";
5
+ import { assert } from "vitest";
5
6
  import { getProgram } from "./typescript/test-host.js";
6
7
 
7
8
  export async function getEmitOutput(tspCode: string, cb: (program: Program) => Children) {
@@ -12,3 +13,9 @@ export async function getEmitOutput(tspCode: string, cb: (program: Program) => C
12
13
 
13
14
  return testFile.contents;
14
15
  }
16
+
17
+ export function assertFileContents(res: OutputDirectory, contents: string) {
18
+ const testFile = res.contents.find((file) => file.path === "test.ts")!;
19
+ assert(testFile, "test.ts file not rendered");
20
+ assert.equal(testFile.contents, contents);
21
+ }