tsc-ts 0.0.5 → 0.0.7

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 (203) hide show
  1. package/README.md +1 -1
  2. package/dist/api/compiler/ast.d.ts +67 -27
  3. package/dist/api/compiler/ast.js +156 -55
  4. package/dist/api/compiler/binder-utilities.js +4 -3
  5. package/dist/api/compiler/binder.js +14 -13
  6. package/dist/api/compiler/checker-calls.js +7 -6
  7. package/dist/api/compiler/checker-contextual.js +1 -1
  8. package/dist/api/compiler/checker-declaration-order.js +2 -1
  9. package/dist/api/compiler/checker-declarations.js +14 -14
  10. package/dist/api/compiler/checker-declared-types.d.ts +1 -1
  11. package/dist/api/compiler/checker-declared-types.js +12 -11
  12. package/dist/api/compiler/checker-decorators.js +3 -3
  13. package/dist/api/compiler/checker-deprecation.js +17 -18
  14. package/dist/api/compiler/checker-destructuring.d.ts +1 -1
  15. package/dist/api/compiler/checker-destructuring.js +3 -3
  16. package/dist/api/compiler/checker-elaboration.js +8 -7
  17. package/dist/api/compiler/checker-emit-helpers.d.ts +2 -1
  18. package/dist/api/compiler/checker-emit-helpers.js +5 -4
  19. package/dist/api/compiler/checker-emit-resolver.d.ts +1 -1
  20. package/dist/api/compiler/checker-emit-resolver.js +5 -4
  21. package/dist/api/compiler/checker-grammar.js +23 -9
  22. package/dist/api/compiler/checker-heritage.d.ts +1 -1
  23. package/dist/api/compiler/checker-heritage.js +14 -13
  24. package/dist/api/compiler/checker-inference.d.ts +21 -11
  25. package/dist/api/compiler/checker-inference.js +57 -34
  26. package/dist/api/compiler/checker-javascript.d.ts +1 -1
  27. package/dist/api/compiler/checker-javascript.js +5 -4
  28. package/dist/api/compiler/checker-jsx.d.ts +1 -1
  29. package/dist/api/compiler/checker-jsx.js +4 -4
  30. package/dist/api/compiler/checker-literals.js +2 -2
  31. package/dist/api/compiler/checker-members.d.ts +32 -3
  32. package/dist/api/compiler/checker-members.js +240 -53
  33. package/dist/api/compiler/checker-modules.d.ts +15 -1
  34. package/dist/api/compiler/checker-modules.js +86 -14
  35. package/dist/api/compiler/checker-object-literals.d.ts +1 -1
  36. package/dist/api/compiler/checker-object-literals.js +11 -7
  37. package/dist/api/compiler/checker-overloads.d.ts +1 -1
  38. package/dist/api/compiler/checker-overloads.js +4 -3
  39. package/dist/api/compiler/checker-property-access.d.ts +1 -1
  40. package/dist/api/compiler/checker-property-access.js +2 -2
  41. package/dist/api/compiler/checker-services.js +2 -2
  42. package/dist/api/compiler/checker-signatures.d.ts +1 -1
  43. package/dist/api/compiler/checker-signatures.js +9 -8
  44. package/dist/api/compiler/checker-symbol-types.d.ts +2 -18
  45. package/dist/api/compiler/checker-symbol-types.js +22 -952
  46. package/dist/api/compiler/checker-symbols.d.ts +2 -2
  47. package/dist/api/compiler/checker-symbols.js +29 -27
  48. package/dist/api/compiler/checker-type-factory.d.ts +2 -2
  49. package/dist/api/compiler/checker-type-factory.js +57 -16
  50. package/dist/api/compiler/checker-type-nodes.d.ts +1 -1
  51. package/dist/api/compiler/checker-type-nodes.js +2 -2
  52. package/dist/api/compiler/checker-type-operators.d.ts +1 -1
  53. package/dist/api/compiler/checker-type-operators.js +5 -5
  54. package/dist/api/compiler/checker-types.d.ts +73 -7
  55. package/dist/api/compiler/checker-types.js +220 -43
  56. package/dist/api/compiler/checker-unused.d.ts +1 -1
  57. package/dist/api/compiler/checker-unused.js +3 -3
  58. package/dist/api/compiler/checker.d.ts +45 -8
  59. package/dist/api/compiler/checker.js +46 -9
  60. package/dist/api/compiler/diagnostics.d.ts +2 -8
  61. package/dist/api/compiler/diagnostics.js +13 -45
  62. package/dist/api/compiler/file-include.d.ts +35 -8
  63. package/dist/api/compiler/file-include.js +81 -18
  64. package/dist/api/compiler/file-loader.d.ts +33 -4
  65. package/dist/api/compiler/file-loader.js +90 -24
  66. package/dist/api/compiler/instantiation.d.ts +1 -1
  67. package/dist/api/compiler/instantiation.js +86 -50
  68. package/dist/api/compiler/link-tables.d.ts +69 -0
  69. package/dist/api/compiler/link-tables.js +236 -0
  70. package/dist/api/compiler/name-resolver.js +3 -3
  71. package/dist/api/compiler/node-builder-names.d.ts +1 -1
  72. package/dist/api/compiler/node-builder-names.js +12 -12
  73. package/dist/api/compiler/node-builder-pseudo.d.ts +1 -1
  74. package/dist/api/compiler/node-builder-pseudo.js +2 -1
  75. package/dist/api/compiler/node-builder.d.ts +1 -1
  76. package/dist/api/compiler/node-builder.js +16 -16
  77. package/dist/api/compiler/parallel-parse.d.ts +2 -0
  78. package/dist/api/compiler/parallel-parse.js +7 -1
  79. package/dist/api/compiler/parser.d.ts +33 -16
  80. package/dist/api/compiler/parser.js +127 -98
  81. package/dist/api/compiler/printer/emit-context.d.ts +5 -2
  82. package/dist/api/compiler/printer/emit-context.js +19 -13
  83. package/dist/api/compiler/printer/factory-base.js +8 -8
  84. package/dist/api/compiler/printer/factory.js +4 -4
  85. package/dist/api/compiler/printer/printer.js +6 -6
  86. package/dist/api/compiler/program-checker.d.ts +5 -0
  87. package/dist/api/compiler/program-checker.js +14 -1
  88. package/dist/api/compiler/program.js +9 -1
  89. package/dist/api/compiler/relations.d.ts +1 -1
  90. package/dist/api/compiler/relations.js +18 -13
  91. package/dist/api/compiler/reparser.js +13 -5
  92. package/dist/api/compiler/symbol-accessibility.d.ts +1 -1
  93. package/dist/api/compiler/symbol-accessibility.js +123 -26
  94. package/dist/api/compiler/symbol.d.ts +45 -3
  95. package/dist/api/compiler/symbol.js +121 -10
  96. package/dist/api/compiler/transformers/alias-references.d.ts +1 -1
  97. package/dist/api/compiler/transformers/alias-references.js +2 -1
  98. package/dist/api/compiler/transformers/class-fields.js +3 -3
  99. package/dist/api/compiler/transformers/commonjs-module.js +3 -3
  100. package/dist/api/compiler/transformers/declarations-diagnostics.js +2 -1
  101. package/dist/api/compiler/transformers/declarations-pseudo.js +6 -4
  102. package/dist/api/compiler/transformers/declarations-syntactic.d.ts +1 -1
  103. package/dist/api/compiler/transformers/declarations-syntactic.js +9 -9
  104. package/dist/api/compiler/transformers/declarations-tracker.d.ts +1 -1
  105. package/dist/api/compiler/transformers/declarations-tracker.js +3 -2
  106. package/dist/api/compiler/transformers/declarations.js +6 -5
  107. package/dist/api/compiler/transformers/module.js +2 -2
  108. package/dist/api/compiler/transformers/optional-chain.js +3 -3
  109. package/dist/api/compiler/transformers/reference-resolver.d.ts +2 -1
  110. package/dist/api/compiler/transformers/reference-resolver.js +26 -24
  111. package/dist/api/compiler/transformers/subtree-facts.d.ts +1 -0
  112. package/dist/api/compiler/transformers/subtree-facts.js +9 -7
  113. package/dist/api/compiler/transformers/syntax-facts.d.ts +1 -0
  114. package/dist/api/compiler/transformers/syntax-facts.js +9 -5
  115. package/dist/api/compiler/transformers/tagged-template.js +2 -2
  116. package/dist/api/compiler/transformers/type-eraser.js +2 -2
  117. package/dist/api/compiler/transformers/type-serialization.js +12 -12
  118. package/dist/api/compiler/transformers/visitor-base.js +3 -3
  119. package/dist/api/compiler/tsconfig.js +2 -2
  120. package/package.json +9 -9
  121. package/src/compiler/ast.ts +211 -78
  122. package/src/compiler/binder-utilities.ts +4 -3
  123. package/src/compiler/binder.ts +26 -16
  124. package/src/compiler/checker-calls.ts +8 -6
  125. package/src/compiler/checker-contextual.ts +1 -1
  126. package/src/compiler/checker-declaration-order.ts +2 -2
  127. package/src/compiler/checker-declarations.ts +21 -14
  128. package/src/compiler/checker-declared-types.ts +12 -12
  129. package/src/compiler/checker-decorators.ts +3 -3
  130. package/src/compiler/checker-deprecation.ts +17 -21
  131. package/src/compiler/checker-destructuring.ts +3 -3
  132. package/src/compiler/checker-elaboration.ts +8 -8
  133. package/src/compiler/checker-emit-helpers.ts +5 -4
  134. package/src/compiler/checker-emit-resolver.ts +5 -5
  135. package/src/compiler/checker-grammar.ts +22 -8
  136. package/src/compiler/checker-heritage.ts +21 -14
  137. package/src/compiler/checker-inference.ts +66 -34
  138. package/src/compiler/checker-javascript.ts +5 -5
  139. package/src/compiler/checker-jsx.ts +12 -8
  140. package/src/compiler/checker-literals.ts +2 -2
  141. package/src/compiler/checker-members.ts +255 -47
  142. package/src/compiler/checker-modules.ts +115 -14
  143. package/src/compiler/checker-object-literals.ts +22 -6
  144. package/src/compiler/checker-overloads.ts +6 -6
  145. package/src/compiler/checker-property-access.ts +2 -2
  146. package/src/compiler/checker-services.ts +2 -2
  147. package/src/compiler/checker-signatures.ts +9 -9
  148. package/src/compiler/checker-symbol-types.ts +20 -940
  149. package/src/compiler/checker-symbols.ts +42 -35
  150. package/src/compiler/checker-type-factory.ts +53 -16
  151. package/src/compiler/checker-type-nodes.ts +2 -2
  152. package/src/compiler/checker-type-operators.ts +13 -5
  153. package/src/compiler/checker-types.ts +223 -43
  154. package/src/compiler/checker-unused.ts +3 -3
  155. package/src/compiler/checker.ts +50 -9
  156. package/src/compiler/decoder.ts +5 -4
  157. package/src/compiler/diagnostics.ts +14 -48
  158. package/src/compiler/encoder.ts +10 -5
  159. package/src/compiler/file-include.ts +85 -17
  160. package/src/compiler/file-loader.ts +104 -35
  161. package/src/compiler/incremental.ts +39 -5
  162. package/src/compiler/instantiation.ts +99 -55
  163. package/src/compiler/link-tables.ts +245 -0
  164. package/src/compiler/name-resolver.ts +3 -3
  165. package/src/compiler/node-builder-names.ts +21 -19
  166. package/src/compiler/node-builder-pseudo.ts +2 -2
  167. package/src/compiler/node-builder.ts +24 -16
  168. package/src/compiler/parallel-check.ts +11 -8
  169. package/src/compiler/parallel-parse.ts +8 -1
  170. package/src/compiler/parse-thread.ts +0 -4
  171. package/src/compiler/parser.ts +173 -93
  172. package/src/compiler/printer/emit-context.ts +20 -15
  173. package/src/compiler/printer/factory-base.ts +14 -7
  174. package/src/compiler/printer/factory.ts +5 -3
  175. package/src/compiler/printer/printer.ts +8 -6
  176. package/src/compiler/program-checker.ts +12 -1
  177. package/src/compiler/program.ts +8 -1
  178. package/src/compiler/relations.ts +20 -12
  179. package/src/compiler/reparser.ts +14 -4
  180. package/src/compiler/symbol-accessibility.ts +120 -25
  181. package/src/compiler/symbol.ts +127 -9
  182. package/src/compiler/transformers/alias-references.ts +2 -2
  183. package/src/compiler/transformers/class-fields.ts +12 -2
  184. package/src/compiler/transformers/commonjs-module.ts +3 -2
  185. package/src/compiler/transformers/declarations-diagnostics.ts +2 -1
  186. package/src/compiler/transformers/declarations-pseudo.ts +9 -5
  187. package/src/compiler/transformers/declarations-syntactic.ts +17 -9
  188. package/src/compiler/transformers/declarations-tracker.ts +3 -3
  189. package/src/compiler/transformers/declarations.ts +7 -7
  190. package/src/compiler/transformers/module.ts +2 -1
  191. package/src/compiler/transformers/optional-chain.ts +5 -2
  192. package/src/compiler/transformers/reference-resolver.ts +37 -25
  193. package/src/compiler/transformers/subtree-facts.ts +15 -6
  194. package/src/compiler/transformers/syntax-facts.ts +10 -5
  195. package/src/compiler/transformers/tagged-template.ts +2 -2
  196. package/src/compiler/transformers/type-eraser.ts +2 -1
  197. package/src/compiler/transformers/type-serialization.ts +12 -12
  198. package/src/compiler/transformers/visitor-base.ts +10 -5
  199. package/src/compiler/tsc-build.ts +17 -13
  200. package/src/compiler/tsc-cli.ts +9 -6
  201. package/src/compiler/tsc-compile.ts +16 -7
  202. package/src/compiler/tsc-watch.ts +94 -26
  203. package/src/compiler/tsconfig.ts +2 -1
