@alloy-js/typescript 0.11.0 → 0.12.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.
Files changed (95) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/src/components/ClassDeclaration.d.ts +2 -0
  3. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  4. package/dist/src/components/ClassDeclaration.js +14 -2
  5. package/dist/src/components/FunctionBase.d.ts.map +1 -1
  6. package/dist/src/components/FunctionBase.js +13 -3
  7. package/dist/src/components/FunctionType.d.ts +7 -8
  8. package/dist/src/components/FunctionType.d.ts.map +1 -1
  9. package/dist/src/components/FunctionType.js +29 -25
  10. package/dist/src/components/ImportStatement.d.ts.map +1 -1
  11. package/dist/src/components/ImportStatement.js +9 -2
  12. package/dist/src/components/Interface.d.ts +3 -2
  13. package/dist/src/components/Interface.d.ts.map +1 -1
  14. package/dist/src/components/Interface.js +8 -4
  15. package/dist/src/components/JSDocComment.js +1 -1
  16. package/dist/src/components/MemberDeclaration.d.ts +4 -0
  17. package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
  18. package/dist/src/components/MemberDeclaration.js +1 -1
  19. package/dist/src/components/MemberExpression.d.ts.map +1 -1
  20. package/dist/src/components/MemberExpression.js +3 -2
  21. package/dist/src/components/Reference.d.ts +8 -1
  22. package/dist/src/components/Reference.d.ts.map +1 -1
  23. package/dist/src/components/Reference.js +7 -2
  24. package/dist/src/components/TypeDeclaration.d.ts +1 -1
  25. package/dist/src/components/TypeDeclaration.d.ts.map +1 -1
  26. package/dist/src/components/TypeDeclaration.js +3 -2
  27. package/dist/src/components/TypeRefContext.d.ts +22 -0
  28. package/dist/src/components/TypeRefContext.d.ts.map +1 -0
  29. package/dist/src/components/TypeRefContext.js +41 -0
  30. package/dist/src/components/VarDeclaration.d.ts +1 -0
  31. package/dist/src/components/VarDeclaration.d.ts.map +1 -1
  32. package/dist/src/components/VarDeclaration.js +9 -3
  33. package/dist/src/components/index.d.ts +1 -0
  34. package/dist/src/components/index.d.ts.map +1 -1
  35. package/dist/src/components/index.js +1 -0
  36. package/dist/src/components/stc/index.d.ts +1 -0
  37. package/dist/src/components/stc/index.d.ts.map +1 -1
  38. package/dist/src/components/stc/index.js +1 -0
  39. package/dist/src/context/type-ref-context.d.ts +15 -0
  40. package/dist/src/context/type-ref-context.d.ts.map +1 -0
  41. package/dist/src/context/type-ref-context.js +12 -0
  42. package/dist/src/parameter-descriptor.d.ts +6 -0
  43. package/dist/src/parameter-descriptor.d.ts.map +1 -1
  44. package/dist/src/symbols/reference.d.ts +4 -1
  45. package/dist/src/symbols/reference.d.ts.map +1 -1
  46. package/dist/src/symbols/reference.js +11 -5
  47. package/dist/src/symbols/ts-module-scope.d.ts +4 -1
  48. package/dist/src/symbols/ts-module-scope.d.ts.map +1 -1
  49. package/dist/src/symbols/ts-module-scope.js +10 -3
  50. package/dist/src/symbols/ts-output-symbol.d.ts +2 -1
  51. package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -1
  52. package/dist/src/symbols/ts-output-symbol.js +1 -0
  53. package/dist/test/class.test.js +4 -4
  54. package/dist/test/enum-jsdoc.test.js +6 -6
  55. package/dist/test/function-declaration-jsdoc.test.js +3 -3
  56. package/dist/test/imports.test.d.ts.map +1 -1
  57. package/dist/test/imports.test.js +208 -2
  58. package/dist/test/interface-jsdoc.test.js +9 -9
  59. package/dist/test/jsdoc.test.js +5 -5
  60. package/dist/test/jsdocparam.test.js +8 -8
  61. package/dist/test/member-expression.test.js +218 -2
  62. package/dist/test/var-declaration-jsdoc.test.js +3 -3
  63. package/dist/test/var-declaration.test.js +1 -1
  64. package/dist/tsconfig.tsbuildinfo +1 -1
  65. package/package.json +4 -4
  66. package/src/components/ClassDeclaration.tsx +19 -3
  67. package/src/components/FunctionBase.tsx +14 -3
  68. package/src/components/FunctionType.tsx +45 -40
  69. package/src/components/ImportStatement.tsx +30 -3
  70. package/src/components/Interface.tsx +41 -34
  71. package/src/components/JSDocComment.tsx +1 -1
  72. package/src/components/MemberDeclaration.tsx +7 -1
  73. package/src/components/MemberExpression.tsx +7 -2
  74. package/src/components/Reference.tsx +12 -2
  75. package/src/components/TypeDeclaration.tsx +16 -13
  76. package/src/components/TypeRefContext.tsx +49 -0
  77. package/src/components/VarDeclaration.tsx +6 -2
  78. package/src/components/index.ts +1 -0
  79. package/src/components/stc/index.ts +1 -0
  80. package/src/context/type-ref-context.tsx +19 -0
  81. package/src/parameter-descriptor.ts +7 -0
  82. package/src/symbols/reference.ts +17 -5
  83. package/src/symbols/ts-module-scope.ts +19 -4
  84. package/src/symbols/ts-output-symbol.ts +1 -0
  85. package/temp/api.json +613 -220
  86. package/test/class.test.tsx +4 -4
  87. package/test/enum-jsdoc.test.tsx +6 -6
  88. package/test/function-declaration-jsdoc.test.tsx +3 -3
  89. package/test/imports.test.tsx +165 -1
  90. package/test/interface-jsdoc.test.tsx +9 -9
  91. package/test/jsdoc.test.tsx +5 -5
  92. package/test/jsdocparam.test.tsx +8 -8
  93. package/test/member-expression.test.tsx +171 -2
  94. package/test/var-declaration-jsdoc.test.tsx +3 -3
  95. package/test/var-declaration.test.tsx +1 -1
