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
@@ -3,31 +3,30 @@
3
3
  import { checkJsxAttribute } from './checker-jsx.js';
4
4
  import { getTargetOfAliasDeclaration, resolveExternalModuleSymbol } from './checker-modules.js';
5
5
  import { cachedTypeKey } from './checker-types.js';
6
- import { NodeVisitor, getArguments, getInitializer, getName, getParameters, getType, getBody, getSymbol, getMembers, getDotDotDotToken, getElements, getExpression, getLocals, getPostfixToken, getQuestionToken, getText, } from './ast.js';
7
- import { declarationNameToString, findConstructorDeclaration, getCombinedModifierFlags, getCombinedNodeFlags, getNameOfDeclaration, getRootDeclaration, getSourceFileOfNode, hasStaticModifier, hasSyntacticModifier, isCatchClauseVariableDeclarationOrBindingElement, isExternalModule, isJsonSourceFile, isTypeNodeKind, skipParentheses, } from './binder-utilities.js';
6
+ import { getArguments, getInitializer, getName, getParameters, getType, getBody, getSymbol, getMembers, getDotDotDotToken, getElements, getExpression, getPostfixToken, getQuestionToken, getText, } from './ast.js';
7
+ import { declarationNameToString, findConstructorDeclaration, getCombinedModifierFlags, getCombinedNodeFlags, getNameOfDeclaration, getRootDeclaration, getSourceFileOfNode, hasStaticModifier, hasSyntacticModifier, isCatchClauseVariableDeclarationOrBindingElement, isJsonSourceFile, isTypeNodeKind, } from './binder-utilities.js';
8
8
  import { identifierToKeywordKind } from './scanner.js';
9
- import { tsgoCheckerSlots } from './symbol-accessibility.js';
10
- import { getSymbolId } from './symbol.js';
11
9
  import { createDiagnosticForNode } from './checker.js';
12
10
  import { IndexInfo, } from './checker-types.js';
13
11
  import { createAnonymousType, createTypeReference, getIntersectionType, getUnionType, getUnionTypeEx, getWidenedLiteralType, isArrayOrTupleType, isEmptyObjectType, newObjectType, removeType, filterType, typesOf, getTypeArguments, } from './checker-type-factory.js';
14
- import { getDeclarationOfAliasSymbol, getParentOfSymbol, getSymbolFlags, getSymbolOfDeclaration, popTypeResolution, pushTypeResolution, reportCircularityError, resolveAlias, getMergedSymbol, getGlobalTypeAliasSymbol, resolveName, getGlobalSymbol, } from './checker-symbols.js';
12
+ import { getDeclarationOfAliasSymbol, getParentOfSymbol, getSymbolFlags, getSymbolOfDeclaration, popTypeResolution, pushTypeResolution, reportCircularityError, resolveAlias, getGlobalTypeAliasSymbol, resolveName, getGlobalSymbol, } from './checker-symbols.js';
15
13
  import { getBaseConstructorTypeOfClass, getDeclaredTypeOfClassOrInterface, getDeclaredTypeOfEnumMember, getDeclaredTypeOfSymbol, } from './checker-declared-types.js';
16
14
  import { getESSymbolLikeTypeForNode, getTypeFromTypeNode, getTypeAliasInstantiation } from './checker-type-nodes.js';
17
15
  import { getFlowTypeInConstructor, getFlowTypeInStaticBlocks, getTypeOfPropertyInBaseClass, } from './checker-classes.js';
18
16
  import { instantiateType } from './instantiation.js';
19
- import { getIndexInfosOfType, getPropertiesOfObjectType, getPropertiesOfType, getPropertyOfObjectType, isObjectLiteralType, } from './checker-members.js';
17
+ import { getIndexInfosOfType, getPropertiesOfObjectType, getPropertiesOfType, getPropertyOfObjectType, isObjectLiteralType, keepPropertiesOnly, scannedLiteralPropertiesLimit, } from './checker-members.js';
20
18
  import { typeToString, symbolToString } from './type-printer.js';
21
19
  import { getTypeOfMappedSymbol, getIndexType } from './checker-type-operators.js';
22
20
  import { getReturnTypeOfSignature, getSignatureFromDeclaration, getTypeOfParameter as getTypeOfParameterOfSignature, } from './checker-signatures.js';
23
- import { getReturnTypeFromBody, getTypeOfObjectLiteralMethod, ReturnCollector, shouldReportErrorsFromWideningWithContextualSignature, } from './checker-functions.js';
21
+ import { getReturnTypeFromBody, getTypeOfObjectLiteralMethod, shouldReportErrorsFromWideningWithContextualSignature, } from './checker-functions.js';
24
22
  import { getTypeOfReverseMappedSymbol } from './checker-inference.js';
25
- import { checkDeclarationInitializer, getContextuallyTypedParameterType, getContextualThisParameterType, checkExpressionCached, checkExpressionEx, isConstTypeReference, } from './checker-expressions.js';
23
+ import { checkDeclarationInitializer, getContextuallyTypedParameterType, getContextualThisParameterType, checkExpressionCached, checkExpressionEx, } from './checker-expressions.js';
26
24
  import { checkPropertyAssignment, checkShorthandPropertyAssignment } from './checker-object-literals.js';
27
25
  import { getTypeForBindingElement, getTypeFromBindingPattern } from './checker-destructuring.js';
28
26
  import { checkRightHandSideOfForOf } from './checker-statements.js';
29
27
  import { getTypeWithFacts, hasTypeFacts, getNonNullableType } from './checker-flow.js';
30
28
  import { getTypeOfModuleExportsSymbol, getWidenedTypeForAssignmentDeclaration } from './checker-javascript.js';
