@alloy-js/typescript 0.14.0 → 0.15.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 (35) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/src/components/FunctionBase.d.ts.map +1 -1
  3. package/dist/src/components/FunctionBase.js +6 -1
  4. package/dist/src/components/ImportStatement.d.ts.map +1 -1
  5. package/dist/src/components/ImportStatement.js +8 -2
  6. package/dist/src/components/ObjectExpression.d.ts +6 -0
  7. package/dist/src/components/ObjectExpression.d.ts.map +1 -1
  8. package/dist/src/components/ObjectExpression.js +17 -1
  9. package/dist/src/parameter-descriptor.d.ts +9 -0
  10. package/dist/src/parameter-descriptor.d.ts.map +1 -1
  11. package/dist/test/arrow-function.test.js +17 -0
  12. package/dist/test/basic.test.js +1 -1
  13. package/dist/test/externals.test.js +3 -3
  14. package/dist/test/function-declaration.test.js +18 -0
  15. package/dist/test/function-expression.test.js +17 -0
  16. package/dist/test/function-type.test.js +14 -0
  17. package/dist/test/imports.test.js +114 -4
  18. package/dist/test/object.test.d.ts +2 -0
  19. package/dist/test/object.test.d.ts.map +1 -0
  20. package/dist/test/object.test.js +57 -0
  21. package/dist/tsconfig.tsbuildinfo +1 -1
  22. package/package.json +3 -3
  23. package/src/components/FunctionBase.tsx +1 -0
  24. package/src/components/ImportStatement.tsx +12 -2
  25. package/src/components/ObjectExpression.tsx +21 -1
  26. package/src/parameter-descriptor.ts +10 -0
  27. package/temp/api.json +175 -0
  28. package/test/arrow-function.test.tsx +22 -0
  29. package/test/basic.test.tsx +1 -1
  30. package/test/externals.test.tsx +3 -3
  31. package/test/function-declaration.test.tsx +22 -0
  32. package/test/function-expression.test.tsx +22 -0
  33. package/test/function-type.test.tsx +17 -0
  34. package/test/imports.test.tsx +75 -4
  35. package/test/object.test.tsx +48 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog - @alloy-js/typescript
2
2
 
