@alloy-js/typescript 0.11.0 → 0.13.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 +17 -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 +108 -5
  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 +293 -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 +75 -5
  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 +215 -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
@@ -20,7 +20,10 @@ import { TSModuleScope } from "./ts-module-scope.js";
20
20
  import { TSOutputSymbol, TSSymbolFlags } from "./ts-output-symbol.js";
21
21
  import { TSPackageScope } from "./ts-package-scope.js";
22
22
 
23
- export function ref(refkey: Refkey): () => string {
23
+ export interface RefOptions {
24
+ type?: boolean;
25
+ }
26
+ export function ref(refkey: Refkey, options?: RefOptions): () => string {
24
27
  const sourceFile = useContext(SourceFileContext);
25
28
  const resolveResult = resolve<TSOutputScope, TSOutputSymbol>(
26
29
  refkey as Refkey,
@@ -103,8 +106,13 @@ export function ref(refkey: Refkey): () => string {
103
106
  ];
104
107
 
105
108
  const importSymbol = symbolPath[0];
109
+
106
110
  localSymbol = untrack(() =>
107
- sourceFile!.scope.addImport(importSymbol, pathDown[0] as TSModuleScope),
111
+ sourceFile!.scope.addImport(
112
+ importSymbol,
113
+ pathDown[0] as TSModuleScope,
114
+ { type: options?.type },
115
+ ),
108
116
  );
109
117
  }
110
118
 
@@ -131,17 +139,21 @@ function buildMemberExpression(path: TSOutputSymbol[]) {
131
139
  path = path.slice(1);
132
140
  }
133
141
 
142
+ let prevNullish = base.tsFlags & TSSymbolFlags.Nullish;
134
143
  for (const sym of path) {
144
+ const optional = prevNullish ? "?" : "";
135
145
  if (sym.tsFlags & TSSymbolFlags.PrivateMember) {
136
- memberExpr += `.#${sym.name}`;
146
+ memberExpr += `${optional}.#${sym.name}`;
137
147
  } else if (
138
148
  isValidJSIdentifier(sym.name) &&
139
149
  !(sym.tsFlags & TSSymbolFlags.TypeSymbol)
140
150
  ) {
141
- memberExpr += `.${sym.name}`;
151
+ memberExpr += `${optional}.${sym.name}`;
142
152
  } else {
143
- memberExpr += `[${JSON.stringify(sym.name)}]`;
153
+ const joiner = sym.tsFlags & TSSymbolFlags.Nullish ? "?." : "";
154
+ memberExpr += `${joiner}[${JSON.stringify(sym.name)}]`;
144
155
  }
156
+ prevNullish = sym.tsFlags & TSSymbolFlags.Nullish;
145
157
  }
146
158
 
147
159
  return memberExpr;
@@ -11,6 +11,10 @@ export interface ImportedSymbol {
11
11
  }
12
12
  export type ImportRecords = Map<TSModuleScope, Set<ImportedSymbol>>;
13
13
 
14
+ export interface AddImportOptions {
15
+ type?: boolean;
16
+ }
17
+
14
18
  export interface TSModuleScope extends OutputScope {
15
19
  kind: "module";
16
20
  exportedSymbols: Map<Refkey, TSOutputSymbol>;
@@ -19,7 +23,11 @@ export interface TSModuleScope extends OutputScope {
19
23
  */
20
24
  importedSymbols: Map<TSOutputSymbol, TSOutputSymbol>;
21
25
  importedModules: ImportRecords;
22
- addImport(symbol: TSOutputSymbol, module: TSModuleScope): TSOutputSymbol;
26
+ addImport(
27
+ symbol: TSOutputSymbol,
28
+ module: TSModuleScope,
29
+ options?: AddImportOptions,
30
+ ): TSOutputSymbol;
23
31
  }
24
32
 
25
33
  export function createTSModuleScope(
@@ -34,9 +42,13 @@ export function createTSModuleScope(
34
42
  exportedSymbols: new Map(),
35
43
  importedSymbols: new Map(),
36
44
  importedModules: new Map(),
37
- addImport(this: TSModuleScope, targetSymbol, targetModule) {
38
- if (this.importedSymbols.has(targetSymbol)) {
39
- return this.importedSymbols.get(targetSymbol)!;
45
+ addImport(this: TSModuleScope, targetSymbol, targetModule, options) {
46
+ const existing = this.importedSymbols.get(targetSymbol);
47
+ if (existing) {
48
+ if (!options?.type && existing.tsFlags & TSSymbolFlags.TypeSymbol) {
49
+ existing.tsFlags &= ~TSSymbolFlags.TypeSymbol;
50
+ }
51
+ return existing;
40
52
  }
41
53
 
42
54
  if (targetModule.kind !== "module") {
@@ -54,6 +66,9 @@ export function createTSModuleScope(
54
66
  refkey: refkey({}),
55
67
  tsFlags: TSSymbolFlags.LocalImportSymbol,
56
68
  });
69
+ if (options?.type) {
70
+ localSymbol.tsFlags |= TSSymbolFlags.TypeSymbol;
71
+ }
57
72
 
58
73
  this.importedSymbols.set(targetSymbol, localSymbol);
59
74
  this.importedModules.get(targetModule)!.add({
@@ -16,6 +16,7 @@ export enum TSSymbolFlags {
16
16
  ParameterSymbol = 1 << 2,
17
17
  PrivateMember = 1 << 3,
18
18
  PrivateMemberContainer = 1 << 4,
19
+ Nullish = 1 << 5,
19
20
  }
20
21
 
21
22
  export interface TSOutputSymbol extends OutputSymbol {