29
+ import { copyDeclarations, getDeclarations, getSymbolId } from './symbol.js';
31
30
  export function getTypeOfSymbol(c, symbol) {
32
31
  const checkFlags = symbol.checkFlags;
33
32
  if ((checkFlags & 65536 /* CheckFlags.DeferredType */) !== 0)
@@ -277,7 +276,7 @@ export function getTypeForVariableLikeDeclaration(c, declaration, includeOptiona
277
276
  // A set accessor parameter takes the getter's type.
278
277
  if (fn.kind === 179 /* SyntaxKind.SetAccessor */) {
279
278
  const fnSymbol = getSymbolOfDeclaration(c, fn);
280
- const getter = fnSymbol?.declarations.find((d) => d.kind === 178 /* SyntaxKind.GetAccessor */);
279
+ const getter = fnSymbol !== undefined ? getDeclarations(fnSymbol).find((d) => d.kind === 178 /* SyntaxKind.GetAccessor */) : undefined;
281
280
  if (getter !== undefined) {
282
281
  const getterSignature = getSignatureFromDeclaration(c, getter);
283
282
  // An unannotated `this` of a setter takes the getter's `this` type.
@@ -436,7 +435,7 @@ function reportWideningErrorsInType(c, t) {
436
435
  continue;
437
436
  // Property types can come from object literal normalization in unions.
438
437
  let valueDeclaration;
439
- for (const d of p.declarations) {
438
+ for (const d of getDeclarations(p)) {
440
439
  const v = getSymbol(d)?.valueDeclaration;
441
440
  if (v !== undefined && v.parent === t.symbol?.valueDeclaration) {
442
441
  valueDeclaration = d;
@@ -623,6 +622,7 @@ function getWidenedTypeOfObjectLiteral(c, t, context) {
623
622
  members.set(prop.name, getUndefinedProperty(c, prop));
624
623
  const infos = getIndexInfosOfType(c, t).map((info) => new IndexInfo(info.keyType, getWidenedType(c, info.valueType), info.isReadonly, info.declaration, info.components));
625
624
  const result = createAnonymousType(c, t.symbol, members, [], [], infos);
625
+ keepPropertiesOnly(c, result, members, scannedLiteralPropertiesLimit);
626
626
  result.objectFlags |= t.objectFlags & (4096 /* ObjectFlags.JSLiteral */ | 262144 /* ObjectFlags.NonInferrableType */);
627
627
  // The root context widens each object literal once; a property's context may widen one literal
628
628
  // for several siblings.
@@ -644,7 +644,7 @@ function getWidenedProperty(c, prop, context) {
644
644
  }
645
645
  function createSymbolWithWidenedType(c, prop, t) {
646
646
  const result = c.newSymbolWithCheckFlags(prop.flags, prop.name, prop.checkFlags & 8 /* CheckFlags.Readonly */);
647
- result.declarations = prop.declarations;
647
+ copyDeclarations(result, prop);
648
648
  result.parent = prop.parent;
649
649
  result.valueDeclaration = prop.valueDeclaration;
650
650
  const id = getSymbolId(result);
@@ -654,950 +654,20 @@ function createSymbolWithWidenedType(c, prop, t) {
654
654
  return result;
655
655
  }
656
656
  /**
657
- * An optional `undefined` property standing in for `prop`, the last property of that name among the
658
- * sibling object literals (getPropertiesOfContext). It carries the declarations of the property it is
659
- * created from, which order it among the members when they are printed.
660
- *
661
- * tsgo caches one such symbol per name and checker, so the declarations come from the first literal
662
- * its checker widened with that name missing in a sibling. Its checkers check the program's files
663
- * round robin, each file from the top. tsc-ts's checkers take files in another order, and a checker
664
- * can infer a file's functions for an importer before it checks the file, so the first union is found
665
- * in the sources of the files tsgo checks with the checker of the property's file, up to that file
666
- * (`firstSiblingProperty`), which doesn't depend on the order of checking. Without one, the property
667
- * is cached per sibling property. The order differs from tsgo's where tsgo's checker first infers a
668
- * function of a file of another checker, or a later function of the file.
657
+ * An optional `undefined` property standing in for `prop`, a property of a sibling object literal
658
+ * (getPropertiesOfContext), one per property. It carries the declarations of `prop`, which order it
659
+ * among the members when they are printed.
669
660
  */
670
661
  function getUndefinedProperty(c, prop) {
671
- const member = prop.declarations.length > 0 ? firstSiblingProperty(c, prop.declarations[0], prop.name) : undefined;
672
- const first = member !== undefined ? getSymbol(member) : undefined;
673
- const source = first ?? prop;
674
- const key = getSymbolId(source);
662
+ const key = getSymbolId(prop);
675
663
  const cached = c.undefinedProperties.get(key);
676
664
  if (cached !== undefined)
677
665
  return cached;
678
- const result = createSymbolWithWidenedType(c, source, c.undefinedOrMissingType);
666
+ const result = createSymbolWithWidenedType(c, prop, c.undefinedOrMissingType);
679
667
  result.flags |= 16777216 /* SymbolFlags.Optional */;
680
- // The properties of a literal in an `as const` assertion are readonly.
681
- if (first !== undefined && isInConstAssertion(member.parent))
682
- result.checkFlags |= 8 /* CheckFlags.Readonly */;
683
668
  c.undefinedProperties.set(key, result);
684
669
  return result;
685
670
  }
686
- /**
687
- * The property of `name` in the first union of object literals that leaves `name` missing in one of
688
- * them, among the files tsgo checks with the checker of the declaration's file, up to that file: the
689
- * last literal's property of that name. tsgo's checker `k` checks files `k`, `k + n`, `k + 2n`, ...
690
- * in order (`tsgoCheckerSlots`). Unions are those of the returned expressions of a function without
691
- * a return type, and of the alternatives of an initializer without a type (`?:`, `||`, `??`, array
692
- * elements, an element access of an array literal, and the values of a property across the literals),
693
- * of the variables of a `for...of`, of the object of a method call and of the expression of a type
694
- * assertion. In a file, they are taken in the order a checker checking the file from the top widens
695
- * them (`SiblingPropertyScan`).
696
- */
697
- function firstSiblingProperty(c, declaration, name) {
698
- const file = getSourceFileOfNode(declaration);
699
- const index = c.fileIndex.get(file);
700
- if (index === undefined)
701
- return siblingPropertiesOfFile(c, file).get(name);
702
- const slots = tsgoCheckerSlots(c);
703
- let slot = c.slotSiblingProperties.get(index % slots);
704
- if (slot === undefined) {
705
- slot = new SlotSiblingProperties(index % slots);
706
- c.slotSiblingProperties.set(index % slots, slot);
707
- }
708
- for (; slot.next <= index; slot.next += slots)
709
- for (const [n, member] of siblingPropertiesOfFile(c, c.files[slot.next]))
710
- if (!slot.members.has(n)) {
711
- slot.members.set(n, member);
712
- slot.fileIndexes.set(n, slot.next);
713
- }
714
- const found = slot.fileIndexes.get(name);
715
- return found !== undefined && found <= index ? slot.members.get(name) : undefined;
716
- }
717
- /**
718
- * By name, the first property of the unions of `file` that leave the name missing: what a parse thread
719
- * found (`SourceFile.siblingProperties`), or else the checker's own scan.
720
- */
721
- function siblingPropertiesOfFile(c, file) {
722
- // A parse thread's scan takes array literals to have the global Array type.
723
- if (file.siblingProperties !== undefined && c.globalArrayType !== c.emptyGenericType)
724
- return file.siblingProperties;
725
- let first = c.firstSiblingProperties.get(file);
726
- if (first === undefined) {
727
- first = scanSiblingProperties(file, c);
728
- c.firstSiblingProperties.set(file, first);
729
- }
730
- return first;
731
- }
732
- /**
733
- * By name, the first property of the unions of `file`'s object literals that leave the name missing.
734
- * Without a checker, as on a parse thread, array literals are taken to have the global Array type, and
735
- * the result is undefined where an enum the scan can't resolve decides which literals subtype reduction
736
- * leaves out (`enumKeyOfMemberAccess`); the checker then scans the file itself.
737
- */
738
- export function scanSiblingProperties(file, c) {
739
- if (file.isDeclarationFile)
740
- return new Map();
741
- const scan = new SiblingPropertyScan(c);
742
- file.forEachChild(scan);
743
- scan.widenAll();
744
- return scan.unresolved ? undefined : scan.members;
745
- }
746
- /** The first property of each name among the files of one of tsgo's checkers, scanned in order. */
747
- export class SlotSiblingProperties {
748
- members = new Map();
749
- /** The index of the file of each name's property. */
750
- fileIndexes = new Map();
751
- /** The index of the next file of the checker to scan. */
752
- next;
753
- constructor(first) {
754
- this.next = first;
755
- }
756
- }
757
- /** Whether an object literal is in an `as const` assertion, through arrays, spreads and properties. */
758
- function isInConstAssertion(literal) {
759
- for (let node = literal; node.parent !== undefined; node = node.parent) {
760
- switch (node.parent.kind) {
761
- case 218 /* SyntaxKind.ParenthesizedExpression */:
762
- case 210 /* SyntaxKind.ArrayLiteralExpression */:
763
- case 231 /* SyntaxKind.SpreadElement */:
764
- case 303 /* SyntaxKind.PropertyAssignment */:
765
- case 211 /* SyntaxKind.ObjectLiteralExpression */:
766
- continue;
767
- case 235 /* SyntaxKind.AsExpression */:
768
- case 217 /* SyntaxKind.TypeAssertionExpression */:
769
- return isConstTypeReference(getType(node.parent));
770
- }
771
- return false;
772
- }
773
- return false;
774
- }
775
- /** An expression whose type is widened, at the position where a checker widens it. */
776
- class WideningRoot {
777
- position;
778
- func;
779
- alternatives;
780
- sources;
781
- calls;
782
- constructor(position,
783
- /** The function whose returned expressions these are, widened once. */
784
- func,
785
- /** The expressions whose types are unioned and widened. */
786
- alternatives,
787
- /** The expressions whose calls, innermost first, infer their functions' return types before. */
788
- sources,
789
- /** The calls of `sources` that may call a function of the file (`SiblingPropertyScan.callees`). */
790
- calls = undefined) {
791
- this.position = position;
792
- this.func = func;
793
- this.alternatives = alternatives;
794
- this.sources = sources;
795
- this.calls = calls;
796
- }
797
- }
798
- /**
799
- * The unions a checker widens while checking the file's statements or the body of one deferred function, and
800
- * the deferred functions it queues there.
801
- */
802
- class CheckScope {
803
- position;
804
- roots = [];
805
- deferred = [];
806
- /** The calls by a name (`calleeName`), which infer where they end. */
807
- calls = [];
808
- constructor(position) {
809
- this.position = position;
810
- }
811
- }
812
- /**
813
- * Whether a checker checks `node`'s body only as a deferred node: a function expression, arrow
814
- * function or object literal method or accessor whose return type is written. Without one, the
815
- * checker infers the return type where it reaches the function, from the returned expressions and
816
- * what they refer to.
817
- */
818
- function isDeferredCheck(node) {
819
- switch (node.kind) {
820
- case 219 /* SyntaxKind.FunctionExpression */:
821
- case 220 /* SyntaxKind.ArrowFunction */:
822
- return getType(node) !== undefined;
823
- case 175 /* SyntaxKind.MethodDeclaration */:
824
- case 178 /* SyntaxKind.GetAccessor */:
825
- return node.parent?.kind === 211 /* SyntaxKind.ObjectLiteralExpression */ && getType(node) !== undefined;
826
- case 179 /* SyntaxKind.SetAccessor */:
827
- return node.parent?.kind === 211 /* SyntaxKind.ObjectLiteralExpression */;
828
- }
829
- return false;
830
- }
831
- /**
832
- * Finds the unions of a file's object literals in the order a checker widens them: an initializer
833
- * where it is, and a function's returned expressions at its first `return`, where the checker
834
- * infers its return type. A call of a function of the file, by a name that resolves to it or as
835
- * `this.name(...)` in a class, infers that function's return type where the call is, and before the
836
- * union of the expression the call is in. The bodies of function expressions, arrow functions and
837
- * object literal methods and accessors with a written return type are checked after the file's
838
- * statements, so their unions widen then.
839
- */
840
- class SiblingPropertyScan extends NodeVisitor {
841
- /** The checker that resolves imported enums, or undefined on a parse thread. */
842
- c;
843
- /** By name, the property of the first union that leaves the name missing in a literal. */
844
- members = new Map();
845
- /**
846
- * Whether array literals have an array type, whose elements are widened as one union. Without the
847
- * global Array type (--noLib) an array literal is an empty object type.
848
- */
849
- arrays;
850
- /** Whether an enum the scan can't resolve without a checker decided which literals were left out. */
851
- unresolved = false;
852
- /** The enums of the members that values refer to, numbered in the order the scan meets them. */
853
- enums = new Map();
854
- fileScope = new CheckScope(0);
855
- /** The scope of the innermost deferred function around the node being visited. */
856
- scope = this.fileScope;
857
- /** The returned expressions of each function without a return type. */
858
- returned = new Map();
859
- /** The names a call can call those functions by (`functionName`). */
860
- callees = new Set();
861
- widened = new Set();
862
- /** The functions and methods of each class, by name. */
863
- classFunctions = new Map();
864
- constructor(c) {
865
- super();
866
- this.c = c;
867
- this.arrays = c === undefined || c.globalArrayType !== c.emptyGenericType;
868
- }
869
- /** The key of the values that are members of `symbol`, an enum, in this scan. */
870
- enumKey(symbol) {
871
- let id = this.enums.get(symbol);
872
- if (id === undefined) {
873
- id = this.enums.size;
874
- this.enums.set(symbol, id);
875
- }
876
- return 'enum ' + id;
877
- }
878
- visitNode(node) {
879
- switch (node.kind) {
880
- case 263 /* SyntaxKind.FunctionDeclaration */:
881
- case 219 /* SyntaxKind.FunctionExpression */:
882
- case 220 /* SyntaxKind.ArrowFunction */:
883
- case 175 /* SyntaxKind.MethodDeclaration */:
884
- case 178 /* SyntaxKind.GetAccessor */: {
885
- const body = getBody(node);
886
- if (body === undefined || getType(node) !== undefined)
887
- break;
888
- const returned = [];
889
- const sources = [];
890
- let position = body.pos;
891
- if (body.kind === 242 /* SyntaxKind.Block */) {
892
- const collector = new ReturnCollector();
893
- collector.visitStatements(body);
894
- if (collector.returns.length > 0)
895
- position = collector.returns[0].pos;
896
- for (const r of collector.returns)
897
- if (r.expression !== undefined) {
898
- addAlternatives(r.expression, returned, this.arrays);
899
- sources.push(r.expression);
900
- }
901
- }
902
- else {
903
- addAlternatives(body, returned, this.arrays);
904
- sources.push(body);
905
- }
906
- if (returned.length === 0)
907
- break;
908
- const root = new WideningRoot(position, node, returned, sources);
909
- this.returned.set(node, root);
910
- this.addRoot(root);
911
- if (node.kind === 175 /* SyntaxKind.MethodDeclaration */ || node.kind === 178 /* SyntaxKind.GetAccessor */)
912
- this.addClassFunction(node, node);
913
- break;
914
- }
915
- case 261 /* SyntaxKind.VariableDeclaration */:
916
- case 173 /* SyntaxKind.PropertyDeclaration */:
917
- case 170 /* SyntaxKind.Parameter */: {
918
- const initializer = getInitializer(node);
919
- if (initializer === undefined || getType(node) !== undefined)
920
- break;
921
- this.addRoot(new WideningRoot(initializer.pos, undefined, addAlternatives(initializer, [], this.arrays), [initializer]));
922
- const value = skipParentheses(initializer);
923
- if (node.kind === 173 /* SyntaxKind.PropertyDeclaration */ &&
924
- (value.kind === 220 /* SyntaxKind.ArrowFunction */ || value.kind === 219 /* SyntaxKind.FunctionExpression */))
925
- this.addClassFunction(node, value);
926
- break;
927
- }
928
- case 278 /* SyntaxKind.ExportAssignment */:
929
- if (getType(node) === undefined) {
930
- const expression = getExpression(node);
931
- this.addRoot(new WideningRoot(node.pos, undefined, addAlternatives(expression, [], this.arrays), [expression]));
932
- }
933
- break;
934
- case 251 /* SyntaxKind.ForOfStatement */: {
935
- // The variable of `for (const x of e)` has the widened type of e's elements, and each element
936
- // of a binding pattern the widened type of its part of them.
937
- const statement = node;
938
- if (statement.initializer.kind !== 262 /* SyntaxKind.VariableDeclarationList */)
939
- break;
940
- const declaration = statement.initializer.declarations.nodes[0];
941
- if (declaration === undefined || getType(declaration) !== undefined || !this.arrays)
942
- break;
943
- const elements = [];
944
- for (const array of iteratedArrayLiterals(statement.expression))
945
- addArrayElements(array, elements);
946
- if (elements.length === 0)
947
- break;
948
- for (const alternatives of bindingAlternatives(getName(declaration), elements))
949
- this.addRoot(new WideningRoot(statement.expression.pos, undefined, alternatives, [statement.expression]));
950
- break;
951
- }
952
- case 214 /* SyntaxKind.CallExpression */: {
953
- // The object of a method call is widened before the call is resolved.
954
- const callee = skipParentheses(getExpression(node));
955
- if ((callee.kind === 212 /* SyntaxKind.PropertyAccessExpression */ || callee.kind === 213 /* SyntaxKind.ElementAccessExpression */) &&
956
- mayHoldLiterals(getExpression(callee)))
957
- this.addExpressionRoot(callee.pos, getExpression(callee));
958
- // Calls elsewhere, as in statements, infer where they end.
959
- if (calleeName(node) !== undefined)
960
- this.scope.calls.push(node);
961
- break;
962
- }
963
- }
964
- if (isTypeNodeKind(node.kind))
965
- return false;
966
- if ((node.kind === 235 /* SyntaxKind.AsExpression */ || node.kind === 217 /* SyntaxKind.TypeAssertionExpression */) &&
967
- !isConstTypeReference(getType(node))) {
968
- // An assertion widens its expression's type in a deferred check, queued after the expression's.
969
- node.forEachChild(this);
970
- const inner = new CheckScope(node.end);
971
- this.scope.deferred.push(inner);
972
- const saveScope = this.scope;
973
- this.scope = inner;
974
- this.addExpressionRoot(node.end, getExpression(node));
975
- this.scope = saveScope;
976
- return false;
977
- }
978
- if (isDeferredCheck(node)) {
979
- const inner = new CheckScope(node.pos);
980
- this.scope.deferred.push(inner);
981
- const saveScope = this.scope;
982
- this.scope = inner;
983
- node.forEachChild(this);
984
- this.scope = saveScope;
985
- }
986
- else
987
- node.forEachChild(this);
988
- return false;
989
- }
990
- addRoot(root) {
991
- this.scope.roots.push(root);
992
- }
993
- /** Adds a root for the widened type of `expression` where it holds object or array literals. */
994
- addExpressionRoot(position, expression) {
995
- const alternatives = addAlternatives(expression, [], this.arrays);
996
- if (alternatives.some((a) => a.kind === 211 /* SyntaxKind.ObjectLiteralExpression */ || a.kind === 210 /* SyntaxKind.ArrayLiteralExpression */))
997
- this.addRoot(new WideningRoot(position, undefined, alternatives, [expression]));
998
- }
999
- /** Names `func` in its class by `member`, a method or a property it initializes. */
1000
- addClassFunction(member, func) {
1001
- const name = getName(member);
1002
- const parent = member.parent;
1003
- if (name === undefined || name.kind !== 79 /* SyntaxKind.Identifier */)
1004
- return;
1005
- if (parent.kind !== 264 /* SyntaxKind.ClassDeclaration */ && parent.kind !== 232 /* SyntaxKind.ClassExpression */)
1006
- return;
1007
- let functions = this.classFunctions.get(parent);
1008
- if (functions === undefined)
1009
- this.classFunctions.set(parent, (functions = new Map()));
1010
- if (!functions.has(getText(name)))
1011
- functions.set(getText(name), func);
1012
- }
1013
- /**
1014
- * Widens the unions of the file's statements in order, then those of each deferred node in the
1015
- * order the checker queues them, which includes the deferred nodes queued while checking one.
1016
- */
1017
- widenAll() {
1018
- for (const func of this.returned.keys()) {
1019
- const name = functionName(func);
1020
- if (name !== undefined)
1021
- this.callees.add(name);
1022
- }
1023
- const queue = [this.fileScope];
1024
- for (let i = 0; i < queue.length; i++) {
1025
- const scope = queue[i];
1026
- if (this.callees.size !== 0)
1027
- for (const call of scope.calls)
1028
- if (this.callees.has(calleeName(call)))
1029
- scope.roots.push(new WideningRoot(call.end, undefined, [], [], [call]));
1030
- scope.roots.sort((a, b) => a.position - b.position);
1031
- scope.deferred.sort((a, b) => a.position - b.position);
1032
- let next = 0;
1033
- for (const root of scope.roots) {
1034
- while (next < scope.deferred.length && scope.deferred[next].position < root.position)
1035
- queue.push(scope.deferred[next++]);
1036
- this.widenRoot(root);
1037
- }
1038
- while (next < scope.deferred.length)
1039
- queue.push(scope.deferred[next++]);
1040
- }
1041
- }
1042
- /** Widens a root's unions, after the returned expressions of the functions its calls infer. */
1043
- widenRoot(root) {
1044
- if (root.func !== undefined) {
1045
- if (this.widened.has(root.func))
1046
- return;
1047
- this.widened.add(root.func);
1048
- }
1049
- if (this.callees.size === 0) {
1050
- this.widen(root.alternatives, true);
1051
- return;
1052
- }
1053
- if (root.calls === undefined) {
1054
- const collector = new CallCollector(this.callees);
1055
- for (const source of root.sources)
1056
- collector.visitNode(source);
1057
- root.calls = collector.calls;
1058
- }
1059
- for (const call of root.calls) {
1060
- const callee = this.calledFunction(call);
1061
- const returned = callee !== undefined ? this.returned.get(callee) : undefined;
1062
- if (returned !== undefined)
1063
- this.widenRoot(returned);
1064
- }
1065
- this.widen(root.alternatives, true);
1066
- }
1067
- /**
1068
- * The function a call calls: a function declaration or a variable initialized with a function that
1069
- * the callee's name resolves to (`getLocals` of the scopes around the call), or a method or property
1070
- * of the class for `this.name(...)` in a class member.
1071
- */
1072
- calledFunction(call) {
1073
- const callee = skipParentheses(getExpression(call));
1074
- if (callee.kind === 79 /* SyntaxKind.Identifier */) {
1075
- const name = getText(callee);
1076
- for (let scope = call.parent; scope !== undefined; scope = scope.parent) {
1077
- const symbol = getLocals(scope)?.get(name);
1078
- // A type alias, interface or type parameter of the name doesn't hide a value of an outer scope.
1079
- if (symbol === undefined || (symbol.flags & (111551 /* SymbolFlags.Value */ | 1048576 /* SymbolFlags.ExportValue */)) === 0)
1080
- continue;
1081
- // An exported declaration's local symbol has no value declaration of its own.
1082
- const declaration = symbol.valueDeclaration ??
1083
- symbol.declarations.find((d) => d.kind === 263 /* SyntaxKind.FunctionDeclaration */ || d.kind === 261 /* SyntaxKind.VariableDeclaration */);
1084
- if (declaration === undefined)
1085
- return undefined;
1086
- if (declaration.kind === 263 /* SyntaxKind.FunctionDeclaration */)
1087
- return declaration;
1088
- const initializer = declaration.kind === 261 /* SyntaxKind.VariableDeclaration */ ? getInitializer(declaration) : undefined;
1089
- const value = initializer !== undefined ? skipParentheses(initializer) : undefined;
1090
- return value !== undefined &&
1091
- (value.kind === 220 /* SyntaxKind.ArrowFunction */ || value.kind === 219 /* SyntaxKind.FunctionExpression */)
1092
- ? value
1093
- : undefined;
1094
- }
1095
- return undefined;
1096
- }
1097
- if (callee.kind !== 212 /* SyntaxKind.PropertyAccessExpression */ || getExpression(callee).kind !== 109 /* SyntaxKind.ThisKeyword */)
1098
- return undefined;
1099
- const name = getName(callee);
1100
- const container = thisClass(call);
1101
- const functions = container !== undefined ? this.classFunctions.get(container) : undefined;
1102
- return functions !== undefined && name.kind === 79 /* SyntaxKind.Identifier */ ? functions.get(getText(name)) : undefined;
1103
- }
1104
- /**
1105
- * Records the unions of the alternatives of one widened expression, and of their nested values.
1106
- * With `reduced`, the alternatives make up one union with subtype reduction (the alternatives of an
1107
- * expression, the elements of an array literal), which drops a literal whose widened type is the
1108
- * type of an earlier one (`removeSubtypes` removes the later of two identical types). The values of
1109
- * a property across literals are not reduced: their union is one of each literal's value.
1110
- */
1111
- widen(alternatives, reduced) {
1112
- let literals = [];
1113
- for (const a of alternatives)
1114
- if (a.kind === 211 /* SyntaxKind.ObjectLiteralExpression */)
1115
- literals.push(a);
1116
- // The elements of a tuple, an array literal in an `as const` assertion, widen separately.
1117
- else if (this.arrays && a.kind === 210 /* SyntaxKind.ArrayLiteralExpression */ && !isInConstAssertion(a))
1118
- this.widen(addArrayElements(a, []), true);
1119
- if (literals.length === 0)
1120
- return;
1121
- if (reduced)
1122
- literals = withoutRepeatedLiterals(this, literals);
1123
- if (literals.length > 1)
1124
- this.record(literals);
1125
- const values = new Map();
1126
- for (const literal of literals)
1127
- for (const member of literal.properties.nodes) {
1128
- if (member.kind !== 303 /* SyntaxKind.PropertyAssignment */)
1129
- continue;
1130
- const name = staticPropertyName(member);
1131
- if (name === undefined)
1132
- continue;
1133
- let list = values.get(name);
1134
- if (list === undefined)
1135
- values.set(name, (list = []));
1136
- // A value's own alternatives make up one reduced union, one member of the property's values.
1137
- const own = addAlternatives(member.initializer, [], this.arrays);
1138
- for (const value of withoutRepeatedLiterals(this, own))
1139
- list.push(value);
1140
- }
1141
- for (const list of values.values())
1142
- this.widen(list, false);
1143
- }
1144
- /**
1145
- * Records, for each name that some literal without a spread lacks, the last such literal's property
1146
- * of that name, unless an earlier union has the name.
1147
- */
1148
- record(literals) {
1149
- const plain = [];
1150
- for (const literal of literals) {
1151
- let members = new Map();
1152
- for (const member of literal.properties.nodes) {
1153
- if (member.kind === 305 /* SyntaxKind.SpreadAssignment */) {
1154
- members = undefined;
1155
- break;
1156
- }
1157
- const name = staticPropertyName(member);
1158
- if (name !== undefined)
1159
- members.set(name, member);
1160
- }
1161
- if (members !== undefined)
1162
- plain.push(members);
1163
- }
1164
- const last = new Map();
1165
- for (const members of plain)
1166
- for (const [name, member] of members)
1167
- last.set(name, member);
1168
- for (const [name, member] of last) {
1169
- // A method's or accessor's symbol has other flags than the property checkObjectLiteral makes of it.
1170
- if (member.kind !== 303 /* SyntaxKind.PropertyAssignment */ && member.kind !== 304 /* SyntaxKind.ShorthandPropertyAssignment */)
1171
- continue;
1172
- if (this.members.has(name) || plain.every((members) => members.has(name)))
1173
- continue;
1174
- this.members.set(name, member);
1175
- }
1176
- }
1177
- }
1178
- /**
1179
- * `nodes` without the object literals whose widened types are certainly those of an earlier one:
1180
- * the same property names, each with a value of the same widened type (`widenedTypeKey`). Literals
1181
- * that can have a contextual type are kept: it can keep their properties' literal types.
1182
- */
1183
- function withoutRepeatedLiterals(scan, nodes) {
1184
- let keys;
1185
- let result;
1186
- for (let i = 0; i < nodes.length; i++) {
1187
- const node = nodes[i];
1188
- const key = node.kind === 211 /* SyntaxKind.ObjectLiteralExpression */ && !mayHaveContextualType(node)
1189
- ? widenedTypeKey(scan, node)
1190
- : undefined;
1191
- if (key !== undefined) {
1192
- if (keys === undefined)
1193
- keys = [];
1194
- else if (keys.includes(key)) {
1195
- // Without a checker, an enum the scan can't resolve may or may not make the types the same.
1196
- if (key.includes(pendingEnumKey))
1197
- scan.unresolved = true;
1198
- if (result === undefined)
1199
- result = nodes.slice(0, i);
1200
- continue;
1201
- }
1202
- keys.push(key);
1203
- }
1204
- result?.push(node);
1205
- }
1206
- return result ?? nodes;
1207
- }
1208
- /**
1209
- * A key that two expressions of one file share only when their types widen to the same type, or
1210
- * undefined where that isn't known from the syntax: literals by their widened type, members of an
1211
- * enum by the enum, and object literals by their properties' names and keys. Other references can
1212
- * name different symbols or be narrowed differently.
1213
- */
1214
- function widenedTypeKey(scan, node) {
1215
- switch (node.kind) {
1216
- case 10 /* SyntaxKind.StringLiteral */:
1217
- case 14 /* SyntaxKind.NoSubstitutionTemplateLiteral */:
1218
- case 229 /* SyntaxKind.TemplateExpression */:
1219
- return 'string';
1220
- case 8 /* SyntaxKind.NumericLiteral */:
1221
- return 'number';
1222
- case 9 /* SyntaxKind.BigIntLiteral */:
1223
- return 'bigint';
1224
- case 111 /* SyntaxKind.TrueKeyword */:
1225
- case 96 /* SyntaxKind.FalseKeyword */:
1226
- return 'boolean';
1227
- case 13 /* SyntaxKind.RegularExpressionLiteral */:
1228
- return 'RegExp';
1229
- case 225 /* SyntaxKind.PrefixUnaryExpression */: {
1230
- const unary = node;
1231
- const operand = unary.operand.kind;
1232
- if (unary.operator === 40 /* SyntaxKind.MinusToken */ || unary.operator === 39 /* SyntaxKind.PlusToken */) {
1233
- if (operand === 8 /* SyntaxKind.NumericLiteral */)
1234
- return 'number';
1235
- if (operand === 9 /* SyntaxKind.BigIntLiteral */ && unary.operator === 40 /* SyntaxKind.MinusToken */)
1236
- return 'bigint';
1237
- }
1238
- return undefined;
1239
- }
1240
- case 212 /* SyntaxKind.PropertyAccessExpression */:
1241
- return enumKeyOfMemberAccess(scan, node);
1242
- case 211 /* SyntaxKind.ObjectLiteralExpression */: {
1243
- const members = [];
1244
- for (const member of node.properties.nodes) {
1245
- if (member.kind !== 303 /* SyntaxKind.PropertyAssignment */)
1246
- return undefined;
1247
- const name = staticPropertyName(member);
1248
- const value = widenedTypeKey(scan, member.initializer);
1249
- if (name === undefined || value === undefined)
1250
- return undefined;
1251
- members.push(JSON.stringify(name) + ': ' + value);
1252
- }
1253
- members.sort();
1254
- return '{' + members.join(', ') + '}';
1255
- }
1256
- }
1257
- return undefined;
1258
- }
1259
- /** The key of a member of an enum that the scan can't resolve without a checker. */
1260
- const pendingEnumKey = 'enum ?';
1261
- /**
1262
- * The key of `E.Member` where `E` names an enum in the scopes around the access (`getLocals`, as
1263
- * `calledFunction` finds names) and `Member` is one of its members, not an export of a namespace merged
1264
- * with it: the key of the enum. With a checker, `E` can be imported. Without one, an import, and a
1265
- * global of a script file (whose enum other files can extend), give `pendingEnumKey`.
1266
- */
1267
- function enumKeyOfMemberAccess(scan, node) {
1268
- const target = getExpression(node);
1269
- const member = getName(node);
1270
- if (target.kind !== 79 /* SyntaxKind.Identifier */ || member.kind !== 79 /* SyntaxKind.Identifier */)
1271
- return undefined;
1272
- const name = getText(target);
1273
- const c = scan.c;
1274
- for (let scope = node.parent; scope !== undefined; scope = scope.parent) {
1275
- let symbol = getLocals(scope)?.get(name);
1276
- if (symbol === undefined ||
1277
- (symbol.flags & (111551 /* SymbolFlags.Value */ | 1048576 /* SymbolFlags.ExportValue */ | 2097152 /* SymbolFlags.Alias */)) === 0)
1278
- continue;
1279
- if (c === undefined) {
1280
- if ((symbol.flags & 2097152 /* SymbolFlags.Alias */) !== 0)
1281
- return pendingEnumKey;
1282
- if ((symbol.flags & (384 /* SymbolFlags.Enum */ | 1536 /* SymbolFlags.Module */)) !== 0 &&
1283
- !isExternalModule(getSourceFileOfNode(node)))
1284
- return pendingEnumKey;
1285
- }
1286
- if (symbol.exportSymbol !== undefined)
1287
- symbol = symbol.exportSymbol;
1288
- if (c !== undefined) {
1289
- symbol = getMergedSymbol(c, symbol);
1290
- if ((symbol.flags & 2097152 /* SymbolFlags.Alias */) !== 0)
1291
- symbol = resolveAlias(c, symbol);
1292
- }
1293
- if ((symbol.flags & 384 /* SymbolFlags.Enum */) === 0)
1294
- return undefined;
1295
- // An enum merged with a namespace also exports the namespace's values.
1296
- const memberSymbol = symbol.exports?.get(getText(member));
1297
- return memberSymbol !== undefined && (memberSymbol.flags & 8 /* SymbolFlags.EnumMember */) !== 0
1298
- ? scan.enumKey(symbol)
1299
- : undefined;
1300
- }
1301
- return undefined;
1302
- }
1303
- /**
1304
- * Whether an object literal of a widened expression can have a contextual type: under `satisfies`,
1305
- * an assertion or an argument, as a parameter's default, or returned from a function that can have
1306
- * a contextual signature (a function expression that doesn't initialize an untyped declaration, or a
1307
- * method of an object literal). Initializers of untyped declarations, the returns of function
1308
- * declarations and class members, and the objects of property and element accesses have none.
1309
- */
1310
- function mayHaveContextualType(node) {
1311
- for (let current = node.parent; current !== undefined; current = current.parent) {
1312
- switch (current.kind) {
1313
- case 218 /* SyntaxKind.ParenthesizedExpression */:
1314
- case 236 /* SyntaxKind.NonNullExpression */:
1315
- case 224 /* SyntaxKind.AwaitExpression */:
1316
- case 228 /* SyntaxKind.ConditionalExpression */:
1317
- case 227 /* SyntaxKind.BinaryExpression */:
1318
- case 210 /* SyntaxKind.ArrayLiteralExpression */:
1319
- case 211 /* SyntaxKind.ObjectLiteralExpression */:
1320
- case 303 /* SyntaxKind.PropertyAssignment */:
1321
- case 254 /* SyntaxKind.ReturnStatement */:
1322
- case 242 /* SyntaxKind.Block */:
1323
- case 246 /* SyntaxKind.IfStatement */:
1324
- case 247 /* SyntaxKind.DoStatement */:
1325
- case 248 /* SyntaxKind.WhileStatement */:
1326
- case 249 /* SyntaxKind.ForStatement */:
1327
- case 250 /* SyntaxKind.ForInStatement */:
1328
- case 251 /* SyntaxKind.ForOfStatement */:
1329
- case 256 /* SyntaxKind.SwitchStatement */:
1330
- case 270 /* SyntaxKind.CaseBlock */:
1331
- case 297 /* SyntaxKind.CaseClause */:
1332
- case 298 /* SyntaxKind.DefaultClause */:
1333
- case 257 /* SyntaxKind.LabeledStatement */:
1334
- case 259 /* SyntaxKind.TryStatement */:
1335
- case 300 /* SyntaxKind.CatchClause */:
1336
- continue;
1337
- case 261 /* SyntaxKind.VariableDeclaration */:
1338
- case 173 /* SyntaxKind.PropertyDeclaration */:
1339
- case 278 /* SyntaxKind.ExportAssignment */:
1340
- case 263 /* SyntaxKind.FunctionDeclaration */:
1341
- // The object of a property access, as in a method call, has no contextual type.
1342
- case 212 /* SyntaxKind.PropertyAccessExpression */:
1343
- case 213 /* SyntaxKind.ElementAccessExpression */:
1344
- return false;
1345
- case 175 /* SyntaxKind.MethodDeclaration */:
1346
- case 178 /* SyntaxKind.GetAccessor */: {
1347
- const parent = current.parent.kind;
1348
- return parent !== 264 /* SyntaxKind.ClassDeclaration */ && parent !== 232 /* SyntaxKind.ClassExpression */;
1349
- }
1350
- case 220 /* SyntaxKind.ArrowFunction */:
1351
- case 219 /* SyntaxKind.FunctionExpression */: {
1352
- let outer = current.parent;
1353
- while (outer.kind === 218 /* SyntaxKind.ParenthesizedExpression */)
1354
- outer = outer.parent;
1355
- return ((outer.kind !== 261 /* SyntaxKind.VariableDeclaration */ && outer.kind !== 173 /* SyntaxKind.PropertyDeclaration */) ||
1356
- getType(outer) !== undefined);
1357
- }
1358
- default:
1359
- return true;
1360
- }
1361
- }
1362
- return true;
1363
- }
1364
- /** Collects the calls by one of `names` in an expression, innermost first, outside the functions in it. */
1365
- class CallCollector extends NodeVisitor {
1366
- names;
1367
- calls = [];
1368
- constructor(names) {
1369
- super();
1370
- this.names = names;
1371
- }
1372
- visitNode(node) {
1373
- if (isFunctionLike(node) || node.kind === 232 /* SyntaxKind.ClassExpression */)
1374
- return false;
1375
- node.forEachChild(this);
1376
- if (node.kind === 214 /* SyntaxKind.CallExpression */) {
1377
- const name = calleeName(node);
1378
- if (name !== undefined && this.names.has(name))
1379
- this.calls.push(node);
1380
- }
1381
- return false;
1382
- }
1383
- }
1384
- /** The name a call calls its function by, `f` in `f(...)` and `this.f(...)`, if the scan can resolve it. */
1385
- function calleeName(call) {
1386
- const callee = skipParentheses(getExpression(call));
1387
- if (callee.kind === 79 /* SyntaxKind.Identifier */)
1388
- return getText(callee);
1389
- if (callee.kind !== 212 /* SyntaxKind.PropertyAccessExpression */ || getExpression(callee).kind !== 109 /* SyntaxKind.ThisKeyword */)
1390
- return undefined;
1391
- const name = getName(callee);
1392
- return name.kind === 79 /* SyntaxKind.Identifier */ ? getText(name) : undefined;
1393
- }
1394
- /**
1395
- * The name calls can call a function without a return type by: a function declaration's, a method's or
1396
- * accessor's, or that of the variable or property a function expression initializes.
1397
- */
1398
- function functionName(func) {
1399
- let node = func;
1400
- if (func.kind === 219 /* SyntaxKind.FunctionExpression */ || func.kind === 220 /* SyntaxKind.ArrowFunction */) {
1401
- while (node.parent?.kind === 218 /* SyntaxKind.ParenthesizedExpression */)
1402
- node = node.parent;
1403
- if (node.parent?.kind !== 261 /* SyntaxKind.VariableDeclaration */ && node.parent?.kind !== 173 /* SyntaxKind.PropertyDeclaration */)
1404
- return undefined;
1405
- node = node.parent;
1406
- }
1407
- const name = getName(node);
1408
- return name?.kind === 79 /* SyntaxKind.Identifier */ ? getText(name) : undefined;
1409
- }
1410
- /** Whether an expression's alternatives (`addAlternatives`) can hold object or array literals. */
1411
- function mayHoldLiterals(node) {
1412
- switch (skipParentheses(node).kind) {
1413
- case 211 /* SyntaxKind.ObjectLiteralExpression */:
1414
- case 210 /* SyntaxKind.ArrayLiteralExpression */:
1415
- case 228 /* SyntaxKind.ConditionalExpression */:
1416
- case 227 /* SyntaxKind.BinaryExpression */:
1417
- case 236 /* SyntaxKind.NonNullExpression */:
1418
- case 239 /* SyntaxKind.SatisfiesExpression */:
1419
- case 224 /* SyntaxKind.AwaitExpression */:
1420
- case 235 /* SyntaxKind.AsExpression */:
1421
- case 217 /* SyntaxKind.TypeAssertionExpression */:
1422
- return true;
1423
- }
1424
- return false;
1425
- }
1426
- /** The class whose instance `this` is at `node`, through arrow functions, in a member or property initializer. */
1427
- function thisClass(node) {
1428
- for (let current = node.parent; current !== undefined; current = current.parent) {
1429
- if (current.kind === 220 /* SyntaxKind.ArrowFunction */)
1430
- continue;
1431
- if (current.kind === 175 /* SyntaxKind.MethodDeclaration */ ||
1432
- current.kind === 178 /* SyntaxKind.GetAccessor */ ||
1433
- current.kind === 179 /* SyntaxKind.SetAccessor */ ||
1434
- current.kind === 177 /* SyntaxKind.Constructor */ ||
1435
- current.kind === 173 /* SyntaxKind.PropertyDeclaration */) {
1436
- const parent = current.parent;
1437
- return parent.kind === 264 /* SyntaxKind.ClassDeclaration */ || parent.kind === 232 /* SyntaxKind.ClassExpression */
1438
- ? parent
1439
- : undefined;
1440
- }
1441
- if (isFunctionLike(current) || current.kind === 307 /* SyntaxKind.SourceFile */)
1442
- return undefined;
1443
- }
1444
- return undefined;
1445
- }
1446
- /**
1447
- * Adds the expressions whose types make up the type of `node`: both sides of `?:`, `||` and `??`,
1448
- * and the elements of an array literal that `node` indexes when `arrays` (see SiblingPropertyScan).
1449
- */
1450
- function addAlternatives(node, out, arrays) {
1451
- switch (node.kind) {
1452
- case 218 /* SyntaxKind.ParenthesizedExpression */:
1453
- case 236 /* SyntaxKind.NonNullExpression */:
1454
- case 239 /* SyntaxKind.SatisfiesExpression */:
1455
- case 224 /* SyntaxKind.AwaitExpression */:
1456
- return addAlternatives(getExpression(node), out, arrays);
1457
- case 235 /* SyntaxKind.AsExpression */:
1458
- case 217 /* SyntaxKind.TypeAssertionExpression */:
1459
- // An object literal in an `as const` assertion is still an object literal type, with readonly
1460
- // properties; an array literal in one is a tuple, whose elements widen separately.
1461
- if (isConstAssertion(node) && skipParentheses(getExpression(node)).kind === 211 /* SyntaxKind.ObjectLiteralExpression */)
1462
- return addAlternatives(getExpression(node), out, arrays);
1463
- break;
1464
- case 228 /* SyntaxKind.ConditionalExpression */:
1465
- addAlternatives(node.whenTrue, out, arrays);
1466
- return addAlternatives(node.whenFalse, out, arrays);
1467
- case 227 /* SyntaxKind.BinaryExpression */: {
1468
- const binary = node;
1469
- switch (binary.operatorToken.kind) {
1470
- case 56 /* SyntaxKind.BarBarToken */:
1471
- case 60 /* SyntaxKind.QuestionQuestionToken */:
1472
- addAlternatives(binary.left, out, arrays);
1473
- return addAlternatives(binary.right, out, arrays);
1474
- case 55 /* SyntaxKind.AmpersandAmpersandToken */:
1475
- case 27 /* SyntaxKind.CommaToken */:
1476
- return addAlternatives(binary.right, out, arrays);
1477
- }
1478
- break;
1479
- }
1480
- case 213 /* SyntaxKind.ElementAccessExpression */: {
1481
- // `[a, b][i]` has the type of the array's elements, which the declaration widens as one union.
1482
- const array = skipParentheses(getExpression(node));
1483
- if (!arrays || array.kind !== 210 /* SyntaxKind.ArrayLiteralExpression */)
1484
- break;
1485
- for (const e of array.elements.nodes)
1486
- if (e.kind !== 231 /* SyntaxKind.SpreadElement */)
1487
- addAlternatives(e, out, arrays);
1488
- return out;
1489
- }
1490
- }
1491
- out.push(node);
1492
- return out;
1493
- }
1494
- /**
1495
- * The alternatives of the elements of an array literal, whose types make its element type, with those
1496
- * of the array literals that its spread elements spread. Used where array literals have an array type
1497
- * (`SiblingPropertyScan.arrays`), as are `iteratedArrayLiterals` and `bindingAlternatives`.
1498
- */
1499
- function addArrayElements(array, out) {
1500
- for (const e of array.elements.nodes)
1501
- if (e.kind !== 231 /* SyntaxKind.SpreadElement */)
1502
- addAlternatives(e, out, true);
1503
- else
1504
- for (const spread of addAlternatives(getExpression(e), [], true))
1505
- if (spread.kind === 210 /* SyntaxKind.ArrayLiteralExpression */)
1506
- addArrayElements(spread, out);
1507
- return out;
1508
- }
1509
- /** The array literals whose elements a `for...of` over `expression` iterates, through `as const`. */
1510
- function iteratedArrayLiterals(expression) {
1511
- const arrays = [];
1512
- for (const a of addAlternatives(expression, [], true)) {
1513
- const value = isConstAssertion(a) ? skipParentheses(getExpression(a)) : a;
1514
- if (value.kind === 210 /* SyntaxKind.ArrayLiteralExpression */)
1515
- arrays.push(value);
1516
- }
1517
- return arrays;
1518
- }
1519
- function isConstAssertion(node) {
1520
- return ((node.kind === 235 /* SyntaxKind.AsExpression */ || node.kind === 217 /* SyntaxKind.TypeAssertionExpression */) &&
1521
- isConstTypeReference(getType(node)));
1522
- }
1523
- /**
1524
- * The alternatives whose union each variable of a binding name is widened from, given the alternatives
1525
- * of the iterated elements: all of them for an identifier; for an element of an array pattern, its
1526
- * element of each `as const` array literal, or every element of another array literal; for an element
1527
- * of an object pattern, the values of its property in each object literal.
1528
- */
1529
- function bindingAlternatives(name, elements) {
1530
- if (name.kind === 79 /* SyntaxKind.Identifier */)
1531
- return [elements];
1532
- const result = [];
1533
- const isArrayPattern = name.kind === 208 /* SyntaxKind.ArrayBindingPattern */;
1534
- const bindings = name.elements.nodes;
1535
- for (let k = 0; k < bindings.length; k++) {
1536
- const binding = bindings[k];
1537
- if (binding.kind !== 209 /* SyntaxKind.BindingElement */)
1538
- continue;
1539
- const element = binding;
1540
- if (element.dotDotDotToken !== undefined || element.initializer !== undefined)
1541
- continue;
1542
- if (element.name?.kind !== 79 /* SyntaxKind.Identifier */)
1543
- continue;
1544
- const alternatives = [];
1545
- if (isArrayPattern) {
1546
- for (const e of elements) {
1547
- const value = isConstAssertion(e) ? skipParentheses(getExpression(e)) : e;
1548
- if (value.kind !== 210 /* SyntaxKind.ArrayLiteralExpression */)
1549
- continue;
1550
- const array = value;
1551
- if (value !== e || isInConstAssertion(array)) {
1552
- const item = array.elements.nodes[k];
1553
- if (item !== undefined && item.kind !== 231 /* SyntaxKind.SpreadElement */)
1554
- addAlternatives(item, alternatives, true);
1555
- }
1556
- else
1557
- addArrayElements(array, alternatives);
1558
- }
1559
- }
1560
- else {
1561
- const property = element.propertyName ?? element.name;
1562
- const propertyName = property.kind === 79 /* SyntaxKind.Identifier */ ? getText(property) : undefined;
1563
- if (propertyName === undefined)
1564
- continue;
1565
- for (const e of elements) {
1566
- if (e.kind !== 211 /* SyntaxKind.ObjectLiteralExpression */)
1567
- continue;
1568
- for (const member of e.properties.nodes)
1569
- if (member.kind === 303 /* SyntaxKind.PropertyAssignment */ && staticPropertyName(member) === propertyName)
1570
- addAlternatives(member.initializer, alternatives, true);
1571
- }
1572
- }
1573
- if (alternatives.length !== 0)
1574
- result.push(alternatives);
1575
- }
1576
- return result;
1577
- }
1578
- /** The name of an object literal member with a literal name. */
1579
- function staticPropertyName(member) {
1580
- switch (member.kind) {
1581
- case 303 /* SyntaxKind.PropertyAssignment */:
1582
- case 304 /* SyntaxKind.ShorthandPropertyAssignment */:
1583
- case 175 /* SyntaxKind.MethodDeclaration */:
1584
- case 178 /* SyntaxKind.GetAccessor */:
1585
- case 179 /* SyntaxKind.SetAccessor */:
1586
- break;
1587
- default:
1588
- return undefined;
1589
- }
1590
- const name = getName(member);
1591
- switch (name.kind) {
1592
- case 79 /* SyntaxKind.Identifier */:
1593
- case 10 /* SyntaxKind.StringLiteral */:
1594
- case 14 /* SyntaxKind.NoSubstitutionTemplateLiteral */:
1595
- return getText(name);
1596
- case 8 /* SyntaxKind.NumericLiteral */:
1597
- return String(Number(getText(name)));
1598
- }
1599
- return undefined;
1600
- }
1601
671
  function getPropertiesOfContext(c, context) {
1602
672
  if (context.resolvedProperties === undefined) {
1603
673
  const names = new Map();
@@ -1678,7 +748,7 @@ export function isReadonlySymbol(c, symbol) {
1678
748
  (getDeclarationNodeFlagsFromSymbol(symbol) & 6 /* NodeFlags.Constant */) !== 0) ||
1679
749
  ((symbol.flags & 98304 /* SymbolFlags.Accessor */) !== 0 && (symbol.flags & 65536 /* SymbolFlags.SetAccessor */) === 0) ||
1680
750
  (symbol.flags & 8 /* SymbolFlags.EnumMember */) !== 0 ||
1681
- someReadonlyAssignmentDeclaration(c, symbol.declarations));
751
+ someReadonlyAssignmentDeclaration(c, getDeclarations(symbol)));
1682
752
  }
1683
753
  function someReadonlyAssignmentDeclaration(c, declarations) {
1684
754
  for (const d of declarations)
@@ -1709,7 +779,7 @@ function isReadonlyAssignmentDeclaration(c, node) {
1709
779
  import { getDeclarationModifierFlagsFromSymbol, getPropertyOfType, getTypeOfPropertyOfType, } from './checker-members.js';
1710
780
  import { checkExpression } from './checker-expressions.js';
1711
781
  import { getParameterTypeOfFullSignature } from './checker-jsdoc.js';
1712
- import { isFunctionLike, isFunctionLikeDeclarationKind, isInJSFile } from './ast-utilities.js';
782
+ import { isFunctionLikeDeclarationKind, isInJSFile } from './ast-utilities.js';
1713
783
  import { isEmptyArrayLiteralType } from './relations.js';
1714
784
  import { isCheckJsEnabledForFile } from './program-utilities.js';
1715
785
  function getDeclarationModifierFlags(c, symbol) {
@@ -1738,7 +808,7 @@ function getAnnotatedAccessorType(c, accessor) {
1738
808
  }
1739
809
  /** The `accessor` property declaration of `symbol`, if any. */
1740
810
  function getAutoAccessorDeclaration(symbol) {
1741
- for (const d of symbol.declarations)
811
+ for (const d of getDeclarations(symbol))
1742
812
  if (d.kind === 173 /* SyntaxKind.PropertyDeclaration */ && (getCombinedModifierFlags(d) & 512 /* ModifierFlags.Accessor */) !== 0)
1743
813
  return d;
1744
814
  return undefined;
@@ -1750,8 +820,8 @@ function getTypeOfAccessors(c, symbol) {
1750
820
  return type;
1751
821
  if (!pushTypeResolution(c, symbol, 0 /* TypeSystemPropertyName.Type */))
1752
822
  return c.errorType;
1753
- const getter = symbol.declarations.find((d) => d.kind === 178 /* SyntaxKind.GetAccessor */);
1754
- const setter = symbol.declarations.find((d) => d.kind === 179 /* SyntaxKind.SetAccessor */);
823
+ const getter = getDeclarations(symbol).find((d) => d.kind === 178 /* SyntaxKind.GetAccessor */);
824
+ const setter = getDeclarations(symbol).find((d) => d.kind === 179 /* SyntaxKind.SetAccessor */);
1755
825
  const accessor = getAutoAccessorDeclaration(symbol);
1756
826
  // Getter annotation, setter annotation, accessor annotation, then getter body.
1757
827
  let t = getAnnotatedAccessorType(c, getter) ?? getAnnotatedAccessorType(c, setter) ?? getAnnotatedAccessorType(c, accessor);
@@ -1788,7 +858,7 @@ export function getWriteTypeOfAccessors(c, symbol) {
1788
858
  return links.writeType;
1789
859
  if (!pushTypeResolution(c, symbol, 7 /* TypeSystemPropertyName.WriteType */))
1790
860
  return c.errorType;
1791
- const setter = symbol.declarations.find((d) => d.kind === 179 /* SyntaxKind.SetAccessor */) ?? getAutoAccessorDeclaration(symbol);
861
+ const setter = getDeclarations(symbol).find((d) => d.kind === 179 /* SyntaxKind.SetAccessor */) ?? getAutoAccessorDeclaration(symbol);
1792
862
  let writeType = getAnnotatedAccessorType(c, setter);
1793
863
  if (!popTypeResolution(c)) {
1794
864
  if (getAnnotatedAccessorTypeNode(setter) !== undefined)