@alloy-js/typescript 0.21.0-dev.0 → 0.21.0-dev.11

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 (122) hide show
  1. package/dist/src/components/ClassDeclaration.d.ts +5 -5
  2. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  3. package/dist/src/components/ClassDeclaration.js +7 -12
  4. package/dist/src/components/ClassDeclaration.js.map +1 -1
  5. package/dist/src/components/Declaration.d.ts +27 -24
  6. package/dist/src/components/Declaration.d.ts.map +1 -1
  7. package/dist/src/components/Declaration.js +12 -8
  8. package/dist/src/components/Declaration.js.map +1 -1
  9. package/dist/src/components/EnumDeclaration.d.ts +2 -2
  10. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  11. package/dist/src/components/EnumDeclaration.js +3 -3
  12. package/dist/src/components/EnumDeclaration.js.map +1 -1
  13. package/dist/src/components/EnumMember.d.ts +2 -2
  14. package/dist/src/components/EnumMember.d.ts.map +1 -1
  15. package/dist/src/components/EnumMember.js +8 -10
  16. package/dist/src/components/EnumMember.js.map +1 -1
  17. package/dist/src/components/FunctionBase.d.ts.map +1 -1
  18. package/dist/src/components/FunctionBase.js +3 -2
  19. package/dist/src/components/FunctionBase.js.map +1 -1
  20. package/dist/src/components/FunctionDeclaration.d.ts +2 -2
  21. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  22. package/dist/src/components/FunctionDeclaration.js +0 -3
  23. package/dist/src/components/FunctionDeclaration.js.map +1 -1
  24. package/dist/src/components/Interface.d.ts +13 -8
  25. package/dist/src/components/Interface.d.ts.map +1 -1
  26. package/dist/src/components/Interface.js +46 -43
  27. package/dist/src/components/Interface.js.map +1 -1
  28. package/dist/src/components/InterfaceMethod.d.ts +2 -2
  29. package/dist/src/components/InterfaceMethod.d.ts.map +1 -1
  30. package/dist/src/components/InterfaceMethod.js +3 -8
  31. package/dist/src/components/InterfaceMethod.js.map +1 -1
  32. package/dist/src/components/MemberDeclaration.d.ts +10 -13
  33. package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
  34. package/dist/src/components/MemberDeclaration.js +4 -5
  35. package/dist/src/components/MemberDeclaration.js.map +1 -1
  36. package/dist/src/components/ObjectExpression.d.ts +2 -2
  37. package/dist/src/components/ObjectExpression.d.ts.map +1 -1
  38. package/dist/src/components/ObjectExpression.js +25 -18
  39. package/dist/src/components/ObjectExpression.js.map +1 -1
  40. package/dist/src/components/PropertyName.d.ts.map +1 -1
  41. package/dist/src/components/PropertyName.js +7 -5
  42. package/dist/src/components/PropertyName.js.map +1 -1
  43. package/dist/src/components/TypeDeclaration.d.ts +2 -2
  44. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  45. package/dist/src/components/TypeDeclaration.js.map +1 -1
  46. package/dist/src/components/VarDeclaration.d.ts +2 -2
  47. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  48. package/dist/src/components/VarDeclaration.js +16 -4
  49. package/dist/src/components/VarDeclaration.js.map +1 -1
  50. package/dist/src/parameter-descriptor.d.ts +3 -3
  51. package/dist/src/parameter-descriptor.d.ts.map +1 -1
  52. package/dist/src/symbols/index.d.ts +9 -9
  53. package/dist/src/symbols/index.d.ts.map +1 -1
  54. package/dist/src/symbols/index.js.map +1 -1
  55. package/dist/src/symbols/reference.d.ts.map +1 -1
  56. package/dist/src/symbols/reference.js +2 -2
  57. package/dist/src/symbols/reference.js.map +1 -1
  58. package/dist/src/symbols/ts-output-symbol.d.ts +2 -2
  59. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  60. package/dist/src/symbols/ts-output-symbol.js.map +1 -1
  61. package/dist/test/class.test.js +29 -1
  62. package/dist/test/class.test.js.map +1 -1
  63. package/dist/test/enum.test.js +30 -1
  64. package/dist/test/enum.test.js.map +1 -1
  65. package/dist/test/function-declaration.test.js +18 -1
  66. package/dist/test/function-declaration.test.js.map +1 -1
  67. package/dist/test/imports.test.js +3 -2
  68. package/dist/test/imports.test.js.map +1 -1
  69. package/dist/test/interface.test.d.ts.map +1 -1
  70. package/dist/test/interface.test.js +25 -1
  71. package/dist/test/interface.test.js.map +1 -1
  72. package/dist/test/object.test.js +55 -2
  73. package/dist/test/object.test.js.map +1 -1
  74. package/dist/test/reference.test.js +14 -0
  75. package/dist/test/reference.test.js.map +1 -1
  76. package/dist/test/var-declaration.test.d.ts.map +1 -1
  77. package/dist/test/var-declaration.test.js +33 -1
  78. package/dist/test/var-declaration.test.js.map +1 -1
  79. package/dist/testing/create-wrapper.d.ts +3 -0
  80. package/dist/testing/create-wrapper.d.ts.map +1 -0
  81. package/dist/testing/create-wrapper.js +12 -0
  82. package/dist/testing/create-wrapper.js.map +1 -0
  83. package/dist/testing/create-wrapper.test.d.ts +2 -0
  84. package/dist/testing/create-wrapper.test.d.ts.map +1 -0
  85. package/dist/testing/create-wrapper.test.js +159 -0
  86. package/dist/testing/create-wrapper.test.js.map +1 -0
  87. package/dist/testing/index.d.ts +2 -0
  88. package/dist/testing/index.d.ts.map +1 -0
  89. package/dist/testing/index.js +2 -0
  90. package/dist/testing/index.js.map +1 -0
  91. package/dist/tsconfig.tsbuildinfo +1 -1
  92. package/package.json +10 -2
  93. package/src/components/ClassDeclaration.tsx +10 -13
  94. package/src/components/Declaration.tsx +39 -32
  95. package/src/components/EnumDeclaration.tsx +4 -4
  96. package/src/components/EnumMember.tsx +8 -13
  97. package/src/components/FunctionBase.tsx +7 -9
  98. package/src/components/FunctionDeclaration.tsx +3 -3
  99. package/src/components/Interface.tsx +57 -51
  100. package/src/components/InterfaceMethod.tsx +12 -7
  101. package/src/components/MemberDeclaration.tsx +20 -24
  102. package/src/components/ObjectExpression.tsx +20 -16
  103. package/src/components/PropertyName.tsx +7 -5
  104. package/src/components/TypeDeclaration.tsx +2 -2
  105. package/src/components/VarDeclaration.tsx +11 -6
  106. package/src/parameter-descriptor.ts +3 -3
  107. package/src/symbols/index.ts +12 -9
  108. package/src/symbols/reference.tsx +3 -1
  109. package/src/symbols/ts-output-symbol.ts +2 -1
  110. package/temp/api.json +745 -492
  111. package/test/class.test.tsx +19 -1
  112. package/test/enum.test.tsx +21 -1
  113. package/test/function-declaration.test.tsx +15 -0
  114. package/test/imports.test.tsx +2 -1
  115. package/test/interface.test.tsx +29 -0
  116. package/test/object.test.tsx +40 -1
  117. package/test/reference.test.tsx +12 -2
  118. package/test/var-declaration.test.tsx +27 -1
  119. package/testing/create-wrapper.test.tsx +107 -0
  120. package/testing/create-wrapper.tsx +12 -0
  121. package/testing/index.ts +1 -0
  122. package/tsconfig.json +8 -1