@@ -22,6 +22,7 @@ import { BaseDeclarationProps, Declaration } from "./Declaration.jsx";
22
22
  import { JSDoc } from "./JSDoc.jsx";
23
23
  import { JSDocParams } from "./JSDocParam.jsx";
24
24
  import { PropertyName } from "./PropertyName.jsx";
25
+ import { TypeRefContext } from "./TypeRefContext.jsx";
25
26
 
26
27
  export interface ClassDeclarationProps extends BaseDeclarationProps {
27
28
  extends?: Children;
@@ -101,6 +102,7 @@ export interface ClassMemberProps {
101
102
  static?: boolean;
102
103
  children?: Children;
103
104
  doc?: Children;
105
+ nullish?: boolean;
104
106
  }
105
107
 
106
108
  export function ClassMember(props: ClassMemberProps) {
@@ -128,12 +130,19 @@ export function ClassMember(props: ClassMemberProps) {
128
130
  : memberScope.instanceMembers!) as TSOutputScope;
129
131
  }
130
132
 
133
+ let tsFlags =
134
+ props.jsPrivate ? TSSymbolFlags.PrivateMember : TSSymbolFlags.None;
135
+
136
+ if (props.nullish) {
137
+ tsFlags |= TSSymbolFlags.Nullish;
138
+ }
139
+
131
140
  const sym = createTSSymbol({
132
141
  name,
133
142
  scope,
134
143
  refkey: props.refkey,
135
144
  flags,
136
- tsFlags: props.jsPrivate ? TSSymbolFlags.PrivateMember : TSSymbolFlags.None,
145
+ tsFlags,
137
146
  });
138
147
 
139
148
  return (
@@ -155,15 +164,22 @@ export function ClassMember(props: ClassMemberProps) {
155
164
 
156
165
  export interface ClassFieldProps extends ClassMemberProps {
157
166
  type?: Children;
167
+ optional?: boolean;
158
168
  children?: Children;
159
169
  }
160
170
 
161
171
  export function ClassField(props: ClassFieldProps) {
162
- const typeSection = props.type && <>: {props.type}</>;
172
+ const optionality = props.optional ? "?" : "";
173
+ const typeSection = props.type && (
174
+ <>
175
+ {optionality}: <TypeRefContext>{props.type}</TypeRefContext>
176
+ </>
177
+ );
163
178
  const initializerSection = props.children && <> = {props.children}</>;
179
+ const nullish = props.nullish ?? props.optional;
164
180
 
165
181
  return (
166
- <ClassMember {...props}>
182
+ <ClassMember {...props} nullish={nullish}>
167
183
  <PropertyName />
168
184
  {typeSection}
169
185
  {initializerSection}
@@ -19,6 +19,7 @@ import {
19
19
  TSOutputSymbol,
20
20
  TSSymbolFlags,
21
21
  } from "../symbols/index.js";
22
+ import { TypeRefContext } from "./TypeRefContext.jsx";
22
23
 
23
24
  const functionParametersTag = Symbol();
24
25
  const typeParametersTag = Symbol();
@@ -124,7 +125,9 @@ function parameter(param: DeclaredParameterDescriptor) {
124
125
  {param.symbol.name}
125
126
  <Show when={!!param.optional}>?</Show>
126
127
  <Show when={!!param.type}>
127
- <indent>: {param.type}</indent>
128
+ <indent>
129
+ : <TypeRefContext>{param.type}</TypeRefContext>
130
+ </indent>
128
131
  </Show>
129
132
  </group>
130
133
  );
@@ -186,13 +189,18 @@ function normalizeAndDeclareParameters(
186
189
  });
187
190
  } else {
188
191
  return (parameters as ParameterDescriptor[]).map((param) => {
192
+ const nullishFlag =
193
+ (param.nullish ?? param.optional) ?
194
+ TSSymbolFlags.Nullish
195
+ : TSSymbolFlags.None;
196
+
189
197
  const symbol = createTSSymbol({
190
198
  name: namePolicy.getName(
191
199
  param.name,
192
200
  flags & TSSymbolFlags.TypeSymbol ? "type" : "parameter",
193
201
  ),
194
202
  refkey: param.refkey,
195
- tsFlags: flags,
203
+ tsFlags: flags | nullishFlag,
196
204
  metadata: param.metadata,
197
205
  });
198
206
 
@@ -255,7 +263,10 @@ function typeParameter(param: DeclaredTypeParameterDescriptor) {
255
263
  <Show when={!!param.extends}>
256
264
  {" "}
257
265
  extends
258
- <indent> {param.extends}</indent>
266
+ <indent>
267
+ {" "}
268
+ <TypeRefContext>{param.extends}</TypeRefContext>
269
+ </indent>
259
270
  </Show>
260
271
  </group>
261
272
  );
@@ -16,9 +16,10 @@ import {
16
16
  } from "./FunctionBase.jsx";
17
17
  import { JSDoc } from "./JSDoc.jsx";
18
18
  import { JSDocParams } from "./JSDocParam.jsx";
19
+ import { ensureTypeRefContext } from "./TypeRefContext.jsx";
19
20
 
20
21
  /**
21
- * Options for {@link (FunctionType:namespace)} component.
22
+ * Options for {@link FunctionType} component.
22
23
  */
23
24
  export interface FunctionTypeProps extends CallSignatureProps {
24
25
  /** If the method is async */
@@ -29,6 +30,43 @@ export interface FunctionTypeProps extends CallSignatureProps {
29
30
  readonly children?: Children;
30
31
  }
31
32
 
33
+ const FunctionTypeComponent = ensureTypeRefContext(
34
+ (props: FunctionTypeProps) => {
35
+ const children = childrenArray(() => props.children);
36
+ const typeParametersChildren =
37
+ findKeyedChild(children, TypeParameters.tag) ?? undefined;
38
+ const parametersChildren =
39
+ findKeyedChild(children, FunctionParameters.tag) ?? undefined;
40
+ const returnType = getReturnType(props.returnType ?? "void", {
41
+ async: props.async,
42
+ });
43
+
44
+ const callSignatureProps = getCallSignatureProps(props, {
45
+ parametersChildren,
46
+ typeParametersChildren,
47
+ });
48
+
49
+ return (
50
+ <>
51
+ <Show when={Boolean(props.doc)}>
52
+ <JSDoc>
53
+ {props.doc && <Prose children={props.doc} />}
54
+ {Array.isArray(props.parameters) && (
55
+ <JSDocParams parameters={props.parameters} />
56
+ )}
57
+ </JSDoc>
58
+ <hbr />
59
+ </Show>
60
+ <Scope kind="function">
61
+ <CallSignature {...callSignatureProps} returnType={null} />
62
+ {" => "}
63
+ {returnType}
64
+ </Scope>
65
+ </>
66
+ );
67
+ },
68
+ );
69
+
32
70
  /**
33
71
  * A TypeScript function declaration.
34
72
  *
@@ -46,43 +84,10 @@ export interface FunctionTypeProps extends CallSignatureProps {
46
84
  * 2. As raw content via the `parametersChildren` or `typeParametersChildren`
47
85
  * props.
48
86
  * 3. As a child of this component via the
49
- * {@link (FunctionType:namespace).Parameters} or
50
- * {@link (FunctionType:namespace).TypeParameters} components.
87
+ * {@link FunctionType.Parameters} or
88
+ * {@link FunctionType.TypeParameters} components.
51
89
  */
52
- export function FunctionType(props: FunctionTypeProps) {
53
- const children = childrenArray(() => props.children);
54
- const typeParametersChildren =
55
- findKeyedChild(children, TypeParameters.tag) ?? undefined;
56
- const parametersChildren =
57
- findKeyedChild(children, FunctionParameters.tag) ?? undefined;
58
- const returnType = getReturnType(props.returnType ?? "void", {
59
- async: props.async,
60
- });
61
-
62
- const callSignatureProps = getCallSignatureProps(props, {
63
- parametersChildren,
64
- typeParametersChildren,
65
- });
66
-
67
- return (
68
- <>
69
- <Show when={Boolean(props.doc)}>
70
- <JSDoc>
71
- {props.doc && <Prose children={props.doc} />}
72
- {Array.isArray(props.parameters) && (
73
- <JSDocParams parameters={props.parameters} />
74
- )}
75
- </JSDoc>
76
- <hbr />
77
- </Show>
78
- <Scope kind="function">
79
- <CallSignature {...callSignatureProps} returnType={null} />
80
- {" => "}
81
- {returnType}
82
- </Scope>
83
- </>
84
- );
85
- }
86
-
87
- FunctionType.TypeParameters = TypeParameters;
88
- FunctionType.Parameters = FunctionTypeParameters;
90
+ export const FunctionType = Object.assign(FunctionTypeComponent, {
91
+ TypeParameters,
92
+ Parameters: FunctionTypeParameters,
93
+ });
@@ -9,6 +9,7 @@ import {
9
9
  ImportedSymbol,
10
10
  ImportRecords,
11
11
  TSPackageScope,
12
+ TSSymbolFlags,
12
13
  } from "../symbols/index.js";
13
14
  import { modulePath } from "../utils.js";
14
15
  import { usePackage } from "./PackageDirectory.js";
@@ -90,11 +91,23 @@ export function ImportStatement(props: ImportStatementProps) {
90
91
  }
91
92
 
92
93
  if (namedImportSymbols.length > 0) {
94
+ const allNamedImportsAreTypes = namedImportSymbols.every(
95
+ (nis) => nis.local.tsFlags & TSSymbolFlags.TypeSymbol,
96
+ );
97
+
98
+ if (allNamedImportsAreTypes) {
99
+ parts.push("type ");
100
+ }
93
101
  parts.push("{ ");
94
102
  parts.push(
95
103
  mapJoin(
96
104
  () => namedImportSymbols,
97
- (nis) => <ImportBinding importedSymbol={nis} />,
105
+ (nis) => (
106
+ <ImportBinding
107
+ importedSymbol={nis}
108
+ inTypeImport={allNamedImportsAreTypes}
109
+ />
110
+ ),
98
111
  { joiner: ", " },
99
112
  ),
100
113
  );
@@ -109,9 +122,10 @@ export function ImportStatement(props: ImportStatementProps) {
109
122
  interface ImportBindingProps {
110
123
  importedSymbol: ImportedSymbol;
111
124
  default?: boolean;
125
+ inTypeImport?: boolean;
112
126
  }
113
127
 
114
- function ImportBinding(props: ImportBindingProps) {
128
+ function ImportBinding(props: Readonly<ImportBindingProps>) {
115
129
  const text = memo(() => {
116
130
  const localName = props.importedSymbol.local.name;
117
131
  const targetName = props.importedSymbol.target.name;
@@ -124,5 +138,18 @@ function ImportBinding(props: ImportBindingProps) {
124
138
  }
125
139
  });
126
140
 
127
- return text;
141
+ const prefix = memo(() =>
142
+ (
143
+ !props.inTypeImport &&
144
+ props.importedSymbol.local.tsFlags & TSSymbolFlags.TypeSymbol
145
+ ) ?
146
+ "type "
147
+ : "",
148
+ );
149
+ return (
150
+ <>
151
+ {prefix}
152
+ {text}
153
+ </>
154
+ );
128
155
  }
@@ -16,6 +16,7 @@ import { JSDoc } from "./JSDoc.jsx";
16
16
 
17
17
  import { MemberDeclaration } from "./MemberDeclaration.jsx";
18
18
  import { PropertyName } from "./PropertyName.jsx";
19
+ import { ensureTypeRefContext } from "./TypeRefContext.jsx";
19
20
 
20
21
  export interface InterfaceDeclarationProps extends BaseDeclarationProps {
21
22
  extends?: Children;
@@ -30,47 +31,51 @@ export interface InterfaceDeclarationProps extends BaseDeclarationProps {
30
31
  * `default` boolean props determine whether and how this symbol is exported
31
32
  * from the package.
32
33
  */
33
- export function InterfaceDeclaration(props: InterfaceDeclarationProps) {
34
- const extendsPart = props.extends ? <> extends {props.extends}</> : "";
35
- const flags = OutputSymbolFlags.StaticMemberContainer;
34
+ export const InterfaceDeclaration = ensureTypeRefContext(
35
+ (props: InterfaceDeclarationProps) => {
36
+ const extendsPart = props.extends ? <> extends {props.extends}</> : "";
37
+ const flags = OutputSymbolFlags.StaticMemberContainer;
36
38
 
37
- return (
38
- <>
39
- <Show when={Boolean(props.doc)}>
40
- <JSDoc children={props.doc} />
41
- <hbr />
42
- </Show>
43
- <Declaration {...props} nameKind="interface" flags={flags} kind="type">
44
- interface <Name />
45
- {extendsPart}{" "}
46
- <InterfaceExpression>{props.children}</InterfaceExpression>
47
- </Declaration>
48
- </>
49
- );
50
- }
39
+ return (
40
+ <>
41
+ <Show when={Boolean(props.doc)}>
42
+ <JSDoc children={props.doc} />
43
+ <hbr />
44
+ </Show>
45
+ <Declaration {...props} nameKind="interface" flags={flags} kind="type">
46
+ interface <Name />
47
+ {extendsPart}{" "}
48
+ <InterfaceExpression>{props.children}</InterfaceExpression>
49
+ </Declaration>
50
+ </>
51
+ );
52
+ },
53
+ );
51
54
 
52
55
  export interface InterfaceExpressionProps {
53
56
  children?: Children;
54
57
  }
55
58
 
56
- export function InterfaceExpression(props: InterfaceExpressionProps) {
57
- const parentMemberSym = useMemberDeclaration();
59
+ export const InterfaceExpression = ensureTypeRefContext(
60
+ (props: InterfaceExpressionProps) => {
61
+ const parentMemberSym = useMemberDeclaration();
58
62
 
59
- if (parentMemberSym) {
60
- parentMemberSym.binder.addStaticMembersToSymbol(parentMemberSym);
61
- }
62
- return (
63
- <group>
64
- <Wrap
65
- when={!!parentMemberSym}
66
- with={MemberScope}
67
- props={{ owner: parentMemberSym! }}
68
- >
69
- <Block>{props.children}</Block>
70
- </Wrap>
71
- </group>
72
- );
73
- }
63
+ if (parentMemberSym) {
64
+ parentMemberSym.binder.addStaticMembersToSymbol(parentMemberSym);
65
+ }
66
+ return (
67
+ <group>
68
+ <Wrap
69
+ when={!!parentMemberSym}
70
+ with={MemberScope}
71
+ props={{ owner: parentMemberSym! }}
72
+ >
73
+ <Block>{props.children}</Block>
74
+ </Wrap>
75
+ </group>
76
+ );
77
+ },
78
+ );
74
79
 
75
80
  export interface InterfaceMemberProps {
76
81
  name?: string;
@@ -78,6 +83,7 @@ export interface InterfaceMemberProps {
78
83
  type?: Children;
79
84
  children?: Children;
80
85
  optional?: boolean;
86
+ nullish?: boolean;
81
87
  readonly?: boolean;
82
88
  doc?: Children;
83
89
  refkey?: Refkey | Refkey[];
@@ -116,6 +122,7 @@ export function InterfaceMember(props: InterfaceMemberProps) {
116
122
  kind="type"
117
123
  nameKind="interface-member"
118
124
  flags={OutputSymbolFlags.StaticMember}
125
+ nullish={props.nullish ?? props.optional}
119
126
  refkey={props.refkey}
120
127
  >
121
128
  <Show when={Boolean(props.doc)}>
@@ -17,7 +17,7 @@ export function JSDocComment(props: JSDocCommentProps) {
17
17
  <hbr />
18
18
  {props.children}
19
19
  </align>
20
- <hbr /> **/
20
+ <hbr /> */
21
21
  </>
22
22
  );
23
23
  }
@@ -40,6 +40,10 @@ export interface MemberDeclarationProps
40
40
  * The symbol to use for this memberdeclaration.
41
41
  */
42
42
  symbol?: TSOutputSymbol;
43
+ /**
44
+ * Whether this member can be null or undefined.
45
+ */
46
+ nullish?: boolean;
43
47
  }
44
48
 
45
49
  export function MemberDeclaration(props: Readonly<MemberDeclarationProps>) {
@@ -50,7 +54,9 @@ export function MemberDeclaration(props: Readonly<MemberDeclarationProps>) {
50
54
  } else {
51
55
  const namePolicy = useTSNamePolicy();
52
56
 
53
- let tsFlags: TSSymbolFlags = TSSymbolFlags.None;
57
+ let tsFlags: TSSymbolFlags =
58
+ props.nullish ? TSSymbolFlags.Nullish : TSSymbolFlags.None;
59
+
54
60
  if (props.kind === "type") {
55
61
  tsFlags |= TSSymbolFlags.TypeSymbol;
56
62
  }
@@ -11,6 +11,7 @@ import {
11
11
  toRef,
12
12
  useBinder,
13
13
  } from "@alloy-js/core";
14
+ import { TSOutputSymbol, TSSymbolFlags } from "../symbols/ts-output-symbol.js";
14
15
  import { isValidJSIdentifier } from "../utils.js";
15
16
 
16
17
  export interface MemberExpressionProps {
@@ -78,7 +79,9 @@ export function MemberExpression(props: MemberExpressionProps): Children {
78
79
  const symbolRef = binder.getSymbolForRefkey(partProps.refkey);
79
80
  part.base = computed(() => {
80
81
  if (symbolRef.value) {
81
- part.nullish.value = !!symbolRef.value.metadata.nullish;
82
+ part.nullish.value = !!(
83
+ (symbolRef.value as TSOutputSymbol).tsFlags & TSSymbolFlags.Nullish
84
+ );
82
85
  part.accessStyle.value = accessStyleForMemberName(
83
86
  symbolRef.value.name,
84
87
  );
@@ -88,7 +91,9 @@ export function MemberExpression(props: MemberExpressionProps): Children {
88
91
  }
89
92
  });
90
93
  } else if (partProps.symbol) {
91
- part.nullish.value = !!partProps.symbol.metadata.nullish;
94
+ part.nullish.value = !!(
95
+ (partProps.symbol as TSOutputSymbol).tsFlags & TSSymbolFlags.Nullish
96
+ );
92
97
  part.accessStyle = computed(() => {
93
98
  return accessStyleForMemberName(partProps.symbol!.name);
94
99
  });
@@ -1,12 +1,22 @@
1
1
  import { Refkey } from "@alloy-js/core";
2
+ import { isTypeRefContext } from "../context/type-ref-context.js";
2
3
  import { ref } from "../symbols/index.js";
3
4
 
4
5
  export interface ReferenceProps {
5
6
  refkey: Refkey;
7
+
8
+ /**
9
+ * Whether this is a reference to a type.
10
+ *
11
+ * @remarks
12
+ * This affect things like import where `type` keyword might be used.
13
+ */
14
+ type?: boolean;
6
15
  }
7
16
 
8
- export function Reference({ refkey }: ReferenceProps) {
9
- const reference = ref(refkey);
17
+ export function Reference({ refkey, type }: ReferenceProps) {
18
+ const inTypeRef = isTypeRefContext();
19
+ const reference = ref(refkey, { type: type ?? inTypeRef });
10
20
 
11
21
  return <>{reference}</>;
12
22
  }
@@ -1,19 +1,22 @@
1
1
  import { Name, Show } from "@alloy-js/core";
2
2
  import { BaseDeclarationProps, Declaration } from "./Declaration.js";
3
3
  import { JSDoc } from "./JSDoc.jsx";
4
+ import { ensureTypeRefContext } from "./TypeRefContext.jsx";
4
5
 
5
6
  export interface TypeDeclarationProps extends BaseDeclarationProps {}
6
7
 
7
- export function TypeDeclaration(props: TypeDeclarationProps) {
8
- return (
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
- </>
18
- );
19
- }
8
+ export const TypeDeclaration = ensureTypeRefContext(
9
+ (props: TypeDeclarationProps) => {
10
+ return (
11
+ <>
12
+ <Show when={Boolean(props.doc)}>
13
+ <JSDoc children={props.doc} />
14
+ <hbr />
15
+ </Show>
16
+ <Declaration {...props} nameKind="type">
17
+ type <Name /> = {props.children};
18
+ </Declaration>
19
+ </>
20
+ );
21
+ },
22
+ );
@@ -0,0 +1,49 @@
1
+ import type { Children } from "@alloy-js/core/jsx-runtime";
2
+ import {
3
+ isTypeRefContext,
4
+ TypeRefContext as TypeRefContextDef,
5
+ } from "../context/type-ref-context.jsx";
6
+
7
+ export interface TypeRefContextProps {
8
+ /**
9
+ * Children
10
+ */
11
+ children: Children;
12
+ }
13
+
14
+ /**
15
+ * Set the current context of reference to be type reference.
16
+ *
17
+ * @remarks
18
+ * References used inside the children of this component will be treated as type reference {@link Reference}.
19
+ *
20
+ * Prefer using {@link ensureTypeRefContext} when wrapping the whole component to reduce tree nodes.
21
+ */
22
+ export const TypeRefContext = ({ children }: TypeRefContextProps) => {
23
+ return (
24
+ <TypeRefContextDef.Provider value={{ type: true }}>
25
+ {children}
26
+ </TypeRefContextDef.Provider>
27
+ );
28
+ };
29
+
30
+ /** Ensure the current component is inside a type ref context.
31
+ * If not it will wrap in a {@link TypeRefContext} component.
32
+ * If yes it will not add an extra node and return the original component.
33
+ */
34
+ export function ensureTypeRefContext<A extends unknown[]>(
35
+ fn: (...args: A) => Children,
36
+ ): (...args: A) => Children {
37
+ return (...args: A) => {
38
+ const ref = isTypeRefContext();
39
+ if (!ref) {
40
+ return (
41
+ <TypeRefContextDef.Provider value={{ type: true }}>
42
+ {fn(...args)}
43
+ </TypeRefContextDef.Provider>
44
+ );
45
+ }
46
+
47
+ return fn(...args);
48
+ };
49
+ }
@@ -7,9 +7,10 @@ import {
7
7
  Show,
8
8
  } from "@alloy-js/core";
9
9
  import { useTSNamePolicy } from "../name-policy.js";
10
- import { createTSSymbol } from "../symbols/index.js";
10
+ import { createTSSymbol, TSSymbolFlags } from "../symbols/index.js";
11
11
  import { BaseDeclarationProps } from "./Declaration.js";
12
12
  import { JSDoc } from "./JSDoc.jsx";
13
+ import { TypeRefContext } from "./TypeRefContext.jsx";
13
14
 
14
15
  export interface VarDeclarationProps extends BaseDeclarationProps {
15
16
  const?: boolean;
@@ -17,6 +18,7 @@ export interface VarDeclarationProps extends BaseDeclarationProps {
17
18
  var?: boolean;
18
19
  initializer?: Children;
19
20
  type?: Children;
21
+ nullish?: boolean;
20
22
  }
21
23
 
22
24
  export function VarDeclaration(props: VarDeclarationProps) {
@@ -24,7 +26,8 @@ export function VarDeclaration(props: VarDeclarationProps) {
24
26
  props.var ? "var"
25
27
  : props.let ? "let"
26
28
  : "const";
27
- const type = props.type ? <>: {props.type}</> : undefined;
29
+ const type =
30
+ props.type ? <TypeRefContext>: {props.type}</TypeRefContext> : undefined;
28
31
  const name = useTSNamePolicy().getName(props.name, "variable");
29
32
  const sym = createTSSymbol({
30
33
  name: name,
@@ -32,6 +35,7 @@ export function VarDeclaration(props: VarDeclarationProps) {
32
35
  default: props.default,
33
36
  export: props.export,
34
37
  metadata: props.metadata,
38
+ tsFlags: props.nullish ? TSSymbolFlags.Nullish : TSSymbolFlags.None,
35
39
  });
36
40
 
37
41
  const assignmentContext = createAssignmentContext(sym);
@@ -36,5 +36,6 @@ export * from "./SourceFile.js";
36
36
  export * from "./SwitchStatement.jsx";
37
37
  export * from "./TsConfigJson.js";
38
38
  export * from "./TypeDeclaration.js";
39
+ export * from "./TypeRefContext.jsx";
39
40
  export * from "./ValueExpression.js";
40
41
  export * from "./VarDeclaration.js";
@@ -42,5 +42,6 @@ export const CaseClause = stc(base.CaseClause);
42
42
  export const TSConfigJson = stc(base.TSConfigJson);
43
43
  export const FunctionType = stc(base.FunctionType);
44
44
  export const TypeDeclaration = stc(base.TypeDeclaration);
45
+ export const TypeRefContext = stc(base.TypeRefContext);
45
46
  export const ValueExpression = stc(base.ValueExpression);
46
47
  export const VarDeclaration = stc(base.VarDeclaration);
@@ -0,0 +1,19 @@
1
+ import { ComponentContext, createContext, useContext } from "@alloy-js/core";
2
+
3
+ interface TypeRefContext {
4
+ /** If in a type context */
5
+ type?: boolean;
6
+ }
7
+
8
+ /**
9
+ * Provides scopes for instance and static private members.
10
+ */
11
+ export const TypeRefContext: ComponentContext<TypeRefContext> =
12
+ createContext<TypeRefContext>();
13
+
14
+ /**
15
+ * @returns 'true' if in a type context, 'false' if in a value context.
16
+ */
17
+ export function isTypeRefContext(): boolean {
18
+ return useContext(TypeRefContext)?.type ?? false;
19
+ }
@@ -24,6 +24,13 @@ export interface ParameterDescriptorBase {
24
24
  */
25
25
  readonly optional?: boolean;
26
26
 
27
+ /**
28
+ * Whether the parameter is can be null or undefined.
29
+ * when optional is true, this will be set to true by default,
30
+ * but explicitly setting this to false will override that.
31
+ */
32
+ readonly nullish?: boolean;
33
+
27
34
  /**
28
35
  * Arbitrary metadata for the parameter symbol.
29
36
  */