@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/CHANGELOG.md +12 -0
- 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/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/test/member-expression.test.js +285 -5
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/components/MemberExpression.tsx +364 -103
- package/src/components/index.ts +0 -1
- package/src/components/stc/index.ts +0 -1
- package/temp/api.json +56 -94
- package/test/member-expression.test.tsx +221 -8
- 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,12 +1,12 @@
|
|
|
1
1
|
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
-
import { code, List, refkey, StatementList } from "@alloy-js/core";
|
|
2
|
+
import { code, List, Output, refkey, render, StatementList } from "@alloy-js/core";
|
|
3
3
|
import { d } from "@alloy-js/core/testing";
|
|
4
4
|
import { describe, expect, it } from "vitest";
|
|
5
|
+
import { InterfaceMember, ObjectExpression } from "../src/components/index.js";
|
|
5
6
|
import { MemberExpression } from "../src/components/MemberExpression.js";
|
|
6
|
-
import { InterfaceMember, ObjectExpression } from "../src/components/stc/index.js";
|
|
7
7
|
import { VarDeclaration } from "../src/components/VarDeclaration.js";
|
|
8
|
-
import { ClassDeclaration, ClassField, FunctionDeclaration, InterfaceDeclaration } from "../src/index.js";
|
|
9
|
-
import { toSourceText } from "./utils.js";
|
|
8
|
+
import { ClassDeclaration, ClassField, FunctionDeclaration, InterfaceDeclaration, ObjectProperty, SourceFile } from "../src/index.js";
|
|
9
|
+
import { assertFileContents, toSourceText } from "./utils.js";
|
|
10
10
|
it("renders basic member expression with dot notation", () => {
|
|
11
11
|
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
12
12
|
get children() {
|
|
@@ -33,6 +33,19 @@ it("renders member expression with bracket notation for invalid identifiers", ()
|
|
|
33
33
|
obj["property-name"]
|
|
34
34
|
`);
|
|
35
35
|
});
|
|
36
|
+
it("renders member expressions with quotes", () => {
|
|
37
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
38
|
+
get children() {
|
|
39
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
40
|
+
id: "obj"
|
|
41
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
42
|
+
id: `property-"name"`
|
|
43
|
+
})];
|
|
44
|
+
}
|
|
45
|
+
}))).toBe(d`
|
|
46
|
+
obj["property-\\"name\\""]
|
|
47
|
+
`);
|
|
48
|
+
});
|
|
36
49
|
it("handles nullish chaining", () => {
|
|
37
50
|
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
38
51
|
get children() {
|
|
@@ -77,6 +90,19 @@ it("ignores non-part children", () => {
|
|
|
77
90
|
obj.property
|
|
78
91
|
`);
|
|
79
92
|
});
|
|
93
|
+
it("ignores allows parts to define children", () => {
|
|
94
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
95
|
+
get children() {
|
|
96
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
97
|
+
children: "well"
|
|
98
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
99
|
+
children: "hello"
|
|
100
|
+
})];
|
|
101
|
+
}
|
|
102
|
+
}))).toBe(d`
|
|
103
|
+
well.hello
|
|
104
|
+
`);
|
|
105
|
+
});
|
|
80
106
|
it("flattens nested member expressions", () => {
|
|
81
107
|
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
82
108
|
get children() {
|
|
@@ -136,6 +162,59 @@ it("handles nullish chaining at multiple levels", () => {
|
|
|
136
162
|
a.b?.c.d?.e
|
|
137
163
|
`);
|
|
138
164
|
});
|
|
165
|
+
it("throws an error when providing conflicting part props", () => {
|
|
166
|
+
expect(() => toSourceText(_$createComponent(MemberExpression, {
|
|
167
|
+
get children() {
|
|
168
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
169
|
+
id: "obj"
|
|
170
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
171
|
+
id: "property",
|
|
172
|
+
nullish: true,
|
|
173
|
+
args: [1, 2]
|
|
174
|
+
})];
|
|
175
|
+
}
|
|
176
|
+
}))).toThrowError(`Only one of args, id can be used for a MemberExpression part at a time`);
|
|
177
|
+
});
|
|
178
|
+
it("takes children for the id part", () => {
|
|
179
|
+
expect(toSourceText(_$createComponent(List, {
|
|
180
|
+
get children() {
|
|
181
|
+
return [_$createComponent(MemberExpression, {
|
|
182
|
+
get children() {
|
|
183
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
184
|
+
children: "child1"
|
|
185
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
186
|
+
quoteId: true,
|
|
187
|
+
children: "child2"
|
|
188
|
+
})];
|
|
189
|
+
}
|
|
190
|
+
}), _$createComponent(MemberExpression, {
|
|
191
|
+
get children() {
|
|
192
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
193
|
+
children: "child1"
|
|
194
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
195
|
+
quoteId: true,
|
|
196
|
+
children: "child2"
|
|
197
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
198
|
+
args: true
|
|
199
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
200
|
+
quoteId: true,
|
|
201
|
+
nullish: true,
|
|
202
|
+
children: "child3"
|
|
203
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
204
|
+
args: true
|
|
205
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
206
|
+
children: "[\"foo\" + 1]"
|
|
207
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
208
|
+
args: true
|
|
209
|
+
})];
|
|
210
|
+
}
|
|
211
|
+
})];
|
|
212
|
+
}
|
|
213
|
+
}))).toBe(d`
|
|
214
|
+
child1["child2"]
|
|
215
|
+
child1["child2"]()["child3"]?.().["foo" + 1]()
|
|
216
|
+
`);
|
|
217
|
+
});
|
|
139
218
|
describe("with refkeys", () => {
|
|
140
219
|
it("handles symbols correctly", () => {
|
|
141
220
|
const rk1 = refkey();
|
|
@@ -410,6 +489,64 @@ describe("with refkeys", () => {
|
|
|
410
489
|
const test1_2 = 2;
|
|
411
490
|
`);
|
|
412
491
|
});
|
|
492
|
+
it("creates a full reference to the first refkey", () => {
|
|
493
|
+
const rk1 = refkey();
|
|
494
|
+
const res = render(_$createComponent(Output, {
|
|
495
|
+
get children() {
|
|
496
|
+
return [_$createComponent(SourceFile, {
|
|
497
|
+
path: "source.ts",
|
|
498
|
+
get children() {
|
|
499
|
+
return _$createComponent(VarDeclaration, {
|
|
500
|
+
name: "importMe",
|
|
501
|
+
get children() {
|
|
502
|
+
return _$createComponent(ObjectExpression, {
|
|
503
|
+
get children() {
|
|
504
|
+
return _$createComponent(ObjectProperty, {
|
|
505
|
+
name: "prop",
|
|
506
|
+
refkey: rk1
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
}), _$createComponent(SourceFile, {
|
|
514
|
+
path: "index.ts",
|
|
515
|
+
get children() {
|
|
516
|
+
return _$createComponent(StatementList, {
|
|
517
|
+
get children() {
|
|
518
|
+
return [_$createComponent(MemberExpression, {
|
|
519
|
+
get children() {
|
|
520
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
521
|
+
refkey: rk1
|
|
522
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
523
|
+
id: "foo"
|
|
524
|
+
})];
|
|
525
|
+
}
|
|
526
|
+
}), _$createComponent(MemberExpression, {
|
|
527
|
+
get children() {
|
|
528
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
529
|
+
children: rk1
|
|
530
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
531
|
+
id: "foo"
|
|
532
|
+
})];
|
|
533
|
+
}
|
|
534
|
+
})];
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
})];
|
|
539
|
+
}
|
|
540
|
+
}));
|
|
541
|
+
assertFileContents(res, {
|
|
542
|
+
"index.ts": d`
|
|
543
|
+
import { importMe } from "./source.js";
|
|
544
|
+
|
|
545
|
+
importMe.prop.foo;
|
|
546
|
+
importMe.prop.foo;
|
|
547
|
+
`
|
|
548
|
+
});
|
|
549
|
+
});
|
|
413
550
|
});
|
|
414
551
|
describe("with function calls", () => {
|
|
415
552
|
it("handles simple function calls correctly", () => {
|
|
@@ -466,6 +603,22 @@ describe("with function calls", () => {
|
|
|
466
603
|
myFunction(1, 2)?.prop
|
|
467
604
|
`);
|
|
468
605
|
});
|
|
606
|
+
it("handles function calls returning nullish correctly", () => {
|
|
607
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
608
|
+
get children() {
|
|
609
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
610
|
+
id: "myFunction"
|
|
611
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
612
|
+
args: [1, 2],
|
|
613
|
+
nullish: true
|
|
614
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
615
|
+
id: "prop"
|
|
616
|
+
})];
|
|
617
|
+
}
|
|
618
|
+
}))).toBe(d`
|
|
619
|
+
myFunction(1, 2)?.prop
|
|
620
|
+
`);
|
|
621
|
+
});
|
|
469
622
|
});
|
|
470
623
|
describe("formatting", () => {
|
|
471
624
|
describe("simple chains", () => {
|
|
@@ -540,7 +693,41 @@ describe("formatting", () => {
|
|
|
540
693
|
})
|
|
541
694
|
`);
|
|
542
695
|
});
|
|
543
|
-
it
|
|
696
|
+
it("handles single calls with multiple parameters", () => {
|
|
697
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
698
|
+
get children() {
|
|
699
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
700
|
+
id: "z"
|
|
701
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
702
|
+
id: "object"
|
|
703
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
704
|
+
get args() {
|
|
705
|
+
return [_$createComponent(ObjectExpression, {
|
|
706
|
+
jsValue: {
|
|
707
|
+
x: 1
|
|
708
|
+
}
|
|
709
|
+
}), _$createComponent(ObjectExpression, {
|
|
710
|
+
jsValue: {
|
|
711
|
+
y: 2
|
|
712
|
+
}
|
|
713
|
+
})];
|
|
714
|
+
}
|
|
715
|
+
})];
|
|
716
|
+
}
|
|
717
|
+
}), {
|
|
718
|
+
printWidth: 12
|
|
719
|
+
})).toBe(d`
|
|
720
|
+
z.object(
|
|
721
|
+
{
|
|
722
|
+
x: 1,
|
|
723
|
+
},
|
|
724
|
+
{
|
|
725
|
+
y: 2,
|
|
726
|
+
}
|
|
727
|
+
)
|
|
728
|
+
`);
|
|
729
|
+
});
|
|
730
|
+
it("handles multiple calls", () => {
|
|
544
731
|
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
545
732
|
get children() {
|
|
546
733
|
return [_$createComponent(MemberExpression.Part, {
|
|
@@ -571,5 +758,98 @@ describe("formatting", () => {
|
|
|
571
758
|
.partial()
|
|
572
759
|
`);
|
|
573
760
|
});
|
|
761
|
+
it("renders multiple calls on the same line when there are no breaks and they fit", () => {
|
|
762
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
763
|
+
get children() {
|
|
764
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
765
|
+
id: "z"
|
|
766
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
767
|
+
id: "object"
|
|
768
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
769
|
+
args: true
|
|
770
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
771
|
+
id: "partial"
|
|
772
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
773
|
+
args: true
|
|
774
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
775
|
+
id: "optional"
|
|
776
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
777
|
+
args: true
|
|
778
|
+
})];
|
|
779
|
+
}
|
|
780
|
+
}))).toBe(d`
|
|
781
|
+
z.object().partial().optional()
|
|
782
|
+
`);
|
|
783
|
+
});
|
|
784
|
+
it("handles multiple calls with id parts", () => {
|
|
785
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
786
|
+
get children() {
|
|
787
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
788
|
+
id: "z"
|
|
789
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
790
|
+
id: "z1",
|
|
791
|
+
nullish: true
|
|
792
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
793
|
+
id: "object"
|
|
794
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
795
|
+
get args() {
|
|
796
|
+
return [_$createComponent(ObjectExpression, {
|
|
797
|
+
jsValue: {
|
|
798
|
+
x: 1
|
|
799
|
+
}
|
|
800
|
+
})];
|
|
801
|
+
}
|
|
802
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
803
|
+
id: "foo"
|
|
804
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
805
|
+
id: "partial"
|
|
806
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
807
|
+
args: []
|
|
808
|
+
})];
|
|
809
|
+
}
|
|
810
|
+
}))).toBe(d`
|
|
811
|
+
z.z1
|
|
812
|
+
?.object({
|
|
813
|
+
x: 1,
|
|
814
|
+
})
|
|
815
|
+
.foo.partial()
|
|
816
|
+
`);
|
|
817
|
+
});
|
|
818
|
+
it("handles the first part being a call", () => {
|
|
819
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
820
|
+
get children() {
|
|
821
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
822
|
+
id: "z"
|
|
823
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
824
|
+
args: true
|
|
825
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
826
|
+
id: "z1",
|
|
827
|
+
nullish: true
|
|
828
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
829
|
+
id: "object"
|
|
830
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
831
|
+
get args() {
|
|
832
|
+
return [_$createComponent(ObjectExpression, {
|
|
833
|
+
jsValue: {
|
|
834
|
+
x: 1
|
|
835
|
+
}
|
|
836
|
+
})];
|
|
837
|
+
}
|
|
838
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
839
|
+
id: "foo"
|
|
840
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
841
|
+
id: "partial"
|
|
842
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
843
|
+
args: []
|
|
844
|
+
})];
|
|
845
|
+
}
|
|
846
|
+
}))).toBe(d`
|
|
847
|
+
z()
|
|
848
|
+
.z1?.object({
|
|
849
|
+
x: 1,
|
|
850
|
+
})
|
|
851
|
+
.foo.partial()
|
|
852
|
+
`);
|
|
853
|
+
});
|
|
574
854
|
});
|
|
575
855
|
});
|