@alloy-js/typescript 0.9.0 → 0.11.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 +22 -0
- package/dist/src/builtins/node.js +1 -2
- package/dist/src/components/ArrayExpression.js +2 -5
- package/dist/src/components/ArrowFunction.d.ts +40 -0
- package/dist/src/components/ArrowFunction.d.ts.map +1 -0
- package/dist/src/components/ArrowFunction.js +67 -0
- package/dist/src/components/BarrelFile.js +1 -2
- package/dist/src/components/BlockScope.js +2 -4
- package/dist/src/components/CallSignature.js +2 -4
- package/dist/src/components/ClassDeclaration.js +2 -6
- package/dist/src/components/CommaList.js +2 -5
- package/dist/src/components/Declaration.js +3 -5
- package/dist/src/components/EnumDeclaration.js +2 -5
- package/dist/src/components/EnumMember.js +2 -4
- package/dist/src/components/ExportStatement.js +1 -2
- package/dist/src/components/FunctionBase.d.ts +60 -0
- package/dist/src/components/FunctionBase.d.ts.map +1 -0
- package/dist/src/components/FunctionBase.js +207 -0
- package/dist/src/components/FunctionCallExpression.js +2 -5
- package/dist/src/components/FunctionDeclaration.d.ts +3 -18
- package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
- package/dist/src/components/FunctionDeclaration.js +9 -157
- package/dist/src/components/FunctionExpression.d.ts +39 -0
- package/dist/src/components/FunctionExpression.d.ts.map +1 -0
- package/dist/src/components/FunctionExpression.js +66 -0
- package/dist/src/components/FunctionType.d.ts +39 -0
- package/dist/src/components/FunctionType.d.ts.map +1 -0
- package/dist/src/components/FunctionType.js +73 -0
- package/dist/src/components/IfStatement.js +2 -4
- package/dist/src/components/ImportStatement.js +1 -2
- package/dist/src/components/Interface.d.ts.map +1 -1
- package/dist/src/components/Interface.js +32 -11
- package/dist/src/components/InterfaceMethod.d.ts +43 -0
- package/dist/src/components/InterfaceMethod.d.ts.map +1 -0
- package/dist/src/components/InterfaceMethod.js +87 -0
- package/dist/src/components/JSDoc.js +1 -2
- package/dist/src/components/JSDocComment.js +2 -4
- package/dist/src/components/JSDocExample.js +2 -5
- package/dist/src/components/JSDocParam.js +2 -5
- package/dist/src/components/MemberChainExpression.js +2 -4
- package/dist/src/components/MemberDeclaration.d.ts +30 -0
- package/dist/src/components/MemberDeclaration.d.ts.map +1 -0
- package/dist/src/components/MemberDeclaration.js +29 -0
- package/dist/src/components/MemberExpression.d.ts +1 -2
- package/dist/src/components/MemberExpression.d.ts.map +1 -1
- package/dist/src/components/MemberExpression.js +2 -5
- package/dist/src/components/ObjectExpression.js +2 -5
- package/dist/src/components/PackageDirectory.js +2 -5
- package/dist/src/components/PackageJson.js +1 -2
- package/dist/src/components/PropertyName.js +1 -2
- package/dist/src/components/Reference.js +1 -2
- package/dist/src/components/SingleLineCommentBlock.js +2 -4
- package/dist/src/components/SourceFile.js +2 -4
- package/dist/src/components/SwitchStatement.js +2 -5
- package/dist/src/components/TsConfigJson.js +1 -2
- package/dist/src/components/TypeDeclaration.js +2 -6
- package/dist/src/components/ValueExpression.js +1 -2
- package/dist/src/components/VarDeclaration.js +2 -5
- package/dist/src/components/index.d.ts +7 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +7 -1
- package/dist/src/components/stc/index.d.ts +8 -0
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +9 -2
- package/dist/src/context/private-scope.js +1 -2
- package/dist/src/create-package.js +1 -2
- package/dist/src/index.js +1 -2
- package/dist/src/name-conflict-resolver.js +1 -2
- package/dist/src/name-policy.js +1 -2
- package/dist/src/parameter-descriptor.d.ts +27 -17
- package/dist/src/parameter-descriptor.d.ts.map +1 -1
- package/dist/src/source-directory-data.js +1 -2
- package/dist/src/symbols/index.js +1 -2
- package/dist/src/symbols/reference.js +2 -3
- package/dist/src/symbols/scopes.js +1 -2
- package/dist/src/symbols/ts-member-scope.js +1 -2
- package/dist/src/symbols/ts-module-scope.js +1 -2
- package/dist/src/symbols/ts-output-symbol.js +1 -2
- package/dist/src/symbols/ts-package-scope.js +1 -2
- package/dist/src/utils.js +1 -2
- package/dist/test/array.test.js +105 -0
- package/dist/test/arrow-function.test.d.ts +2 -0
- package/dist/test/arrow-function.test.d.ts.map +1 -0
- package/dist/test/arrow-function.test.js +194 -0
- package/dist/test/barrel.test.js +53 -0
- package/dist/test/basic.test.js +91 -0
- package/dist/test/block-scope.test.js +30 -0
- package/dist/test/class.test.js +438 -0
- package/dist/test/enum-jsdoc.test.js +88 -0
- package/dist/test/enum.test.js +247 -0
- package/dist/test/externals.test.js +145 -0
- package/dist/test/function-call-expression.test.js +87 -0
- package/dist/test/function-declaration-jsdoc.test.js +91 -0
- package/dist/test/function-declaration.test.js +228 -0
- package/dist/test/function-expression.test.d.ts +2 -0
- package/dist/test/function-expression.test.d.ts.map +1 -0
- package/dist/test/function-expression.test.js +194 -0
- package/dist/test/function-type.test.d.ts +2 -0
- package/dist/test/function-type.test.d.ts.map +1 -0
- package/dist/test/function-type.test.js +122 -0
- package/dist/test/if-statement.test.js +25 -0
- package/dist/test/imports.test.js +434 -0
- package/dist/test/interface-jsdoc.test.js +178 -0
- package/dist/test/interface.test.d.ts.map +1 -1
- package/dist/test/interface.test.js +490 -0
- package/dist/test/jscomment.test.js +70 -0
- package/dist/test/jsdoc.test.js +134 -0
- package/dist/test/jsdocparam.test.js +164 -0
- package/dist/test/member-chain.test.js +141 -0
- package/dist/test/member-expression.test.js +359 -0
- package/dist/test/name-conflict.test.js +108 -0
- package/dist/test/name-policy.test.js +122 -0
- package/dist/test/package-external.test.js +196 -0
- package/dist/test/package-json.test.js +93 -0
- package/dist/test/package.test.js +136 -0
- package/dist/test/reference.test.js +81 -0
- package/dist/test/source-file.test.js +36 -0
- package/dist/test/switch-statement.test.js +61 -0
- package/dist/test/utils.js +45 -0
- package/dist/test/value-expression.test.js +30 -0
- package/dist/test/values.test.js +351 -0
- package/dist/test/var-declaration-jsdoc.test.js +50 -0
- package/dist/test/var-declaration.test.js +63 -0
- package/dist/test/vitest.d.js +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +12 -17
- package/src/components/ArrowFunction.tsx +87 -0
- package/src/components/Declaration.tsx +1 -1
- package/src/components/FunctionBase.tsx +271 -0
- package/src/components/FunctionDeclaration.tsx +13 -195
- package/src/components/FunctionExpression.tsx +86 -0
- package/src/components/FunctionType.tsx +88 -0
- package/src/components/Interface.tsx +33 -5
- package/src/components/InterfaceMethod.tsx +96 -0
- package/src/components/MemberDeclaration.tsx +70 -0
- package/src/components/MemberExpression.tsx +1 -1
- package/src/components/index.ts +11 -0
- package/src/components/stc/index.ts +8 -0
- package/src/parameter-descriptor.ts +29 -18
- package/src/symbols/reference.ts +4 -1
- package/temp/api.json +3099 -863
- package/test/arrow-function.test.tsx +188 -0
- package/test/class.test.tsx +1 -2
- package/test/function-call-expression.test.tsx +1 -2
- package/test/function-declaration.test.tsx +1 -2
- package/test/function-expression.test.tsx +195 -0
- package/test/function-type.test.tsx +118 -0
- package/test/interface.test.tsx +256 -6
- package/tsdoc-metadata.json +1 -1
- package/babel.config.cjs +0 -4
- package/dist/src/builtins/index.js.map +0 -1
- package/dist/src/builtins/node.js.map +0 -1
- package/dist/src/components/ArrayExpression.js.map +0 -1
- package/dist/src/components/BarrelFile.js.map +0 -1
- package/dist/src/components/BlockScope.js.map +0 -1
- package/dist/src/components/CallSignature.js.map +0 -1
- package/dist/src/components/ClassDeclaration.js.map +0 -1
- package/dist/src/components/CommaList.js.map +0 -1
- package/dist/src/components/Declaration.js.map +0 -1
- package/dist/src/components/EnumDeclaration.js.map +0 -1
- package/dist/src/components/EnumMember.js.map +0 -1
- package/dist/src/components/ExportStatement.js.map +0 -1
- package/dist/src/components/FunctionCallExpression.js.map +0 -1
- package/dist/src/components/FunctionDeclaration.js.map +0 -1
- package/dist/src/components/IfStatement.js.map +0 -1
- package/dist/src/components/ImportStatement.js.map +0 -1
- package/dist/src/components/Interface.js.map +0 -1
- package/dist/src/components/JSDoc.js.map +0 -1
- package/dist/src/components/JSDocComment.js.map +0 -1
- package/dist/src/components/JSDocExample.js.map +0 -1
- package/dist/src/components/JSDocParam.js.map +0 -1
- package/dist/src/components/MemberChainExpression.js.map +0 -1
- package/dist/src/components/MemberExpression.js.map +0 -1
- package/dist/src/components/ObjectExpression.js.map +0 -1
- package/dist/src/components/PackageDirectory.js.map +0 -1
- package/dist/src/components/PackageJson.js.map +0 -1
- package/dist/src/components/PropertyName.js.map +0 -1
- package/dist/src/components/Reference.js.map +0 -1
- package/dist/src/components/SingleLineCommentBlock.js.map +0 -1
- package/dist/src/components/SourceFile.js.map +0 -1
- package/dist/src/components/SwitchStatement.js.map +0 -1
- package/dist/src/components/TsConfigJson.js.map +0 -1
- package/dist/src/components/TypeDeclaration.js.map +0 -1
- package/dist/src/components/ValueExpression.js.map +0 -1
- package/dist/src/components/VarDeclaration.js.map +0 -1
- package/dist/src/components/index.js.map +0 -1
- package/dist/src/components/stc/index.js.map +0 -1
- package/dist/src/context/index.js.map +0 -1
- package/dist/src/context/private-scope.js.map +0 -1
- package/dist/src/create-package.js.map +0 -1
- package/dist/src/index.js.map +0 -1
- package/dist/src/name-conflict-resolver.js.map +0 -1
- package/dist/src/name-policy.js.map +0 -1
- package/dist/src/parameter-descriptor.js.map +0 -1
- package/dist/src/source-directory-data.js.map +0 -1
- package/dist/src/symbols/index.js.map +0 -1
- package/dist/src/symbols/reference.js.map +0 -1
- package/dist/src/symbols/scopes.js.map +0 -1
- package/dist/src/symbols/ts-member-scope.js.map +0 -1
- package/dist/src/symbols/ts-module-scope.js.map +0 -1
- package/dist/src/symbols/ts-output-symbol.js.map +0 -1
- package/dist/src/symbols/ts-package-scope.js.map +0 -1
- package/dist/src/utils.js.map +0 -1
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { d } from "@alloy-js/core/testing";
|
|
3
|
+
import { expect, it } from "vitest";
|
|
4
|
+
import { JSDocParam } from "../src/components/JSDocParam.js";
|
|
5
|
+
import { JSDoc } from "../src/index.js";
|
|
6
|
+
it("name only", () => {
|
|
7
|
+
const template = _$createComponent(JSDoc, {
|
|
8
|
+
get children() {
|
|
9
|
+
return _$createComponent(JSDocParam, {
|
|
10
|
+
name: "somebody"
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
expect(template).toRenderTo(d`
|
|
15
|
+
/**
|
|
16
|
+
* @param somebody
|
|
17
|
+
**/
|
|
18
|
+
`);
|
|
19
|
+
});
|
|
20
|
+
it("name and type", () => {
|
|
21
|
+
const template = _$createComponent(JSDoc, {
|
|
22
|
+
get children() {
|
|
23
|
+
return _$createComponent(JSDocParam, {
|
|
24
|
+
name: "somebody",
|
|
25
|
+
type: "string"
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
expect(template).toRenderTo(d`
|
|
30
|
+
/**
|
|
31
|
+
* @param {string} somebody
|
|
32
|
+
**/
|
|
33
|
+
`);
|
|
34
|
+
});
|
|
35
|
+
it("name, type and description", () => {
|
|
36
|
+
const template = _$createComponent(JSDoc, {
|
|
37
|
+
get children() {
|
|
38
|
+
return _$createComponent(JSDocParam, {
|
|
39
|
+
name: "somebody",
|
|
40
|
+
type: "string",
|
|
41
|
+
children: "Somebody's name."
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
expect(template).toRenderTo(d`
|
|
46
|
+
/**
|
|
47
|
+
* @param {string} somebody Somebody's name.
|
|
48
|
+
**/
|
|
49
|
+
`);
|
|
50
|
+
});
|
|
51
|
+
it("name, type and description with hyphen", () => {
|
|
52
|
+
const template = _$createComponent(JSDoc, {
|
|
53
|
+
get children() {
|
|
54
|
+
return _$createComponent(JSDocParam, {
|
|
55
|
+
name: "somebody",
|
|
56
|
+
type: "string",
|
|
57
|
+
hyphen: true,
|
|
58
|
+
children: "Somebody's name."
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
expect(template).toRenderTo(d`
|
|
63
|
+
/**
|
|
64
|
+
* @param {string} somebody - Somebody's name.
|
|
65
|
+
**/
|
|
66
|
+
`);
|
|
67
|
+
});
|
|
68
|
+
it("name, type, description, hyphen and optional", () => {
|
|
69
|
+
const template = _$createComponent(JSDoc, {
|
|
70
|
+
get children() {
|
|
71
|
+
return _$createComponent(JSDocParam, {
|
|
72
|
+
name: "somebody",
|
|
73
|
+
type: "string",
|
|
74
|
+
optional: true,
|
|
75
|
+
hyphen: true,
|
|
76
|
+
children: "Somebody's name."
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
expect(template).toRenderTo(d`
|
|
81
|
+
/**
|
|
82
|
+
* @param {string} [somebody] - Somebody's name.
|
|
83
|
+
**/
|
|
84
|
+
`);
|
|
85
|
+
});
|
|
86
|
+
it("name, type, description, hyphen and optional with default value", () => {
|
|
87
|
+
const template = _$createComponent(JSDoc, {
|
|
88
|
+
get children() {
|
|
89
|
+
return _$createComponent(JSDocParam, {
|
|
90
|
+
name: "somebody",
|
|
91
|
+
type: "string",
|
|
92
|
+
optional: true,
|
|
93
|
+
defaultValue: "John Doe",
|
|
94
|
+
hyphen: true,
|
|
95
|
+
children: "Somebody's name."
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
expect(template).toRenderTo(d`
|
|
100
|
+
/**
|
|
101
|
+
* @param {string} [somebody=John Doe] - Somebody's name.
|
|
102
|
+
**/
|
|
103
|
+
`);
|
|
104
|
+
});
|
|
105
|
+
it("name, type, description, hyphen and optional with default value with a very lon description", () => {
|
|
106
|
+
const template = _$createComponent(JSDoc, {
|
|
107
|
+
get children() {
|
|
108
|
+
return [_$createComponent(JSDocParam, {
|
|
109
|
+
name: "somebody",
|
|
110
|
+
type: "string",
|
|
111
|
+
optional: true,
|
|
112
|
+
defaultValue: "John Doe",
|
|
113
|
+
hyphen: true,
|
|
114
|
+
children: "Somebody's name. This can be any string representing a person, whether it's a first name, full name, nickname, or even a codename (e.g., \"Agent X\"). It's used primarily for display purposes, logging, or greeting messages and is not required to be unique or validated unless specified by the caller."
|
|
115
|
+
}), _$createComponent(JSDocParam, {
|
|
116
|
+
name: "somebody2",
|
|
117
|
+
type: "string",
|
|
118
|
+
hyphen: true,
|
|
119
|
+
children: "Somebody's name. This can be any string representing a person, whether it's a first name, full name, nickname, or even a codename (e.g., \"Agent X\"). It's used primarily for display purposes, logging, or greeting messages and is not required to be unique or validated unless specified by the caller."
|
|
120
|
+
})];
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
expect(template).toRenderTo(d`
|
|
124
|
+
/**
|
|
125
|
+
* @param {string} [somebody=John Doe] - Somebody's name. This can be any string
|
|
126
|
+
* representing a person, whether it's a first name, full name, nickname, or
|
|
127
|
+
* even a codename (e.g., "Agent X"). It's used primarily for display
|
|
128
|
+
* purposes, logging, or greeting messages and is not required to be unique or
|
|
129
|
+
* validated unless specified by the caller.
|
|
130
|
+
*
|
|
131
|
+
* @param {string} somebody2 - Somebody's name. This can be any string
|
|
132
|
+
* representing a person, whether it's a first name, full name, nickname, or
|
|
133
|
+
* even a codename (e.g., "Agent X"). It's used primarily for display
|
|
134
|
+
* purposes, logging, or greeting messages and is not required to be unique or
|
|
135
|
+
* validated unless specified by the caller.
|
|
136
|
+
**/
|
|
137
|
+
`, {
|
|
138
|
+
printWidth: 80
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
it("name, type, description, hyphen and optional with default value with a description containing a linebreak", () => {
|
|
142
|
+
const template = _$createComponent(JSDoc, {
|
|
143
|
+
get children() {
|
|
144
|
+
return _$createComponent(JSDocParam, {
|
|
145
|
+
name: "somebody",
|
|
146
|
+
type: "string",
|
|
147
|
+
optional: true,
|
|
148
|
+
defaultValue: "John Doe",
|
|
149
|
+
hyphen: true,
|
|
150
|
+
get children() {
|
|
151
|
+
return ["Somebody's name. This is one line", _$createIntrinsic("hbr", {}), "This is another line"];
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
expect(template).toRenderTo(d`
|
|
157
|
+
/**
|
|
158
|
+
* @param {string} [somebody=John Doe] - Somebody's name. This is one line
|
|
159
|
+
* This is another line
|
|
160
|
+
**/
|
|
161
|
+
`, {
|
|
162
|
+
printWidth: 80
|
|
163
|
+
});
|
|
164
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { refkey, StatementList } from "@alloy-js/core";
|
|
3
|
+
import { d } from "@alloy-js/core/testing";
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
import { MemberExpression } from "../src/components/MemberExpression.js";
|
|
6
|
+
import { ObjectExpression } from "../src/components/stc/index.js";
|
|
7
|
+
import { VarDeclaration } from "../src/components/VarDeclaration.js";
|
|
8
|
+
import { toSourceText } from "./utils.js";
|
|
9
|
+
it("renders basic member expression with dot notation", () => {
|
|
10
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
11
|
+
get children() {
|
|
12
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
13
|
+
id: "obj"
|
|
14
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
15
|
+
id: "property"
|
|
16
|
+
})];
|
|
17
|
+
}
|
|
18
|
+
}))).toBe(d`
|
|
19
|
+
obj.property
|
|
20
|
+
`);
|
|
21
|
+
});
|
|
22
|
+
it("renders member expression with bracket notation for invalid identifiers", () => {
|
|
23
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
24
|
+
get children() {
|
|
25
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
26
|
+
id: "obj"
|
|
27
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
28
|
+
id: "property-name"
|
|
29
|
+
})];
|
|
30
|
+
}
|
|
31
|
+
}))).toBe(d`
|
|
32
|
+
obj["property-name"]
|
|
33
|
+
`);
|
|
34
|
+
});
|
|
35
|
+
it("handles nullish chaining", () => {
|
|
36
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
37
|
+
get children() {
|
|
38
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
39
|
+
id: "obj",
|
|
40
|
+
nullish: true
|
|
41
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
42
|
+
id: "property"
|
|
43
|
+
})];
|
|
44
|
+
}
|
|
45
|
+
}))).toBe(d`
|
|
46
|
+
obj?.property
|
|
47
|
+
`);
|
|
48
|
+
});
|
|
49
|
+
it("supports multiple levels of nesting", () => {
|
|
50
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
51
|
+
get children() {
|
|
52
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
53
|
+
id: "a"
|
|
54
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
55
|
+
id: "b"
|
|
56
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
57
|
+
id: "c"
|
|
58
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
59
|
+
id: "d"
|
|
60
|
+
})];
|
|
61
|
+
}
|
|
62
|
+
}))).toBe(d`
|
|
63
|
+
a.b.c.d
|
|
64
|
+
`);
|
|
65
|
+
});
|
|
66
|
+
it("ignores non-part children", () => {
|
|
67
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
68
|
+
get children() {
|
|
69
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
70
|
+
id: "obj"
|
|
71
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
72
|
+
id: "property"
|
|
73
|
+
})];
|
|
74
|
+
}
|
|
75
|
+
}))).toBe(d`
|
|
76
|
+
obj.property
|
|
77
|
+
`);
|
|
78
|
+
});
|
|
79
|
+
it("flattens nested member expressions", () => {
|
|
80
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
81
|
+
get children() {
|
|
82
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
83
|
+
id: "outer"
|
|
84
|
+
}), _$createComponent(MemberExpression, {
|
|
85
|
+
get children() {
|
|
86
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
87
|
+
id: "inner"
|
|
88
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
89
|
+
id: "prop"
|
|
90
|
+
})];
|
|
91
|
+
}
|
|
92
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
93
|
+
id: "last"
|
|
94
|
+
})];
|
|
95
|
+
}
|
|
96
|
+
}))).toBe(d`
|
|
97
|
+
outer.inner.prop.last
|
|
98
|
+
`);
|
|
99
|
+
});
|
|
100
|
+
it("handles a mix of dot and bracket notation", () => {
|
|
101
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
102
|
+
get children() {
|
|
103
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
104
|
+
id: "obj"
|
|
105
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
106
|
+
id: "normalProp"
|
|
107
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
108
|
+
id: "special-prop"
|
|
109
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
110
|
+
id: "123"
|
|
111
|
+
})];
|
|
112
|
+
}
|
|
113
|
+
}))).toBe(d`
|
|
114
|
+
obj.normalProp["special-prop"]["123"]
|
|
115
|
+
`);
|
|
116
|
+
});
|
|
117
|
+
it("handles nullish chaining at multiple levels", () => {
|
|
118
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
119
|
+
get children() {
|
|
120
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
121
|
+
id: "a"
|
|
122
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
123
|
+
id: "b",
|
|
124
|
+
nullish: true
|
|
125
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
126
|
+
id: "c"
|
|
127
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
128
|
+
id: "d",
|
|
129
|
+
nullish: true
|
|
130
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
131
|
+
id: "e"
|
|
132
|
+
})];
|
|
133
|
+
}
|
|
134
|
+
}))).toBe(d`
|
|
135
|
+
a.b?.c.d?.e
|
|
136
|
+
`);
|
|
137
|
+
});
|
|
138
|
+
describe("with refkeys", () => {
|
|
139
|
+
it("handles symbols correctly", () => {
|
|
140
|
+
const rk1 = refkey();
|
|
141
|
+
const rk2 = refkey();
|
|
142
|
+
expect(toSourceText(_$createComponent(StatementList, {
|
|
143
|
+
get children() {
|
|
144
|
+
return [_$createComponent(VarDeclaration, {
|
|
145
|
+
name: "test1",
|
|
146
|
+
refkey: rk1,
|
|
147
|
+
initializer: 1
|
|
148
|
+
}), _$createComponent(VarDeclaration, {
|
|
149
|
+
name: "test1",
|
|
150
|
+
refkey: rk2,
|
|
151
|
+
initializer: 2
|
|
152
|
+
}), _$createComponent(MemberExpression, {
|
|
153
|
+
get children() {
|
|
154
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
155
|
+
refkey: rk1
|
|
156
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
157
|
+
refkey: rk2
|
|
158
|
+
})];
|
|
159
|
+
}
|
|
160
|
+
})];
|
|
161
|
+
}
|
|
162
|
+
}))).toBe(d`
|
|
163
|
+
const test1 = 1;
|
|
164
|
+
const test1_2 = 2;
|
|
165
|
+
test1.test1_2;
|
|
166
|
+
`);
|
|
167
|
+
});
|
|
168
|
+
it("handles late resolved refkeys correctly", () => {
|
|
169
|
+
const rk1 = refkey();
|
|
170
|
+
const rk2 = refkey();
|
|
171
|
+
expect(toSourceText(_$createComponent(StatementList, {
|
|
172
|
+
get children() {
|
|
173
|
+
return [_$createComponent(MemberExpression, {
|
|
174
|
+
get children() {
|
|
175
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
176
|
+
refkey: rk1
|
|
177
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
178
|
+
refkey: rk2
|
|
179
|
+
})];
|
|
180
|
+
}
|
|
181
|
+
}), _$createComponent(VarDeclaration, {
|
|
182
|
+
name: "test1",
|
|
183
|
+
refkey: rk1,
|
|
184
|
+
initializer: 1
|
|
185
|
+
}), _$createComponent(VarDeclaration, {
|
|
186
|
+
name: "test1",
|
|
187
|
+
refkey: rk2,
|
|
188
|
+
initializer: 2
|
|
189
|
+
})];
|
|
190
|
+
}
|
|
191
|
+
}))).toBe(d`
|
|
192
|
+
test1.test1_2;
|
|
193
|
+
const test1 = 1;
|
|
194
|
+
const test1_2 = 2;
|
|
195
|
+
`);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
describe("with function calls", () => {
|
|
199
|
+
it("handles simple function calls correctly", () => {
|
|
200
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
201
|
+
get children() {
|
|
202
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
203
|
+
id: "myFunction"
|
|
204
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
205
|
+
args: [1, 2]
|
|
206
|
+
})];
|
|
207
|
+
}
|
|
208
|
+
}))).toBe(d`
|
|
209
|
+
myFunction(1, 2)
|
|
210
|
+
`);
|
|
211
|
+
});
|
|
212
|
+
it("handles nullish function calls correctly", () => {
|
|
213
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
214
|
+
get children() {
|
|
215
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
216
|
+
id: "method1",
|
|
217
|
+
nullish: true
|
|
218
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
219
|
+
nullish: true,
|
|
220
|
+
args: [1, 2]
|
|
221
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
222
|
+
nullish: true,
|
|
223
|
+
args: []
|
|
224
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
225
|
+
id: "method2",
|
|
226
|
+
nullish: true
|
|
227
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
228
|
+
args: []
|
|
229
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
230
|
+
id: "prop"
|
|
231
|
+
})];
|
|
232
|
+
}
|
|
233
|
+
}))).toBe(d`
|
|
234
|
+
method1?.(1, 2)?.()?.method2?.().prop
|
|
235
|
+
`);
|
|
236
|
+
});
|
|
237
|
+
it("handles function calls returning nullish correctly", () => {
|
|
238
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
239
|
+
get children() {
|
|
240
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
241
|
+
id: "myFunction"
|
|
242
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
243
|
+
args: [1, 2],
|
|
244
|
+
nullish: true
|
|
245
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
246
|
+
id: "prop"
|
|
247
|
+
})];
|
|
248
|
+
}
|
|
249
|
+
}))).toBe(d`
|
|
250
|
+
myFunction(1, 2)?.prop
|
|
251
|
+
`);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
describe("formatting", () => {
|
|
255
|
+
describe("simple chains", () => {
|
|
256
|
+
it("just dots", () => {
|
|
257
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
258
|
+
get children() {
|
|
259
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
260
|
+
id: "four"
|
|
261
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
262
|
+
id: "four"
|
|
263
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
264
|
+
id: "four"
|
|
265
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
266
|
+
id: "four"
|
|
267
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
268
|
+
id: "four"
|
|
269
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
270
|
+
id: "four"
|
|
271
|
+
})];
|
|
272
|
+
}
|
|
273
|
+
}), {
|
|
274
|
+
printWidth: 12
|
|
275
|
+
})).toBe(d`
|
|
276
|
+
four.four
|
|
277
|
+
.four.four
|
|
278
|
+
.four.four
|
|
279
|
+
`);
|
|
280
|
+
});
|
|
281
|
+
it("bracket breaks", () => {
|
|
282
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
283
|
+
get children() {
|
|
284
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
285
|
+
id: "obj"
|
|
286
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
287
|
+
id: "property-name"
|
|
288
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
289
|
+
id: "prop"
|
|
290
|
+
})];
|
|
291
|
+
}
|
|
292
|
+
}), {
|
|
293
|
+
printWidth: 12
|
|
294
|
+
})).toBe(d`
|
|
295
|
+
obj[
|
|
296
|
+
"property-name"
|
|
297
|
+
].prop
|
|
298
|
+
`);
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
describe("call chains", () => {
|
|
302
|
+
it("handles single calls", () => {
|
|
303
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
304
|
+
get children() {
|
|
305
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
306
|
+
id: "z"
|
|
307
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
308
|
+
id: "object"
|
|
309
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
310
|
+
get args() {
|
|
311
|
+
return [_$createComponent(ObjectExpression, {
|
|
312
|
+
jsValue: {
|
|
313
|
+
x: 1
|
|
314
|
+
}
|
|
315
|
+
})];
|
|
316
|
+
}
|
|
317
|
+
})];
|
|
318
|
+
}
|
|
319
|
+
}), {
|
|
320
|
+
printWidth: 12
|
|
321
|
+
})).toBe(d`
|
|
322
|
+
z.object({
|
|
323
|
+
x: 1,
|
|
324
|
+
})
|
|
325
|
+
`);
|
|
326
|
+
});
|
|
327
|
+
it.skip("handles multiple calls", () => {
|
|
328
|
+
expect(toSourceText(_$createComponent(MemberExpression, {
|
|
329
|
+
get children() {
|
|
330
|
+
return [_$createComponent(MemberExpression.Part, {
|
|
331
|
+
id: "z"
|
|
332
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
333
|
+
id: "object"
|
|
334
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
335
|
+
get args() {
|
|
336
|
+
return [_$createComponent(ObjectExpression, {
|
|
337
|
+
jsValue: {
|
|
338
|
+
x: 1
|
|
339
|
+
}
|
|
340
|
+
})];
|
|
341
|
+
}
|
|
342
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
343
|
+
id: "partial"
|
|
344
|
+
}), _$createComponent(MemberExpression.Part, {
|
|
345
|
+
args: []
|
|
346
|
+
})];
|
|
347
|
+
}
|
|
348
|
+
}), {
|
|
349
|
+
printWidth: 12
|
|
350
|
+
})).toBe(d`
|
|
351
|
+
z
|
|
352
|
+
.object({
|
|
353
|
+
x: 1,
|
|
354
|
+
})
|
|
355
|
+
.partial()
|
|
356
|
+
`);
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
});
|