@alloy-js/typescript 0.15.0 → 0.16.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.
package/temp/api.json CHANGED
@@ -7863,103 +7863,10 @@
7863
7863
  ],
7864
7864
  "extendsTokenRanges": []
7865
7865
  },
7866
- {
7867
- "kind": "Function",
7868
- "canonicalReference": "@alloy-js/typescript!MemberChainExpression:function(1)",
7869
- "docComment": "/**\n * Create a member chain expression, which is a member expression comprised of\n * either children, {@link FunctionCallExpression} components, or nested member\n * chain expression components. Other component types are ignored.\n */\n",
7870
- "excerptTokens": [
7871
- {
7872
- "kind": "Content",
7873
- "text": "export declare function MemberChainExpression(props: "
7874
- },
7875
- {
7876
- "kind": "Reference",
7877
- "text": "MemberChainExpressionProps",
7878
- "canonicalReference": "@alloy-js/typescript!MemberChainExpressionProps:interface"
7879
- },
7880
- {
7881
- "kind": "Content",
7882
- "text": "): "
7883
- },
7884
- {
7885
- "kind": "Reference",
7886
- "text": "Children",
7887
- "canonicalReference": "@alloy-js/core!Children:type"
7888
- },
7889
- {
7890
- "kind": "Content",
7891
- "text": ";"
7892
- }
7893
- ],
7894
- "fileUrlPath": "src/components/MemberChainExpression.tsx",
7895
- "returnTypeTokenRange": {
7896
- "startIndex": 3,
7897
- "endIndex": 4
7898
- },
7899
- "releaseTag": "Public",
7900
- "overloadIndex": 1,
7901
- "parameters": [
7902
- {
7903
- "parameterName": "props",
7904
- "parameterTypeTokenRange": {
7905
- "startIndex": 1,
7906
- "endIndex": 2
7907
- },
7908
- "isOptional": false
7909
- }
7910
- ],
7911
- "name": "MemberChainExpression"
7912
- },
7913
- {
7914
- "kind": "Interface",
7915
- "canonicalReference": "@alloy-js/typescript!MemberChainExpressionProps:interface",
7916
- "docComment": "",
7917
- "excerptTokens": [
7918
- {
7919
- "kind": "Content",
7920
- "text": "export interface MemberChainExpressionProps "
7921
- }
7922
- ],
7923
- "fileUrlPath": "src/components/MemberChainExpression.tsx",
7924
- "releaseTag": "Public",
7925
- "name": "MemberChainExpressionProps",
7926
- "preserveMemberOrder": false,
7927
- "members": [
7928
- {
7929
- "kind": "PropertySignature",
7930
- "canonicalReference": "@alloy-js/typescript!MemberChainExpressionProps#children:member",
7931
- "docComment": "",
7932
- "excerptTokens": [
7933
- {
7934
- "kind": "Content",
7935
- "text": "children: "
7936
- },
7937
- {
7938
- "kind": "Reference",
7939
- "text": "Children",
7940
- "canonicalReference": "@alloy-js/core!Children:type"
7941
- },
7942
- {
7943
- "kind": "Content",
7944
- "text": ";"
7945
- }
7946
- ],
7947
- "isReadonly": false,
7948
- "isOptional": false,
7949
- "releaseTag": "Public",
7950
- "name": "children",
7951
- "propertyTypeTokenRange": {
7952
- "startIndex": 1,
7953
- "endIndex": 2
7954
- }
7955
- }
7956
- ],
7957
- "extendsTokenRanges": []
7958
- },
7959
7866
  {
7960
7867
  "kind": "Function",
7961
7868
  "canonicalReference": "@alloy-js/typescript!MemberExpression:function(1)",
7962
- "docComment": "/**\n * Create a member expression from parts. Each part can provide one of\n * the following:\n *\n * * **id**: The identifier for the member expression part\n * * **refkey**: A refkey for a symbol whose name becomes the identifier\n * * **symbol**: a symbol whose name becomes the identifier part\n * * **args**: create a method call with the given args\n *\n * Additionally, a part can be `nullish`, which means subsequent parts\n * will use the appropriate conditional access operator e.g. `?.`.\n *\n * @example\n *\n *\n *\n * ```tsx\n * <MemberExpression>\n * <MemberExpression.Part id=\"base\" />\n * <MemberExpression.Part refkey={rk} nullish />\n * <MemberExpression.Part symbol={sym} />\n * <MemberExpression.Part args={[\"hello\", \"world\"]} />\n * </MemberExpression>\n * ```\n *\n *\n * Assuming `rk` is a refkey to a symbol name \"prop1\", and `sym` is a symbol\n * with a name of \"prop2\", this will render:\n *\n * ```ts\n * base.prop1?.prop2(\"hello\", \"world\")\n * ```\n *\n */\n",
7869
+ "docComment": "/**\n * Create a member expression from parts. Each part can provide one of\n * the following:\n *\n * * **id**: The identifier for the member expression part\n * * **refkey**: a refkey for a symbol whose name becomes the identifier\n * * **symbol**: a symbol whose name becomes the identifier part\n * * **args**: create a method call with the given args\n * * **children**: arbitrary contents for the identifier part.\n *\n * Each part can have a nullish prop, which indicates that the part may be null\n * or undefined.\n *\n * Each part can also have a quoteId prop, which indicates that the identifier\n * of the part should be quoted (i.e. `[\"foo\"]` instead of `.foo`). This is only\n * necessary when providing the children prop, otherwise it is determined\n * automatically.\n *\n * @example\n *\n *\n *\n * ```tsx\n * <MemberExpression>\n * <MemberExpression.Part id=\"base\" />\n * <MemberExpression.Part refkey={rk} nullish />\n * <MemberExpression.Part symbol={sym} />\n * <MemberExpression.Part args={[\"hello\", \"world\"]} />\n * <MemberExpression.Part>SomeValue</MemberExpression.Part>\n * </MemberExpression>\n * ```\n *\n *\n * Assuming `rk` is a refkey to a symbol name \"prop1\", and `sym` is a symbol\n * with a name of \"prop2\", this will render:\n *\n * ```ts\n * base.prop1?.prop2(\"hello\", \"world\").SomeValue\n * ```\n *\n */\n",
7963
7870
  "excerptTokens": [
7964
7871
  {
7965
7872
  "kind": "Content",
@@ -8108,6 +8015,34 @@
8108
8015
  "endIndex": 3
8109
8016
  }
8110
8017
  },
8018
+ {
8019
+ "kind": "PropertySignature",
8020
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps#children:member",
8021
+ "docComment": "/**\n * The contents of this part. When passed, overrides the other props except\n * nullish.\n */\n",
8022
+ "excerptTokens": [
8023
+ {
8024
+ "kind": "Content",
8025
+ "text": "children?: "
8026
+ },
8027
+ {
8028
+ "kind": "Reference",
8029
+ "text": "Children",
8030
+ "canonicalReference": "@alloy-js/core!Children:type"
8031
+ },
8032
+ {
8033
+ "kind": "Content",
8034
+ "text": ";"
8035
+ }
8036
+ ],
8037
+ "isReadonly": false,
8038
+ "isOptional": true,
8039
+ "releaseTag": "Public",
8040
+ "name": "children",
8041
+ "propertyTypeTokenRange": {
8042
+ "startIndex": 1,
8043
+ "endIndex": 2
8044
+ }
8045
+ },
8111
8046
  {
8112
8047
  "kind": "PropertySignature",
8113
8048
  "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps#id:member",
@@ -8162,6 +8097,33 @@
8162
8097
  "endIndex": 2
8163
8098
  }
8164
8099
  },
