@alloy-js/typescript 0.7.0 → 0.9.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 +35 -0
- package/dist/src/components/ArrayExpression.d.ts.map +1 -1
- package/dist/src/components/ArrayExpression.js +9 -3
- package/dist/src/components/ArrayExpression.js.map +1 -1
- package/dist/src/components/BlockScope.d.ts +13 -0
- package/dist/src/components/BlockScope.d.ts.map +1 -0
- package/dist/src/components/BlockScope.js +20 -0
- package/dist/src/components/BlockScope.js.map +1 -0
- package/dist/src/components/CallSignature.d.ts +38 -0
- package/dist/src/components/CallSignature.d.ts.map +1 -0
- package/dist/src/components/CallSignature.js +26 -0
- package/dist/src/components/CallSignature.js.map +1 -0
- package/dist/src/components/ClassDeclaration.d.ts +3 -4
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
- package/dist/src/components/ClassDeclaration.js +99 -41
- package/dist/src/components/ClassDeclaration.js.map +1 -1
- package/dist/src/components/Declaration.d.ts +21 -8
- package/dist/src/components/Declaration.d.ts.map +1 -1
- package/dist/src/components/Declaration.js +39 -21
- package/dist/src/components/Declaration.js.map +1 -1
- package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
- package/dist/src/components/EnumDeclaration.js +18 -6
- package/dist/src/components/EnumDeclaration.js.map +1 -1
- package/dist/src/components/EnumMember.d.ts +8 -0
- package/dist/src/components/EnumMember.d.ts.map +1 -1
- package/dist/src/components/EnumMember.js +27 -5
- package/dist/src/components/EnumMember.js.map +1 -1
- package/dist/src/components/FunctionCallExpression.js +1 -1
- package/dist/src/components/FunctionCallExpression.js.map +1 -1
- package/dist/src/components/FunctionDeclaration.d.ts +21 -13
- package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
- package/dist/src/components/FunctionDeclaration.js +79 -45
- package/dist/src/components/FunctionDeclaration.js.map +1 -1
- package/dist/src/components/IfStatement.d.ts +25 -0
- package/dist/src/components/IfStatement.d.ts.map +1 -0
- package/dist/src/components/IfStatement.js +34 -0
- package/dist/src/components/IfStatement.js.map +1 -0
- package/dist/src/components/Interface.d.ts +10 -2
- package/dist/src/components/Interface.d.ts.map +1 -1
- package/dist/src/components/Interface.js +66 -6
- package/dist/src/components/Interface.js.map +1 -1
- package/dist/src/components/JSDoc.d.ts.map +1 -1
- package/dist/src/components/JSDoc.js +1 -5
- package/dist/src/components/JSDoc.js.map +1 -1
- package/dist/src/components/JSDocParam.d.ts +22 -0
- package/dist/src/components/JSDocParam.d.ts.map +1 -0
- package/dist/src/components/JSDocParam.js +121 -0
- package/dist/src/components/JSDocParam.js.map +1 -0
- package/dist/src/components/MemberExpression.d.ts +62 -0
- package/dist/src/components/MemberExpression.d.ts.map +1 -0
- package/dist/src/components/MemberExpression.js +179 -0
- package/dist/src/components/MemberExpression.js.map +1 -0
- package/dist/src/components/ObjectExpression.d.ts +1 -1
- package/dist/src/components/ObjectExpression.d.ts.map +1 -1
- package/dist/src/components/ObjectExpression.js +9 -2
- package/dist/src/components/ObjectExpression.js.map +1 -1
- package/dist/src/components/PropertyName.d.ts +19 -0
- package/dist/src/components/PropertyName.d.ts.map +1 -0
- package/dist/src/components/PropertyName.js +36 -0
- package/dist/src/components/PropertyName.js.map +1 -0
- package/dist/src/components/SingleLineCommentBlock.d.ts +10 -0
- package/dist/src/components/SingleLineCommentBlock.d.ts.map +1 -0
- package/dist/src/components/SingleLineCommentBlock.js +20 -0
- package/dist/src/components/SingleLineCommentBlock.js.map +1 -0
- package/dist/src/components/SourceFile.d.ts +6 -0
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +31 -0
- package/dist/src/components/SourceFile.js.map +1 -1
- package/dist/src/components/SwitchStatement.d.ts +31 -0
- package/dist/src/components/SwitchStatement.d.ts.map +1 -0
- package/dist/src/components/SwitchStatement.js +48 -0
- package/dist/src/components/SwitchStatement.js.map +1 -0
- package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
- package/dist/src/components/TypeDeclaration.js +16 -3
- package/dist/src/components/TypeDeclaration.js.map +1 -1
- package/dist/src/components/VarDeclaration.d.ts +1 -1
- package/dist/src/components/VarDeclaration.d.ts.map +1 -1
- package/dist/src/components/VarDeclaration.js +19 -6
- package/dist/src/components/VarDeclaration.js.map +1 -1
- 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 -0
- package/dist/src/components/index.js.map +1 -1
- package/dist/src/components/stc/index.d.ts +35 -112
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +8 -1
- package/dist/src/components/stc/index.js.map +1 -1
- package/dist/src/context/index.d.ts +2 -0
- package/dist/src/context/index.d.ts.map +1 -0
- package/dist/src/context/index.js +1 -0
- package/dist/src/context/index.js.map +1 -0
- package/dist/src/context/private-scope.d.ts +18 -0
- package/dist/src/context/private-scope.d.ts.map +1 -0
- package/dist/src/context/private-scope.js +9 -0
- package/dist/src/context/private-scope.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/parameter-descriptor.d.ts +60 -0
- package/dist/src/parameter-descriptor.d.ts.map +1 -0
- package/dist/src/parameter-descriptor.js +1 -0
- package/dist/src/parameter-descriptor.js.map +1 -0
- package/dist/src/symbols/reference.d.ts.map +1 -1
- package/dist/src/symbols/reference.js +50 -12
- package/dist/src/symbols/reference.js.map +1 -1
- package/dist/src/symbols/ts-member-scope.d.ts +1 -1
- package/dist/src/symbols/ts-member-scope.d.ts.map +1 -1
- package/dist/src/symbols/ts-member-scope.js +2 -0
- package/dist/src/symbols/ts-member-scope.js.map +1 -1
- package/dist/src/symbols/ts-output-symbol.d.ts +7 -4
- package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
- package/dist/src/symbols/ts-output-symbol.js +11 -2
- package/dist/src/symbols/ts-output-symbol.js.map +1 -1
- package/dist/src/utils.d.ts +7 -0
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +17 -0
- package/dist/src/utils.js.map +1 -1
- package/dist/test/block-scope.test.d.ts +2 -0
- package/dist/test/block-scope.test.d.ts.map +1 -0
- package/dist/test/enum-jsdoc.test.d.ts +2 -0
- package/dist/test/enum-jsdoc.test.d.ts.map +1 -0
- package/dist/test/function-declaration-jsdoc.test.d.ts +2 -0
- package/dist/test/function-declaration-jsdoc.test.d.ts.map +1 -0
- package/dist/test/if-statement.test.d.ts +2 -0
- package/dist/test/if-statement.test.d.ts.map +1 -0
- package/dist/test/interface-jsdoc.test.d.ts +2 -0
- package/dist/test/interface-jsdoc.test.d.ts.map +1 -0
- package/dist/test/jscomment.test.d.ts +2 -0
- package/dist/test/jscomment.test.d.ts.map +1 -0
- package/dist/test/jsdoc.test.d.ts.map +1 -1
- package/dist/test/jsdocparam.test.d.ts +2 -0
- package/dist/test/jsdocparam.test.d.ts.map +1 -0
- package/dist/test/member-expression.test.d.ts +2 -0
- package/dist/test/member-expression.test.d.ts.map +1 -0
- package/dist/test/name-conflict.test.d.ts +2 -0
- package/dist/test/name-conflict.test.d.ts.map +1 -0
- package/dist/test/package-json.test.d.ts +2 -0
- package/dist/test/package-json.test.d.ts.map +1 -0
- package/dist/test/source-file.test.d.ts +2 -0
- package/dist/test/source-file.test.d.ts.map +1 -0
- package/dist/test/switch-statement.test.d.ts +2 -0
- package/dist/test/switch-statement.test.d.ts.map +1 -0
- package/dist/test/var-declaration-jsdoc.test.d.ts +2 -0
- package/dist/test/var-declaration-jsdoc.test.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/components/ArrayExpression.tsx +13 -12
- package/src/components/BlockScope.tsx +31 -0
- package/src/components/CallSignature.tsx +67 -0
- package/src/components/ClassDeclaration.tsx +103 -50
- package/src/components/Declaration.tsx +72 -27
- package/src/components/EnumDeclaration.tsx +26 -18
- package/src/components/EnumMember.tsx +24 -4
- package/src/components/FunctionCallExpression.tsx +1 -1
- package/src/components/FunctionDeclaration.tsx +106 -82
- package/src/components/IfStatement.tsx +51 -0
- package/src/components/Interface.tsx +61 -14
- package/src/components/JSDoc.tsx +1 -10
- package/src/components/JSDocParam.tsx +115 -0
- package/src/components/MemberExpression.tsx +237 -0
- package/src/components/ObjectExpression.tsx +8 -3
- package/src/components/PropertyName.tsx +50 -0
- package/src/components/SingleLineCommentBlock.tsx +21 -0
- package/src/components/SourceFile.tsx +29 -0
- package/src/components/SwitchStatement.tsx +78 -0
- package/src/components/TypeDeclaration.tsx +11 -4
- package/src/components/VarDeclaration.tsx +19 -11
- package/src/components/index.ts +7 -0
- package/src/components/stc/index.ts +8 -1
- package/src/context/index.ts +1 -0
- package/src/context/private-scope.ts +23 -0
- package/src/index.ts +1 -0
- package/src/parameter-descriptor.ts +71 -0
- package/src/symbols/reference.ts +70 -18
- package/src/symbols/ts-member-scope.ts +6 -2
- package/src/symbols/ts-output-symbol.ts +24 -11
- package/src/utils.ts +33 -0
- package/temp/api.json +3819 -1915
- package/test/array.test.tsx +60 -0
- package/test/basic.test.tsx +2 -2
- package/test/block-scope.test.tsx +29 -0
- package/test/class.test.tsx +173 -5
- package/test/enum-jsdoc.test.tsx +74 -0
- package/test/enum.test.tsx +50 -3
- package/test/function-call-expression.test.tsx +5 -2
- package/test/function-declaration-jsdoc.test.tsx +89 -0
- package/test/function-declaration.test.tsx +13 -9
- package/test/if-statement.test.tsx +28 -0
- package/test/interface-jsdoc.test.tsx +164 -0
- package/test/interface.test.tsx +14 -0
- package/test/jscomment.test.tsx +104 -0
- package/test/jsdoc.test.tsx +9 -15
- package/test/jsdocparam.test.tsx +188 -0
- package/test/member-expression.test.tsx +297 -0
- package/test/name-conflict.test.tsx +105 -0
- package/test/name-policy.test.tsx +1 -1
- package/test/package-json.test.tsx +93 -0
- package/test/reference.test.tsx +6 -2
- package/test/source-file.test.tsx +38 -0
- package/test/switch-statement.test.tsx +50 -0
- package/test/value-expression.test.tsx +9 -0
- package/test/var-declaration-jsdoc.test.tsx +70 -0
- package/test/var-declaration.test.tsx +1 -1
- package/tsconfig.json +1 -8
- package/vitest.config.ts +2 -10
- package/dist/src/components/JSDocParagraph.d.ts +0 -10
- package/dist/src/components/JSDocParagraph.d.ts.map +0 -1
- package/dist/src/components/JSDocParagraph.js +0 -23
- package/dist/src/components/JSDocParagraph.js.map +0 -1
- package/src/components/JSDocParagraph.tsx +0 -35
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Children,
|
|
3
|
+
childrenArray,
|
|
4
|
+
computed,
|
|
5
|
+
For,
|
|
6
|
+
isComponentCreator,
|
|
7
|
+
OutputSymbol,
|
|
8
|
+
Ref,
|
|
9
|
+
ref,
|
|
10
|
+
Refkey,
|
|
11
|
+
toRef,
|
|
12
|
+
useBinder,
|
|
13
|
+
} from "@alloy-js/core";
|
|
14
|
+
import { isValidJSIdentifier } from "../utils.js";
|
|
15
|
+
|
|
16
|
+
export interface MemberExpressionProps {
|
|
17
|
+
children: Children;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface PartDescriptor {
|
|
21
|
+
base: string | Ref<string>;
|
|
22
|
+
accessStyle: Ref<"dot" | "bracket">;
|
|
23
|
+
nullish: Ref<boolean>;
|
|
24
|
+
args?: Children[] | boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create a member expression from parts. Each part can provide one of
|
|
29
|
+
* the following:
|
|
30
|
+
*
|
|
31
|
+
* * **id**: The identifier for the member expression part
|
|
32
|
+
* * **refkey**: A refkey for a symbol whose name becomes the identifier
|
|
33
|
+
* * **symbol**: a symbol whose name becomes the identifier part
|
|
34
|
+
* * **args**: create a method call with the given args
|
|
35
|
+
*
|
|
36
|
+
* Additionally, a part can be `nullish`, which means subsequent parts
|
|
37
|
+
* will use the appropriate conditional access operator e.g. `?.`.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
*
|
|
41
|
+
* ```tsx
|
|
42
|
+
* <MemberExpression>
|
|
43
|
+
* <MemberExpression.Part id="base" />
|
|
44
|
+
* <MemberExpression.Part refkey={rk} nullish />
|
|
45
|
+
* <MemberExpression.Part symbol={sym} />
|
|
46
|
+
* <MemberExpression.Part args={["hello", "world"]} />
|
|
47
|
+
* </MemberExpression>
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* Assuming `rk` is a refkey to a symbol name "prop1", and `sym` is a symbol
|
|
51
|
+
* with a name of "prop2", this will render:
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* base.prop1?.prop2("hello", "world")
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export function MemberExpression(props: MemberExpressionProps): Children {
|
|
58
|
+
const children = flattenMemberExpression(childrenArray(() => props.children));
|
|
59
|
+
const parts: PartDescriptor[] = [];
|
|
60
|
+
for (const child of children) {
|
|
61
|
+
if (!isComponentCreator(child, MemberExpression.Part)) {
|
|
62
|
+
// we ignore non-parts
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const partProps = child.props;
|
|
67
|
+
const part: PartDescriptor = {
|
|
68
|
+
base: "",
|
|
69
|
+
accessStyle: ref("dot"),
|
|
70
|
+
nullish: ref(!!partProps.nullish),
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
if (partProps.id) {
|
|
74
|
+
part.base = partProps.id;
|
|
75
|
+
part.accessStyle.value = accessStyleForMemberName(partProps.id);
|
|
76
|
+
} else if (partProps.refkey) {
|
|
77
|
+
const binder = useBinder();
|
|
78
|
+
const symbolRef = binder.getSymbolForRefkey(partProps.refkey);
|
|
79
|
+
part.base = computed(() => {
|
|
80
|
+
if (symbolRef.value) {
|
|
81
|
+
part.nullish.value = !!symbolRef.value.metadata.nullish;
|
|
82
|
+
part.accessStyle.value = accessStyleForMemberName(
|
|
83
|
+
symbolRef.value.name,
|
|
84
|
+
);
|
|
85
|
+
return symbolRef.value.name;
|
|
86
|
+
} else {
|
|
87
|
+
return "<unresolved symbol>";
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
} else if (partProps.symbol) {
|
|
91
|
+
part.nullish.value = !!partProps.symbol.metadata.nullish;
|
|
92
|
+
part.accessStyle = computed(() => {
|
|
93
|
+
return accessStyleForMemberName(partProps.symbol!.name);
|
|
94
|
+
});
|
|
95
|
+
part.base = toRef(partProps.symbol, "name");
|
|
96
|
+
} else if (partProps.args) {
|
|
97
|
+
part.args = partProps.args;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
parts.push(part);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (parts.length === 0) {
|
|
104
|
+
return <></>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// construct a member expression from the parts. When a part is nullish,
|
|
108
|
+
// and there is a subsequent part, we use `?.` instead of `.`. accessStyle determines
|
|
109
|
+
// whether we use dot or bracket notation.
|
|
110
|
+
|
|
111
|
+
const result = computed(() => {
|
|
112
|
+
const expression: Children[] = [];
|
|
113
|
+
|
|
114
|
+
for (let i = 0; i < parts.length; i++) {
|
|
115
|
+
const part = parts[i];
|
|
116
|
+
const base = typeof part.base === "string" ? part.base : part.base.value;
|
|
117
|
+
const hasName = base !== "";
|
|
118
|
+
|
|
119
|
+
if (i === 0) {
|
|
120
|
+
expression.push(base);
|
|
121
|
+
} else {
|
|
122
|
+
// Determine if we should use nullish operator from previous part
|
|
123
|
+
const prevPart = parts[i - 1];
|
|
124
|
+
const isNullish = prevPart.nullish.value;
|
|
125
|
+
|
|
126
|
+
// Handle parts with name vs. parts with only args differently
|
|
127
|
+
if (hasName) {
|
|
128
|
+
// Apply dot or bracket notation based on accessStyle for named parts
|
|
129
|
+
if (part.accessStyle.value === "dot") {
|
|
130
|
+
expression.push(
|
|
131
|
+
<group>
|
|
132
|
+
<indent>
|
|
133
|
+
<sbr />
|
|
134
|
+
{isNullish ? "?." : "."}
|
|
135
|
+
{base}
|
|
136
|
+
</indent>
|
|
137
|
+
</group>,
|
|
138
|
+
);
|
|
139
|
+
} else {
|
|
140
|
+
// bracket notation - don't include the dot
|
|
141
|
+
expression.push(
|
|
142
|
+
<group>
|
|
143
|
+
{isNullish ? "?." : ""}[
|
|
144
|
+
<indent>
|
|
145
|
+
<sbr />
|
|
146
|
+
{JSON.stringify(base)}
|
|
147
|
+
</indent>
|
|
148
|
+
<sbr />]
|
|
149
|
+
</group>,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
} else if (part.args !== undefined) {
|
|
153
|
+
// For parts with only args (no name), append function call directly with appropriate nullish operator
|
|
154
|
+
expression.push(
|
|
155
|
+
<group>
|
|
156
|
+
{isNullish ? "?." : ""}
|
|
157
|
+
<group>
|
|
158
|
+
(
|
|
159
|
+
<For
|
|
160
|
+
each={typeof part.args === "boolean" ? [] : (part.args ?? [])}
|
|
161
|
+
comma
|
|
162
|
+
line
|
|
163
|
+
>
|
|
164
|
+
{(arg) => arg}
|
|
165
|
+
</For>
|
|
166
|
+
)
|
|
167
|
+
</group>
|
|
168
|
+
</group>,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return expression;
|
|
175
|
+
});
|
|
176
|
+
return <>{result.value}</>;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function accessStyleForMemberName(name: string) {
|
|
180
|
+
if (isValidJSIdentifier(name)) {
|
|
181
|
+
return "dot";
|
|
182
|
+
}
|
|
183
|
+
return "bracket";
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function flattenMemberExpression(children: Children[]): Children[] {
|
|
187
|
+
const flattened: Children[] = [];
|
|
188
|
+
for (const child of children) {
|
|
189
|
+
if (isComponentCreator(child, MemberExpression)) {
|
|
190
|
+
flattened.push(
|
|
191
|
+
...flattenMemberExpression(childrenArray(() => child.props.children)),
|
|
192
|
+
);
|
|
193
|
+
} else {
|
|
194
|
+
flattened.push(child);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return flattened;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
interface MemberExpressionPartProps {
|
|
201
|
+
/**
|
|
202
|
+
* The identifier for this part of the member expression.
|
|
203
|
+
*/
|
|
204
|
+
id?: string;
|
|
205
|
+
/**
|
|
206
|
+
* A refkey for a symbol whose name becomes the identifier.
|
|
207
|
+
*/
|
|
208
|
+
refkey?: Refkey;
|
|
209
|
+
/**
|
|
210
|
+
* A symbol whose name becomes the identifier.
|
|
211
|
+
*/
|
|
212
|
+
symbol?: OutputSymbol;
|
|
213
|
+
/**
|
|
214
|
+
* Arguments to construct a call expression.
|
|
215
|
+
*/
|
|
216
|
+
args?: Children[] | boolean;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* This part is nullish. Subsequent parts use conditional access operators.
|
|
220
|
+
*/
|
|
221
|
+
nullish?: boolean;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* A part of a member expression. Each part can provide one of the following
|
|
225
|
+
* props:
|
|
226
|
+
*
|
|
227
|
+
* * **id**: The identifier for the member expression part
|
|
228
|
+
* * **refkey**: A refkey for a symbol whose name becomes the identifier
|
|
229
|
+
* * **symbol**: a symbol whose name becomes the identifier part
|
|
230
|
+
* * **args**: create a method call with the given args
|
|
231
|
+
*/
|
|
232
|
+
MemberExpression.Part = function (props: MemberExpressionPartProps) {
|
|
233
|
+
/**
|
|
234
|
+
* This component does nothing except hold props which are retrieved by
|
|
235
|
+
* the `MemberExpression` component.
|
|
236
|
+
*/
|
|
237
|
+
};
|
|
@@ -13,7 +13,9 @@ import {
|
|
|
13
13
|
Switch,
|
|
14
14
|
Wrap,
|
|
15
15
|
} from "@alloy-js/core";
|
|
16
|
+
import { useTSNamePolicy } from "../name-policy.js";
|
|
16
17
|
import { createTSSymbol } from "../symbols/index.js";
|
|
18
|
+
import { PropertyName } from "./PropertyName.jsx";
|
|
17
19
|
import { ValueExpression } from "./ValueExpression.js";
|
|
18
20
|
|
|
19
21
|
export interface ObjectExpressionProps {
|
|
@@ -90,13 +92,16 @@ export interface ObjectPropertyProps {
|
|
|
90
92
|
value?: Children;
|
|
91
93
|
jsValue?: unknown;
|
|
92
94
|
children?: Children;
|
|
93
|
-
refkey?: Refkey;
|
|
95
|
+
refkey?: Refkey | Refkey[];
|
|
94
96
|
}
|
|
95
97
|
|
|
96
98
|
export function ObjectProperty(props: ObjectPropertyProps) {
|
|
97
99
|
let name;
|
|
98
100
|
if (props.name) {
|
|
99
|
-
|
|
101
|
+
const namer = useTSNamePolicy();
|
|
102
|
+
name = (
|
|
103
|
+
<PropertyName name={namer.getName(props.name, "object-member-data")} />
|
|
104
|
+
);
|
|
100
105
|
} else if (props.nameExpression) {
|
|
101
106
|
name = <>[{props.nameExpression}]</>;
|
|
102
107
|
} else {
|
|
@@ -126,7 +131,7 @@ export function ObjectProperty(props: ObjectPropertyProps) {
|
|
|
126
131
|
sym ? createAssignmentContext(sym) : undefined;
|
|
127
132
|
return (
|
|
128
133
|
<>
|
|
129
|
-
{
|
|
134
|
+
{name}:{" "}
|
|
130
135
|
<AssignmentContext.Provider value={assignmentContext}>
|
|
131
136
|
{value}
|
|
132
137
|
</AssignmentContext.Provider>
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { MemberDeclarationContext, useContext } from "@alloy-js/core";
|
|
2
|
+
import { TSOutputSymbol, TSSymbolFlags } from "../symbols/ts-output-symbol.js";
|
|
3
|
+
import { isValidJSIdentifier } from "../utils.js";
|
|
4
|
+
|
|
5
|
+
export interface PropertyNameProps {
|
|
6
|
+
/**
|
|
7
|
+
* The name of the property.
|
|
8
|
+
*/
|
|
9
|
+
name?: string;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Whether the property is a private property. If `true`, the property will be
|
|
13
|
+
* prefixed with `#`.
|
|
14
|
+
*/
|
|
15
|
+
private?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A TypeScript property name for an interface, class, or object member. If the
|
|
20
|
+
* name is not a valid JavaScript identifier, it will be quoted. If a `name` prop
|
|
21
|
+
* is provided, it will be used as the property name. Otherwise, the name will be
|
|
22
|
+
* taken from the {@link (MemberDeclarationContext:variable)}.
|
|
23
|
+
*/
|
|
24
|
+
export function PropertyName(props: PropertyNameProps) {
|
|
25
|
+
if (props.name) {
|
|
26
|
+
if (props.private) {
|
|
27
|
+
return "#" + props.name;
|
|
28
|
+
}
|
|
29
|
+
return quoteIfNeeded(props.name);
|
|
30
|
+
} else {
|
|
31
|
+
const declSymbol = useContext(MemberDeclarationContext) as TSOutputSymbol;
|
|
32
|
+
if (!declSymbol) {
|
|
33
|
+
return "(no member declaration context)";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (declSymbol.tsFlags & TSSymbolFlags.PrivateMember) {
|
|
37
|
+
return <>#{declSymbol.name}</>;
|
|
38
|
+
} else {
|
|
39
|
+
return <>{quoteIfNeeded(declSymbol.name)}</>;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function quoteIfNeeded(name: string) {
|
|
45
|
+
if (isValidJSIdentifier(name)) {
|
|
46
|
+
return name;
|
|
47
|
+
} else {
|
|
48
|
+
return `"${name.replace(/"/g, '\\"')}"`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Prose } from "@alloy-js/core";
|
|
2
|
+
import { Children } from "@alloy-js/core/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
export interface SingleLineCommentBlockProps {
|
|
5
|
+
children: Children;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A single line comment block. The children are rendered as a prose element, which means that they
|
|
10
|
+
* are broken into multiple lines
|
|
11
|
+
*/
|
|
12
|
+
export function SingleLineCommentBlock(props: SingleLineCommentBlockProps) {
|
|
13
|
+
return (
|
|
14
|
+
<>
|
|
15
|
+
//{" "}
|
|
16
|
+
<align string="// ">
|
|
17
|
+
<Prose>{props.children}</Prose>
|
|
18
|
+
</align>
|
|
19
|
+
</>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
@@ -17,6 +17,7 @@ import { createTSModuleScope, TSModuleScope } from "../symbols/index.js";
|
|
|
17
17
|
import { ImportStatements } from "./ImportStatement.js";
|
|
18
18
|
import { PackageContext } from "./PackageDirectory.js";
|
|
19
19
|
import { Reference } from "./Reference.js";
|
|
20
|
+
import { SingleLineCommentBlock } from "./SingleLineCommentBlock.jsx";
|
|
20
21
|
export interface SourceFileContext {
|
|
21
22
|
scope: TSModuleScope;
|
|
22
23
|
}
|
|
@@ -30,6 +31,8 @@ export function useSourceFile() {
|
|
|
30
31
|
export interface SourceFileProps {
|
|
31
32
|
path: string;
|
|
32
33
|
children?: Children;
|
|
34
|
+
header?: Children;
|
|
35
|
+
headerComment?: string;
|
|
33
36
|
export?: boolean | string;
|
|
34
37
|
}
|
|
35
38
|
|
|
@@ -59,11 +62,20 @@ export function SourceFile(props: SourceFileProps) {
|
|
|
59
62
|
}
|
|
60
63
|
}
|
|
61
64
|
|
|
65
|
+
const header =
|
|
66
|
+
props.header || props.headerComment ?
|
|
67
|
+
<SourceFileHeader
|
|
68
|
+
header={props.header}
|
|
69
|
+
headerComment={props.headerComment}
|
|
70
|
+
/>
|
|
71
|
+
: undefined;
|
|
72
|
+
|
|
62
73
|
return (
|
|
63
74
|
<CoreSourceFile
|
|
64
75
|
path={props.path}
|
|
65
76
|
filetype="typescript"
|
|
66
77
|
reference={Reference}
|
|
78
|
+
header={header}
|
|
67
79
|
>
|
|
68
80
|
<Show when={scope.importedModules.size > 0}>
|
|
69
81
|
<ImportStatements records={scope.importedModules} />
|
|
@@ -76,3 +88,20 @@ export function SourceFile(props: SourceFileProps) {
|
|
|
76
88
|
</CoreSourceFile>
|
|
77
89
|
);
|
|
78
90
|
}
|
|
91
|
+
|
|
92
|
+
export interface SourceFileHeaderProps {
|
|
93
|
+
header?: Children;
|
|
94
|
+
headerComment?: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function SourceFileHeader(props: SourceFileHeaderProps) {
|
|
98
|
+
return (
|
|
99
|
+
<>
|
|
100
|
+
<Show when={props.headerComment !== undefined}>
|
|
101
|
+
<SingleLineCommentBlock>{props.headerComment}</SingleLineCommentBlock>
|
|
102
|
+
</Show>
|
|
103
|
+
<Show when={props.header !== undefined}>{props.header}</Show>
|
|
104
|
+
<hbr />
|
|
105
|
+
</>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Block, Indent, Match, Switch } from "@alloy-js/core";
|
|
2
|
+
import { Children } from "@alloy-js/core/jsx-runtime";
|
|
3
|
+
import { BlockScope } from "./BlockScope.jsx";
|
|
4
|
+
import { ValueExpression } from "./ValueExpression.jsx";
|
|
5
|
+
|
|
6
|
+
export interface SwitchStatementProps {
|
|
7
|
+
expression: Children;
|
|
8
|
+
children: Children;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A switch statement.
|
|
13
|
+
*/
|
|
14
|
+
export function SwitchStatement(props: SwitchStatementProps) {
|
|
15
|
+
return (
|
|
16
|
+
<>
|
|
17
|
+
switch ({props.expression}) <Block>{props.children}</Block>
|
|
18
|
+
</>
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface CaseClausePropsBase {
|
|
23
|
+
children: Children;
|
|
24
|
+
block?: boolean;
|
|
25
|
+
break?: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface CaseClausePropsWithExpression extends CaseClausePropsBase {
|
|
29
|
+
expression: Children;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface CaseClausePropsWithValue extends CaseClausePropsBase {
|
|
33
|
+
jsValue: unknown;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface CaseClausePropsWithDefault extends CaseClausePropsBase {
|
|
37
|
+
default: true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type CaseClauseProps =
|
|
41
|
+
| CaseClausePropsWithExpression
|
|
42
|
+
| CaseClausePropsWithValue
|
|
43
|
+
| CaseClausePropsWithDefault;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A case clause in a switch statement. Provide either an expression or a JS
|
|
47
|
+
* value to compare to the switch expression. Can optionally be wrapped in a
|
|
48
|
+
* block. Pass `break` to finish with a break statement.
|
|
49
|
+
*/
|
|
50
|
+
export function CaseClause(props: CaseClauseProps) {
|
|
51
|
+
const Wrapper = props.block ? BlockScope : Indent;
|
|
52
|
+
return (
|
|
53
|
+
<>
|
|
54
|
+
<Switch>
|
|
55
|
+
<Match when={"default" in props && props.default}>default </Match>
|
|
56
|
+
<Match else>
|
|
57
|
+
case{" "}
|
|
58
|
+
{"expression" in props ?
|
|
59
|
+
props.expression
|
|
60
|
+
: <ValueExpression
|
|
61
|
+
jsValue={(props as CaseClausePropsWithValue).jsValue}
|
|
62
|
+
/>
|
|
63
|
+
}
|
|
64
|
+
</Match>
|
|
65
|
+
</Switch>
|
|
66
|
+
{": "}
|
|
67
|
+
<Wrapper>
|
|
68
|
+
{props.children}
|
|
69
|
+
{props.break && (
|
|
70
|
+
<>
|
|
71
|
+
<hbr />
|
|
72
|
+
break;
|
|
73
|
+
</>
|
|
74
|
+
)}
|
|
75
|
+
</Wrapper>
|
|
76
|
+
</>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
@@ -1,12 +1,19 @@
|
|
|
1
|
-
import { Name } from "@alloy-js/core";
|
|
1
|
+
import { Name, Show } from "@alloy-js/core";
|
|
2
2
|
import { BaseDeclarationProps, Declaration } from "./Declaration.js";
|
|
3
|
+
import { JSDoc } from "./JSDoc.jsx";
|
|
3
4
|
|
|
4
5
|
export interface TypeDeclarationProps extends BaseDeclarationProps {}
|
|
5
6
|
|
|
6
7
|
export function TypeDeclaration(props: TypeDeclarationProps) {
|
|
7
8
|
return (
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
<>
|
|
10
|
+
<Show when={Boolean(props.doc)}>
|
|
11
|
+
<JSDoc children={props.doc} />
|
|
12
|
+
<hbr />
|
|
13
|
+
</Show>
|
|
14
|
+
<Declaration {...props} nameKind="type">
|
|
15
|
+
type <Name /> = {props.children};
|
|
16
|
+
</Declaration>
|
|
17
|
+
</>
|
|
11
18
|
);
|
|
12
19
|
}
|
|
@@ -4,16 +4,18 @@ import {
|
|
|
4
4
|
Declaration as CoreDeclaration,
|
|
5
5
|
createAssignmentContext,
|
|
6
6
|
Name,
|
|
7
|
+
Show,
|
|
7
8
|
} from "@alloy-js/core";
|
|
8
9
|
import { useTSNamePolicy } from "../name-policy.js";
|
|
9
10
|
import { createTSSymbol } from "../symbols/index.js";
|
|
10
11
|
import { BaseDeclarationProps } from "./Declaration.js";
|
|
12
|
+
import { JSDoc } from "./JSDoc.jsx";
|
|
11
13
|
|
|
12
14
|
export interface VarDeclarationProps extends BaseDeclarationProps {
|
|
13
15
|
const?: boolean;
|
|
14
16
|
let?: boolean;
|
|
15
17
|
var?: boolean;
|
|
16
|
-
|
|
18
|
+
initializer?: Children;
|
|
17
19
|
type?: Children;
|
|
18
20
|
}
|
|
19
21
|
|
|
@@ -27,22 +29,28 @@ export function VarDeclaration(props: VarDeclarationProps) {
|
|
|
27
29
|
const sym = createTSSymbol({
|
|
28
30
|
name: name,
|
|
29
31
|
refkey: props.refkey,
|
|
30
|
-
refkeys: props.refkeys,
|
|
31
32
|
default: props.default,
|
|
32
33
|
export: props.export,
|
|
34
|
+
metadata: props.metadata,
|
|
33
35
|
});
|
|
34
36
|
|
|
35
37
|
const assignmentContext = createAssignmentContext(sym);
|
|
36
38
|
|
|
37
39
|
return (
|
|
38
|
-
|
|
39
|
-
{props.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
<
|
|
44
|
-
{props.
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
<>
|
|
41
|
+
<Show when={Boolean(props.doc)}>
|
|
42
|
+
<JSDoc children={props.doc} />
|
|
43
|
+
<hbr />
|
|
44
|
+
</Show>
|
|
45
|
+
<CoreDeclaration symbol={sym}>
|
|
46
|
+
{props.export ? "export " : ""}
|
|
47
|
+
{props.default ? "default " : ""}
|
|
48
|
+
{keyword} <Name />
|
|
49
|
+
{type} ={" "}
|
|
50
|
+
<AssignmentContext.Provider value={assignmentContext}>
|
|
51
|
+
{props.initializer ?? props.children}
|
|
52
|
+
</AssignmentContext.Provider>
|
|
53
|
+
</CoreDeclaration>
|
|
54
|
+
</>
|
|
47
55
|
);
|
|
48
56
|
}
|
package/src/components/index.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
export * from "../parameter-descriptor.js";
|
|
1
2
|
export * from "./ArrayExpression.js";
|
|
2
3
|
export * from "./BarrelFile.js";
|
|
4
|
+
export * from "./BlockScope.js";
|
|
5
|
+
export * from "./CallSignature.jsx";
|
|
3
6
|
export * from "./ClassDeclaration.jsx";
|
|
4
7
|
export * from "./CommaList.jsx";
|
|
5
8
|
export * from "./Declaration.js";
|
|
@@ -10,12 +13,16 @@ export * from "./FunctionCallExpression.js";
|
|
|
10
13
|
export * from "./FunctionDeclaration.js";
|
|
11
14
|
export * from "./ImportStatement.js";
|
|
12
15
|
export * from "./Interface.js";
|
|
16
|
+
export * from "./JSDoc.jsx";
|
|
17
|
+
export * from "./JSDocComment.jsx";
|
|
18
|
+
export * from "./JSDocExample.jsx";
|
|
13
19
|
export * from "./MemberChainExpression.jsx";
|
|
14
20
|
export * from "./ObjectExpression.js";
|
|
15
21
|
export * from "./PackageDirectory.js";
|
|
16
22
|
export * from "./PackageJson.js";
|
|
17
23
|
export * from "./Reference.js";
|
|
18
24
|
export * from "./SourceFile.js";
|
|
25
|
+
export * from "./SwitchStatement.jsx";
|
|
19
26
|
export * from "./TsConfigJson.js";
|
|
20
27
|
export * from "./TypeDeclaration.js";
|
|
21
28
|
export * from "./ValueExpression.js";
|
|
@@ -3,10 +3,12 @@ import * as base from "../index.js";
|
|
|
3
3
|
|
|
4
4
|
export const ArrayExpression = stc(base.ArrayExpression);
|
|
5
5
|
export const BarrelFile = stc(base.BarrelFile);
|
|
6
|
+
export const CallSignature = stc(base.CallSignature);
|
|
6
7
|
export const ClassDeclaration = stc(base.ClassDeclaration);
|
|
7
8
|
export const ClassField = stc(base.ClassField);
|
|
8
9
|
export const ClassMember = stc(base.ClassMember);
|
|
9
10
|
export const ClassMethod = stc(base.ClassMethod);
|
|
11
|
+
export const CommaList = stc(base.CommaList);
|
|
10
12
|
export const Declaration = stc(base.Declaration);
|
|
11
13
|
export const EnumDeclaration = stc(base.EnumDeclaration);
|
|
12
14
|
export const EnumMember = stc(base.EnumMember);
|
|
@@ -18,13 +20,18 @@ export const ImportStatements = stc(base.ImportStatements);
|
|
|
18
20
|
export const InterfaceDeclaration = stc(base.InterfaceDeclaration);
|
|
19
21
|
export const InterfaceExpression = stc(base.InterfaceExpression);
|
|
20
22
|
export const InterfaceMember = stc(base.InterfaceMember);
|
|
21
|
-
export const
|
|
23
|
+
export const JSDoc = stc(base.JSDoc);
|
|
24
|
+
export const JSDocExample = stc(base.JSDocExample);
|
|
25
|
+
export const JSDocComment = stc(base.JSDocComment);
|
|
26
|
+
export const MemberChainExpression = stc(base.MemberChainExpression);
|
|
22
27
|
export const ObjectExpression = stc(base.ObjectExpression);
|
|
23
28
|
export const ObjectProperty = stc(base.ObjectProperty);
|
|
24
29
|
export const PackageDirectory = stc(base.PackageDirectory);
|
|
25
30
|
export const PackageJsonFile = stc(base.PackageJsonFile);
|
|
26
31
|
export const Reference = stc(base.Reference);
|
|
27
32
|
export const SourceFile = stc(base.SourceFile);
|
|
33
|
+
export const SwitchStatement = stc(base.SwitchStatement);
|
|
34
|
+
export const CaseClause = stc(base.CaseClause);
|
|
28
35
|
export const TSConfigJson = stc(base.TSConfigJson);
|
|
29
36
|
export const TypeDeclaration = stc(base.TypeDeclaration);
|
|
30
37
|
export const ValueExpression = stc(base.ValueExpression);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./private-scope.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ComponentContext, createContext, useContext } from "@alloy-js/core";
|
|
2
|
+
import { TSMemberScope } from "../symbols/ts-member-scope.js";
|
|
3
|
+
|
|
4
|
+
export interface PrivateScopeContext {
|
|
5
|
+
/**
|
|
6
|
+
* Scope for private static members.
|
|
7
|
+
*/
|
|
8
|
+
staticMembers: TSMemberScope;
|
|
9
|
+
/**
|
|
10
|
+
* Scope for private instance members.
|
|
11
|
+
*/
|
|
12
|
+
instanceMembers: TSMemberScope;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Provides scopes for instance and static private members.
|
|
17
|
+
*/
|
|
18
|
+
export const PrivateScopeContext: ComponentContext<PrivateScopeContext> =
|
|
19
|
+
createContext<PrivateScopeContext>();
|
|
20
|
+
|
|
21
|
+
export function usePrivateScope() {
|
|
22
|
+
return useContext(PrivateScopeContext);
|
|
23
|
+
}
|