@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
@@ -1,4 +1,5 @@
1
1
  import {
2
+ computed,
2
3
  mapJoin,
3
4
  memo,
4
5
  SourceDirectoryContext,
@@ -21,9 +22,15 @@ export interface ImportStatementsProps {
21
22
  export function ImportStatements(props: ImportStatementsProps) {
22
23
  const pkg = usePackage();
23
24
 
25
+ const imports = computed(() =>
26
+ [...props.records].sort(([a], [b]) => {
27
+ return a.name.localeCompare(b.name);
28
+ }),
29
+ );
30
+
24
31
  return mapJoin(
25
- () => props.records,
26
- (module, importedSymbols) => {
32
+ () => imports.value,
33
+ ([module, importedSymbols]) => {
27
34
  let targetPath: string;
28
35
 
29
36
  if (
@@ -91,6 +98,9 @@ export function ImportStatement(props: ImportStatementProps) {
91
98
  }
92
99
 
93
100
  if (namedImportSymbols.length > 0) {
101
+ namedImportSymbols.sort((a, b) => {
102
+ return a.local.name.localeCompare(b.local.name);
103
+ });
94
104
  const allNamedImportsAreTypes = namedImportSymbols.every(
95
105
  (nis) => nis.local.tsFlags & TSSymbolFlags.TypeSymbol,
96
106
  );
@@ -120,7 +120,7 @@ export function ObjectProperty(props: ObjectPropertyProps) {
120
120
  let value;
121
121
  if (props.value) {
122
122
  value = props.value;
123
- } else if (Object.prototype.hasOwnProperty.call(props, "jsValue")) {
123
+ } else if (Object.hasOwn(props, "jsValue")) {
124
124
  // need the hasOwnProperty check because the value might be falsy.
125
125
  value = <ValueExpression jsValue={props.jsValue} />;
126
126
  } else if (props.children) {
@@ -138,3 +138,23 @@ export function ObjectProperty(props: ObjectPropertyProps) {
138
138
  </>
139
139
  );
140
140
  }
141
+
142
+ export interface ObjectSpreadPropertyProps {
143
+ value?: Children;
144
+ jsValue?: unknown;
145
+ children?: Children;
146
+ }
147
+
148
+ export function ObjectSpreadProperty(props: ObjectSpreadPropertyProps) {
149
+ let value;
150
+ if (props.value) {
151
+ value = props.value;
152
+ } else if (Object.hasOwn(props, "jsValue")) {
153
+ // need the hasOwnProperty check because the value might be falsy.
154
+ value = <ValueExpression jsValue={props.jsValue} />;
155
+ } else if (props.children) {
156
+ value = props.children;
157
+ }
158
+
159
+ return <>...{value}</>;
160
+ }
@@ -24,6 +24,16 @@ export interface ParameterDescriptorBase {
24
24
  */
25
25
  readonly optional?: boolean;
26
26
 
27
+ /**
28
+ * Weather the parameter is rest parameter.
29
+ * @example
30
+ *
31
+ * ```ts
32
+ * ...args: string[]
33
+ * ```
34
+ */
35
+ readonly rest?: boolean;
36
+
27
37
  /**
28
38
  * Whether the parameter is can be null or undefined.
29
39
  * when optional is true, this will be set to true by default,
package/temp/api.json CHANGED
@@ -10338,6 +10338,154 @@
10338
10338
  ],
10339
10339
  "extendsTokenRanges": []
10340
10340
  },
10341
+ {
10342
+ "kind": "Function",
10343
+ "canonicalReference": "@alloy-js/typescript!ObjectSpreadProperty:function(1)",
10344
+ "docComment": "",
10345
+ "excerptTokens": [
10346
+ {
10347
+ "kind": "Content",
10348
+ "text": "export declare function ObjectSpreadProperty(props: "
10349
+ },
10350
+ {
10351
+ "kind": "Reference",
10352
+ "text": "ObjectSpreadPropertyProps",
10353
+ "canonicalReference": "@alloy-js/typescript!ObjectSpreadPropertyProps:interface"
10354
+ },
10355
+ {
10356
+ "kind": "Content",
10357
+ "text": "): "
10358
+ },
10359
+ {
10360
+ "kind": "Reference",
10361
+ "text": "Children",
10362
+ "canonicalReference": "@alloy-js/core!Children:type"
10363
+ },
10364
+ {
10365
+ "kind": "Content",
10366
+ "text": ";"
10367
+ }
10368
+ ],
10369
+ "fileUrlPath": "src/components/ObjectExpression.tsx",
10370
+ "returnTypeTokenRange": {
10371
+ "startIndex": 3,
10372
+ "endIndex": 4
10373
+ },
10374
+ "releaseTag": "Public",
10375
+ "overloadIndex": 1,
10376
+ "parameters": [
10377
+ {
10378
+ "parameterName": "props",
10379
+ "parameterTypeTokenRange": {
10380
+ "startIndex": 1,
10381
+ "endIndex": 2
10382
+ },
10383
+ "isOptional": false
10384
+ }
10385
+ ],
10386
+ "name": "ObjectSpreadProperty"
10387
+ },
10388
+ {
10389
+ "kind": "Interface",
10390
+ "canonicalReference": "@alloy-js/typescript!ObjectSpreadPropertyProps:interface",
10391
+ "docComment": "",
10392
+ "excerptTokens": [
10393
+ {
10394
+ "kind": "Content",
10395
+ "text": "export interface ObjectSpreadPropertyProps "
10396
+ }
10397
+ ],
10398
+ "fileUrlPath": "src/components/ObjectExpression.tsx",
10399
+ "releaseTag": "Public",
10400
+ "name": "ObjectSpreadPropertyProps",
10401
+ "preserveMemberOrder": false,
10402
+ "members": [
10403
+ {
10404
+ "kind": "PropertySignature",
10405
+ "canonicalReference": "@alloy-js/typescript!ObjectSpreadPropertyProps#children:member",
10406
+ "docComment": "",
10407
+ "excerptTokens": [
10408
+ {
10409
+ "kind": "Content",
10410
+ "text": "children?: "
10411
+ },
10412
+ {
10413
+ "kind": "Reference",
10414
+ "text": "Children",
10415
+ "canonicalReference": "@alloy-js/core!Children:type"
10416
+ },
10417
+ {
10418
+ "kind": "Content",
10419
+ "text": ";"
10420
+ }
10421
+ ],
10422
+ "isReadonly": false,
10423
+ "isOptional": true,
10424
+ "releaseTag": "Public",
10425
+ "name": "children",
10426
+ "propertyTypeTokenRange": {
10427
+ "startIndex": 1,
10428
+ "endIndex": 2
10429
+ }
10430
+ },
10431
+ {
10432
+ "kind": "PropertySignature",
10433
+ "canonicalReference": "@alloy-js/typescript!ObjectSpreadPropertyProps#jsValue:member",
10434
+ "docComment": "",
10435
+ "excerptTokens": [
10436
+ {
10437
+ "kind": "Content",
10438
+ "text": "jsValue?: "
10439
+ },
10440
+ {
10441
+ "kind": "Content",
10442
+ "text": "unknown"
10443
+ },
10444
+ {
10445
+ "kind": "Content",
10446
+ "text": ";"
10447
+ }
10448
+ ],
10449
+ "isReadonly": false,
10450
+ "isOptional": true,
10451
+ "releaseTag": "Public",
10452
+ "name": "jsValue",
10453
+ "propertyTypeTokenRange": {
10454
+ "startIndex": 1,
10455
+ "endIndex": 2
10456
+ }
10457
+ },
10458
+ {
10459
+ "kind": "PropertySignature",
10460
+ "canonicalReference": "@alloy-js/typescript!ObjectSpreadPropertyProps#value:member",
10461
+ "docComment": "",
10462
+ "excerptTokens": [
10463
+ {
10464
+ "kind": "Content",
10465
+ "text": "value?: "
10466
+ },
10467
+ {
10468
+ "kind": "Reference",
10469
+ "text": "Children",
10470
+ "canonicalReference": "@alloy-js/core!Children:type"
10471
+ },
10472
+ {
10473
+ "kind": "Content",
10474
+ "text": ";"
10475
+ }
10476
+ ],
10477
+ "isReadonly": false,
10478
+ "isOptional": true,
10479
+ "releaseTag": "Public",
10480
+ "name": "value",
10481
+ "propertyTypeTokenRange": {
10482
+ "startIndex": 1,
10483
+ "endIndex": 2
10484
+ }
10485
+ }
10486
+ ],
10487
+ "extendsTokenRanges": []
10488
+ },
10341
10489
  {
10342
10490
  "kind": "Interface",
10343
10491
  "canonicalReference": "@alloy-js/typescript!PackageContext:interface",
@@ -11526,6 +11674,33 @@
11526
11674
  "endIndex": 5
11527
11675
  }
11528
11676
  },
11677
+ {
11678
+ "kind": "PropertySignature",
11679
+ "canonicalReference": "@alloy-js/typescript!ParameterDescriptorBase#rest:member",
11680
+ "docComment": "/**\n * Weather the parameter is rest parameter.\n *\n * @example\n *\n *\n *\n * ```ts\n * ...args: string[]\n * ```\n *\n */\n",
11681
+ "excerptTokens": [
11682
+ {
11683
+ "kind": "Content",
11684
+ "text": "readonly rest?: "
11685
+ },
11686
+ {
11687
+ "kind": "Content",
11688
+ "text": "boolean"
11689
+ },
11690
+ {
11691
+ "kind": "Content",
11692
+ "text": ";"
11693
+ }
11694
+ ],
11695
+ "isReadonly": true,
11696
+ "isOptional": true,
11697
+ "releaseTag": "Public",
11698
+ "name": "rest",
11699
+ "propertyTypeTokenRange": {
11700
+ "startIndex": 1,
11701
+ "endIndex": 2
11702
+ }
11703
+ },
11529
11704
  {
11530
11705
  "kind": "PropertySignature",
11531
11706
  "canonicalReference": "@alloy-js/typescript!ParameterDescriptorBase#type:member",
@@ -185,4 +185,26 @@ describe("symbols", () => {
185
185
  }
186
186
  `);
187
187
  });
188
+
189
+ it("create rest parameters", () => {
190
+ const paramDesc: ParameterDescriptor = {
191
+ name: "foo",
192
+ refkey: refkey(),
193
+ type: "any[]",
194
+ rest: true,
195
+ };
196
+ const decl = (
197
+ <>
198
+ <ArrowFunction parameters={[paramDesc]}>
199
+ console.log(foo);
200
+ </ArrowFunction>
201
+ </>
202
+ );
203
+
204
+ expect(toSourceText(decl)).toBe(d`
205
+ (...foo: any[]) => {
206
+ console.log(foo);
207
+ }
208
+ `);
209
+ });
188
210
  });
@@ -75,7 +75,7 @@ it("works", () => {
75
75
  `);
76
76
 
77
77
  expect(findFile(res, "test2.ts").contents).toEqual(d`
78
- import { sayHello, sayGoodbye } from "./test1.js";
78
+ import { sayGoodbye, sayHello } from "./test1.js";
79
79
 
80
80
  console.log(sayHello("world"));
81
81
  console.log(sayGoodbye("world"));
@@ -50,8 +50,8 @@ it("can import builtins", () => {
50
50
  }
51
51
  `,
52
52
  "index.ts": `
53
- import { nice } from "testLib/subpath";
54
53
  import { readFile } from "node:fs/promises";
54
+ import { nice } from "testLib/subpath";
55
55
 
56
56
  nice;
57
57
  await readFile();
@@ -85,8 +85,8 @@ it("can import builtins without a package", () => {
85
85
 
86
86
  assertFileContents(res, {
87
87
  "index.ts": `
88
- import { nice } from "testLib/subpath";
89
88
  import { readFile } from "node:fs/promises";
89
+ import { nice } from "testLib/subpath";
90
90
 
91
91
  nice;
92
92
  await readFile();
@@ -124,8 +124,8 @@ it("can import builtins without a package", () => {
124
124
 
125
125
  assertFileContents(res, {
126
126
  "index.ts": `
127
- import { nice } from "testLib/subpath";
128
127
  import { readFile } from "node:fs/promises";
128
+ import { nice } from "testLib/subpath";
129
129
 
130
130
  function foo() {
131
131
  function bar() {
@@ -219,4 +219,26 @@ describe("symbols", () => {
219
219
  }
220
220
  `);
221
221
  });
222
+
223
+ it("create optional parameters", () => {
224
+ const paramDesc: ParameterDescriptor = {
225
+ name: "foo",
226
+ refkey: refkey(),
227
+ type: "any[]",
228
+ rest: true,
229
+ };
230
+ const decl = (
231
+ <>
232
+ <FunctionDeclaration name="foo" parameters={[paramDesc]}>
233
+ console.log(foo);
234
+ </FunctionDeclaration>
235
+ </>
236
+ );
237
+
238
+ expect(toSourceText(decl)).toBe(d`
239
+ function foo(...foo: any[]) {
240
+ console.log(foo);
241
+ }
242
+ `);
243
+ });
222
244
  });
@@ -192,4 +192,26 @@ describe("symbols", () => {
192
192
  }
193
193
  `);
194
194
  });
195
+
196
+ it("create rest parameters", () => {
197
+ const paramDesc: ParameterDescriptor = {
198
+ name: "foo",
199
+ refkey: refkey(),
200
+ type: "any[]",
201
+ rest: true,
202
+ };
203
+ const decl = (
204
+ <>
205
+ <FunctionExpression parameters={[paramDesc]}>
206
+ console.log(foo);
207
+ </FunctionExpression>
208
+ </>
209
+ );
210
+
211
+ expect(toSourceText(decl)).toBe(d`
212
+ function (...foo: any[]) {
213
+ console.log(foo);
214
+ }
215
+ `);
216
+ });
195
217
  });
@@ -115,4 +115,21 @@ describe("symbols", () => {
115
115
  (foo?: any) => void
116
116
  `);
117
117
  });
118
+ it("create rest parameters", () => {
119
+ const paramDesc: ParameterDescriptor = {
120
+ name: "foo",
121
+ refkey: refkey(),
122
+ type: "any[]",
123
+ rest: true,
124
+ };
125
+ const decl = (
126
+ <>
127
+ <FunctionType parameters={[paramDesc]}></FunctionType>
128
+ </>
129
+ );
130
+
131
+ expect(toSourceText(decl)).toBe(d`
132
+ (...foo: any[]) => void
133
+ `);
134
+ });
118
135
  });
@@ -70,6 +70,77 @@ it("works with named imports", () => {
70
70
  });
71
71
  });
72
72
 
73
+ it("sort named imports", () => {
74
+ const res = render(
75
+ <Output>
76
+ <ts.SourceFile path="test1.ts">
77
+ <List>
78
+ <>
79
+ <ts.ClassDeclaration export name="B" refkey={refkey("B")} />
80
+ </>
81
+ <>
82
+ <ts.FunctionDeclaration export name="a" refkey={refkey("a")} />
83
+ </>
84
+ </List>
85
+ </ts.SourceFile>
86
+
87
+ <ts.SourceFile path="test2.ts">
88
+ <List>
89
+ <>
90
+ const b = <Reference refkey={refkey("B")} />;
91
+ </>
92
+ <>
93
+ const a = <Reference refkey={refkey("a")} />;
94
+ </>
95
+ </List>
96
+ </ts.SourceFile>
97
+ </Output>,
98
+ );
99
+
100
+ assertFileContents(res, {
101
+ "test2.ts": `
102
+ import { a, B } from "./test1.js";
103
+
104
+ const b = B;
105
+ const a = a;
106
+ `,
107
+ });
108
+ });
109
+
110
+ it("sort statements by import paths", () => {
111
+ const res = render(
112
+ <Output>
113
+ <ts.SourceFile path="a.ts">
114
+ <ts.ClassDeclaration export default name="A" refkey={refkey("A")} />
115
+ </ts.SourceFile>
116
+ <ts.SourceFile path="b.ts">
117
+ <ts.ClassDeclaration export default name="B" refkey={refkey("B")} />
118
+ </ts.SourceFile>
119
+
120
+ <ts.SourceFile path="test2.ts">
121
+ <List>
122
+ <>
123
+ const b = <Reference refkey={refkey("B")} />;
124
+ </>
125
+ <>
126
+ const a = <Reference refkey={refkey("A")} />;
127
+ </>
128
+ </List>
129
+ </ts.SourceFile>
130
+ </Output>,
131
+ );
132
+
133
+ assertFileContents(res, {
134
+ "test2.ts": `
135
+ import A from "./a.js";
136
+ import B from "./b.js";
137
+
138
+ const b = B;
139
+ const a = A;
140
+ `,
141
+ });
142
+ });
143
+
73
144
  it("works with default and named imports", () => {
74
145
  const res = render(
75
146
  <Output>
@@ -214,9 +285,9 @@ it("works with importing the same name many times from different files with the
214
285
 
215
286
  assertFileContents(res, {
216
287
  "test-import.ts": `
217
- import { type MyInterface, conflict } from "./test1.js";
218
- import { type MyInterface as MyInterface_2, conflict as conflict_2 } from "./test2.js";
219
- import { type MyInterface as MyInterface_3, conflict as conflict_3 } from "./test3.js";
288
+ import { conflict, type MyInterface } from "./test1.js";
289
+ import { conflict as conflict_2, type MyInterface as MyInterface_2 } from "./test2.js";
290
+ import { conflict as conflict_3, type MyInterface as MyInterface_3 } from "./test3.js";
220
291
 
221
292
  const one: MyInterface = conflict;
222
293
  const two: MyInterface_2 = conflict_2;
@@ -402,7 +473,7 @@ describe("type imports", () => {
402
473
 
403
474
  assertFileContents(res, {
404
475
  "test2.ts": `
405
- import { type TypeA, ClassA } from "./test1.js";
476
+ import { ClassA, type TypeA } from "./test1.js";
406
477
 
407
478
  type A = TypeA
408
479
  class B extends ClassA {}
@@ -0,0 +1,48 @@
1
+ import { List } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { expect, it } from "vitest";
4
+ import {
5
+ ObjectExpression,
6
+ ObjectProperty,
7
+ ObjectSpreadProperty,
8
+ } from "../src/index.js";
9
+ import { toSourceText } from "./utils.jsx";
10
+
11
+ it("from js value", () => {
12
+ expect(toSourceText(<ObjectExpression jsValue={{ a: 1, b: 2 }} />)).toBe(d`
13
+ {
14
+ a: 1,
15
+ b: 2,
16
+ }
17
+ `);
18
+ });
19
+
20
+ it("from object property", () => {
21
+ const comp = (
22
+ <ObjectExpression>
23
+ <List joiner={",\n"}>
24
+ <ObjectProperty name="a" value={1} />
25
+ <ObjectProperty name="b" value={2} />
26
+ </List>
27
+ </ObjectExpression>
28
+ );
29
+ expect(toSourceText(comp)).toBe(d`
30
+ {
31
+ a: 1,
32
+ b: 2
33
+ }
34
+ `);
35
+ });
36
+
37
+ it("spread property", () => {
38
+ const comp = (
39
+ <ObjectExpression>
40
+ <ObjectSpreadProperty>abc</ObjectSpreadProperty>
41
+ </ObjectExpression>
42
+ );
43
+ expect(toSourceText(comp)).toBe(d`
44
+ {
45
+ ...abc
46
+ }
47
+ `);
48
+ });