@alloy-js/typescript 0.19.0-dev.0 → 0.19.0-dev.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alloy-js/typescript",
3
- "version": "0.19.0-dev.0",
3
+ "version": "0.19.0-dev.2",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
@@ -14,7 +14,7 @@
14
14
  "author": "brian.terlson@microsoft.com",
15
15
  "license": "MIT",
16
16
  "dependencies": {
17
- "@alloy-js/core": "~0.18.0 || >= 0.19.0-dev.0",
17
+ "@alloy-js/core": "~0.18.2 || >= 0.19.0-dev.2",
18
18
  "change-case": "^5.4.4",
19
19
  "pathe": "^2.0.3"
20
20
  },
@@ -25,7 +25,7 @@
25
25
  "@rollup/plugin-typescript": "^12.1.2",
26
26
  "concurrently": "^9.1.2",
27
27
  "typescript": "^5.8.3",
28
- "vitest": "^3.1.1"
28
+ "vitest": "^3.2.4"
29
29
  },
30
30
  "type": "module",
31
31
  "scripts": {
@@ -15,8 +15,6 @@ import { TypeDeclaration } from "./TypeDeclaration.jsx";
15
15
  import { PrivateScopeContext } from "../context/private-scope.js";
16
16
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
17
17
  import { EnumDeclaration } from "./EnumDeclaration.jsx";
18
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
19
- import { InterfaceDeclaration } from "./Interface.jsx";
20
18
 
21
19
  export interface BaseDeclarationProps {
22
20
  /**
@@ -84,7 +82,7 @@ export interface DeclarationProps extends Omit<BaseDeclarationProps, "name"> {
84
82
  /**
85
83
  * Create a TypeScript declaration. Generally, this component shouldn't be used
86
84
  * directly, and instead prefer components for specific declarations, e.g.
87
- * {@link EnumDeclaration}, {@link InterfaceDeclaration},
85
+ * {@link EnumDeclaration}, {@link (InterfaceDeclaration:function)},
88
86
  * {@link TypeDeclaration}, etc.
89
87
  *
90
88
  * @remarks
@@ -142,6 +142,10 @@ function parameter(param: DeclaredParameterDescriptor) {
142
142
  </SymbolSlot>
143
143
  </indent>
144
144
  </Show>
145
+ <Show when={!!param.default}>
146
+ {" = "}
147
+ <SymbolSlot>{param.default!}</SymbolSlot>
148
+ </Show>
145
149
  </group>
146
150
  );
147
151
  }
@@ -284,6 +288,10 @@ function typeParameter(param: DeclaredTypeParameterDescriptor) {
284
288
  <TypeRefContext>{param.extends}</TypeRefContext>
285
289
  </indent>
286
290
  </Show>
291
+ <Show when={!!param.default}>
292
+ {" = "}
293
+ <TypeRefContext>{param.default}</TypeRefContext>
294
+ </Show>
287
295
  </group>
288
296
  );
289
297
  }
@@ -1,6 +1,9 @@
1
1
  import {
2
2
  Block,
3
3
  Children,
4
+ childrenArray,
5
+ findKeyedChild,
6
+ findUnkeyedChildren,
4
7
  MemberScope,
5
8
  Name,
6
9
  OutputSymbolFlags,
@@ -14,28 +17,42 @@ import { useTSNamePolicy } from "../name-policy.js";
14
17
  import { BaseDeclarationProps, Declaration } from "./Declaration.js";
15
18
  import { JSDoc } from "./JSDoc.jsx";
16
19
 
20
+ import { TypeParameterDescriptor } from "../parameter-descriptor.js";
21
+ import { TypeParameters } from "./FunctionBase.jsx";
17
22
  import { MemberDeclaration } from "./MemberDeclaration.jsx";
18
23
  import { PropertyName } from "./PropertyName.jsx";
19
24
  import { ensureTypeRefContext } from "./TypeRefContext.jsx";
20
25
 
21
26
  export interface InterfaceDeclarationProps extends BaseDeclarationProps {
22
27
  extends?: Children;
28
+
29
+ /**
30
+ * The generic type parameters of the interface.
31
+ */
32
+ typeParameters?: TypeParameterDescriptor[] | string[];
23
33
  }
24
34
 
25
- /**
26
- * Create a TypeScript interface declaration.
27
- *
28
- * @remarks
29
- *
30
- * This component will declare a symbol for this interface. The `export` and
31
- * `default` boolean props determine whether and how this symbol is exported
32
- * from the package.
33
- */
34
- export const InterfaceDeclaration = ensureTypeRefContext(
35
+ const _InterfaceDeclaration = ensureTypeRefContext(
35
36
  (props: InterfaceDeclarationProps) => {
37
+ const children = childrenArray(() => props.children);
38
+
39
+ const typeParametersChildren =
40
+ findKeyedChild(children, TypeParameters.tag) ?? undefined;
41
+
42
+ const sTypeParameters =
43
+ typeParametersChildren ?
44
+ <>
45
+ {"<"}
46
+ {typeParametersChildren}
47
+ {">"}
48
+ </>
49
+ : <TypeParameters parameters={props.typeParameters} />;
50
+
36
51
  const extendsPart = props.extends ? <> extends {props.extends}</> : "";
37
52
  const flags = OutputSymbolFlags.StaticMemberContainer;
38
53
 
54
+ const filteredChildren = findUnkeyedChildren(children);
55
+
39
56
  return (
40
57
  <>
41
58
  <Show when={Boolean(props.doc)}>
@@ -44,14 +61,30 @@ export const InterfaceDeclaration = ensureTypeRefContext(
44
61
  </Show>
45
62
  <Declaration {...props} nameKind="interface" flags={flags} kind="type">
46
63
  interface <Name />
64
+ {sTypeParameters}
47
65
  {extendsPart}{" "}
48
- <InterfaceExpression>{props.children}</InterfaceExpression>
66
+ <InterfaceExpression>{filteredChildren}</InterfaceExpression>
49
67
  </Declaration>
50
68
  </>
51
69
  );
52
70
  },
53
71
  );
54
72
 
73
+ /**
74
+ * Create a TypeScript interface declaration.
75
+ *
76
+ * @remarks
77
+ *
78
+ * This component will declare a symbol for this interface. The `export` and
79
+ * `default` boolean props determine whether and how this symbol is exported
80
+ * from the package.
81
+ */
82
+ export function InterfaceDeclaration(props: InterfaceDeclarationProps) {
83
+ return _InterfaceDeclaration(props);
84
+ }
85
+
86
+ InterfaceDeclaration.TypeParameters = TypeParameters;
87
+
55
88
  export interface InterfaceExpressionProps {
56
89
  children?: Children;
57
90
  }
package/temp/api.json CHANGED
@@ -3226,7 +3226,7 @@
3226
3226
  {
3227
3227
  "kind": "Function",
3228
3228
  "canonicalReference": "@alloy-js/typescript!Declaration:function(1)",
3229
- "docComment": "/**\n * Create a TypeScript declaration. Generally, this component shouldn't be used\n * directly, and instead prefer components for specific declarations, e.g.\n * {@link EnumDeclaration}, {@link InterfaceDeclaration},\n * {@link TypeDeclaration}, etc.\n *\n * @remarks\n *\n *\n *\n * This component will wrap its contents in a {@link @alloy-js/core#Declaration} component,\n * so children can make use of declaration context. Additionally, if the\n * provided symbol flags have {@link @alloy-js/core#OutputSymbolFlags.MemberContainer}, this\n * component will create a {@link @alloy-js/core#MemberScope}.\n *\n */\n",
3229
+ "docComment": "/**\n * Create a TypeScript declaration. Generally, this component shouldn't be used\n * directly, and instead prefer components for specific declarations, e.g.\n * {@link EnumDeclaration}, {@link (InterfaceDeclaration:function)},\n * {@link TypeDeclaration}, etc.\n *\n * @remarks\n *\n *\n *\n * This component will wrap its contents in a {@link @alloy-js/core#Declaration} component,\n * so children can make use of declaration context. Additionally, if the\n * provided symbol flags have {@link @alloy-js/core#OutputSymbolFlags.MemberContainer}, this\n * component will create a {@link @alloy-js/core#MemberScope}.\n *\n */\n",
3230
3230
  "excerptTokens": [
3231
3231
  {
3232
3232
  "kind": "Content",
@@ -6193,7 +6193,7 @@
6193
6193
  "excerptTokens": [
6194
6194
  {
6195
6195
  "kind": "Content",
6196
- "text": "InterfaceDeclaration: (props: "
6196
+ "text": "export declare function InterfaceDeclaration(props: "
6197
6197
  },
6198
6198
  {
6199
6199
  "kind": "Reference",
@@ -6202,12 +6202,16 @@
6202
6202
  },
6203
6203
  {
6204
6204
  "kind": "Content",
6205
- "text": ") => "
6205
+ "text": "): "
6206
6206
  },
6207
6207
  {
6208
6208
  "kind": "Reference",
6209
6209
  "text": "Children",
6210
6210
  "canonicalReference": "@alloy-js/core!Children:type"
6211
+ },
6212
+ {
6213
+ "kind": "Content",
6214
+ "text": ";"
6211
6215
  }
6212
6216
  ],
6213
6217
  "fileUrlPath": "src/components/Interface.tsx",
@@ -6229,6 +6233,99 @@
6229
6233
  ],
6230
6234
  "name": "InterfaceDeclaration"
6231
6235
  },
6236
+ {
6237
+ "kind": "Namespace",
6238
+ "canonicalReference": "@alloy-js/typescript!InterfaceDeclaration:namespace",
6239
+ "docComment": "",
6240
+ "excerptTokens": [
6241
+ {
6242
+ "kind": "Content",
6243
+ "text": "export declare namespace InterfaceDeclaration "
6244
+ }
6245
+ ],
6246
+ "fileUrlPath": "src/components/Interface.tsx",
6247
+ "releaseTag": "Public",
6248
+ "name": "InterfaceDeclaration",
6249
+ "preserveMemberOrder": false,
6250
+ "members": [
6251
+ {
6252
+ "kind": "Variable",
6253
+ "canonicalReference": "@alloy-js/typescript!InterfaceDeclaration.TypeParameters:var",
6254
+ "docComment": "",
6255
+ "excerptTokens": [
6256
+ {
6257
+ "kind": "Content",
6258
+ "text": "TypeParameters: "
6259
+ },
6260
+ {
6261
+ "kind": "Content",
6262
+ "text": "import(\"@alloy-js/core\")."
6263
+ },
6264
+ {
6265
+ "kind": "Reference",
6266
+ "text": "Component",
6267
+ "canonicalReference": "@alloy-js/core!Component:interface"
6268
+ },
6269
+ {
6270
+ "kind": "Content",
6271
+ "text": "<import(\"./FunctionBase.jsx\")."
6272
+ },
6273
+ {
6274
+ "kind": "Reference",
6275
+ "text": "TypeParametersProps",
6276
+ "canonicalReference": "@alloy-js/typescript!~TypeParametersProps:interface"
6277
+ },
6278
+ {
6279
+ "kind": "Content",
6280
+ "text": "> & "
6281
+ },
6282
+ {
6283
+ "kind": "Reference",
6284
+ "text": "Required",
6285
+ "canonicalReference": "!Required:type"
6286
+ },
6287
+ {
6288
+ "kind": "Content",
6289
+ "text": "<"
6290
+ },
6291
+ {
6292
+ "kind": "Reference",
6293
+ "text": "Pick",
6294
+ "canonicalReference": "!Pick:type"
6295
+ },
6296
+ {
6297
+ "kind": "Content",
6298
+ "text": "<import(\"@alloy-js/core\")."
6299
+ },
6300
+ {
6301
+ "kind": "Reference",
6302
+ "text": "Component",
6303
+ "canonicalReference": "@alloy-js/core!Component:interface"
6304
+ },
6305
+ {
6306
+ "kind": "Content",
6307
+ "text": "<import(\"./FunctionBase.jsx\")."
6308
+ },
6309
+ {
6310
+ "kind": "Reference",
6311
+ "text": "TypeParametersProps",
6312
+ "canonicalReference": "@alloy-js/typescript!~TypeParametersProps:interface"
6313
+ },
6314
+ {
6315
+ "kind": "Content",
6316
+ "text": ">, \"tag\">>"
6317
+ }
6318
+ ],
6319
+ "isReadonly": false,
6320
+ "releaseTag": "Public",
6321
+ "name": "TypeParameters",
6322
+ "variableTypeTokenRange": {
6323
+ "startIndex": 1,
6324
+ "endIndex": 14
6325
+ }
6326
+ }
6327
+ ]
6328
+ },
6232
6329
  {
6233
6330
  "kind": "Interface",
6234
6331
  "canonicalReference": "@alloy-js/typescript!InterfaceDeclarationProps:interface",
@@ -6280,6 +6377,38 @@
6280
6377
  "startIndex": 1,
6281
6378
  "endIndex": 2
6282
6379
  }
6380
+ },
6381
+ {
6382
+ "kind": "PropertySignature",
6383
+ "canonicalReference": "@alloy-js/typescript!InterfaceDeclarationProps#typeParameters:member",
6384
+ "docComment": "/**\n * The generic type parameters of the interface.\n */\n",
6385
+ "excerptTokens": [
6386
+ {
6387
+ "kind": "Content",
6388
+ "text": "typeParameters?: "
6389
+ },
6390
+ {
6391
+ "kind": "Reference",
6392
+ "text": "TypeParameterDescriptor",
6393
+ "canonicalReference": "@alloy-js/typescript!TypeParameterDescriptor:interface"
6394
+ },
6395
+ {
6396
+ "kind": "Content",
6397
+ "text": "[] | string[]"
6398
+ },
6399
+ {
6400
+ "kind": "Content",
6401
+ "text": ";"
6402
+ }
6403
+ ],
6404
+ "isReadonly": false,
6405
+ "isOptional": true,
6406
+ "releaseTag": "Public",
6407
+ "name": "typeParameters",
6408
+ "propertyTypeTokenRange": {
6409
+ "startIndex": 1,
6410
+ "endIndex": 3
6411
+ }
6283
6412
  }
6284
6413
  ],
6285
6414
  "extendsTokenRanges": [
@@ -267,6 +267,50 @@ describe("symbols", () => {
267
267
  `);
268
268
  });
269
269
 
270
+ it("creates parameters with default values", () => {
271
+ const paramDesc: ParameterDescriptor = {
272
+ name: "foo",
273
+ refkey: refkey(),
274
+ type: "string",
275
+ default: '"bar"',
276
+ };
277
+
278
+ const decl = (
279
+ <>
280
+ <FunctionDeclaration name="foo" parameters={[paramDesc]}>
281
+ console.log(foo);
282
+ </FunctionDeclaration>
283
+ </>
284
+ );
285
+
286
+ expect(toSourceText(decl)).toBe(d`
287
+ function foo(foo: string = "bar") {
288
+ console.log(foo);
289
+ }
290
+ `);
291
+ });
292
+
293
+ it("correctly renders mixed parameters", () => {
294
+ const params: ParameterDescriptor[] = [
295
+ { name: "a", refkey: refkey(), type: "string" },
296
+ { name: "b", refkey: refkey(), type: "number", optional: true },
297
+ { name: "c", refkey: refkey(), type: "boolean", default: "false" },
298
+ { name: "d", refkey: refkey(), type: "any[]", rest: true },
299
+ ];
300
+
301
+ const decl = (
302
+ <FunctionDeclaration name="foo" parameters={params}>
303
+ console.log(a, b, c, d);
304
+ </FunctionDeclaration>
305
+ );
306
+
307
+ expect(toSourceText(decl)).toBe(d`
308
+ function foo(a: string, b?: number, c: boolean = false, ...d: any[]) {
309
+ console.log(a, b, c, d);
310
+ }
311
+ `);
312
+ });
313
+
270
314
  it("adds symbols for members of parameters when a type is provided", () => {
271
315
  const ifaceRk = refkey();
272
316
  const ifaceMemberRk = refkey();
@@ -229,6 +229,76 @@ it("handles invalid identifier names", () => {
229
229
  `);
230
230
  });
231
231
 
232
+ it("accepts type parameters by descriptors", () => {
233
+ const typeParams: ts.TypeParameterDescriptor[] = [
234
+ { name: "T", refkey: refkey() },
235
+ { name: "U", extends: "number", refkey: refkey() },
236
+ { name: "V", default: "object", refkey: refkey() },
237
+ { name: "W", extends: "string", default: '"test"', refkey: refkey() },
238
+ ];
239
+
240
+ const res = toSourceText(
241
+ <ts.InterfaceDeclaration name="Foo" typeParameters={typeParams}>
242
+ <ts.InterfaceMember name="member" type={typeParams[0].refkey} />;
243
+ <hbr />
244
+ <ts.InterfaceMember name="member2" type={typeParams[1].refkey} />;
245
+ <hbr />
246
+ <ts.InterfaceMember name="member3" type={typeParams[2].refkey} />;
247
+ <hbr />
248
+ <ts.InterfaceMember name="member4" type={typeParams[3].refkey} />;
249
+ </ts.InterfaceDeclaration>,
250
+ );
251
+
252
+ expect(res).toEqual(d`
253
+ interface Foo<T, U extends number, V = object, W extends string = "test"> {
254
+ member: T;
255
+ member2: U;
256
+ member3: V;
257
+ member4: W;
258
+ }
259
+ `);
260
+ });
261
+
262
+ it("accepts type parameters with extends", () => {
263
+ const res = toSourceText(
264
+ <ts.InterfaceDeclaration name="Foo" typeParameters={["T"]} extends="Bar">
265
+ <ts.InterfaceMember name="member" type="T" />;
266
+ </ts.InterfaceDeclaration>,
267
+ );
268
+
269
+ expect(res).toEqual(d`
270
+ interface Foo<T> extends Bar {
271
+ member: T;
272
+ }
273
+ `);
274
+ });
275
+
276
+ it("accepts type parameters children", () => {
277
+ const res = toSourceText(
278
+ <ts.InterfaceDeclaration name="Foo">
279
+ <ts.InterfaceDeclaration.TypeParameters>
280
+ T, U extends number, V = object, W extends string = "test"
281
+ </ts.InterfaceDeclaration.TypeParameters>
282
+ <ts.InterfaceMember name="member" type={"T"} />;
283
+ <hbr />
284
+ <ts.InterfaceMember name="member2" type="U" />;
285
+ <hbr />
286
+ <ts.InterfaceMember name="member3" type="V" />;
287
+ <hbr />
288
+ <ts.InterfaceMember name="member4" type="W" />;
289
+ </ts.InterfaceDeclaration>,
290
+ );
291
+
292
+ expect(res).toEqual(d`
293
+ interface Foo<T, U extends number, V = object, W extends string = "test"> {
294
+ member: T;
295
+ member2: U;
296
+ member3: V;
297
+ member4: W;
298
+ }
299
+ `);
300
+ });
301
+
232
302
  describe("method members", () => {
233
303
  it("render basic", () => {
234
304
  expect(
@@ -238,15 +238,20 @@ describe("symbols", () => {
238
238
  </Output>
239
239
  );
240
240
 
241
- expect(decl).toRenderTo(`
242
- export const refme = {
243
- foo: {
244
- bar: "hello",
241
+ expect(decl).toRenderTo({
242
+ "foo.ts": `
243
+ export const refme = {
244
+ foo: {
245
+ bar: "hello",
246
+ }
245
247
  }
246
- }import { refme } from "./foo.js";
248
+ `,
249
+ "bar.ts": `
250
+ import { refme } from "./foo.js";
247
251
 
248
- console.log(refme.foo.bar);
249
- `);
252
+ console.log(refme.foo.bar);
253
+ `,
254
+ });
250
255
  });
251
256
 
252
257
  it("can reference nested members in other packages", () => {