@alloy-js/typescript 0.14.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/CHANGELOG.md +21 -0
- package/dist/src/components/FunctionBase.d.ts.map +1 -1
- package/dist/src/components/FunctionBase.js +6 -1
- package/dist/src/components/ImportStatement.d.ts.map +1 -1
- package/dist/src/components/ImportStatement.js +8 -2
- package/dist/src/components/MemberExpression.d.ts +21 -4
- package/dist/src/components/MemberExpression.d.ts.map +1 -1
- package/dist/src/components/MemberExpression.js +302 -88
- package/dist/src/components/ObjectExpression.d.ts +6 -0
- package/dist/src/components/ObjectExpression.d.ts.map +1 -1
- package/dist/src/components/ObjectExpression.js +17 -1
- package/dist/src/components/index.d.ts +0 -1
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +0 -1
- package/dist/src/components/stc/index.d.ts +0 -1
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +0 -1
- package/dist/src/parameter-descriptor.d.ts +9 -0
- package/dist/src/parameter-descriptor.d.ts.map +1 -1
- package/dist/test/arrow-function.test.js +17 -0
- package/dist/test/basic.test.js +1 -1
- package/dist/test/externals.test.js +3 -3
- package/dist/test/function-declaration.test.js +18 -0
- package/dist/test/function-expression.test.js +17 -0
- package/dist/test/function-type.test.js +14 -0
- package/dist/test/imports.test.js +114 -4
- package/dist/test/member-expression.test.js +285 -5
- package/dist/test/object.test.d.ts +2 -0
- package/dist/test/object.test.d.ts.map +1 -0
- package/dist/test/object.test.js +57 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/components/FunctionBase.tsx +1 -0
- package/src/components/ImportStatement.tsx +12 -2
- package/src/components/MemberExpression.tsx +364 -103
- package/src/components/ObjectExpression.tsx +21 -1
- package/src/components/index.ts +0 -1
- package/src/components/stc/index.ts +0 -1
- package/src/parameter-descriptor.ts +10 -0
- package/temp/api.json +231 -94
- package/test/arrow-function.test.tsx +22 -0
- package/test/basic.test.tsx +1 -1
- package/test/externals.test.tsx +3 -3
- package/test/function-declaration.test.tsx +22 -0
- package/test/function-expression.test.tsx +22 -0
- package/test/function-type.test.tsx +17 -0
- package/test/imports.test.tsx +75 -4
- package/test/member-expression.test.tsx +221 -8
- package/test/object.test.tsx +48 -0
- package/dist/src/components/MemberChainExpression.d.ts +0 -11
- package/dist/src/components/MemberChainExpression.d.ts.map +0 -1
- package/dist/src/components/MemberChainExpression.js +0 -98
- package/dist/test/member-chain.test.d.ts +0 -2
- package/dist/test/member-chain.test.d.ts.map +0 -1
- package/dist/test/member-chain.test.js +0 -141
- package/src/components/MemberChainExpression.tsx +0 -91
- package/test/member-chain.test.tsx +0 -124
|
@@ -1,20 +1,26 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
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
|
});
|
|
@@ -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
|
+
});
|
|
@@ -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 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"member-chain.test.d.ts","sourceRoot":"","sources":["../../test/member-chain.test.tsx"],"names":[],"mappings":""}
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
-
import { d } from "@alloy-js/core/testing";
|
|
3
|
-
import { expect, it } from "vitest";
|
|
4
|
-
import { MemberChainExpression } from "../src/components/MemberChainExpression.js";
|
|
5
|
-
import { FunctionCallExpression, ValueExpression } from "../src/index.js";
|
|
6
|
-
import { toSourceText } from "./utils.js";
|
|
7
|
-
it("works", () => {
|
|
8
|
-
expect(toSourceText(_$createComponent(MemberChainExpression, {
|
|
9
|
-
get children() {
|
|
10
|
-
return [["z"], _$createComponent(FunctionCallExpression, {
|
|
11
|
-
target: "string"
|
|
12
|
-
}), _$createComponent(FunctionCallExpression, {
|
|
13
|
-
target: "array"
|
|
14
|
-
}), _$createComponent(FunctionCallExpression, {
|
|
15
|
-
target: "min",
|
|
16
|
-
args: [5]
|
|
17
|
-
}), _$createComponent(FunctionCallExpression, {
|
|
18
|
-
target: "max",
|
|
19
|
-
args: [10]
|
|
20
|
-
})];
|
|
21
|
-
}
|
|
22
|
-
}), {
|
|
23
|
-
printWidth: 10
|
|
24
|
-
})).toBe(d`
|
|
25
|
-
z.string()
|
|
26
|
-
.array()
|
|
27
|
-
.min(5)
|
|
28
|
-
.max(10)
|
|
29
|
-
`);
|
|
30
|
-
});
|
|
31
|
-
it("works with args which break", () => {
|
|
32
|
-
expect(toSourceText(_$createComponent(MemberChainExpression, {
|
|
33
|
-
get children() {
|
|
34
|
-
return [["z"], _$createComponent(FunctionCallExpression, {
|
|
35
|
-
target: "object",
|
|
36
|
-
get args() {
|
|
37
|
-
return [_$createComponent(ValueExpression, {
|
|
38
|
-
jsValue: {
|
|
39
|
-
a: 1,
|
|
40
|
-
b: 2
|
|
41
|
-
}
|
|
42
|
-
})];
|
|
43
|
-
}
|
|
44
|
-
}), _$createComponent(FunctionCallExpression, {
|
|
45
|
-
target: "array"
|
|
46
|
-
})];
|
|
47
|
-
}
|
|
48
|
-
}), {
|
|
49
|
-
printWidth: 10
|
|
50
|
-
})).toBe(d`
|
|
51
|
-
z.object({
|
|
52
|
-
a: 1,
|
|
53
|
-
b: 2,
|
|
54
|
-
})
|
|
55
|
-
.array()
|
|
56
|
-
`);
|
|
57
|
-
});
|
|
58
|
-
it("works with args when it doesn't break", () => {
|
|
59
|
-
expect(toSourceText(_$createComponent(MemberChainExpression, {
|
|
60
|
-
get children() {
|
|
61
|
-
return [["z"], _$createComponent(FunctionCallExpression, {
|
|
62
|
-
target: "object",
|
|
63
|
-
get args() {
|
|
64
|
-
return [_$createComponent(ValueExpression, {
|
|
65
|
-
jsValue: {
|
|
66
|
-
a: 1,
|
|
67
|
-
b: 2
|
|
68
|
-
}
|
|
69
|
-
})];
|
|
70
|
-
}
|
|
71
|
-
})];
|
|
72
|
-
}
|
|
73
|
-
}))).toBe(d`
|
|
74
|
-
z.object({
|
|
75
|
-
a: 1,
|
|
76
|
-
b: 2,
|
|
77
|
-
})
|
|
78
|
-
`);
|
|
79
|
-
expect(toSourceText(_$createComponent(MemberChainExpression, {
|
|
80
|
-
get children() {
|
|
81
|
-
return [["z"], ["z2"], ["z3"], _$createComponent(FunctionCallExpression, {
|
|
82
|
-
target: "object",
|
|
83
|
-
get args() {
|
|
84
|
-
return [_$createComponent(ValueExpression, {
|
|
85
|
-
jsValue: {
|
|
86
|
-
a: 1,
|
|
87
|
-
b: 2
|
|
88
|
-
}
|
|
89
|
-
})];
|
|
90
|
-
}
|
|
91
|
-
})];
|
|
92
|
-
}
|
|
93
|
-
}))).toBe(d`
|
|
94
|
-
z.z2.z3.object({
|
|
95
|
-
a: 1,
|
|
96
|
-
b: 2,
|
|
97
|
-
})
|
|
98
|
-
`);
|
|
99
|
-
});
|
|
100
|
-
it("works with nested call member chains", () => {
|
|
101
|
-
expect(toSourceText(_$createComponent(MemberChainExpression, {
|
|
102
|
-
get children() {
|
|
103
|
-
return [["z"], _$createComponent(FunctionCallExpression, {
|
|
104
|
-
target: "string"
|
|
105
|
-
}), _$createComponent(FunctionCallExpression, {
|
|
106
|
-
target: "array"
|
|
107
|
-
}), _$createComponent(MemberChainExpression, {
|
|
108
|
-
get children() {
|
|
109
|
-
return [_$createComponent(FunctionCallExpression, {
|
|
110
|
-
target: "min",
|
|
111
|
-
args: [5]
|
|
112
|
-
}), _$createComponent(FunctionCallExpression, {
|
|
113
|
-
target: "max",
|
|
114
|
-
args: [10]
|
|
115
|
-
})];
|
|
116
|
-
}
|
|
117
|
-
}), _$createComponent(FunctionCallExpression, {
|
|
118
|
-
target: "array"
|
|
119
|
-
})];
|
|
120
|
-
}
|
|
121
|
-
}), {
|
|
122
|
-
printWidth: 10
|
|
123
|
-
})).toBe(d`
|
|
124
|
-
z.string()
|
|
125
|
-
.array()
|
|
126
|
-
.min(5)
|
|
127
|
-
.max(10)
|
|
128
|
-
.array()
|
|
129
|
-
`);
|
|
130
|
-
});
|
|
131
|
-
it("works without function call expressions", () => {
|
|
132
|
-
expect(toSourceText(_$createComponent(MemberChainExpression, {
|
|
133
|
-
get children() {
|
|
134
|
-
return [["z"], ["string"], ["min"]];
|
|
135
|
-
}
|
|
136
|
-
}), {
|
|
137
|
-
printWidth: 5
|
|
138
|
-
})).toBe(d`
|
|
139
|
-
z.string.min
|
|
140
|
-
`);
|
|
141
|
-
});
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { childrenArray, computed, For, List, Show } from "@alloy-js/core";
|
|
2
|
-
import { Children, isComponentCreator } from "@alloy-js/core/jsx-runtime";
|
|
3
|
-
import { FunctionCallExpression } from "./FunctionCallExpression.jsx";
|
|
4
|
-
|
|
5
|
-
export interface MemberChainExpressionProps {
|
|
6
|
-
children: Children;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Create a member chain expression, which is a member expression comprised of
|
|
11
|
-
* either children, {@link FunctionCallExpression} components, or nested member
|
|
12
|
-
* chain expression components. Other component types are ignored.
|
|
13
|
-
*/
|
|
14
|
-
export function MemberChainExpression(props: MemberChainExpressionProps) {
|
|
15
|
-
// chunks are constructed by consuming as many non-call expressions as
|
|
16
|
-
// possible, then placing an indent and soft line break before and after the
|
|
17
|
-
// any subsequent call expressions
|
|
18
|
-
|
|
19
|
-
const chunks = computed(() => {
|
|
20
|
-
const children = flattenCallChains(childrenArray(() => props.children));
|
|
21
|
-
const chunks: Children[][] = [];
|
|
22
|
-
|
|
23
|
-
let currentChunk: Children[] = [];
|
|
24
|
-
|
|
25
|
-
for (const child of children) {
|
|
26
|
-
if (
|
|
27
|
-
isComponentCreator(child) &&
|
|
28
|
-
child.component === FunctionCallExpression
|
|
29
|
-
) {
|
|
30
|
-
if (currentChunk.length > 0) {
|
|
31
|
-
chunks.push(currentChunk);
|
|
32
|
-
currentChunk = [];
|
|
33
|
-
}
|
|
34
|
-
chunks.push([child]);
|
|
35
|
-
} else {
|
|
36
|
-
currentChunk.push(child);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (currentChunk.length > 0) {
|
|
41
|
-
chunks.push(currentChunk);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return [chunks[0], chunks.slice(1)];
|
|
45
|
-
});
|
|
46
|
-
const groupId = Symbol();
|
|
47
|
-
|
|
48
|
-
return (
|
|
49
|
-
<group>
|
|
50
|
-
<group id={groupId}>
|
|
51
|
-
<List joiner="." children={chunks.value[0]} />
|
|
52
|
-
</group>
|
|
53
|
-
<Show when={chunks.value[1].length === 1}>
|
|
54
|
-
<For each={chunks.value[1]} softline>
|
|
55
|
-
{(chunk) => (
|
|
56
|
-
<>
|
|
57
|
-
.<List joiner="." children={chunk} />
|
|
58
|
-
</>
|
|
59
|
-
)}
|
|
60
|
-
</For>
|
|
61
|
-
</Show>
|
|
62
|
-
<Show when={chunks.value[1].length > 1}>
|
|
63
|
-
<indent>
|
|
64
|
-
<For each={chunks.value[1]} softline>
|
|
65
|
-
{(chunk) => (
|
|
66
|
-
<>
|
|
67
|
-
.<List joiner="." children={chunk} />
|
|
68
|
-
</>
|
|
69
|
-
)}
|
|
70
|
-
</For>
|
|
71
|
-
</indent>
|
|
72
|
-
</Show>
|
|
73
|
-
</group>
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function flattenCallChains(children: Children[]): Children[] {
|
|
78
|
-
const flatChildren = [];
|
|
79
|
-
for (const child of children) {
|
|
80
|
-
if (
|
|
81
|
-
isComponentCreator(child) &&
|
|
82
|
-
child.component === MemberChainExpression
|
|
83
|
-
) {
|
|
84
|
-
flatChildren.push(...flattenCallChains(child.props.children));
|
|
85
|
-
} else {
|
|
86
|
-
flatChildren.push(child);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return flatChildren;
|
|
91
|
-
}
|