@@ -1,4 +1,4 @@
1
- import { List, Output, refkey, StatementList } from "@alloy-js/core";
1
+ import { List, namekey, Output, refkey, StatementList } from "@alloy-js/core";
2
2
  import "@alloy-js/core/testing";
3
3
  import { d } from "@alloy-js/core/testing";
4
4
  import { describe, expect, it } from "vitest";
@@ -27,6 +27,24 @@ it("creates extends", () => {
27
27
  `);
28
28
  });
29
29
 
30
+ it("takes namekeys for all elements", () => {
31
+ const res = toSourceText(
32
+ <ts.ClassDeclaration name={namekey("foo")}>
33
+ <StatementList>
34
+ <ts.ClassField name={namekey("myField")} />
35
+ <ts.ClassMethod name={namekey("myMethod")} />
36
+ </StatementList>
37
+ </ts.ClassDeclaration>,
38
+ );
39
+
40
+ expect(res).toRenderTo(`
41
+ class foo {
42
+ myField;
43
+ myMethod() {};
44
+ }
45
+ `);
46
+ });
47
+
30
48
  describe("instance members", () => {
31
49
  it("can be created", () => {
32
50
  const res = toSourceText(
@@ -1,4 +1,4 @@
1
- import { Output, refkey } from "@alloy-js/core";
1
+ import { namekey, Output, refkey } from "@alloy-js/core";
2
2
  import "@alloy-js/core/testing";
3
3
  import { describe, expect, it } from "vitest";
4
4
 
@@ -149,6 +149,26 @@ it("uses the naming policy", () => {
149
149
  `);
150
150
  });