8100
+ {
8101
+ "kind": "PropertySignature",
8102
+ "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps#quoteId:member",
8103
+ "docComment": "/**\n * Whether the identifier should be quoted or not. Only needed when\n * providing the `children` prop, otherwise it is determined automatically.\n */\n",
8104
+ "excerptTokens": [
8105
+ {
8106
+ "kind": "Content",
8107
+ "text": "quoteId?: "
8108
+ },
8109
+ {
8110
+ "kind": "Content",
8111
+ "text": "boolean"
8112
+ },
8113
+ {
8114
+ "kind": "Content",
8115
+ "text": ";"
8116
+ }
8117
+ ],
8118
+ "isReadonly": false,
8119
+ "isOptional": true,
8120
+ "releaseTag": "Public",
8121
+ "name": "quoteId",
8122
+ "propertyTypeTokenRange": {
8123
+ "startIndex": 1,
8124
+ "endIndex": 2
8125
+ }
8126
+ },
8165
8127
  {
8166
8128
  "kind": "PropertySignature",
8167
8129
  "canonicalReference": "@alloy-js/typescript!MemberExpressionPartProps#refkey:member",
@@ -1,20 +1,26 @@
1
- import { code, List, refkey, StatementList } from "@alloy-js/core";
1
+ import {
2
+ code,
3
+ List,
4
+ Output,
5
+ refkey,
6
+ render,
7
+ StatementList,
8
+ } from "@alloy-js/core";
2
9
  import { d } from "@alloy-js/core/testing";
3
10
  import { describe, expect, it } from "vitest";
11
+ import { InterfaceMember, ObjectExpression } from "../src/components/index.js";
4
12
  import { MemberExpression } from "../src/components/MemberExpression.jsx";
5
- import {
6
- InterfaceMember,
7
- ObjectExpression,
8
- } from "../src/components/stc/index.js";
9
13
  import { VarDeclaration } from "../src/components/VarDeclaration.jsx";
10
14
  import {
11
15
  ClassDeclaration,
12
16
  ClassField,
13
17
  FunctionDeclaration,
14
18
  InterfaceDeclaration,
19
+ ObjectProperty,
15
20
  ParameterDescriptor,
21
+ SourceFile,
16
22
  } from "../src/index.js";
17
- import { toSourceText } from "./utils.js";
23
+ import { assertFileContents, toSourceText } from "./utils.js";
18
24
 
19
25
  it("renders basic member expression with dot notation", () => {
20
26
  expect(
@@ -42,6 +48,19 @@ it("renders member expression with bracket notation for invalid identifiers", ()
42
48
  `);
43
49
  });
44
50
 
51
+ it("renders member expressions with quotes", () => {
52
+ expect(
53
+ toSourceText(
54
+ <MemberExpression>
55
+ <MemberExpression.Part id="obj" />
56
+ <MemberExpression.Part id={`property-"name"`} />
57
+ </MemberExpression>,
58
+ ),
59
+ ).toBe(d`
60
+ obj["property-\\"name\\""]
61
+ `);
62
+ });
63
+
45
64
  it("handles nullish chaining", () => {
46
65
  expect(
47
66
  toSourceText(
@@ -83,6 +102,19 @@ it("ignores non-part children", () => {
83
102
  `);
84
103
  });
85
104
 
105
+ it("ignores allows parts to define children", () => {
106
+ expect(
107
+ toSourceText(
108
+ <MemberExpression>
109
+ <MemberExpression.Part>well</MemberExpression.Part>
110
+ <MemberExpression.Part>hello</MemberExpression.Part>
111
+ </MemberExpression>,
112
+ ),
113
+ ).toBe(d`
114
+ well.hello
115
+ `);
116
+ });
117
+
86
118
  it("flattens nested member expressions", () => {
87
119
  expect(
88
120
  toSourceText(
@@ -131,6 +163,46 @@ it("handles nullish chaining at multiple levels", () => {
131
163
  `);
132
164
  });
133
165
 
166
+ it("throws an error when providing conflicting part props", () => {
167
+ expect(() =>
168
+ toSourceText(
169
+ <MemberExpression>
170
+ <MemberExpression.Part id="obj" />
171
+ <MemberExpression.Part id="property" nullish={true} args={[1, 2]} />
172
+ </MemberExpression>,
173
+ ),
174
+ ).toThrowError(
175
+ `Only one of args, id can be used for a MemberExpression part at a time`,
176
+ );
177
+ });
178
+
179
+ it("takes children for the id part", () => {
180
+ expect(
181
+ toSourceText(
182
+ <List>
183
+ <MemberExpression>
184
+ <MemberExpression.Part>child1</MemberExpression.Part>
185
+ <MemberExpression.Part quoteId>child2</MemberExpression.Part>
186
+ </MemberExpression>
187
+ <MemberExpression>
188
+ <MemberExpression.Part>child1</MemberExpression.Part>
189
+ <MemberExpression.Part quoteId>child2</MemberExpression.Part>
190
+ <MemberExpression.Part args />
191
+ <MemberExpression.Part quoteId nullish>
192
+ child3
193
+ </MemberExpression.Part>
194
+ <MemberExpression.Part args />
195
+ <MemberExpression.Part>["foo" + 1]</MemberExpression.Part>
196
+ <MemberExpression.Part args />
197
+ </MemberExpression>
198
+ </List>,
199
+ ),
200
+ ).toBe(d`
201
+ child1["child2"]
202
+ child1["child2"]()["child3"]?.().["foo" + 1]()
203
+ `);
204
+ });
205
+
134
206
  describe("with refkeys", () => {
135
207
  it("handles symbols correctly", () => {
136
208
  const rk1 = refkey();
@@ -332,6 +404,42 @@ describe("with refkeys", () => {
332
404
  const test1_2 = 2;
333
405
  `);
334
406
  });
407
+
408
+ it("creates a full reference to the first refkey", () => {
409
+ const rk1 = refkey();
410
+ const res = render(
411
+ <Output>
412
+ <SourceFile path="source.ts">
413
+ <VarDeclaration name="importMe">
414
+ <ObjectExpression>
415
+ <ObjectProperty name="prop" refkey={rk1} />
416
+ </ObjectExpression>
417
+ </VarDeclaration>
418
+ </SourceFile>
419
+ <SourceFile path="index.ts">
420
+ <StatementList>
421
+ <MemberExpression>
422
+ <MemberExpression.Part refkey={rk1} />
423
+ <MemberExpression.Part id="foo" />
424
+ </MemberExpression>
425
+ <MemberExpression>
426
+ <MemberExpression.Part>{rk1}</MemberExpression.Part>
427
+ <MemberExpression.Part id="foo" />
428
+ </MemberExpression>
429
+ </StatementList>
430
+ </SourceFile>
431
+ </Output>,
432
+ );
433
+
434
+ assertFileContents(res, {
435
+ "index.ts": d`
436
+ import { importMe } from "./source.js";
437
+
438
+ importMe.prop.foo;
439
+ importMe.prop.foo;
440
+ `,
441
+ });
442
+ });
335
443
  });
336
444
 
337
445
  describe("with function calls", () => {
@@ -378,6 +486,20 @@ describe("with function calls", () => {
378
486
  myFunction(1, 2)?.prop
379
487
  `);
380
488
  });
489
+
490
+ it("handles function calls returning nullish correctly", () => {
491
+ expect(
492
+ toSourceText(
493
+ <MemberExpression>
494
+ <MemberExpression.Part id="myFunction" />
495
+ <MemberExpression.Part args={[1, 2]} nullish />
496
+ <MemberExpression.Part id="prop" />
497
+ </MemberExpression>,
498
+ ),
499
+ ).toBe(d`
500
+ myFunction(1, 2)?.prop
501
+ `);
502
+ });
381
503
  });
382
504
 
383
505
  describe("formatting", () => {
@@ -439,8 +561,33 @@ describe("formatting", () => {
439
561
  })
440
562
  `);
441
563
  });
442
-
443
- it.skip("handles multiple calls", () => {
564
+ it("handles single calls with multiple parameters", () => {
565
+ expect(
566
+ toSourceText(
567
+ <MemberExpression>
568
+ <MemberExpression.Part id="z" />
569
+ <MemberExpression.Part id="object" />
570
+ <MemberExpression.Part
571
+ args={[
572
+ <ObjectExpression jsValue={{ x: 1 }} />,
573
+ <ObjectExpression jsValue={{ y: 2 }} />,
574
+ ]}
575
+ />
576
+ </MemberExpression>,
577
+ { printWidth: 12 },
578
+ ),
579
+ ).toBe(d`
580
+ z.object(
581
+ {
582
+ x: 1,
583
+ },
584
+ {
585
+ y: 2,
586
+ }
587
+ )
588
+ `);
589
+ });
590
+ it("handles multiple calls", () => {
444
591
  expect(
445
592
  toSourceText(
446
593
  <MemberExpression>
@@ -462,5 +609,71 @@ describe("formatting", () => {
462
609
  .partial()
463
610
  `);
464
611
  });
612
+
613
+ it("renders multiple calls on the same line when there are no breaks and they fit", () => {
614
+ expect(
615
+ toSourceText(
616
+ <MemberExpression>
617
+ <MemberExpression.Part id="z" />
618
+ <MemberExpression.Part id="object" />
619
+ <MemberExpression.Part args />
620
+ <MemberExpression.Part id="partial" />
621
+ <MemberExpression.Part args />
622
+ <MemberExpression.Part id="optional" />
623
+ <MemberExpression.Part args />
624
+ </MemberExpression>,
625
+ ),
626
+ ).toBe(d`
627
+ z.object().partial().optional()
628
+ `);
629
+ });
630
+ it("handles multiple calls with id parts", () => {
631
+ expect(
632
+ toSourceText(
633
+ <MemberExpression>
634
+ <MemberExpression.Part id="z" />
635
+ <MemberExpression.Part id="z1" nullish />
636
+ <MemberExpression.Part id="object" />
637
+ <MemberExpression.Part
638
+ args={[<ObjectExpression jsValue={{ x: 1 }} />]}
639
+ />
640
+ <MemberExpression.Part id="foo" />
641
+ <MemberExpression.Part id="partial" />
642
+ <MemberExpression.Part args={[]} />
643
+ </MemberExpression>,
644
+ ),
645
+ ).toBe(d`
646
+ z.z1
647
+ ?.object({
648
+ x: 1,
649
+ })
650
+ .foo.partial()
651
+ `);
652
+ });
653
+
654
+ it("handles the first part being a call", () => {
655
+ expect(
656
+ toSourceText(
657
+ <MemberExpression>
658
+ <MemberExpression.Part id="z" />
659
+ <MemberExpression.Part args />
660
+ <MemberExpression.Part id="z1" nullish />
661
+ <MemberExpression.Part id="object" />
662
+ <MemberExpression.Part
663
+ args={[<ObjectExpression jsValue={{ x: 1 }} />]}
664
+ />
665
+ <MemberExpression.Part id="foo" />
666
+ <MemberExpression.Part id="partial" />
667
+ <MemberExpression.Part args={[]} />
668
+ </MemberExpression>,
669
+ ),
670
+ ).toBe(d`
671
+ z()
672
+ .z1?.object({
673
+ x: 1,
674
+ })
675
+ .foo.partial()
676
+ `);
677
+ });
465
678
  });
466
679
  });
@@ -1,11 +0,0 @@
1
- import { Children } from "@alloy-js/core/jsx-runtime";
2
- export interface MemberChainExpressionProps {
3
- children: Children;
4
- }
5
- /**
6
- * Create a member chain expression, which is a member expression comprised of
7
- * either children, {@link FunctionCallExpression} components, or nested member
8
- * chain expression components. Other component types are ignored.
9
- */
10
- export declare function MemberChainExpression(props: MemberChainExpressionProps): Children;
11
- //# sourceMappingURL=MemberChainExpression.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"MemberChainExpression.d.ts","sourceRoot":"","sources":["../../../src/components/MemberChainExpression.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAsB,MAAM,4BAA4B,CAAC;AAG1E,MAAM,WAAW,0BAA0B;IACzC,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,0BAA0B,YA6DtE"}
@@ -1,98 +0,0 @@
1
- import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
- import { childrenArray, computed, For, List, Show } from "@alloy-js/core";
3
- import { isComponentCreator } from "@alloy-js/core/jsx-runtime";
4
- import { FunctionCallExpression } from "./FunctionCallExpression.js";
5
- /**
6
- * Create a member chain expression, which is a member expression comprised of
7
- * either children, {@link FunctionCallExpression} components, or nested member
8
- * chain expression components. Other component types are ignored.
9
- */
10
- export function MemberChainExpression(props) {
11
- // chunks are constructed by consuming as many non-call expressions as
12
- // possible, then placing an indent and soft line break before and after the
13
- // any subsequent call expressions
14
-
15
- const chunks = computed(() => {
16
- const children = flattenCallChains(childrenArray(() => props.children));
17
- const chunks = [];
18
- let currentChunk = [];
19
- for (const child of children) {
20
- if (isComponentCreator(child) && child.component === FunctionCallExpression) {
21
- if (currentChunk.length > 0) {
22
- chunks.push(currentChunk);
23
- currentChunk = [];
24
- }
25
- chunks.push([child]);
26
- } else {
27
- currentChunk.push(child);
28
- }
29
- }
30
- if (currentChunk.length > 0) {
31
- chunks.push(currentChunk);
32
- }
33
- return [chunks[0], chunks.slice(1)];
34
- });
35
- const groupId = Symbol();
36
- return _$createIntrinsic("group", {
37
- get children() {
38
- return [_$createIntrinsic("group", {
39
- id: groupId,
40
- get children() {
41
- return _$createComponent(List, {
42
- joiner: ".",
43
- get children() {
44
- return chunks.value[0];
45
- }
46
- });
47
- }
48
- }), _$createComponent(Show, {
49
- get when() {
50
- return chunks.value[1].length === 1;
51
- },
52
- get children() {
53
- return _$createComponent(For, {
54
- get each() {
55
- return chunks.value[1];
56
- },
57
- softline: true,
58
- children: chunk => [".", _$createComponent(List, {
59
- joiner: ".",
60
- children: chunk
61
- })]
62
- });
63
- }
64
- }), _$createComponent(Show, {
65
- get when() {
66
- return chunks.value[1].length > 1;
67
- },
68
- get children() {
69
- return _$createIntrinsic("indent", {
70
- get children() {
71
- return _$createComponent(For, {
72
- get each() {
73
- return chunks.value[1];
74
- },
75
- softline: true,
76
- children: chunk => [".", _$createComponent(List, {
77
- joiner: ".",
78
- children: chunk
79
- })]
80
- });
81
- }
82
- });
83
- }
84
- })];
85
- }
86
- });
87
- }
88
- function flattenCallChains(children) {
89
- const flatChildren = [];
90
- for (const child of children) {
91
- if (isComponentCreator(child) && child.component === MemberChainExpression) {
92
- flatChildren.push(...flattenCallChains(child.props.children));
93
- } else {
94
- flatChildren.push(child);
95
- }
96
- }
97
- return flatChildren;
98
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=member-chain.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"member-chain.test.d.ts","sourceRoot":"","sources":["../../test/member-chain.test.tsx"],"names":[],"mappings":""}