@@ -4,17 +4,7 @@ import { checkJsxAttribute } from './checker-jsx.js';
4
4
  import { getTargetOfAliasDeclaration, resolveExternalModuleSymbol } from './checker-modules.js';
5
5
  import { CachedTypeKind, cachedTypeKey, CheckMode } from './checker-types.js';
6
6
  import {
7
- type ArrayLiteralExpression,
8
- type BindingElement,
9
- type BindingPattern,
10
- type BinaryExpression,
11
- type Block,
12
- type ConditionalExpression,
13
- type ForInOrOfStatement,
14
7
  ModifierFlags,
15
- NodeVisitor,
16
- type ObjectLiteralExpression,
17
- type PrefixUnaryExpression,
18
8
  type Node,
19
9
  NodeFlags,
20
10
  getArguments,
@@ -28,14 +18,12 @@ import {
28
18
  getDotDotDotToken,
29
19
  getElements,
30
20
  getExpression,
31
- getLocals,
32
21
  getPostfixToken,
33
22
  getQuestionToken,
34
23
  getText,
35
24
  type PropertyAssignment,
36
25
  type ShorthandPropertyAssignment,
37
26
  type SourceFile,
38
- type VariableDeclarationList,
39
27
  } from './ast.js';
40
28
  import {
41
29
  declarationNameToString,
@@ -48,15 +36,11 @@ import {
48
36
  hasStaticModifier,
49
37
  hasSyntacticModifier,
50
38
  isCatchClauseVariableDeclarationOrBindingElement,
51
- isExternalModule,
52
39
  isJsonSourceFile,
53
40
  isTypeNodeKind,
54
- skipParentheses,
55
41
  } from './binder-utilities.js';
56
42
  import { identifierToKeywordKind } from './scanner.js';
57
- import { tsgoCheckerSlots } from './symbol-accessibility.js';
58
43
  import { type DiagnosticMessage, Diagnostics } from './diagnostics.js';
59
- import { CheckFlags, type Symbol, SymbolFlags, getSymbolId } from './symbol.js';
60
44
  import { SyntaxKind } from './syntax-kind.js';
61
45
  import { type Checker, createDiagnosticForNode } from './checker.js';
62
46
  import {
@@ -93,7 +77,6 @@ import {
93
77
  pushTypeResolution,
94
78
  reportCircularityError,
95
79
  resolveAlias,
96
- getMergedSymbol,
97
80
  TypeSystemPropertyName,
98
81
  getGlobalTypeAliasSymbol,
99
82
  resolveName,
@@ -118,6 +101,8 @@ import {
118
101
  getPropertiesOfType,
119
102
  getPropertyOfObjectType,
120
103
  isObjectLiteralType,
104
+ keepPropertiesOnly,
105
+ scannedLiteralPropertiesLimit,
121
106
  } from './checker-members.js';
122
107
  import { typeToString, symbolToString } from './type-printer.js';
123
108
  import { getTypeOfMappedSymbol, getIndexType } from './checker-type-operators.js';
@@ -129,7 +114,6 @@ import {
129
114
  import {
130
115
  getReturnTypeFromBody,
131
116
  getTypeOfObjectLiteralMethod,
132
- ReturnCollector,
133
117
  shouldReportErrorsFromWideningWithContextualSignature,
134
118
  } from './checker-functions.js';
135
119
  import { getTypeOfReverseMappedSymbol } from './checker-inference.js';
@@ -139,13 +123,13 @@ import {
139
123
  getContextualThisParameterType,
140
124
  checkExpressionCached,
141
125
  checkExpressionEx,
142
- isConstTypeReference,
143
126
  } from './checker-expressions.js';
144
127
  import { checkPropertyAssignment, checkShorthandPropertyAssignment } from './checker-object-literals.js';
145
128
  import { getTypeForBindingElement, getTypeFromBindingPattern } from './checker-destructuring.js';
146
129
  import { checkRightHandSideOfForOf } from './checker-statements.js';
147
130
  import { getTypeWithFacts, TypeFacts, hasTypeFacts, getNonNullableType } from './checker-flow.js';
148
131
  import { getTypeOfModuleExportsSymbol, getWidenedTypeForAssignmentDeclaration } from './checker-javascript.js';
132
+ import { CheckFlags, copyDeclarations, getDeclarations, getSymbolId, type Symbol, SymbolFlags } from './symbol.js';
149
133
 
150
134
  export function getTypeOfSymbol(c: Checker, symbol: Symbol): Type {
151
135
  const checkFlags = symbol.checkFlags;
@@ -420,7 +404,8 @@ export function getTypeForVariableLikeDeclaration(
420
404
  // A set accessor parameter takes the getter's type.
421
405
  if (fn.kind === SyntaxKind.SetAccessor) {
422
406
  const fnSymbol = getSymbolOfDeclaration(c, fn);
423
- const getter = fnSymbol?.declarations.find((d) => d.kind === SyntaxKind.GetAccessor);
407
+ const getter =
408
+ fnSymbol !== undefined ? getDeclarations(fnSymbol).find((d) => d.kind === SyntaxKind.GetAccessor) : undefined;
424
409
  if (getter !== undefined) {
425
410
  const getterSignature = getSignatureFromDeclaration(c, getter);
426
411
  // An unannotated `this` of a setter takes the getter's `this` type.
@@ -589,7 +574,7 @@ function reportWideningErrorsInType(c: Checker, t: Type): boolean {
589
574
  if (errorReported) continue;
590
575
  // Property types can come from object literal normalization in unions.
591
576
  let valueDeclaration: Node | undefined;
592
- for (const d of p.declarations) {
577
+ for (const d of getDeclarations(p)) {
593
578
  const v = getSymbol(d)?.valueDeclaration;
594
579
  if (v !== undefined && v.parent === t.symbol?.valueDeclaration) {
595
580
  valueDeclaration = d;
@@ -813,6 +798,7 @@ function getWidenedTypeOfObjectLiteral(c: Checker, t: Type, context: WideningCon
813
798
  ),
814
799
  );
815
800
  const result = createAnonymousType(c, t.symbol, members, [], [], infos);
801
+ keepPropertiesOnly(c, result, members, scannedLiteralPropertiesLimit);
816
802
  result.objectFlags |= t.objectFlags & (ObjectFlags.JSLiteral | ObjectFlags.NonInferrableType);
817
803
  // The root context widens each object literal once; a property's context may widen one literal
818
804
  // for several siblings.
@@ -834,7 +820,7 @@ function getWidenedProperty(c: Checker, prop: Symbol, context: WideningContext |
834
820
 
835
821
  function createSymbolWithWidenedType(c: Checker, prop: Symbol, t: Type): Symbol {
836
822
  const result = c.newSymbolWithCheckFlags(prop.flags, prop.name, prop.checkFlags & CheckFlags.Readonly);
837
- result.declarations = prop.declarations;
823
+ copyDeclarations(result, prop);
838
824
  result.parent = prop.parent;
839
825
  result.valueDeclaration = prop.valueDeclaration;
840
826
  const id = getSymbolId(result);
@@ -845,926 +831,20 @@ function createSymbolWithWidenedType(c: Checker, prop: Symbol, t: Type): Symbol
845
831
  }
846
832
 
847
833
  /**
848
- * An optional `undefined` property standing in for `prop`, the last property of that name among the
849
- * sibling object literals (getPropertiesOfContext). It carries the declarations of the property it is
850
- * created from, which order it among the members when they are printed.
851
- *
852
- * tsgo caches one such symbol per name and checker, so the declarations come from the first literal
853
- * its checker widened with that name missing in a sibling. Its checkers check the program's files
854
- * round robin, each file from the top. tsc-ts's checkers take files in another order, and a checker
855
- * can infer a file's functions for an importer before it checks the file, so the first union is found
856
- * in the sources of the files tsgo checks with the checker of the property's file, up to that file
857
- * (`firstSiblingProperty`), which doesn't depend on the order of checking. Without one, the property
858
- * is cached per sibling property. The order differs from tsgo's where tsgo's checker first infers a
859
- * function of a file of another checker, or a later function of the file.
834
+ * An optional `undefined` property standing in for `prop`, a property of a sibling object literal
835
+ * (getPropertiesOfContext), one per property. It carries the declarations of `prop`, which order it
836
+ * among the members when they are printed.
860
837
  */
861
838
  function getUndefinedProperty(c: Checker, prop: Symbol): Symbol {
862
- const member = prop.declarations.length > 0 ? firstSiblingProperty(c, prop.declarations[0]!, prop.name) : undefined;
863
- const first = member !== undefined ? getSymbol(member) : undefined;
864
- const source = first ?? prop;
865
- const key = getSymbolId(source);
839
+ const key = getSymbolId(prop);
866
840
  const cached = c.undefinedProperties.get(key);
867
841
  if (cached !== undefined) return cached;
868
- const result = createSymbolWithWidenedType(c, source, c.undefinedOrMissingType);
842
+ const result = createSymbolWithWidenedType(c, prop, c.undefinedOrMissingType);
869
843
  result.flags |= SymbolFlags.Optional;
870
- // The properties of a literal in an `as const` assertion are readonly.
871
- if (first !== undefined && isInConstAssertion(member!.parent!)) result.checkFlags |= CheckFlags.Readonly;
872
844
  c.undefinedProperties.set(key, result);
873
845
  return result;
874
846
  }
875
847
 
876
- /**
877
- * The property of `name` in the first union of object literals that leaves `name` missing in one of
878
- * them, among the files tsgo checks with the checker of the declaration's file, up to that file: the
879
- * last literal's property of that name. tsgo's checker `k` checks files `k`, `k + n`, `k + 2n`, ...
880
- * in order (`tsgoCheckerSlots`). Unions are those of the returned expressions of a function without
881
- * a return type, and of the alternatives of an initializer without a type (`?:`, `||`, `??`, array
882
- * elements, an element access of an array literal, and the values of a property across the literals),
883
- * of the variables of a `for...of`, of the object of a method call and of the expression of a type
884
- * assertion. In a file, they are taken in the order a checker checking the file from the top widens
885
- * them (`SiblingPropertyScan`).
886
- */
887
- function firstSiblingProperty(c: Checker, declaration: Node, name: string): Node | undefined {
888
- const file = getSourceFileOfNode(declaration);
889
- const index = c.fileIndex.get(file);
890
- if (index === undefined) return siblingPropertiesOfFile(c, file).get(name);
891
- const slots = tsgoCheckerSlots(c);
892
- let slot = c.slotSiblingProperties.get(index % slots);
893
- if (slot === undefined) {
894
- slot = new SlotSiblingProperties(index % slots);
895
- c.slotSiblingProperties.set(index % slots, slot);
896
- }
897
- for (; slot.next <= index; slot.next += slots)
898
- for (const [n, member] of siblingPropertiesOfFile(c, c.files[slot.next]!))
899
- if (!slot.members.has(n)) {
900
- slot.members.set(n, member);
901
- slot.fileIndexes.set(n, slot.next);
902
- }
903
- const found = slot.fileIndexes.get(name);
904
- return found !== undefined && found <= index ? slot.members.get(name) : undefined;
905
- }
906
-
907
- /**
908
- * By name, the first property of the unions of `file` that leave the name missing: what a parse thread
909
- * found (`SourceFile.siblingProperties`), or else the checker's own scan.
910
- */
911
- function siblingPropertiesOfFile(c: Checker, file: SourceFile): Map<string, Node> {
912
- // A parse thread's scan takes array literals to have the global Array type.
913
- if (file.siblingProperties !== undefined && c.globalArrayType !== c.emptyGenericType) return file.siblingProperties;
914
- let first = c.firstSiblingProperties.get(file);
915
- if (first === undefined) {
916
- first = scanSiblingProperties(file, c)!;
917
- c.firstSiblingProperties.set(file, first);
918
- }
919
- return first;
920
- }
921
-
922
- /**
923
- * By name, the first property of the unions of `file`'s object literals that leave the name missing.
924
- * Without a checker, as on a parse thread, array literals are taken to have the global Array type, and
925
- * the result is undefined where an enum the scan can't resolve decides which literals subtype reduction
926
- * leaves out (`enumKeyOfMemberAccess`); the checker then scans the file itself.
927
- */
928
- export function scanSiblingProperties(file: SourceFile, c: Checker | undefined): Map<string, Node> | undefined {
929
- if (file.isDeclarationFile) return new Map<string, Node>();
930
- const scan = new SiblingPropertyScan(c);
931
- file.forEachChild(scan);
932
- scan.widenAll();
933
- return scan.unresolved ? undefined : scan.members;
934
- }
935
-
936
- /** The first property of each name among the files of one of tsgo's checkers, scanned in order. */
937
- export class SlotSiblingProperties {
938
- readonly members = new Map<string, Node>();
939
- /** The index of the file of each name's property. */
940
- readonly fileIndexes = new Map<string, number>();
941
- /** The index of the next file of the checker to scan. */
942
- next: number;
943
-
944
- constructor(first: number) {
945
- this.next = first;
946
- }
947
- }
948
-
949
- /** Whether an object literal is in an `as const` assertion, through arrays, spreads and properties. */
950
- function isInConstAssertion(literal: Node): boolean {
951
- for (let node = literal; node.parent !== undefined; node = node.parent) {
952
- switch (node.parent.kind) {
953
- case SyntaxKind.ParenthesizedExpression:
954
- case SyntaxKind.ArrayLiteralExpression:
955
- case SyntaxKind.SpreadElement:
956
- case SyntaxKind.PropertyAssignment:
957
- case SyntaxKind.ObjectLiteralExpression:
958
- continue;
959
- case SyntaxKind.AsExpression:
960
- case SyntaxKind.TypeAssertionExpression:
961
- return isConstTypeReference(getType(node.parent)!);
962
- }
963
- return false;
964
- }
965
- return false;
966
- }
967
-
968
- /** An expression whose type is widened, at the position where a checker widens it. */
969
- class WideningRoot {
970
- constructor(
971
- readonly position: number,
972
- /** The function whose returned expressions these are, widened once. */
973
- readonly func: Node | undefined,
974
- /** The expressions whose types are unioned and widened. */
975
- readonly alternatives: Node[],
976
- /** The expressions whose calls, innermost first, infer their functions' return types before. */
977
- readonly sources: Node[],
978
- /** The calls of `sources` that may call a function of the file (`SiblingPropertyScan.callees`). */
979
- public calls: Node[] | undefined = undefined,
980
- ) {}
981
- }
982
-
983
- /**
984
- * The unions a checker widens while checking the file's statements or the body of one deferred function, and
985
- * the deferred functions it queues there.
986
- */
987
- class CheckScope {
988
- readonly roots: WideningRoot[] = [];
989
- readonly deferred: CheckScope[] = [];
990
- /** The calls by a name (`calleeName`), which infer where they end. */
991
- readonly calls: Node[] = [];
992
-
993
- constructor(readonly position: number) {}
994
- }
995
-
996
- /**
997
- * Whether a checker checks `node`'s body only as a deferred node: a function expression, arrow
998
- * function or object literal method or accessor whose return type is written. Without one, the
999
- * checker infers the return type where it reaches the function, from the returned expressions and
1000
- * what they refer to.
1001
- */
1002
- function isDeferredCheck(node: Node): boolean {
1003
- switch (node.kind) {
1004
- case SyntaxKind.FunctionExpression:
1005
- case SyntaxKind.ArrowFunction:
1006
- return getType(node) !== undefined;
1007
- case SyntaxKind.MethodDeclaration:
1008
- case SyntaxKind.GetAccessor:
1009
- return node.parent?.kind === SyntaxKind.ObjectLiteralExpression && getType(node) !== undefined;
1010
- case SyntaxKind.SetAccessor:
1011
- return node.parent?.kind === SyntaxKind.ObjectLiteralExpression;
1012
- }
1013
- return false;
1014
- }
1015
-
1016
- /**
1017
- * Finds the unions of a file's object literals in the order a checker widens them: an initializer
1018
- * where it is, and a function's returned expressions at its first `return`, where the checker
1019
- * infers its return type. A call of a function of the file, by a name that resolves to it or as
1020
- * `this.name(...)` in a class, infers that function's return type where the call is, and before the
1021
- * union of the expression the call is in. The bodies of function expressions, arrow functions and
1022
- * object literal methods and accessors with a written return type are checked after the file's
1023
- * statements, so their unions widen then.
1024
- */
1025
- class SiblingPropertyScan extends NodeVisitor {
1026
- /** The checker that resolves imported enums, or undefined on a parse thread. */
1027
- readonly c: Checker | undefined;
1028
- /** By name, the property of the first union that leaves the name missing in a literal. */
1029
- readonly members = new Map<string, Node>();
1030
- /**
1031
- * Whether array literals have an array type, whose elements are widened as one union. Without the
1032
- * global Array type (--noLib) an array literal is an empty object type.
1033
- */
1034
- private readonly arrays: boolean;
1035
- /** Whether an enum the scan can't resolve without a checker decided which literals were left out. */
1036
- unresolved = false;
1037
- /** The enums of the members that values refer to, numbered in the order the scan meets them. */
1038
- private readonly enums = new Map<Symbol, number>();
1039
- private readonly fileScope = new CheckScope(0);
1040
- /** The scope of the innermost deferred function around the node being visited. */
1041
- private scope = this.fileScope;
1042
- /** The returned expressions of each function without a return type. */
1043
- private readonly returned = new Map<Node, WideningRoot>();
1044
- /** The names a call can call those functions by (`functionName`). */
1045
- private readonly callees = new Set<string>();
1046
- private readonly widened = new Set<Node>();
1047
- /** The functions and methods of each class, by name. */
1048
- private readonly classFunctions = new Map<Node, Map<string, Node>>();
1049
-
1050
- constructor(c: Checker | undefined) {
1051
- super();
1052
- this.c = c;
1053
- this.arrays = c === undefined || c.globalArrayType !== c.emptyGenericType;
1054
- }
1055
-
1056
- /** The key of the values that are members of `symbol`, an enum, in this scan. */
1057
- enumKey(symbol: Symbol): string {
1058
- let id = this.enums.get(symbol);
1059
- if (id === undefined) {
1060
- id = this.enums.size;
1061
- this.enums.set(symbol, id);
1062
- }
1063
- return 'enum ' + id;
1064
- }
1065
-
1066
- visitNode(node: Node): boolean {
1067
- switch (node.kind) {
1068
- case SyntaxKind.FunctionDeclaration:
1069
- case SyntaxKind.FunctionExpression:
1070
- case SyntaxKind.ArrowFunction:
1071
- case SyntaxKind.MethodDeclaration:
1072
- case SyntaxKind.GetAccessor: {
1073
- const body = getBody(node);
1074
- if (body === undefined || getType(node) !== undefined) break;
1075
- const returned: Node[] = [];
1076
- const sources: Node[] = [];
1077
- let position = body.pos;
1078
- if (body.kind === SyntaxKind.Block) {
1079
- const collector = new ReturnCollector();
1080
- collector.visitStatements(body as Block);
1081
- if (collector.returns.length > 0) position = collector.returns[0]!.pos;
1082
- for (const r of collector.returns)
1083
- if (r.expression !== undefined) {
1084
- addAlternatives(r.expression, returned, this.arrays);
1085
- sources.push(r.expression);
1086
- }
1087
- } else {
1088
- addAlternatives(body, returned, this.arrays);
1089
- sources.push(body);
1090
- }
1091
- if (returned.length === 0) break;
1092
- const root = new WideningRoot(position, node, returned, sources);
1093
- this.returned.set(node, root);
1094
- this.addRoot(root);
1095
- if (node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.GetAccessor)
1096
- this.addClassFunction(node, node);
1097
- break;
1098
- }
1099
- case SyntaxKind.VariableDeclaration:
1100
- case SyntaxKind.PropertyDeclaration:
1101
- case SyntaxKind.Parameter: {
1102
- const initializer = getInitializer(node);
1103
- if (initializer === undefined || getType(node) !== undefined) break;
1104
- this.addRoot(
1105
- new WideningRoot(initializer.pos, undefined, addAlternatives(initializer, [], this.arrays), [initializer]),
1106
- );
1107
- const value = skipParentheses(initializer);
1108
- if (
1109
- node.kind === SyntaxKind.PropertyDeclaration &&
1110
- (value.kind === SyntaxKind.ArrowFunction || value.kind === SyntaxKind.FunctionExpression)
1111
- )
1112
- this.addClassFunction(node, value);
1113
- break;
1114
- }
1115
- case SyntaxKind.ExportAssignment:
1116
- if (getType(node) === undefined) {
1117
- const expression = getExpression(node)!;
1118
- this.addRoot(
1119
- new WideningRoot(node.pos, undefined, addAlternatives(expression, [], this.arrays), [expression]),
1120
- );
1121
- }
1122
- break;
1123
- case SyntaxKind.ForOfStatement: {
1124
- // The variable of `for (const x of e)` has the widened type of e's elements, and each element
1125
- // of a binding pattern the widened type of its part of them.
1126
- const statement = node as ForInOrOfStatement;
1127
- if (statement.initializer.kind !== SyntaxKind.VariableDeclarationList) break;
1128
- const declaration = (statement.initializer as VariableDeclarationList).declarations.nodes[0];
1129
- if (declaration === undefined || getType(declaration) !== undefined || !this.arrays) break;
1130
- const elements: Node[] = [];
1131
- for (const array of iteratedArrayLiterals(statement.expression)) addArrayElements(array, elements);
1132
- if (elements.length === 0) break;
1133
- for (const alternatives of bindingAlternatives(getName(declaration)!, elements))
1134
- this.addRoot(new WideningRoot(statement.expression.pos, undefined, alternatives, [statement.expression]));
1135
- break;
1136
- }
1137
- case SyntaxKind.CallExpression: {
1138
- // The object of a method call is widened before the call is resolved.
1139
- const callee = skipParentheses(getExpression(node)!);
1140
- if (
1141
- (callee.kind === SyntaxKind.PropertyAccessExpression || callee.kind === SyntaxKind.ElementAccessExpression) &&
1142
- mayHoldLiterals(getExpression(callee)!)
1143
- )
1144
- this.addExpressionRoot(callee.pos, getExpression(callee)!);
1145
- // Calls elsewhere, as in statements, infer where they end.
1146
- if (calleeName(node) !== undefined) this.scope.calls.push(node);
1147
- break;
1148
- }
1149
- }
1150
- if (isTypeNodeKind(node.kind)) return false;
1151
- if (
1152
- (node.kind === SyntaxKind.AsExpression || node.kind === SyntaxKind.TypeAssertionExpression) &&
1153
- !isConstTypeReference(getType(node)!)
1154
- ) {
1155
- // An assertion widens its expression's type in a deferred check, queued after the expression's.
1156
- node.forEachChild(this);
1157
- const inner = new CheckScope(node.end);
1158
- this.scope.deferred.push(inner);
1159
- const saveScope = this.scope;
1160
- this.scope = inner;
1161
- this.addExpressionRoot(node.end, getExpression(node)!);
1162
- this.scope = saveScope;
1163
- return false;
1164
- }
1165
- if (isDeferredCheck(node)) {
1166
- const inner = new CheckScope(node.pos);
1167
- this.scope.deferred.push(inner);
1168
- const saveScope = this.scope;
1169
- this.scope = inner;
1170
- node.forEachChild(this);
1171
- this.scope = saveScope;
1172
- } else node.forEachChild(this);
1173
- return false;
1174
- }
1175
-
1176
- private addRoot(root: WideningRoot): void {
1177
- this.scope.roots.push(root);
1178
- }
1179
-
1180
- /** Adds a root for the widened type of `expression` where it holds object or array literals. */
1181
- private addExpressionRoot(position: number, expression: Node): void {
1182
- const alternatives = addAlternatives(expression, [], this.arrays);
1183
- if (
1184
- alternatives.some(
1185
- (a) => a.kind === SyntaxKind.ObjectLiteralExpression || a.kind === SyntaxKind.ArrayLiteralExpression,
1186
- )
1187
- )
1188
- this.addRoot(new WideningRoot(position, undefined, alternatives, [expression]));
1189
- }
1190
-
1191
- /** Names `func` in its class by `member`, a method or a property it initializes. */
1192
- private addClassFunction(member: Node, func: Node): void {
1193
- const name = getName(member);
1194
- const parent = member.parent!;
1195
- if (name === undefined || name.kind !== SyntaxKind.Identifier) return;
1196
- if (parent.kind !== SyntaxKind.ClassDeclaration && parent.kind !== SyntaxKind.ClassExpression) return;
1197
- let functions = this.classFunctions.get(parent);
1198
- if (functions === undefined) this.classFunctions.set(parent, (functions = new Map()));
1199
- if (!functions.has(getText(name))) functions.set(getText(name), func);
1200
- }
1201
-
1202
- /**
1203
- * Widens the unions of the file's statements in order, then those of each deferred node in the
1204
- * order the checker queues them, which includes the deferred nodes queued while checking one.
1205
- */
1206
- widenAll(): void {
1207
- for (const func of this.returned.keys()) {
1208
- const name = functionName(func);
1209
- if (name !== undefined) this.callees.add(name);
1210
- }
1211
- const queue = [this.fileScope];
1212
- for (let i = 0; i < queue.length; i++) {
1213
- const scope = queue[i]!;
1214
- if (this.callees.size !== 0)
1215
- for (const call of scope.calls)
1216
- if (this.callees.has(calleeName(call)!))
1217
- scope.roots.push(new WideningRoot(call.end, undefined, [], [], [call]));
1218
- scope.roots.sort((a, b) => a.position - b.position);
1219
- scope.deferred.sort((a, b) => a.position - b.position);
1220
- let next = 0;
1221
- for (const root of scope.roots) {
1222
- while (next < scope.deferred.length && scope.deferred[next]!.position < root.position)
1223
- queue.push(scope.deferred[next++]!);
1224
- this.widenRoot(root);
1225
- }
1226
- while (next < scope.deferred.length) queue.push(scope.deferred[next++]!);
1227
- }
1228
- }
1229
-
1230
- /** Widens a root's unions, after the returned expressions of the functions its calls infer. */
1231
- private widenRoot(root: WideningRoot): void {
1232
- if (root.func !== undefined) {
1233
- if (this.widened.has(root.func)) return;
1234
- this.widened.add(root.func);
1235
- }
1236
- if (this.callees.size === 0) {
1237
- this.widen(root.alternatives, true);
1238
- return;
1239
- }
1240
- if (root.calls === undefined) {
1241
- const collector = new CallCollector(this.callees);
1242
- for (const source of root.sources) collector.visitNode(source);
1243
- root.calls = collector.calls;
1244
- }
1245
- for (const call of root.calls) {
1246
- const callee = this.calledFunction(call);
1247
- const returned = callee !== undefined ? this.returned.get(callee) : undefined;
1248
- if (returned !== undefined) this.widenRoot(returned);
1249
- }
1250
- this.widen(root.alternatives, true);
1251
- }
1252
-
1253
- /**
1254
- * The function a call calls: a function declaration or a variable initialized with a function that
1255
- * the callee's name resolves to (`getLocals` of the scopes around the call), or a method or property
1256
- * of the class for `this.name(...)` in a class member.
1257
- */
1258
- private calledFunction(call: Node): Node | undefined {
1259
- const callee = skipParentheses(getExpression(call)!);
1260
- if (callee.kind === SyntaxKind.Identifier) {
1261
- const name = getText(callee);
1262
- for (let scope = call.parent; scope !== undefined; scope = scope.parent) {
1263
- const symbol = getLocals(scope)?.get(name);
1264
- // A type alias, interface or type parameter of the name doesn't hide a value of an outer scope.
1265
- if (symbol === undefined || (symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) === 0) continue;
1266
- // An exported declaration's local symbol has no value declaration of its own.
1267
- const declaration =
1268
- symbol.valueDeclaration ??
1269
- symbol.declarations.find(
1270
- (d) => d.kind === SyntaxKind.FunctionDeclaration || d.kind === SyntaxKind.VariableDeclaration,
1271
- );
1272
- if (declaration === undefined) return undefined;
1273
- if (declaration.kind === SyntaxKind.FunctionDeclaration) return declaration;
1274
- const initializer =
1275
- declaration.kind === SyntaxKind.VariableDeclaration ? getInitializer(declaration) : undefined;
1276
- const value = initializer !== undefined ? skipParentheses(initializer) : undefined;
1277
- return value !== undefined &&
1278
- (value.kind === SyntaxKind.ArrowFunction || value.kind === SyntaxKind.FunctionExpression)
1279
- ? value
1280
- : undefined;
1281
- }
1282
- return undefined;
1283
- }
1284
- if (callee.kind !== SyntaxKind.PropertyAccessExpression || getExpression(callee)!.kind !== SyntaxKind.ThisKeyword)
1285
- return undefined;
1286
- const name = getName(callee)!;
1287
- const container = thisClass(call);
1288
- const functions = container !== undefined ? this.classFunctions.get(container) : undefined;
1289
- return functions !== undefined && name.kind === SyntaxKind.Identifier ? functions.get(getText(name)) : undefined;
1290
- }
1291
-
1292
- /**
1293
- * Records the unions of the alternatives of one widened expression, and of their nested values.
1294
- * With `reduced`, the alternatives make up one union with subtype reduction (the alternatives of an
1295
- * expression, the elements of an array literal), which drops a literal whose widened type is the
1296
- * type of an earlier one (`removeSubtypes` removes the later of two identical types). The values of
1297
- * a property across literals are not reduced: their union is one of each literal's value.
1298
- */
1299
- private widen(alternatives: Node[], reduced: boolean): void {
1300
- let literals: ObjectLiteralExpression[] = [];
1301
- for (const a of alternatives)
1302
- if (a.kind === SyntaxKind.ObjectLiteralExpression) literals.push(a as ObjectLiteralExpression);
1303
- // The elements of a tuple, an array literal in an `as const` assertion, widen separately.
1304
- else if (this.arrays && a.kind === SyntaxKind.ArrayLiteralExpression && !isInConstAssertion(a))
1305
- this.widen(addArrayElements(a as ArrayLiteralExpression, []), true);
1306
- if (literals.length === 0) return;
1307
- if (reduced) literals = withoutRepeatedLiterals(this, literals);
1308
- if (literals.length > 1) this.record(literals);
1309
- const values = new Map<string, Node[]>();
1310
- for (const literal of literals)
1311
- for (const member of literal.properties.nodes) {
1312
- if (member.kind !== SyntaxKind.PropertyAssignment) continue;
1313
- const name = staticPropertyName(member);
1314
- if (name === undefined) continue;
1315
- let list = values.get(name);
1316
- if (list === undefined) values.set(name, (list = []));
1317
- // A value's own alternatives make up one reduced union, one member of the property's values.
1318
- const own = addAlternatives((member as PropertyAssignment).initializer, [], this.arrays);
1319
- for (const value of withoutRepeatedLiterals(this, own)) list.push(value);
1320
- }
1321
- for (const list of values.values()) this.widen(list, false);
1322
- }
1323
-
1324
- /**
1325
- * Records, for each name that some literal without a spread lacks, the last such literal's property
1326
- * of that name, unless an earlier union has the name.
1327
- */
1328
- private record(literals: ObjectLiteralExpression[]): void {
1329
- const plain: Map<string, Node>[] = [];
1330
- for (const literal of literals) {
1331
- let members: Map<string, Node> | undefined = new Map();
1332
- for (const member of literal.properties.nodes) {
1333
- if (member.kind === SyntaxKind.SpreadAssignment) {
1334
- members = undefined;
1335
- break;
1336
- }
1337
- const name = staticPropertyName(member);
1338
- if (name !== undefined) members.set(name, member);
1339
- }
1340
- if (members !== undefined) plain.push(members);
1341
- }
1342
- const last = new Map<string, Node>();
1343
- for (const members of plain) for (const [name, member] of members) last.set(name, member);
1344
- for (const [name, member] of last) {
1345
- // A method's or accessor's symbol has other flags than the property checkObjectLiteral makes of it.
1346
- if (member.kind !== SyntaxKind.PropertyAssignment && member.kind !== SyntaxKind.ShorthandPropertyAssignment)
1347
- continue;
1348
- if (this.members.has(name) || plain.every((members) => members.has(name))) continue;
1349
- this.members.set(name, member);
1350
- }
1351
- }
1352
- }
1353
-
1354
- /**
1355
- * `nodes` without the object literals whose widened types are certainly those of an earlier one:
1356
- * the same property names, each with a value of the same widened type (`widenedTypeKey`). Literals
1357
- * that can have a contextual type are kept: it can keep their properties' literal types.
1358
- */
1359
- function withoutRepeatedLiterals<T extends Node>(scan: SiblingPropertyScan, nodes: T[]): T[] {
1360
- let keys: string[] | undefined;
1361
- let result: T[] | undefined;
1362
- for (let i = 0; i < nodes.length; i++) {
1363
- const node = nodes[i]!;
1364
- const key =
1365
- node.kind === SyntaxKind.ObjectLiteralExpression && !mayHaveContextualType(node)
1366
- ? widenedTypeKey(scan, node)
1367
- : undefined;
1368
- if (key !== undefined) {
1369
- if (keys === undefined) keys = [];
1370
- else if (keys.includes(key)) {
1371
- // Without a checker, an enum the scan can't resolve may or may not make the types the same.
1372
- if (key.includes(pendingEnumKey)) scan.unresolved = true;
1373
- if (result === undefined) result = nodes.slice(0, i);
1374
- continue;
1375
- }
1376
- keys.push(key);
1377
- }
1378
- result?.push(node);
1379
- }
1380
- return result ?? nodes;
1381
- }
1382
-
1383
- /**
1384
- * A key that two expressions of one file share only when their types widen to the same type, or
1385
- * undefined where that isn't known from the syntax: literals by their widened type, members of an
1386
- * enum by the enum, and object literals by their properties' names and keys. Other references can
1387
- * name different symbols or be narrowed differently.
1388
- */
1389
- function widenedTypeKey(scan: SiblingPropertyScan, node: Node): string | undefined {
1390
- switch (node.kind) {
1391
- case SyntaxKind.StringLiteral:
1392
- case SyntaxKind.NoSubstitutionTemplateLiteral:
1393
- case SyntaxKind.TemplateExpression:
1394
- return 'string';
1395
- case SyntaxKind.NumericLiteral:
1396
- return 'number';
1397
- case SyntaxKind.BigIntLiteral:
1398
- return 'bigint';
1399
- case SyntaxKind.TrueKeyword:
1400
- case SyntaxKind.FalseKeyword:
1401
- return 'boolean';
1402
- case SyntaxKind.RegularExpressionLiteral:
1403
- return 'RegExp';
1404
- case SyntaxKind.PrefixUnaryExpression: {
1405
- const unary = node as PrefixUnaryExpression;
1406
- const operand = unary.operand.kind;
1407
- if (unary.operator === SyntaxKind.MinusToken || unary.operator === SyntaxKind.PlusToken) {
1408
- if (operand === SyntaxKind.NumericLiteral) return 'number';
1409
- if (operand === SyntaxKind.BigIntLiteral && unary.operator === SyntaxKind.MinusToken) return 'bigint';
1410
- }
1411
- return undefined;
1412
- }
1413
- case SyntaxKind.PropertyAccessExpression:
1414
- return enumKeyOfMemberAccess(scan, node);
1415
- case SyntaxKind.ObjectLiteralExpression: {
1416
- const members: string[] = [];
1417
- for (const member of (node as ObjectLiteralExpression).properties.nodes) {
1418
- if (member.kind !== SyntaxKind.PropertyAssignment) return undefined;
1419
- const name = staticPropertyName(member);
1420
- const value = widenedTypeKey(scan, (member as PropertyAssignment).initializer);
1421
- if (name === undefined || value === undefined) return undefined;
1422
- members.push(JSON.stringify(name) + ': ' + value);
1423
- }
1424
- members.sort();
1425
- return '{' + members.join(', ') + '}';
1426
- }
1427
- }
1428
- return undefined;
1429
- }
1430
-
1431
- /** The key of a member of an enum that the scan can't resolve without a checker. */
1432
- const pendingEnumKey = 'enum ?';
1433
-
1434
- /**
1435
- * The key of `E.Member` where `E` names an enum in the scopes around the access (`getLocals`, as
1436
- * `calledFunction` finds names) and `Member` is one of its members, not an export of a namespace merged
1437
- * with it: the key of the enum. With a checker, `E` can be imported. Without one, an import, and a
1438
- * global of a script file (whose enum other files can extend), give `pendingEnumKey`.
1439
- */
1440
- function enumKeyOfMemberAccess(scan: SiblingPropertyScan, node: Node): string | undefined {
1441
- const target = getExpression(node)!;
1442
- const member = getName(node)!;
1443
- if (target.kind !== SyntaxKind.Identifier || member.kind !== SyntaxKind.Identifier) return undefined;
1444
- const name = getText(target);
1445
- const c = scan.c;
1446
- for (let scope = node.parent; scope !== undefined; scope = scope.parent) {
1447
- let symbol = getLocals(scope)?.get(name);
1448
- if (
1449
- symbol === undefined ||
1450
- (symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias)) === 0
1451
- )
1452
- continue;
1453
- if (c === undefined) {
1454
- if ((symbol.flags & SymbolFlags.Alias) !== 0) return pendingEnumKey;
1455
- if (
1456
- (symbol.flags & (SymbolFlags.Enum | SymbolFlags.Module)) !== 0 &&
1457
- !isExternalModule(getSourceFileOfNode(node))
1458
- )
1459
- return pendingEnumKey;
1460
- }
1461
- if (symbol.exportSymbol !== undefined) symbol = symbol.exportSymbol;
1462
- if (c !== undefined) {
1463
- symbol = getMergedSymbol(c, symbol);
1464
- if ((symbol.flags & SymbolFlags.Alias) !== 0) symbol = resolveAlias(c, symbol);
1465
- }
1466
- if ((symbol.flags & SymbolFlags.Enum) === 0) return undefined;
1467
- // An enum merged with a namespace also exports the namespace's values.
1468
- const memberSymbol = symbol.exports?.get(getText(member));
1469
- return memberSymbol !== undefined && (memberSymbol.flags & SymbolFlags.EnumMember) !== 0
1470
- ? scan.enumKey(symbol)
1471
- : undefined;
1472
- }
1473
- return undefined;
1474
- }
1475
-
1476
- /**
1477
- * Whether an object literal of a widened expression can have a contextual type: under `satisfies`,
1478
- * an assertion or an argument, as a parameter's default, or returned from a function that can have
1479
- * a contextual signature (a function expression that doesn't initialize an untyped declaration, or a
1480
- * method of an object literal). Initializers of untyped declarations, the returns of function
1481
- * declarations and class members, and the objects of property and element accesses have none.
1482
- */
1483
- function mayHaveContextualType(node: Node): boolean {
1484
- for (let current = node.parent; current !== undefined; current = current.parent) {
1485
- switch (current.kind) {
1486
- case SyntaxKind.ParenthesizedExpression:
1487
- case SyntaxKind.NonNullExpression:
1488
- case SyntaxKind.AwaitExpression:
1489
- case SyntaxKind.ConditionalExpression:
1490
- case SyntaxKind.BinaryExpression:
1491
- case SyntaxKind.ArrayLiteralExpression:
1492
- case SyntaxKind.ObjectLiteralExpression:
1493
- case SyntaxKind.PropertyAssignment:
1494
- case SyntaxKind.ReturnStatement:
1495
- case SyntaxKind.Block:
1496
- case SyntaxKind.IfStatement:
1497
- case SyntaxKind.DoStatement:
1498
- case SyntaxKind.WhileStatement:
1499
- case SyntaxKind.ForStatement:
1500
- case SyntaxKind.ForInStatement:
1501
- case SyntaxKind.ForOfStatement:
1502
- case SyntaxKind.SwitchStatement:
1503
- case SyntaxKind.CaseBlock:
1504
- case SyntaxKind.CaseClause:
1505
- case SyntaxKind.DefaultClause:
1506
- case SyntaxKind.LabeledStatement:
1507
- case SyntaxKind.TryStatement:
1508
- case SyntaxKind.CatchClause:
1509
- continue;
1510
- case SyntaxKind.VariableDeclaration:
1511
- case SyntaxKind.PropertyDeclaration:
1512
- case SyntaxKind.ExportAssignment:
1513
- case SyntaxKind.FunctionDeclaration:
1514
- // The object of a property access, as in a method call, has no contextual type.
1515
- case SyntaxKind.PropertyAccessExpression:
1516
- case SyntaxKind.ElementAccessExpression:
1517
- return false;
1518
- case SyntaxKind.MethodDeclaration:
1519
- case SyntaxKind.GetAccessor: {
1520
- const parent = current.parent!.kind;
1521
- return parent !== SyntaxKind.ClassDeclaration && parent !== SyntaxKind.ClassExpression;
1522
- }
1523
- case SyntaxKind.ArrowFunction:
1524
- case SyntaxKind.FunctionExpression: {
1525
- let outer = current.parent!;
1526
- while (outer.kind === SyntaxKind.ParenthesizedExpression) outer = outer.parent!;
1527
- return (
1528
- (outer.kind !== SyntaxKind.VariableDeclaration && outer.kind !== SyntaxKind.PropertyDeclaration) ||
1529
- getType(outer) !== undefined
1530
- );
1531
- }
1532
- default:
1533
- return true;
1534
- }
1535
- }
1536
- return true;
1537
- }
1538
-
1539
- /** Collects the calls by one of `names` in an expression, innermost first, outside the functions in it. */
1540
- class CallCollector extends NodeVisitor {
1541
- readonly calls: Node[] = [];
1542
-
1543
- constructor(private readonly names: Set<string>) {
1544
- super();
1545
- }
1546
-
1547
- visitNode(node: Node): boolean {
1548
- if (isFunctionLike(node) || node.kind === SyntaxKind.ClassExpression) return false;
1549
- node.forEachChild(this);
1550
- if (node.kind === SyntaxKind.CallExpression) {
1551
- const name = calleeName(node);
1552
- if (name !== undefined && this.names.has(name)) this.calls.push(node);
1553
- }
1554
- return false;
1555
- }
1556
- }
1557
-
1558
- /** The name a call calls its function by, `f` in `f(...)` and `this.f(...)`, if the scan can resolve it. */
1559
- function calleeName(call: Node): string | undefined {
1560
- const callee = skipParentheses(getExpression(call)!);
1561
- if (callee.kind === SyntaxKind.Identifier) return getText(callee);
1562
- if (callee.kind !== SyntaxKind.PropertyAccessExpression || getExpression(callee)!.kind !== SyntaxKind.ThisKeyword)
1563
- return undefined;
1564
- const name = getName(callee)!;
1565
- return name.kind === SyntaxKind.Identifier ? getText(name) : undefined;
1566
- }
1567
-
1568
- /**
1569
- * The name calls can call a function without a return type by: a function declaration's, a method's or
1570
- * accessor's, or that of the variable or property a function expression initializes.
1571
- */
1572
- function functionName(func: Node): string | undefined {
1573
- let node = func;
1574
- if (func.kind === SyntaxKind.FunctionExpression || func.kind === SyntaxKind.ArrowFunction) {
1575
- while (node.parent?.kind === SyntaxKind.ParenthesizedExpression) node = node.parent;
1576
- if (node.parent?.kind !== SyntaxKind.VariableDeclaration && node.parent?.kind !== SyntaxKind.PropertyDeclaration)
1577
- return undefined;
1578
- node = node.parent;
1579
- }
1580
- const name = getName(node);
1581
- return name?.kind === SyntaxKind.Identifier ? getText(name) : undefined;
1582
- }
1583
-
1584
- /** Whether an expression's alternatives (`addAlternatives`) can hold object or array literals. */
1585
- function mayHoldLiterals(node: Node): boolean {
1586
- switch (skipParentheses(node).kind) {
1587
- case SyntaxKind.ObjectLiteralExpression:
1588
- case SyntaxKind.ArrayLiteralExpression:
1589
- case SyntaxKind.ConditionalExpression:
1590
- case SyntaxKind.BinaryExpression:
1591
- case SyntaxKind.NonNullExpression:
1592
- case SyntaxKind.SatisfiesExpression:
1593
- case SyntaxKind.AwaitExpression:
1594
- case SyntaxKind.AsExpression:
1595
- case SyntaxKind.TypeAssertionExpression:
1596
- return true;
1597
- }
1598
- return false;
1599
- }
1600
-
1601
- /** The class whose instance `this` is at `node`, through arrow functions, in a member or property initializer. */
1602
- function thisClass(node: Node): Node | undefined {
1603
- for (let current = node.parent; current !== undefined; current = current.parent) {
1604
- if (current.kind === SyntaxKind.ArrowFunction) continue;
1605
- if (
1606
- current.kind === SyntaxKind.MethodDeclaration ||
1607
- current.kind === SyntaxKind.GetAccessor ||
1608
- current.kind === SyntaxKind.SetAccessor ||
1609
- current.kind === SyntaxKind.Constructor ||
1610
- current.kind === SyntaxKind.PropertyDeclaration
1611
- ) {
1612
- const parent = current.parent!;
1613
- return parent.kind === SyntaxKind.ClassDeclaration || parent.kind === SyntaxKind.ClassExpression
1614
- ? parent
1615
- : undefined;
1616
- }
1617
- if (isFunctionLike(current) || current.kind === SyntaxKind.SourceFile) return undefined;
1618
- }
1619
- return undefined;
1620
- }
1621
-
1622
- /**
1623
- * Adds the expressions whose types make up the type of `node`: both sides of `?:`, `||` and `??`,
1624
- * and the elements of an array literal that `node` indexes when `arrays` (see SiblingPropertyScan).
1625
- */
1626
- function addAlternatives(node: Node, out: Node[], arrays: boolean): Node[] {
1627
- switch (node.kind) {
1628
- case SyntaxKind.ParenthesizedExpression:
1629
- case SyntaxKind.NonNullExpression:
1630
- case SyntaxKind.SatisfiesExpression:
1631
- case SyntaxKind.AwaitExpression:
1632
- return addAlternatives(getExpression(node)!, out, arrays);
1633
- case SyntaxKind.AsExpression:
1634
- case SyntaxKind.TypeAssertionExpression:
1635
- // An object literal in an `as const` assertion is still an object literal type, with readonly
1636
- // properties; an array literal in one is a tuple, whose elements widen separately.
1637
- if (isConstAssertion(node) && skipParentheses(getExpression(node)!).kind === SyntaxKind.ObjectLiteralExpression)
1638
- return addAlternatives(getExpression(node)!, out, arrays);
1639
- break;
1640
- case SyntaxKind.ConditionalExpression:
1641
- addAlternatives((node as ConditionalExpression).whenTrue, out, arrays);
1642
- return addAlternatives((node as ConditionalExpression).whenFalse, out, arrays);
1643
- case SyntaxKind.BinaryExpression: {
1644
- const binary = node as BinaryExpression;
1645
- switch (binary.operatorToken.kind) {
1646
- case SyntaxKind.BarBarToken:
1647
- case SyntaxKind.QuestionQuestionToken:
1648
- addAlternatives(binary.left, out, arrays);
1649
- return addAlternatives(binary.right, out, arrays);
1650
- case SyntaxKind.AmpersandAmpersandToken:
1651
- case SyntaxKind.CommaToken:
1652
- return addAlternatives(binary.right, out, arrays);
1653
- }
1654
- break;
1655
- }
1656
- case SyntaxKind.ElementAccessExpression: {
1657
- // `[a, b][i]` has the type of the array's elements, which the declaration widens as one union.
1658
- const array = skipParentheses(getExpression(node)!);
1659
- if (!arrays || array.kind !== SyntaxKind.ArrayLiteralExpression) break;
1660
- for (const e of (array as ArrayLiteralExpression).elements.nodes)
1661
- if (e.kind !== SyntaxKind.SpreadElement) addAlternatives(e, out, arrays);
1662
- return out;
1663
- }
1664
- }
1665
- out.push(node);
1666
- return out;
1667
- }
1668
-
1669
- /**
1670
- * The alternatives of the elements of an array literal, whose types make its element type, with those
1671
- * of the array literals that its spread elements spread. Used where array literals have an array type
1672
- * (`SiblingPropertyScan.arrays`), as are `iteratedArrayLiterals` and `bindingAlternatives`.
1673
- */
1674
- function addArrayElements(array: ArrayLiteralExpression, out: Node[]): Node[] {
1675
- for (const e of array.elements.nodes)
1676
- if (e.kind !== SyntaxKind.SpreadElement) addAlternatives(e, out, true);
1677
- else
1678
- for (const spread of addAlternatives(getExpression(e)!, [], true))
1679
- if (spread.kind === SyntaxKind.ArrayLiteralExpression) addArrayElements(spread as ArrayLiteralExpression, out);
1680
- return out;
1681
- }
1682
-
1683
- /** The array literals whose elements a `for...of` over `expression` iterates, through `as const`. */
1684
- function iteratedArrayLiterals(expression: Node): ArrayLiteralExpression[] {
1685
- const arrays: ArrayLiteralExpression[] = [];
1686
- for (const a of addAlternatives(expression, [], true)) {
1687
- const value = isConstAssertion(a) ? skipParentheses(getExpression(a)!) : a;
1688
- if (value.kind === SyntaxKind.ArrayLiteralExpression) arrays.push(value as ArrayLiteralExpression);
1689
- }
1690
- return arrays;
1691
- }
1692
-
1693
- function isConstAssertion(node: Node): boolean {
1694
- return (
1695
- (node.kind === SyntaxKind.AsExpression || node.kind === SyntaxKind.TypeAssertionExpression) &&
1696
- isConstTypeReference(getType(node)!)
1697
- );
1698
- }
1699
-
1700
- /**
1701
- * The alternatives whose union each variable of a binding name is widened from, given the alternatives
1702
- * of the iterated elements: all of them for an identifier; for an element of an array pattern, its
1703
- * element of each `as const` array literal, or every element of another array literal; for an element
1704
- * of an object pattern, the values of its property in each object literal.
1705
- */
1706
- function bindingAlternatives(name: Node, elements: Node[]): Node[][] {
1707
- if (name.kind === SyntaxKind.Identifier) return [elements];
1708
- const result: Node[][] = [];
1709
- const isArrayPattern = name.kind === SyntaxKind.ArrayBindingPattern;
1710
- const bindings = (name as BindingPattern).elements.nodes;
1711
- for (let k = 0; k < bindings.length; k++) {
1712
- const binding = bindings[k]!;
1713
- if (binding.kind !== SyntaxKind.BindingElement) continue;
1714
- const element = binding as BindingElement;
1715
- if (element.dotDotDotToken !== undefined || element.initializer !== undefined) continue;
1716
- if (element.name?.kind !== SyntaxKind.Identifier) continue;
1717
- const alternatives: Node[] = [];
1718
- if (isArrayPattern) {
1719
- for (const e of elements) {
1720
- const value = isConstAssertion(e) ? skipParentheses(getExpression(e)!) : e;
1721
- if (value.kind !== SyntaxKind.ArrayLiteralExpression) continue;
1722
- const array = value as ArrayLiteralExpression;
1723
- if (value !== e || isInConstAssertion(array)) {
1724
- const item = array.elements.nodes[k];
1725
- if (item !== undefined && item.kind !== SyntaxKind.SpreadElement) addAlternatives(item, alternatives, true);
1726
- } else addArrayElements(array, alternatives);
1727
- }
1728
- } else {
1729
- const property = element.propertyName ?? element.name;
1730
- const propertyName = property.kind === SyntaxKind.Identifier ? getText(property) : undefined;
1731
- if (propertyName === undefined) continue;
1732
- for (const e of elements) {
1733
- if (e.kind !== SyntaxKind.ObjectLiteralExpression) continue;
1734
- for (const member of (e as ObjectLiteralExpression).properties.nodes)
1735
- if (member.kind === SyntaxKind.PropertyAssignment && staticPropertyName(member) === propertyName)
1736
- addAlternatives((member as PropertyAssignment).initializer, alternatives, true);
1737
- }
1738
- }
1739
- if (alternatives.length !== 0) result.push(alternatives);
1740
- }
1741
- return result;
1742
- }
1743
-
1744
- /** The name of an object literal member with a literal name. */
1745
- function staticPropertyName(member: Node): string | undefined {
1746
- switch (member.kind) {
1747
- case SyntaxKind.PropertyAssignment:
1748
- case SyntaxKind.ShorthandPropertyAssignment:
1749
- case SyntaxKind.MethodDeclaration:
1750
- case SyntaxKind.GetAccessor:
1751
- case SyntaxKind.SetAccessor:
1752
- break;
1753
- default:
1754
- return undefined;
1755
- }
1756
- const name = getName(member)!;
1757
- switch (name.kind) {
1758
- case SyntaxKind.Identifier:
1759
- case SyntaxKind.StringLiteral:
1760
- case SyntaxKind.NoSubstitutionTemplateLiteral:
1761
- return getText(name);
1762
- case SyntaxKind.NumericLiteral:
1763
- return String(Number(getText(name)));
1764
- }
1765
- return undefined;
1766
- }
1767
-
1768
848
  function getPropertiesOfContext(c: Checker, context: WideningContext): Symbol[] {
1769
849
  if (context.resolvedProperties === undefined) {
1770
850
  const names = new Map<string, Symbol>();
@@ -1852,11 +932,11 @@ export function isReadonlySymbol(c: Checker, symbol: Symbol): boolean {
1852
932
  (getDeclarationNodeFlagsFromSymbol(symbol) & NodeFlags.Constant) !== 0) ||
1853
933
  ((symbol.flags & SymbolFlags.Accessor) !== 0 && (symbol.flags & SymbolFlags.SetAccessor) === 0) ||
1854
934
  (symbol.flags & SymbolFlags.EnumMember) !== 0 ||
1855
- someReadonlyAssignmentDeclaration(c, symbol.declarations)
935
+ someReadonlyAssignmentDeclaration(c, getDeclarations(symbol))
1856
936
  );
1857
937
  }
1858
938
 
1859
- function someReadonlyAssignmentDeclaration(c: Checker, declarations: Node[]): boolean {
939
+ function someReadonlyAssignmentDeclaration(c: Checker, declarations: readonly Node[]): boolean {
1860
940
  for (const d of declarations) if (isReadonlyAssignmentDeclaration(c, d)) return true;
1861
941
  return false;
1862
942
  }
@@ -1887,7 +967,7 @@ import {
1887
967
  } from './checker-members.js';
1888
968
  import { checkExpression } from './checker-expressions.js';
1889
969
  import { getParameterTypeOfFullSignature } from './checker-jsdoc.js';
1890
- import { isFunctionLike, isFunctionLikeDeclarationKind, isInJSFile } from './ast-utilities.js';
970
+ import { isFunctionLikeDeclarationKind, isInJSFile } from './ast-utilities.js';
1891
971
  import { isEmptyArrayLiteralType } from './relations.js';
1892
972
  import { isCheckJsEnabledForFile } from './program-utilities.js';
1893
973
 
@@ -1920,7 +1000,7 @@ function getAnnotatedAccessorType(c: Checker, accessor: Node | undefined): Type
1920
1000
 
1921
1001
  /** The `accessor` property declaration of `symbol`, if any. */
1922
1002
  function getAutoAccessorDeclaration(symbol: Symbol): Node | undefined {
1923
- for (const d of symbol.declarations)
1003
+ for (const d of getDeclarations(symbol))
1924
1004
  if (d.kind === SyntaxKind.PropertyDeclaration && (getCombinedModifierFlags(d) & ModifierFlags.Accessor) !== 0)
1925
1005
  return d;
1926
1006
  return undefined;
@@ -1931,8 +1011,8 @@ function getTypeOfAccessors(c: Checker, symbol: Symbol): Type {
1931
1011
  const type = c.symbolTypes.get(id);
1932
1012
  if (type !== undefined) return type;
1933
1013
  if (!pushTypeResolution(c, symbol, TypeSystemPropertyName.Type)) return c.errorType;
1934
- const getter = symbol.declarations.find((d) => d.kind === SyntaxKind.GetAccessor);
1935
- const setter = symbol.declarations.find((d) => d.kind === SyntaxKind.SetAccessor);
1014
+ const getter = getDeclarations(symbol).find((d) => d.kind === SyntaxKind.GetAccessor);
1015
+ const setter = getDeclarations(symbol).find((d) => d.kind === SyntaxKind.SetAccessor);
1936
1016
  const accessor = getAutoAccessorDeclaration(symbol);
1937
1017
  // Getter annotation, setter annotation, accessor annotation, then getter body.
1938
1018
  let t =
@@ -1994,7 +1074,7 @@ export function getWriteTypeOfAccessors(c: Checker, symbol: Symbol): Type {
1994
1074
  if (links.writeType !== undefined) return links.writeType;
1995
1075
  if (!pushTypeResolution(c, symbol, TypeSystemPropertyName.WriteType)) return c.errorType;
1996
1076
  const setter =
1997
- symbol.declarations.find((d) => d.kind === SyntaxKind.SetAccessor) ?? getAutoAccessorDeclaration(symbol);
1077
+ getDeclarations(symbol).find((d) => d.kind === SyntaxKind.SetAccessor) ?? getAutoAccessorDeclaration(symbol);
1998
1078
  let writeType = getAnnotatedAccessorType(c, setter);
1999
1079
  if (!popTypeResolution(c)) {
2000
1080
  if (getAnnotatedAccessorTypeNode(setter) !== undefined)