3
+ ## 0.15.0
4
+
5
+ ### Features
6
+
7
+ - [#146](https://github.com/alloy-framework/alloy/pull/146) Add support for rest parameters in function components
8
+ - [#146](https://github.com/alloy-framework/alloy/pull/146) Add support for object spread properties
9
+ - [#145](https://github.com/alloy-framework/alloy/pull/145) Sort import statements and named imports
10
+
11
+
3
12
  ## 0.14.0
4
13
 
5
14
  ### Bug Fixes
@@ -1 +1 @@
1
- {"version":3,"file":"FunctionBase.d.ts","sourceRoot":"","sources":["../../../src/components/FunctionBase.tsx"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAE3D,OAAO,KAAK,EAEV,mBAAmB,EACpB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAYrE,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;CAC9B;AAED,oCAAoC;AACpC,MAAM,WAAW,uBAAuB;IACtC,iBAAiB;IACjB,QAAQ,CAAC,UAAU,CAAC,EAAE,mBAAmB,EAAE,GAAG,MAAM,EAAE,CAAC;IACvD,mBAAmB;IACnB,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;CAC9B;AAED,iEAAiE;AACjE,MAAM,WAAW,2BAA2B;IAC1C,iBAAiB;IACjB,QAAQ,CAAC,UAAU,CAAC,EAAE,mBAAmB,EAAE,GAAG,MAAM,EAAE,CAAC;IACvD,mBAAmB;IACnB,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;CAC9B;AAED,gCAAgC;AAChC,MAAM,WAAW,mBAAmB;IAClC,iBAAiB;IACjB,UAAU,CAAC,EAAE,uBAAuB,EAAE,GAAG,MAAM,EAAE,CAAC;IAClD,mBAAmB;IACnB,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACrB;AAED,eAAO,MAAM,YAAY,sIAKxB,CAAC;AAEF;;;;;;;GAOG;AACH,e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sIAKxB,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,kJAmB9B,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,sBAAsB,0JAmBlC,CAAC;AAgGF;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,0IAkC1B,CAAC;AAkBF,wBAAgB,aAAa,CAC3B,UAAU,EAAE,QAAQ,GAAG,SAAS,EAChC,OAAO,GAAE;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAqB,YAKhD"}
@@ -90,7 +90,12 @@ export const FunctionTypeParameters = taggedComponent(functionParametersTag, fun
90
90
  function parameter(param) {
91
91
  return _$createIntrinsic("group", {
92
92
  get children() {
93
- return [_$memo(() => param.symbol.name), _$createComponent(Show, {
93
+ return [_$createComponent(Show, {
94
+ get when() {
95
+ return param.rest;
96
+ },
97
+ children: "..."
98
+ }), _$memo(() => param.symbol.name), _$createComponent(Show, {
94
99
  get when() {
95
100
  return !!param.optional;
96
101
  },
@@ -1 +1 @@
1
- {"version":3,"file":"ImportStatement.d.ts","sourceRoot":"","sources":["../../../src/components/ImportStatement.tsx"],"names":[],"mappings":"AAOA,OAAO,EACL,cAAc,EACd,aAAa,EAGd,MAAM,qBAAqB,CAAC;AAI7B,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,aAAa,CAAC;CACxB;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,6CA0C5D;AAED,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,GAAG,CAAC,cAAc,CAAC,CAAC;CAC9B;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,oBAAoB,eAkD1D"}
1
+ {"version":3,"file":"ImportStatement.d.ts","sourceRoot":"","sources":["../../../src/components/ImportStatement.tsx"],"names":[],"mappings":"AAQA,OAAO,EACL,cAAc,EACd,aAAa,EAGd,MAAM,qBAAqB,CAAC;AAI7B,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,aAAa,CAAC;CACxB;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,6CAgD5D;AAED,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,GAAG,CAAC,cAAc,CAAC,CAAC;CAC9B;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,oBAAoB,eAqD1D"}
@@ -1,12 +1,15 @@
1
1
  import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
- import { mapJoin, memo, SourceDirectoryContext, useContext } from "@alloy-js/core";
2
+ import { computed, mapJoin, memo, SourceDirectoryContext, useContext } from "@alloy-js/core";
3
3
  import { relative } from "pathe";
4
4
  import { TSSymbolFlags } from "../symbols/index.js";
5
5
  import { modulePath } from "../utils.js";
6
6
  import { usePackage } from "./PackageDirectory.js";
7
7
  export function ImportStatements(props) {
8
8
  const pkg = usePackage();
9
- return mapJoin(() => props.records, (module, importedSymbols) => {
9
+ const imports = computed(() => [...props.records].sort(([a], [b]) => {
10
+ return a.name.localeCompare(b.name);
11
+ }));
12
+ return mapJoin(() => imports.value, ([module, importedSymbols]) => {
10
13
  let targetPath;
11
14
  if (pkg && pkg.scope !== module.parent || !pkg && module.parent.kind !== "global") {
12
15
  // importing from another package, so let's calculate the import.
@@ -58,6 +61,9 @@ export function ImportStatement(props) {
58
61
  }
59
62
  }
60
63
  if (namedImportSymbols.length > 0) {
64
+ namedImportSymbols.sort((a, b) => {
65
+ return a.local.name.localeCompare(b.local.name);
66
+ });
61
67
  const allNamedImportsAreTypes = namedImportSymbols.every(nis => nis.local.tsFlags & TSSymbolFlags.TypeSymbol);
62
68
  if (allNamedImportsAreTypes) {
63
69
  parts.push("type ");
@@ -13,4 +13,10 @@ export interface ObjectPropertyProps {
13
13
  refkey?: Refkey | Refkey[];
14
14
  }
15
15
  export declare function ObjectProperty(props: ObjectPropertyProps): Children;
16
+ export interface ObjectSpreadPropertyProps {
17
+ value?: Children;
18
+ jsValue?: unknown;
19
+ children?: Children;
20
+ }
21
+ export declare function ObjectSpreadProperty(props: ObjectSpreadPropertyProps): Children;
16
22
  //# sourceMappingURL=ObjectExpression.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ObjectExpression.d.ts","sourceRoot":"","sources":["../../../src/components/ObjectExpression.tsx"],"names":[],"mappings":"AAAA,OAAO,EAGL,QAAQ,EAQR,MAAM,EAGP,MAAM,gBAAgB,CAAC;AAMxB,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,OAAO,CAAC,EACJ,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GACnB,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,GACpB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC7B;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,YA0D5D;AAED,MAAM,WAAW,mBAAmB;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,cAAc,CAAC,EAAE,QAAQ,CAAC;IAC1B,KAAK,CAAC,EAAE,QAAQ,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAC5B;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,mBAAmB,YA0CxD"}
1
+ {"version":3,"file":"ObjectExpression.d.ts","sourceRoot":"","sources":["../../../src/components/ObjectExpression.tsx"],"names":[],"mappings":"AAAA,OAAO,EAGL,QAAQ,EAQR,MAAM,EAGP,MAAM,gBAAgB,CAAC;AAMxB,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,OAAO,CAAC,EACJ,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GACnB,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,GACpB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC7B;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,YA0D5D;AAED,MAAM,WAAW,mBAAmB;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,cAAc,CAAC,EAAE,QAAQ,CAAC;IAC1B,KAAK,CAAC,EAAE,QAAQ,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAC5B;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,mBAAmB,YA0CxD;AAED,MAAM,WAAW,yBAAyB;IACxC,KAAK,CAAC,EAAE,QAAQ,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACrB;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,yBAAyB,YAYpE"}
@@ -90,7 +90,7 @@ export function ObjectProperty(props) {
90
90
  let value;
91
91
  if (props.value) {
92
92
  value = props.value;
93
- } else if (Object.prototype.hasOwnProperty.call(props, "jsValue")) {
93
+ } else if (Object.hasOwn(props, "jsValue")) {
94
94
  // need the hasOwnProperty check because the value might be falsy.
95
95
  value = _$createComponent(ValueExpression, {
96
96
  get jsValue() {
@@ -105,4 +105,20 @@ export function ObjectProperty(props) {
105
105
  value: assignmentContext,
106
106
  children: value
107
107
  })];
108
+ }
109
+ export function ObjectSpreadProperty(props) {
110
+ let value;
111
+ if (props.value) {
112
+ value = props.value;
113
+ } else if (Object.hasOwn(props, "jsValue")) {
114
+ // need the hasOwnProperty check because the value might be falsy.
115
+ value = _$createComponent(ValueExpression, {
116
+ get jsValue() {
117
+ return props.jsValue;
118
+ }
119
+ });
120
+ } else if (props.children) {
121
+ value = props.children;
122
+ }
123
+ return ["...", value];
108
124
  }
@@ -19,6 +19,15 @@ export interface ParameterDescriptorBase {
19
19
  * Whether the parameter is optional.
20
20
  */
21
21
  readonly optional?: boolean;
22
+ /**
23
+ * Weather the parameter is rest parameter.
24
+ * @example
25
+ *
26
+ * ```ts
27
+ * ...args: string[]
28
+ * ```
29
+ */
30
+ readonly rest?: boolean;
22
31
  /**
23
32
  * Whether the parameter is can be null or undefined.
24
33
  * when optional is true, this will be set to true by default,
@@ -1 +1 @@
1
- {"version":3,"file":"parameter-descriptor.d.ts","sourceRoot":"","sources":["../../src/parameter-descriptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAElD;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC;IAEzB;;OAEG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAEpC;;OAEG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAE5B;;;;OAIG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;IAE3B;;OAEG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAE5C;;OAEG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,mBAAoB,SAAQ,uBAAuB;IAClE;;OAEG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC;CAC7B;AAED;;GAEG;AACH,MAAM,WAAW,+BACf,SAAQ,uBAAuB;CAAG;AAEpC;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,OAAO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AAuB;IACtC;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC;IAE5B;;OAEG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC;IAE5B;;OAEG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAEpC;;OAEG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC7C"}
@@ -191,4 +191,21 @@ describe("symbols", () => {
191
191
  }
192
192
  `);
193
193
  });
194
+ it("create rest parameters", () => {
195
+ const paramDesc = {
196
+ name: "foo",
197
+ refkey: refkey(),
198
+ type: "any[]",
199
+ rest: true
200
+ };
201
+ const decl = [_$createComponent(ArrowFunction, {
202
+ parameters: [paramDesc],
203
+ children: "console.log(foo);"
204
+ })];
205
+ expect(toSourceText(decl)).toBe(d`
206
+ (...foo: any[]) => {
207
+ console.log(foo);
208
+ }
209
+ `);
210
+ });
194
211
  });
@@ -83,7 +83,7 @@ it("works", () => {
83
83
  }
84
84
  `);
85
85
  expect(findFile(res, "test2.ts").contents).toEqual(d`
86
- import { sayHello, sayGoodbye } from "./test1.js";
86
+ import { sayGoodbye, sayHello } from "./test1.js";
87
87
 
88
88
  console.log(sayHello("world"));
89
89
  console.log(sayGoodbye("world"));
@@ -51,8 +51,8 @@ it("can import builtins", () => {
51
51
  }
52
52
  `,
53
53
  "index.ts": `
54
- import { nice } from "testLib/subpath";
55
54
  import { readFile } from "node:fs/promises";
55
+ import { nice } from "testLib/subpath";
56
56
 
57
57
  nice;
58
58
  await readFile();
@@ -86,8 +86,8 @@ it("can import builtins without a package", () => {
86
86
  }));
87
87
  assertFileContents(res, {
88
88
  "index.ts": `
89
- import { nice } from "testLib/subpath";
90
89
  import { readFile } from "node:fs/promises";
90
+ import { nice } from "testLib/subpath";
91
91
 
92
92
  nice;
93
93
  await readFile();
@@ -131,8 +131,8 @@ it("can import builtins without a package", () => {
131
131
  }));
132
132
  assertFileContents(res, {
133
133
  "index.ts": `
134
- import { nice } from "testLib/subpath";
135
134
  import { readFile } from "node:fs/promises";
135
+ import { nice } from "testLib/subpath";
136
136
 
137
137
  function foo() {
138
138
  function bar() {
@@ -225,4 +225,22 @@ describe("symbols", () => {
225
225
  }
226
226
  `);
227
227
  });
228
+ it("create optional parameters", () => {
229
+ const paramDesc = {
230
+ name: "foo",
231
+ refkey: refkey(),
232
+ type: "any[]",
233
+ rest: true
234
+ };
235
+ const decl = [_$createComponent(FunctionDeclaration, {
236
+ name: "foo",
237
+ parameters: [paramDesc],
238
+ children: "console.log(foo);"
239
+ })];
240
+ expect(toSourceText(decl)).toBe(d`
241
+ function foo(...foo: any[]) {
242
+ console.log(foo);
243
+ }
244
+ `);
245
+ });
228
246
  });
@@ -191,4 +191,21 @@ describe("symbols", () => {
191
191
  }
192
192
  `);
193
193
  });
194
+ it("create rest parameters", () => {
195
+ const paramDesc = {
196
+ name: "foo",
197
+ refkey: refkey(),
198
+ type: "any[]",
199
+ rest: true
200
+ };
201
+ const decl = [_$createComponent(FunctionExpression, {
202
+ parameters: [paramDesc],
203
+ children: "console.log(foo);"
204
+ })];
205
+ expect(toSourceText(decl)).toBe(d`
206
+ function (...foo: any[]) {
207
+ console.log(foo);
208
+ }
209
+ `);
210
+ });
194
211
  });
@@ -119,4 +119,18 @@ describe("symbols", () => {
119
119
  (foo?: any) => void
120
120
  `);
121
121
  });
122
+ it("create rest parameters", () => {
123
+ const paramDesc = {
124
+ name: "foo",
125
+ refkey: refkey(),
126
+ type: "any[]",
127
+ rest: true
128
+ };
129
+ const decl = [_$createComponent(FunctionType, {
130
+ parameters: [paramDesc]
131
+ })];
132
+ expect(toSourceText(decl)).toBe(d`
133
+ (...foo: any[]) => void
134
+ `);
135
+ });
122
136
  });
@@ -83,6 +83,116 @@ it("works with named imports", () => {
83
83
  `
84
84
  });
85
85
  });
86
+ it("sort named imports", () => {
87
+ const res = render(_$createComponent(Output, {
88
+ get children() {
89
+ return [_$createComponent(ts.SourceFile, {
90
+ path: "test1.ts",
91
+ get children() {
92
+ return _$createComponent(List, {
93
+ get children() {
94
+ return [[_$createComponent(ts.ClassDeclaration, {
95
+ "export": true,
96
+ name: "B",
97
+ get refkey() {
98
+ return refkey("B");
99
+ }
100
+ })], [_$createComponent(ts.FunctionDeclaration, {
101
+ "export": true,
102
+ name: "a",
103
+ get refkey() {
104
+ return refkey("a");
105
+ }
106
+ })]];
107
+ }
108
+ });
109
+ }
110
+ }), _$createComponent(ts.SourceFile, {
111
+ path: "test2.ts",
112
+ get children() {
113
+ return _$createComponent(List, {
114
+ get children() {
115
+ return [["const b = ", _$createComponent(Reference, {
116
+ get refkey() {
117
+ return refkey("B");
118
+ }
119
+ }), ";"], ["const a = ", _$createComponent(Reference, {
120
+ get refkey() {
121
+ return refkey("a");
122
+ }
123
+ }), ";"]];
124
+ }
125
+ });
126
+ }
127
+ })];
128
+ }
129
+ }));
130
+ assertFileContents(res, {
131
+ "test2.ts": `
132
+ import { a, B } from "./test1.js";
133
+
134
+ const b = B;
135
+ const a = a;
136
+ `
137
+ });
138
+ });
139
+ it("sort statements by import paths", () => {
140
+ const res = render(_$createComponent(Output, {
141
+ get children() {
142
+ return [_$createComponent(ts.SourceFile, {
143
+ path: "a.ts",
144
+ get children() {
145
+ return _$createComponent(ts.ClassDeclaration, {
146
+ "export": true,
147
+ "default": true,
148
+ name: "A",
149
+ get refkey() {
150
+ return refkey("A");
151
+ }
152
+ });
153
+ }
154
+ }), _$createComponent(ts.SourceFile, {
155
+ path: "b.ts",
156
+ get children() {
157
+ return _$createComponent(ts.ClassDeclaration, {
158
+ "export": true,
159
+ "default": true,
160
+ name: "B",
161
+ get refkey() {
162
+ return refkey("B");
163
+ }
164
+ });
165
+ }
166
+ }), _$createComponent(ts.SourceFile, {
167
+ path: "test2.ts",
168
+ get children() {
169
+ return _$createComponent(List, {
170
+ get children() {
171
+ return [["const b = ", _$createComponent(Reference, {
172
+ get refkey() {
173
+ return refkey("B");
174
+ }
175
+ }), ";"], ["const a = ", _$createComponent(Reference, {
176
+ get refkey() {
177
+ return refkey("A");
178
+ }
179
+ }), ";"]];
180
+ }
181
+ });
182
+ }
183
+ })];
184
+ }
185
+ }));
186
+ assertFileContents(res, {
187
+ "test2.ts": `
188
+ import A from "./a.js";
189
+ import B from "./b.js";
190
+
191
+ const b = B;
192
+ const a = A;
193
+ `
194
+ });
195
+ });
86
196
  it("works with default and named imports", () => {
87
197
  const res = render(_$createComponent(Output, {
88
198
  get children() {
@@ -301,9 +411,9 @@ it("works with importing the same name many times from different files with the
301
411
  }));
302
412
  assertFileContents(res, {
303
413
  "test-import.ts": `
304
- import { type MyInterface, conflict } from "./test1.js";
305
- import { type MyInterface as MyInterface_2, conflict as conflict_2 } from "./test2.js";
306
- import { type MyInterface as MyInterface_3, conflict as conflict_3 } from "./test3.js";
414
+ import { conflict, type MyInterface } from "./test1.js";
415
+ import { conflict as conflict_2, type MyInterface as MyInterface_2 } from "./test2.js";
416
+ import { conflict as conflict_3, type MyInterface as MyInterface_3 } from "./test3.js";
307
417
 
308
418
  const one: MyInterface = conflict;
309
419
  const two: MyInterface_2 = conflict_2;
@@ -575,7 +685,7 @@ describe("type imports", () => {
575
685
  }));
576
686
  assertFileContents(res, {
577
687
  "test2.ts": `
578
- import { type TypeA, ClassA } from "./test1.js";
688
+ import { ClassA, type TypeA } from "./test1.js";
579
689
 
580
690
  type A = TypeA
581
691
  class B extends ClassA {}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=object.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object.test.d.ts","sourceRoot":"","sources":["../../test/object.test.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,57 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { List } from "@alloy-js/core";
3
+ import { d } from "@alloy-js/core/testing";
4
+ import { expect, it } from "vitest";
5
+ import { ObjectExpression, ObjectProperty, ObjectSpreadProperty } from "../src/index.js";
6
+ import { toSourceText } from "./utils.js";
7
+ it("from js value", () => {
8
+ expect(toSourceText(_$createComponent(ObjectExpression, {
9
+ jsValue: {
10
+ a: 1,
11
+ b: 2
12
+ }
13
+ }))).toBe(d`
14
+ {
15
+ a: 1,
16
+ b: 2,
17
+ }
18
+ `);
19
+ });
20
+ it("from object property", () => {
21
+ const comp = _$createComponent(ObjectExpression, {
22
+ get children() {
23
+ return _$createComponent(List, {
24
+ joiner: ",\n",
25
+ get children() {
26
+ return [_$createComponent(ObjectProperty, {
27
+ name: "a",
28
+ value: 1
29
+ }), _$createComponent(ObjectProperty, {
30
+ name: "b",
31
+ value: 2
32
+ })];
33
+ }
34
+ });
35
+ }
36
+ });
37
+ expect(toSourceText(comp)).toBe(d`
38
+ {
39
+ a: 1,
40
+ b: 2
41
+ }
42
+ `);
43
+ });
44
+ it("spread property", () => {
45
+ const comp = _$createComponent(ObjectExpression, {
46
+ get children() {
47
+ return _$createComponent(ObjectSpreadProperty, {
48
+ children: "abc"
49
+ });
50
+ }
51
+ });
52
+ expect(toSourceText(comp)).toBe(d`
53
+ {
54
+ ...abc
55
+ }
56
+ `);
57
+ });