151
151
 
152
+ it("takes a namekey for all its elements", () => {
153
+ const enumKey = namekey("my-enum");
154
+ const memberKey = namekey("member-one");
155
+ expect(
156
+ <Output>
157
+ <ts.SourceFile path="foo.ts">
158
+ <ts.EnumDeclaration name={enumKey}>
159
+ <ts.CommaList>
160
+ <ts.EnumMember name={memberKey} />
161
+ </ts.CommaList>
162
+ </ts.EnumDeclaration>
163
+ </ts.SourceFile>
164
+ </Output>,
165
+ ).toRenderTo(`
166
+ enum my-enum {
167
+ "member-one",
168
+ }
169
+ `);
170
+ });
171
+
152
172
  describe("symbols", () => {
153
173
  it("can reference enum members by refkey", () => {
154
174
  const memberOne = refkey();
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  List,
3
3
  memberRefkey,
4
+ namekey,
4
5
  Props,
5
6
  refkey,
6
7
  StatementList,
@@ -96,7 +97,21 @@ it("supports type parameters by descriptor object", () => {
96
97
  function foo<a extends any, b extends any>() {}
97
98
  `);
98
99
  });
100
+ it("supports type parameters with namekeys", () => {
101
+ const decl = (
102
+ <FunctionDeclaration
103
+ name="foo"
104
+ typeParameters={[
105
+ { name: namekey("a"), extends: "any" },
106
+ { name: namekey("b"), extends: "any" },
107
+ ]}
108
+ ></FunctionDeclaration>
109
+ );
99
110
 
111
+ expect(toSourceText(decl)).toBe(d`
112
+ function foo<a extends any, b extends any>() {}
113
+ `);
114
+ });
100
115
  it("supports type parameters by descriptor array", () => {
101
116
  const decl = (
102
117
  <FunctionDeclaration
@@ -410,10 +410,11 @@ it("handles conflicts with local declarations", () => {
410
410
  <ts.FunctionDeclaration export name="test" />
411
411
  </ts.SourceFile>
412
412
  </Output>,
413
+ // cspell:ignore stest2
413
414
  ).toRenderTo({
414
415
  "src/test1.ts": `
415
416
  export function test() {}
416
- const v = <Unresolved Symbol>;
417
+ const v = <Unresolved Symbol: refkey[stest2]>;
417
418
  `,
418
419
  "test2.ts": `
419
420
  import { test as test_1 } from "./src/test1.js";
@@ -1,4 +1,5 @@
1
1
  import {
2
+ namekey,
2
3
  NamePolicyContext,
3
4
  Props,
4
5
  refkey,
@@ -299,6 +300,34 @@ it("accepts type parameters children", () => {
299
300
  `);
300
301
  });
301
302
 
303
+ it("takes namekeys for all its elements", () => {
304
+ const ifaceKey = namekey("Foo");
305
+ const TKey = namekey("T");
306
+ const memberKey = namekey("member");
307
+
308
+ const res = toSourceText(
309
+ <>
310
+ <ts.InterfaceDeclaration
311
+ name={ifaceKey}
312
+ typeParameters={[{ name: TKey }]}
313
+ >
314
+ <ts.InterfaceMember name={memberKey} type={TKey} />;
315
+ </ts.InterfaceDeclaration>
316
+ <hbr />
317
+ {ifaceKey};<hbr />
318
+ {memberKey};
319
+ </>,
320
+ );
321
+
322
+ expect(res).toRenderTo(`
323
+ interface Foo<T> {
324
+ member: T;
325
+ }
326
+ Foo;
327
+ Foo["member"];
328
+ `);
329
+ });
330
+
302
331
  describe("method members", () => {
303
332
  it("render basic", () => {
304
333
  expect(
@@ -1,7 +1,8 @@
1
- import { List } from "@alloy-js/core";
1
+ import { List, namekey } from "@alloy-js/core";
2
2
  import { d } from "@alloy-js/core/testing";
3
3
  import { expect, it } from "vitest";
4
4
  import {
5
+ CommaList,
5
6
  ObjectExpression,
6
7
  ObjectProperty,
7
8
  ObjectSpreadProperty,
@@ -46,3 +47,41 @@ it("spread property", () => {
46
47
  }
47
48
  `);
48
49
  });
50
+
51
+ it("Works with both children and jsvalue", () => {
52
+ const comp = (
53
+ <ObjectExpression jsValue={{ a: 1, b: 2 }}>
54
+ <ObjectProperty name="c" value={3} />
55
+ </ObjectExpression>
56
+ );
57
+ expect(toSourceText(comp)).toBe(d`
58
+ {
59
+ a: 1,
60
+ b: 2,
61
+ c: 3
62
+ }
63
+ `);
64
+ });
65
+
66
+ it("Handles name conflicts and namekeys", () => {
67
+ const nk1 = namekey("a");
68
+ const nk2 = namekey("a");
69
+ const nk3 = namekey("a", { ignoreNameConflict: true });
70
+
71
+ const comp = (
72
+ <ObjectExpression>
73
+ <CommaList>
74
+ <ObjectProperty name={nk1} value={3} />
75
+ <ObjectProperty name={nk2} value={1} />
76
+ <ObjectProperty name={nk3} value={1} />
77
+ </CommaList>
78
+ </ObjectExpression>
79
+ );
80
+ expect(toSourceText(comp)).toBe(d`
81
+ {
82
+ a: 3,
83
+ a_2: 1,
84
+ a: 1,
85
+ }
86
+ `);
87
+ });
@@ -4,11 +4,21 @@ import { expect, it } from "vitest";
4
4
  import * as ts from "../src/components/index.js";
5
5
  import { Reference } from "../src/components/Reference.js";
6
6
 
7
+ it("using refkey that is missing declration show info about it", () => {
8
+ const key = refkey("foo");
9
+ expect(
10
+ <Output>
11
+ <ts.SourceFile path="test1.ts">{key}</ts.SourceFile>
12
+ </Output>,
13
+ // cspell:ignore sfoo
14
+ ).toRenderTo(`<Unresolved Symbol: refkey[sfoo]>`);
15
+ });
16
+
7
17
  it("works with back references", () => {
8
18
  expect(
9
19
  <Output>
10
20
  <ts.SourceFile path="test1.ts">
11
- <ts.Declaration name="foo" refkey={refkey("foo")}>
21
+ <ts.Declaration name="foo" nameKind="variable" refkey={refkey("foo")}>
12
22
  const foo = 1;
13
23
  </ts.Declaration>
14
24
  </ts.SourceFile>
@@ -36,7 +46,7 @@ it("works with forward references", () => {
36
46
  const v = <Reference refkey={refkey("foo")} />;
37
47
  </ts.SourceFile>
38
48
  <ts.SourceFile path="test1.ts">
39
- <ts.Declaration name="foo" refkey={refkey("foo")}>
49
+ <ts.Declaration nameKind="variable" name="foo" refkey={refkey("foo")}>
40
50
  const foo = 1;
41
51
  </ts.Declaration>
42
52
  </ts.SourceFile>
@@ -1,4 +1,10 @@
1
- import { memberRefkey, Output, refkey, StatementList } from "@alloy-js/core";
1
+ import {
2
+ memberRefkey,
3
+ namekey,
4
+ Output,
5
+ refkey,
6
+ StatementList,
7
+ } from "@alloy-js/core";
2
8
  import "@alloy-js/core/testing";
3
9
  import { expect, it } from "vitest";
4
10
  import * as ts from "../src/index.js";
@@ -13,6 +19,26 @@ it("works", () => {
13
19
  ).toRenderTo("const hi = 12;");
14
20
  });
15
21
 
22
+ it("allows omitting the initializer", () => {
23
+ expect(
24
+ <Output>
25
+ <ts.SourceFile path="test.js">
26
+ <ts.VarDeclaration let name="hi" />;
27
+ </ts.SourceFile>
28
+ </Output>,
29
+ ).toRenderTo("let hi;");
30
+ });
31
+
32
+ it("takes namekeys", () => {
33
+ expect(
34
+ <Output>
35
+ <ts.SourceFile path="test.js">
36
+ <ts.VarDeclaration name={namekey("foo")} initializer="12" />;
37
+ </ts.SourceFile>
38
+ </Output>,
39
+ ).toRenderTo("const foo = 12;");
40
+ });
41
+
16
42
  it("works end-to-end", () => {
17
43
  const TestType = refkey("TestType");
18
44
 
@@ -0,0 +1,107 @@
1
+ import {
2
+ ClassDeclaration,
3
+ ClassField,
4
+ ClassMethod,
5
+ } from "#components/index.js";
6
+ import { List, Refkey } from "@alloy-js/core";
7
+ import { d } from "@alloy-js/core/testing";
8
+ import { expect, it } from "vitest";
9
+ import { createTSTestWrapper } from "./create-wrapper.jsx";
10
+
11
+ it("should render defkey inline", async () => {
12
+ const { Wrapper, defkey } = createTSTestWrapper();
13
+
14
+ expect(<Wrapper>return {defkey("myResult")};</Wrapper>).toRenderTo(
15
+ `return myResult;`,
16
+ );
17
+ });
18
+
19
+ it("reuses declarations across multiple usage sites", () => {
20
+ const { Wrapper, defkey } = createTSTestWrapper();
21
+ const T = defkey("Thing");
22
+ const R = defkey("Result");
23
+
24
+ expect(
25
+ <Wrapper>
26
+ <ClassDeclaration name="A">
27
+ <List>
28
+ <ClassMethod
29
+ name="a"
30
+ returnType={R}
31
+ parameters={[{ name: "x", type: T }]}
32
+ />
33
+ <ClassMethod
34
+ name="b"
35
+ returnType={R}
36
+ parameters={[{ name: "y", type: T }]}
37
+ />
38
+ </List>
39
+ </ClassDeclaration>
40
+ </Wrapper>,
41
+ ).toRenderTo(d`
42
+ class A {
43
+ a(x: Thing): Result {}
44
+ b(y: Thing): Result {}
45
+ }`);
46
+ });
47
+
48
+ it("reuses declarations across multiple defkeys", () => {
49
+ const { Wrapper, defkey } = createTSTestWrapper();
50
+
51
+ expect(
52
+ <Wrapper>
53
+ <ClassDeclaration name="A">
54
+ <List>
55
+ <ClassMethod
56
+ name="a"
57
+ returnType={defkey("Result")}
58
+ parameters={[{ name: "x", type: defkey("Thing") }]}
59
+ />
60
+ <ClassMethod
61
+ name="b"
62
+ returnType={defkey("Result")}
63
+ parameters={[{ name: "y", type: defkey("Thing") }]}
64
+ />
65
+ </List>
66
+ </ClassDeclaration>
67
+ </Wrapper>,
68
+ ).toRenderTo(d`
69
+ class A {
70
+ a(x: Thing): Result {}
71
+ b(y: Thing): Result {}
72
+ }`);
73
+ });
74
+
75
+ it("should render defkey in nested component", async () => {
76
+ function TestComponent(props: { returnTypeRef: Refkey }) {
77
+ return <ClassMethod name="foo" returnType={props.returnTypeRef} />;
78
+ }
79
+
80
+ const { Wrapper, defkey } = createTSTestWrapper();
81
+
82
+ expect(
83
+ <Wrapper>
84
+ <ClassDeclaration name="MyClass">
85
+ <TestComponent returnTypeRef={defkey("MyType")} />
86
+ </ClassDeclaration>
87
+ </Wrapper>,
88
+ ).toRenderTo(d`
89
+ class MyClass {
90
+ foo(): MyType {}
91
+ }`);
92
+ });
93
+
94
+ it("should render defkey in class property", async () => {
95
+ const { Wrapper, defkey } = createTSTestWrapper();
96
+
97
+ expect(
98
+ <Wrapper>
99
+ <ClassDeclaration name="TestClass">
100
+ <ClassField name="MyProperty" type={defkey("MyType")} />
101
+ </ClassDeclaration>
102
+ </Wrapper>,
103
+ ).toRenderTo(d`
104
+ class TestClass {
105
+ MyProperty: MyType
106
+ }`);
107
+ });
@@ -0,0 +1,12 @@
1
+ import { SourceFile } from "#components/index.js";
2
+ import { createTestWrapper, type TestWrapper } from "@alloy-js/core/testing";
3
+ import { TSOutputSymbol, useTSScope } from "../src/index.js";
4
+
5
+ export function createTSTestWrapper(): TestWrapper {
6
+ return createTestWrapper({
7
+ filePath: "test.ts",
8
+ useScope: useTSScope,
9
+ makeSymbol: (nk, scope) => new TSOutputSymbol(nk, scope.spaces),
10
+ SourceFile,
11
+ });
12
+ }
@@ -0,0 +1 @@
1
+ export * from "./create-wrapper.jsx";
package/tsconfig.json CHANGED
@@ -7,6 +7,13 @@
7
7
  "types": ["@alloy-js/core/testing/matchers"]
8
8
  },
9
9
  "references": [{ "path": "../core" }],
10
- "include": ["src/**/*.ts", "src/**/*.tsx", "test/**/*.ts", "test/**/*.tsx"],
10
+ "include": [
11
+ "src/**/*.ts",
12
+ "src/**/*.tsx",
13
+ "test/**/*.ts",
14
+ "test/**/*.tsx",
15
+ "testing/**/*.ts",
16
+ "testing/**/*.tsx"
17
+ ],
11
18
  "exclude": ["node_modules", "dist"]
12
19
  }