@inthedark/dxlint 0.0.0-stage → 0.1.1

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 (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +84 -2
  3. package/bin/dxlint +15 -0
  4. package/bin/dxlint-linux-x64 +0 -0
  5. package/bin/dxlint-win32-x64.exe +0 -0
  6. package/package.json +40 -4
  7. package/rules/conventions.dx +34 -0
  8. package/rules/correctness.dx +33 -0
  9. package/rules/es-no-semi.dx +11 -0
  10. package/rules/format.dx +49 -0
  11. package/rules/recommended.dx +51 -0
  12. package/rules/suspicious.dx +31 -0
  13. package/rules/types.dx +21 -0
  14. package/std/c.dx +23 -0
  15. package/std/core/lines.dx +51 -0
  16. package/std/core/lists.dx +76 -0
  17. package/std/core.dx +30 -0
  18. package/std/css.dx +17 -0
  19. package/std/dx.dx +31 -0
  20. package/std/json.dx +104 -0
  21. package/std/markdown.dx +11 -0
  22. package/std/python.dx +21 -0
  23. package/std/rust/format.dx +316 -0
  24. package/std/rust.dx +34 -0
  25. package/std/toml.dx +19 -0
  26. package/std/ts/arithmetic.dx +87 -0
  27. package/std/ts/assertions.dx +477 -0
  28. package/std/ts/calls.dx +246 -0
  29. package/std/ts/comments.dx +10 -0
  30. package/std/ts/conditions.dx +531 -0
  31. package/std/ts/constants.dx +335 -0
  32. package/std/ts/deprecation.dx +185 -0
  33. package/std/ts/directives.dx +60 -0
  34. package/std/ts/effects.dx +219 -0
  35. package/std/ts/enums.dx +233 -0
  36. package/std/ts/expression-fixes.dx +67 -0
  37. package/std/ts/expressions.dx +270 -0
  38. package/std/ts/flow.dx +2 -0
  39. package/std/ts/format.dx +755 -0
  40. package/std/ts/globals.dx +106 -0
  41. package/std/ts/grammar.dx +488 -0
  42. package/std/ts/helpers.dx +102 -0
  43. package/std/ts/import-attributes.dx +88 -0
  44. package/std/ts/import-no-deprecated.dx +73 -0
  45. package/std/ts/indentation.dx +442 -0
  46. package/std/ts/jsx.dx +646 -0
  47. package/std/ts/layout/blocks.dx +281 -0
  48. package/std/ts/layout/calls.dx +79 -0
  49. package/std/ts/layout/expressions.dx +213 -0
  50. package/std/ts/layout/literals.dx +114 -0
  51. package/std/ts/layout/separators.dx +184 -0
  52. package/std/ts/layout/shared.dx +203 -0
  53. package/std/ts/layout/text.dx +142 -0
  54. package/std/ts/layout/types.dx +182 -0
  55. package/std/ts/layout.dx +9 -0
  56. package/std/ts/license.dx +54 -0
  57. package/std/ts/literal-text.dx +3 -0
  58. package/std/ts/literals.dx +95 -0
  59. package/std/ts/methods.dx +69 -0
  60. package/std/ts/modules.dx +268 -0
  61. package/std/ts/names.dx +348 -0
  62. package/std/ts/naming.dx +169 -0
  63. package/std/ts/narrowing.dx +96 -0
  64. package/std/ts/parentheses.dx +262 -0
  65. package/std/ts/precedence.dx +25 -0
  66. package/std/ts/problems/calls.dx +34 -0
  67. package/std/ts/problems/classes.dx +39 -0
  68. package/std/ts/problems/control-flow.dx +257 -0
  69. package/std/ts/problems/declarations.dx +67 -0
  70. package/std/ts/problems/duplicates.dx +70 -0
  71. package/std/ts/problems/expressions.dx +154 -0
  72. package/std/ts/problems/helpers.dx +22 -0
  73. package/std/ts/problems/imports.dx +43 -0
  74. package/std/ts/problems/non-null.dx +78 -0
  75. package/std/ts/problems/numeric-precision.dx +66 -0
  76. package/std/ts/problems/regexp.dx +66 -0
  77. package/std/ts/problems/statements.dx +36 -0
  78. package/std/ts/problems/typescript.dx +55 -0
  79. package/std/ts/problems/whitespace.dx +40 -0
  80. package/std/ts/problems.dx +20 -0
  81. package/std/ts/regex/checks.dx +249 -0
  82. package/std/ts/regex/findings.dx +209 -0
  83. package/std/ts/regex/grammar.dx +80 -0
  84. package/std/ts/regex/suggestions.dx +94 -0
  85. package/std/ts/regex/values.dx +378 -0
  86. package/std/ts/regex.dx +5 -0
  87. package/std/ts/restrictions/comments.dx +76 -0
  88. package/std/ts/restrictions.dx +811 -0
  89. package/std/ts/scopes.dx +68 -0
  90. package/std/ts/scoping/atomic.dx +40 -0
  91. package/std/ts/scoping/bindings.dx +126 -0
  92. package/std/ts/scoping/constness.dx +72 -0
  93. package/std/ts/scoping/globals.dx +52 -0
  94. package/std/ts/scoping/loops.dx +86 -0
  95. package/std/ts/scoping/member-uses.dx +111 -0
  96. package/std/ts/scoping/receivers.dx +107 -0
  97. package/std/ts/scoping/shadowing.dx +202 -0
  98. package/std/ts/scoping/usage.dx +393 -0
  99. package/std/ts/scoping.dx +842 -0
  100. package/std/ts/selectors.dx +10 -0
  101. package/std/ts/spreads.dx +97 -0
  102. package/std/ts/structure/declarations.dx +52 -0
  103. package/std/ts/structure/lines.dx +79 -0
  104. package/std/ts/structure/lists.dx +144 -0
  105. package/std/ts/structure/padding.dx +258 -0
  106. package/std/ts/structure.dx +963 -0
  107. package/std/ts/style/classes.dx +157 -0
  108. package/std/ts/style/comments.dx +75 -0
  109. package/std/ts/style/declarations.dx +82 -0
  110. package/std/ts/style/expressions.dx +125 -0
  111. package/std/ts/style/imports.dx +44 -0
  112. package/std/ts/style/ordering.dx +192 -0
  113. package/std/ts/style/returns.dx +152 -0
  114. package/std/ts/style/signatures.dx +308 -0
  115. package/std/ts/style/type-exports.dx +62 -0
  116. package/std/ts/style/type-imports.dx +166 -0
  117. package/std/ts/style/types.dx +474 -0
  118. package/std/ts/style.dx +29 -0
  119. package/std/ts/suggestions/accessors.dx +138 -0
  120. package/std/ts/suggestions/builtins.dx +110 -0
  121. package/std/ts/suggestions/comparisons.dx +41 -0
  122. package/std/ts/suggestions/computed-keys.dx +20 -0
  123. package/std/ts/suggestions/constructors.dx +54 -0
  124. package/std/ts/suggestions/control-flow.dx +204 -0
  125. package/std/ts/suggestions/curly.dx +124 -0
  126. package/std/ts/suggestions/directives.dx +79 -0
  127. package/std/ts/suggestions/errors.dx +106 -0
  128. package/std/ts/suggestions/expressions.dx +230 -0
  129. package/std/ts/suggestions/functions.dx +260 -0
  130. package/std/ts/suggestions/globals.dx +187 -0
  131. package/std/ts/suggestions/helpers.dx +17 -0
  132. package/std/ts/suggestions/object-shorthand.dx +148 -0
  133. package/std/ts/suggestions/object-spread.dx +96 -0
  134. package/std/ts/suggestions/regexp.dx +59 -0
  135. package/std/ts/suggestions/renames.dx +50 -0
  136. package/std/ts/suggestions/strings.dx +47 -0
  137. package/std/ts/suggestions/templates.dx +104 -0
  138. package/std/ts/suggestions.dx +24 -0
  139. package/std/ts/template-fixes.dx +22 -0
  140. package/std/ts/tokens.dx +124 -0
  141. package/std/ts/type-specifiers.dx +93 -0
  142. package/std/ts/typed/assignments.dx +154 -0
  143. package/std/ts/typed/awaitability.dx +157 -0
  144. package/std/ts/typed/boolean-comparisons.dx +82 -0
  145. package/std/ts/typed/defaults.dx +97 -0
  146. package/std/ts/typed/enum-consistency.dx +68 -0
  147. package/std/ts/typed/enums.dx +52 -0
  148. package/std/ts/typed/errors.dx +32 -0
  149. package/std/ts/typed/floating-promises.dx +147 -0
  150. package/std/ts/typed/for-in-arrays.dx +21 -0
  151. package/std/ts/typed/includes.dx +102 -0
  152. package/std/ts/typed/nullish-coalescing.dx +149 -0
  153. package/std/ts/typed/optional-chains.dx +317 -0
  154. package/std/ts/typed/promises.dx +205 -0
  155. package/std/ts/typed/qualifiers.dx +108 -0
  156. package/std/ts/typed/readonly-members.dx +159 -0
  157. package/std/ts/typed/readonly-parameters.dx +121 -0
  158. package/std/ts/typed/redundant-constituents.dx +171 -0
  159. package/std/ts/typed/regexp-exec.dx +79 -0
  160. package/std/ts/typed/rejection-handlers.dx +91 -0
  161. package/std/ts/typed/return-await.dx +116 -0
  162. package/std/ts/typed/return-this.dx +67 -0
  163. package/std/ts/typed/strict-void-return.dx +121 -0
  164. package/std/ts/typed/string-boundaries.dx +153 -0
  165. package/std/ts/typed/switches.dx +147 -0
  166. package/std/ts/typed/template-expressions.dx +127 -0
  167. package/std/ts/typed/template-values.dx +88 -0
  168. package/std/ts/typed/transfers.dx +106 -0
  169. package/std/ts/typed/type-arguments.dx +337 -0
  170. package/std/ts/typed/type-constituents.dx +143 -0
  171. package/std/ts/typed/type-conversions.dx +82 -0
  172. package/std/ts/typed/type-parameters.dx +280 -0
  173. package/std/ts/typed/unbound-method.dx +167 -0
  174. package/std/ts/typed/unsafe-calls.dx +73 -0
  175. package/std/ts/typed/unsafe-members.dx +36 -0
  176. package/std/ts/typed/void-expressions.dx +101 -0
  177. package/std/ts/typed.dx +585 -0
  178. package/std/ts/values/arguments.dx +13 -0
  179. package/std/ts/values/enums.dx +104 -0
  180. package/std/ts/values/inspection.dx +274 -0
  181. package/std/ts/values.dx +1217 -0
  182. package/std/ts.dx +77 -0
  183. package/std/whitespace.dx +37 -0
@@ -0,0 +1,1217 @@
1
+ import .ts/values/enums
2
+ import .ts/values/arguments
3
+ import .ts/values/inspection
4
+
5
+ define {
6
+
7
+ # A primitive intersected with an object type stays (a branded `string &
8
+ # { tag }`, `string & number[]`); only nullish values exclude objects.
9
+ exclusive null, undefined, string, number, boolean, bigint, symbol
10
+ exclusive null, undefined, object
11
+ exclusive falsy, truthy
12
+ exclusive thenable, nonthenable
13
+ # TypeScript's `object` keyword takes every non-primitive: a shape with members is one.
14
+ trait shaped implies object
15
+ top unknown
16
+ dynamic any
17
+ keys text string, nonthenable
18
+ keys ordinal number, nonthenable
19
+ keys symbol symbol, nonthenable
20
+ keys truthiness truthy falsy
21
+ apparent number inherits instance "Number"
22
+ apparent string inherits instance "String"
23
+ apparent boolean inherits instance "Boolean"
24
+ apparent bigint inherits instance "BigInt"
25
+ apparent symbol inherits instance "Symbol"
26
+ apparent callable inherits instance "Function"
27
+ product ts_tuple items fits object, truthy, tuple as { kind: "tuple" } inherits instance "Array" of ...items readonly inherits instance "ReadonlyArray" of ...items optional trait undefined, nullable, falsy, nonthenable count "length"
28
+ product ts_array items fits object, truthy, array as { kind: "array" } inherits instance "Array" of ...items readonly inherits instance "ReadonlyArray" of ...items
29
+
30
+ in ts_program { fits object, truthy in module }
31
+ in ts_namespace {
32
+ unwraps trait unknown for type
33
+ let ts_value_namespace_runtime ts_namespace_runtime(node)
34
+ when ts_value_namespace_runtime {
35
+ unwraps scope namespace from it
36
+ fits object, truthy in namespace
37
+ }
38
+ when !ts_value_namespace_runtime { unwraps unknown }
39
+ }
40
+
41
+ fn ts_value_key(key) = {
42
+ value literal: if key is ts_computed_key then key.expression.node else key
43
+ value allowed: key not ts_computed_key || literal is (ts_string, ts_number, ts_bigint, ts_boolean, ts_null)
44
+ || literal is ts_template && count(literal.expression.nodes) == 0
45
+ value if !allowed then null
46
+ else if literal is ts_string then js_string(literal.text)
47
+ else if literal is ts_template && count(literal.expression.nodes) == 0 then js_string_parts(literal.text).text
48
+ else if literal is ts_number then "" literal.ts_value_number
49
+ else if literal is ts_bigint then js_bigint(literal.text)
50
+ else if literal is ts_identifier && !contains(literal.text, "\\") then literal.text
51
+ else { value "\"" literal.text "\""; js_string }
52
+ }
53
+ fn ts_value_static(modifiers) = any(modifiers, modifier => modifier.text == "static")
54
+ fn ts_value_negated_bigint(text) = {
55
+ value magnitude: js_bigint(text)
56
+ value if magnitude == null || magnitude == "0" then magnitude else "-" magnitude
57
+ }
58
+ fn ts_value_this_context(n) = {
59
+ value chain: walk(n, item => item.parent.node)
60
+ value owner: find(chain, item => item is (ts_function, ts_method, ts_class_property, ts_static_block, ts_class))
61
+ value parameters: filter(owner.parameter.nodes, parameter => parameter.name is ts_this || parameter.name == "this")
62
+ value container: if owner is ts_class then owner
63
+ else if owner is ts_function && owner.parent is ts_property then owner.parent.parent.node else owner.parent.node
64
+ value { owner: owner, explicit: first(parameters).type.node, container: container,
65
+ static: owner is ts_static_block || ts_value_static(owner.modifier.nodes) }
66
+ }
67
+ # A local declaration already answers the common case; only an import can
68
+ # name a class through another module and still fail to carry it.
69
+ fn ts_value_missing_name(n, class) = {
70
+ value local: n.scope_declaration ?? n
71
+ value if local.parent not (ts_import_specifier, ts_import, ts_import_equals)
72
+ && contains(name_role_classes(local.node, "declaration"), class) then false else {
73
+ value classes: name_classes(n, n.text, null, [class])
74
+ value classes != null && get(classes, class) == false
75
+ }
76
+ }
77
+ fn ts_value_global_declaration(n, name) = {
78
+ value identity: declaration_identity(n)
79
+ value targets: global_declarations(n, name, "type")
80
+ value identity != null && targets != null
81
+ && any(targets, target => target.file == identity.file && target.node == identity.node)
82
+ }
83
+ fn ts_value_library_type_sites(n, name) = {
84
+ value sites: global_declarations(n, name, "type")
85
+ value origins: map(sites ?? [], site => declaration_origin(n, site))
86
+ value if sites == null || any(origins, origin => origin == null) then null
87
+ else filter(sites, site => declaration_origin(n, site).kind == "library")
88
+ }
89
+ # Syntax first: only `const x = Symbol(...)` / `Symbol.for(...)` reaches the
90
+ # semantic checks, so other declarators never ask (or wait on) them.
91
+ fn ts_value_unique_symbol_binding(n) = n.type == null && n.name is ts_identifier && n.parent.kind == "const"
92
+ && n.parent.parent not (ts_for, ts_for_in, ts_for_of)
93
+ && ts_value_symbol_call(unparenthesised_node(n.init.node), n)
94
+ fn ts_value_symbol_call(init, n) = init is ts_call && init.optional == null
95
+ && ts_value_symbol_base(if init.callee.node is ts_member && init.callee.property.text == "for"
96
+ then init.callee.object.node else init.callee.node, n)
97
+ && init always symbol
98
+ fn ts_value_symbol_base(base, n) = base is ts_identifier && base.text == "Symbol" && {
99
+ value identity: declaration_identity(base)
100
+ value globals: global_declarations(n, "Symbol", "value")
101
+ value identity != null && globals != null && any(globals, global => global == identity)
102
+ }
103
+ fn ts_value_array_part(part, n) = {
104
+ value carrier: part.carrier
105
+ value name: carrier.description.name
106
+ value tuple: part.carrier_known && carrier.description.kind == "tuple"
107
+ value intrinsic: part.carrier_known && carrier.description.kind == "array"
108
+ value named: part.carrier_known && carrier.description.kind == "named"
109
+ && (name == "Array" || name == "ReadonlyArray")
110
+ value declarations: if named then global_declarations(n, name, "type") else null
111
+ value array: intrinsic || named && declarations != null
112
+ && any(declarations, declaration => declaration.file == carrier.file && declaration.node == carrier.node)
113
+ value scalar: any(["null", "undefined", "string", "number", "boolean", "bigint", "symbol"],
114
+ trait => contains(part.traits, trait))
115
+ value indeterminate: any(["any", "unknown", "error", "parameter"], trait => contains(part.traits, trait))
116
+ value known: !indeterminate && (tuple || scalar || part.carrier_known && !named
117
+ || named && declarations != null)
118
+ value { array: array, tuple: tuple, known: known }
119
+ }
120
+ fn ts_value_array_parts(parts, n) = if parts == null then { array: false, tuple: false, known: false } else {
121
+ value arms: map(parts, arm => {
122
+ value shapes: map(arm, part => ts_value_array_part(part, n))
123
+ value array: any(shapes, shape => shape.known && shape.array)
124
+ value tuple: any(shapes, shape => shape.known && shape.tuple)
125
+ value { array: array, tuple: tuple, known: array || tuple || all(shapes, shape => shape.known) }
126
+ })
127
+ value array: len(arms) > 0 && all(arms, arm => arm.known && arm.array && !arm.tuple)
128
+ value tuple: len(arms) > 0 && all(arms, arm => arm.known && arm.tuple && !arm.array)
129
+ value neither: all(arms, arm => arm.known && !arm.array && !arm.tuple)
130
+ value { array: array, tuple: tuple, known: array || tuple || neither }
131
+ }
132
+ fn ts_value_array_shape(n) = {
133
+ value parts: value_constituents(n)
134
+ value ts_value_array_parts(parts, n)
135
+ }
136
+ fn ts_value_array_like(n) = {
137
+ value arms: value_parts(n)
138
+ value arms != null && count(arms) > 0 && all(arms, arm => any(arm.parts, part => {
139
+ value shape: ts_value_array_part(part, n)
140
+ value shape.known && (shape.array || shape.tuple)
141
+ }))
142
+ }
143
+ # A rest element's remaining values: a plain array gives the same array (a
144
+ # tuple would give the tail, which no recipe can slice yet).
145
+ fn ts_value_array_rest(n) = {
146
+ value arms: value_parts(n)
147
+ value arms != null && count(arms) > 0 && all(arms, arm =>
148
+ any(arm.parts, part => contains(part.traits, "array") && !contains(part.traits, "tuple")))
149
+ }
150
+ # One alternative of the library's `Promise` and declarations merged into it
151
+ # (not a subclass: its first type argument need not be what it resolves to).
152
+ fn ts_value_lib_promise(n) = {
153
+ value arms: value_parts(n)
154
+ value arms != null && count(arms) == 1
155
+ && any(first(arms).parts, part => ts_value_builtin_part(part, n, ["Promise"]) == true)
156
+ && all(first(arms).parts, part => part.carrier.description.name == "Promise")
157
+ }
158
+ fn ts_value_string_array(n) = {
159
+ value arms: value_parts(n)
160
+ value ts_value_array_like(n) && all(arms, arm => {
161
+ # A type parameter's own part stands beside its constraint's.
162
+ value parts: filter(arm.parts, part => !contains(part.traits, "parameter"))
163
+ value empty: any(parts, part => part.product_known && part.product != null && count(part.product.elements) == 0)
164
+ value element: value_constituents(value_lookup(arm.handle, 0).member)
165
+ value (count(parts) == 1 && empty) || (element != null && count(element) > 0
166
+ && all(element, item => any(item, part => contains(part.traits, "string"))))
167
+ })
168
+ }
169
+ fn ts_value_const_context(n) = {
170
+ value chain: walk(n, item => if item.parent is (ts_parenthesized, ts_array, ts_object, ts_spread, ts_property) then item.parent.node else null)
171
+ value outer: last(chain).parent.node
172
+ value outer is (ts_as, ts_type_assertion) && outer.type.text == "const"
173
+ }
174
+ fn ts_value_destructuring_receiver(n) = {
175
+ value path: walk(n, current => if current is (ts_parenthesized.expression, ts_array.element) then current.parent.node else null)
176
+ value terminal: last(path)
177
+ value receiver: if terminal is (ts_declarator.init, ts_parameter.init) then terminal.parent.name.node
178
+ else if terminal is (ts_assignment.right, ts_assignment_pattern.right) then terminal.parent.left.node else null
179
+ value fold(reverse(path), receiver, (target, current) => if current is ts_array.element then {
180
+ value position: index_of(current.parent.element.nodes, current)
181
+ value { value target.element.nodes; slice position (position + 1); first }
182
+ } else target)
183
+ }
184
+ fn ts_value_tuple_context(n) = {
185
+ value receiver: ts_value_destructuring_receiver(n)
186
+ value n.flow_destructuring_target || receiver is (ts_array, ts_array_pattern)
187
+ || !any(n.element.nodes, item => item is ts_spread)
188
+ && (ts_value_const_context(n) || value_has_expected(n) && n.expected could tuple)
189
+ }
190
+ fn ts_value_scalar_part(n) = {
191
+ value parts: value_constituents(n)
192
+ value arm: first(parts)
193
+ value if parts != null && len(parts) == 1 && len(arm) == 1 then first(arm) else null
194
+ }
195
+ fn ts_value_literal_branch(n, part) = {
196
+ value branch: ts_value_scalar_part(n)
197
+ value part != null && branch != null && part.identity_known && branch.identity_known
198
+ && part.identity != null && part.identity == branch.identity
199
+ }
200
+ fn ts_value_logical_choice(n) = {
201
+ value left: {
202
+ value n.left.node
203
+ walk (current) if current is (ts_parenthesized, ts_as, ts_satisfies, ts_non_null, ts_type_assertion)
204
+ then current.expression.node else null
205
+ last
206
+ }
207
+ value if left is ts_new && n.operator == "&&" then n.right.node
208
+ else if left is ts_new && contains(["||", "??"], n.operator) then n.left.node
209
+ else if n.operator == "||" && n.left always falsy then n.right.node
210
+ else if n.operator == "||" && n.left always truthy then n.left.node
211
+ else if n.operator == "&&" && n.left always falsy then n.left.node
212
+ else if n.operator == "&&" && n.left always truthy then n.right.node
213
+ else if n.operator == "??" && n.left always nullable then n.right.node
214
+ else if n.operator == "??" && !(n.left could nullable) then n.left.node else null
215
+ }
216
+ fn ts_value_fresh_step(bare, part) = {
217
+ value if bare is (ts_number, ts_string, ts_boolean, ts_bigint) then {valid: true, children: []}
218
+ else if bare is ts_unary && (bare.operator == "-" || bare.operator == "+") then {valid: true, children: [bare.argument.node]}
219
+ else if bare is ts_identifier then {
220
+ value declaration: bare.scope_declaration.parent.node
221
+ value valid: declaration is ts_declarator && declaration.type == null && declaration.parent.kind == "const"
222
+ value {valid: valid, children: if valid then [declaration.init.node] else []}
223
+ } else if bare is ts_conditional then {
224
+ value branches: filter([bare.consequent.node, bare.alternate.node], branch => ts_value_literal_branch(branch, part))
225
+ value {valid: len(branches) > 0, children: branches}
226
+ } else if bare is ts_logical then {
227
+ value chosen: ts_value_logical_choice(bare)
228
+ value if chosen != null then {valid: true, children: [chosen]} else {
229
+ value branches: filter([bare.left.node, bare.right.node], branch => ts_value_literal_branch(branch, part))
230
+ value {valid: len(branches) > 0, children: branches}
231
+ }
232
+ } else if bare is ts_sequence then {valid: true, children: [last(bare.expression.nodes)]}
233
+ else if bare is ts_satisfies then {valid: true, children: [bare.expression.node]}
234
+ else {valid: false, children: []}
235
+ }
236
+ fn ts_value_fresh_literal(n, part) = {
237
+ value {node: unparenthesised_node(n), seen: []}
238
+ walk (entry) if contains(entry.seen, entry.node) then [] else {
239
+ value children: ts_value_fresh_step(entry.node, part).children
240
+ value map(children, child => {node: unparenthesised_node(child), seen: entry.seen [entry.node]})
241
+ }
242
+ all (entry) !contains(entry.seen, entry.node) && ts_value_fresh_step(entry.node, part).valid
243
+ }
244
+ fn ts_value_contextual_literal(n, part) = {
245
+ value category: find(["string", "number", "boolean", "bigint"], trait => contains(part.traits, trait))
246
+ value wanted: if category == "boolean" then ["boolean"] else if category == null then null else [category, "literal"]
247
+ # A type parameter in the context (one being inferred) stands for its
248
+ # constraint only, as in TypeScript: an unconstrained one keeps no literal.
249
+ value arms: if wanted == null || !value_has_expected(n) then null else value_parts(value_expected_handle(n))
250
+ value when {
251
+ wanted == null || !value_has_expected(n) then false
252
+ arms != null && any(arms, arm => any(arm.parts, item => contains(item.traits, "parameter")))
253
+ then any(arms, arm => all(wanted, trait => any(arm.parts, item => !contains(item.traits, "parameter") && contains(item.traits, trait))))
254
+ category == "boolean" then n.expected could boolean
255
+ category == "string" then n.expected could (string, literal)
256
+ category == "number" then n.expected could (number, literal)
257
+ else then n.expected could (bigint, literal)
258
+ }
259
+ }
260
+ in ts_node let ts_value_preserved_element {
261
+ value bare: unparenthesised_node(node)
262
+ value if ts_value_const_context(node) || bare not (ts_null, ts_boolean, ts_number, ts_bigint, ts_string,
263
+ ts_template, ts_unary, ts_identifier, ts_conditional, ts_logical, ts_sequence, ts_satisfies) then node else {
264
+ value part: ts_value_scalar_part(node)
265
+ value if !ts_value_fresh_literal(node, part) || ts_value_contextual_literal(node, part) then node else null
266
+ }
267
+ }
268
+ fn ts_value_closed_type_members(items) = all(items, member => member is ts_call_signature
269
+ || (if member is ts_index_signature then member.parameter.type always number || member.parameter.type always symbol
270
+ else ts_value_key(member.key.node) != null
271
+ || member.key is ts_computed_key && member.key.expression.node always symbol))
272
+ fn ts_value_then_headers(signatures) = {
273
+ value headers: { value signatures ?? []; flat_map (arm) arm }
274
+ value callbacks: { value headers; flat_map (header) first(header.parameters) ?? []; flat_map (arm) arm }
275
+ value thenable: any(callbacks, part => contains(part.traits, "callable"))
276
+ value {
277
+ known: signatures != null && all(headers, header => header.parameters_known)
278
+ && !any(callbacks, part => contains(part.uncertain_traits, "callable")),
279
+ thenable: thenable
280
+ }
281
+ }
282
+ fn ts_value_then_contract(member) = if member == null then { known: true, thenable: false }
283
+ else { value signatures: call_parameters(member); value ts_value_then_headers(signatures) }
284
+ fn ts_value_nonthenable(n) = {
285
+ value parts: value_parts(n)
286
+ value certain: parts != null && all(parts, arm => {
287
+ value fixed: any(arm.parts, part => contains(part.traits, "nonthenable")
288
+ && !contains(part.uncertain_traits, "nonthenable"))
289
+ value members: filter(arm.parts, part => part.traits != ["intersection"] && part.traits != ["enum"]
290
+ && part.traits != ["parameter"])
291
+ value fixed || count(members) > 0 && all(members, part => {
292
+ value closed: part.carrier_known && (part.carrier == null || part.carrier.description.kind == "enum")
293
+ && count(part.uncertain_traits) == 0
294
+ && (any(part.traits, trait => contains(["number", "string", "boolean", "bigint", "symbol", "structural"], trait))
295
+ || part.carrier.description.kind == "enum" && contains(part.traits, "object"))
296
+ # An enum's primitive type or member has no members of its own.
297
+ value children: if !closed then null
298
+ else if part.carrier.description.kind == "enum" && !contains(part.traits, "object") then []
299
+ else value_children(part.handle, true, [])
300
+ value children != null && !any(children, child => child.member.key == "then")
301
+ })
302
+ })
303
+ value if certain then true else {
304
+ value shape: ts_value_then_headers(member_call_parameters(n, "then"))
305
+ value if parts == null || len(parts) == 0 || !shape.known then null else !shape.thenable
306
+ }
307
+ }
308
+ # Read from certain traits: `always any` stays unknown for an open type
309
+ # parameter, which would leave its intersections without a value.
310
+ fn ts_value_dynamic_part(item) = {
311
+ value parts: value_constituents(item)
312
+ value parts != null && len(parts) > 0 && all(parts, arm => any(arm, part => contains(part.traits, "any")))
313
+ }
314
+ fn ts_value_intersection_parts(types) = {
315
+ value never: filter(types, item => { value parts: value_constituents(item); value parts != null && len(parts) == 0 })
316
+ value dynamic: filter(types, item => ts_value_dynamic_part(item))
317
+ value if len(never) > 0 then never else if len(dynamic) > 0 then dynamic else types
318
+ }
319
+
320
+ in ts_null {
321
+ fits null, nullable, falsy
322
+ identity null
323
+ }
324
+ in ts_null { fits nonthenable }
325
+ in ts_boolean, ts_number, ts_bigint, ts_string { fits literal }
326
+ in ts_boolean {
327
+ fits boolean
328
+ widens (trait boolean, truthy) | (trait boolean, falsy)
329
+ fits falsy when text == "false"
330
+ fits truthy when text == "true"
331
+ identity text == "true"
332
+ }
333
+ in ts_number {
334
+ let ts_value_number_raw replace(text, "_", "")
335
+ let ts_value_number js_number(ts_value_number_raw)
336
+ fits number
337
+ widens (trait number, truthy) | (trait number, falsy)
338
+ fits falsy when ts_value_number == 0
339
+ fits truthy when ts_value_number != 0
340
+ identity ts_value_number
341
+ }
342
+ in ts_bigint {
343
+ fits bigint
344
+ widens (trait bigint, truthy) | (trait bigint, falsy)
345
+ fits falsy when js_bigint(text) == "0"
346
+ fits truthy when js_bigint(text) != "0"
347
+ identity { type: "bigint", value: js_bigint(text) }
348
+ }
349
+ in ts_string {
350
+ fits string
351
+ widens (trait string, truthy) | (trait string, falsy)
352
+ fits falsy when js_string(text) == ""
353
+ fits truthy when js_string(text) != ""
354
+ identity js_string(text)
355
+ }
356
+ in ts_template {
357
+ # An element access types its template key as a template literal type:
358
+ # literal holes make it one literal key (`foo[`ba${"zz"}`]` reads `bazz`).
359
+ let ts_value_template_key if is ts_index.index && count(expression.nodes) > 0 then ts_value_template_text(node) else null
360
+ when !(is ts_tagged_template.quasi) {
361
+ fits string
362
+ widens (trait string, truthy) | (trait string, falsy)
363
+ fits falsy when count(expression.nodes) == 0 && text == "``"
364
+ fits truthy when count(expression.nodes) == 0 && text != "``"
365
+ when type_of(ts_value_template_key) == "str" {
366
+ fits literal
367
+ identity ts_value_template_key
368
+ }
369
+ }
370
+ when is ts_tagged_template.quasi { unwraps instance "TemplateStringsArray" }
371
+ }
372
+ in ts_regex { unwraps instance "RegExp" }
373
+
374
+ in ts_identifier {
375
+ let ts_value_undefined text == "undefined" && scope_declaration == null
376
+ && contains(name_role_classes(node, "reference"), "value")
377
+ let ts_value_unresolved_value !ts_value_undefined && ts_value_missing_name(node, "value")
378
+ # A name a destructuring pattern binds takes the value that pattern gives
379
+ # it, not the declarator's whole value.
380
+ let ts_value_pattern_name if declaration != null
381
+ && (declaration.parent is (ts_array_pattern, ts_pattern_property, ts_assignment_pattern)
382
+ || declaration.parent is ts_rest
383
+ && declaration.parent.parent is (ts_array_pattern, ts_object_pattern))
384
+ then declaration else null
385
+ let ts_value_pattern_slot if ts_value_pattern_name != null && ts_value_pattern_name.parent is ts_array_pattern
386
+ then index_of(ts_value_pattern_name.parent.element.nodes, ts_value_pattern_name) else 0
387
+ let ts_value_pattern_any ts_value_pattern_name != null && ts_value_pattern_name.parent is ts_array_pattern
388
+ && ts_value_has_trait(ts_value_pattern_name.parent.node, "any") == true
389
+ when ts_value_undefined {
390
+ fits undefined, nullable, falsy, nonthenable
391
+ identity { type: "undefined" }
392
+ }
393
+ when ts_value_unresolved_value { fits any, error open }
394
+ when ts_value_pattern_name != null {
395
+ when ts_value_pattern_any { unwraps ts_value_pattern_name.parent }
396
+ when ts_value_pattern_name.parent is ts_array_pattern && !ts_value_pattern_any {
397
+ unwraps ts_value_pattern_name.parent.[ts_value_pattern_slot]
398
+ }
399
+ when ts_value_pattern_name.parent is (ts_pattern_property, ts_assignment_pattern, ts_rest) {
400
+ unwraps ts_value_pattern_name.parent
401
+ }
402
+ }
403
+ when ts_value_pattern_name == null && !ts_value_unresolved_value && !ts_value_undefined && !ts_value_implicit_arguments {
404
+ unwraps definition
405
+ widens definition.widened
406
+ }
407
+ }
408
+ in ts_variable_declaration {
409
+ when count(declaration.nodes) == 1 { unwraps declaration }
410
+ when count(declaration.nodes) != 1 { unwraps unknown }
411
+ }
412
+ in ts_for_of {
413
+ let ts_value_iteration_arguments []
414
+ let ts_value_iteration_string right always string
415
+ when modifier == null && !ts_value_iteration_string {
416
+ calls right.[(from instance "Symbol" in value at "iterator")] with ts_value_iteration_arguments
417
+ unwraps result
418
+ }
419
+ }
420
+ in ts_declarator {
421
+ let ts_value_iteration_arguments []
422
+ let ts_value_iteration_binding name is ts_identifier && parent is ts_for_of.left && parent.parent.modifier == null
423
+ let ts_value_iteration_pattern name is (ts_array_pattern, ts_object_pattern) && parent is ts_for_of.left && parent.parent.modifier == null
424
+ let ts_value_unique_symbol ts_value_unique_symbol_binding(node)
425
+ let ts_value_evolving_array name is ts_identifier && type == null && init is ts_array && count(init.element.nodes) == 0
426
+ # Read before any member use, an evolving `[]` is `any[]`; later pushes
427
+ # evolve it, which no recipe follows yet.
428
+ let ts_value_evolving_any ts_value_evolving_array && !any(name.references, reference => reference is (ts_member.object, ts_index.object))
429
+ when ts_value_evolving_any {
430
+ unwraps product ts_array of init using trait any repeated values ts_value_array_element
431
+ widens product ts_array of init using trait any repeated values ts_value_array_element
432
+ }
433
+ when ts_value_evolving_array && !ts_value_evolving_any {
434
+ unwraps product ts_array of init using trait unknown repeated values ts_value_array_element
435
+ widens product ts_array of init using trait unknown repeated values ts_value_array_element
436
+ }
437
+ when !ts_value_evolving_array { widens type ?? init.widened }
438
+ when ts_value_unique_symbol {
439
+ fits symbol, truthy
440
+ identity node
441
+ }
442
+ when ts_value_iteration_binding {
443
+ when {
444
+ parent.parent.ts_value_iteration_string then { unwraps type ?? trait string }
445
+ else then {
446
+ calls parent.parent.["next"] with ts_value_iteration_arguments
447
+ unwraps type ?? result.["value"]
448
+ }
449
+ }
450
+ }
451
+ when !ts_value_iteration_binding && !ts_value_unique_symbol && !ts_value_evolving_array && name is ts_identifier && parent.kind == "const" { unwraps type ?? init }
452
+ when !ts_value_iteration_binding && !ts_value_evolving_array && name is ts_identifier && parent.kind != "const" { unwraps type ?? init.widened }
453
+ when ts_value_iteration_pattern {
454
+ when {
455
+ parent.parent.ts_value_iteration_string then { unwraps trait string }
456
+ else then {
457
+ calls parent.parent.["next"] with ts_value_iteration_arguments
458
+ unwraps result.["value"]
459
+ }
460
+ }
461
+ }
462
+ when !ts_value_iteration_pattern && name is (ts_array_pattern, ts_object_pattern) {
463
+ when parent.kind == "const" { unwraps type ?? init }
464
+ when parent.kind != "const" { unwraps type ?? init.widened }
465
+ }
466
+ when !(name is (ts_identifier, ts_array_pattern, ts_object_pattern)) { unwraps unknown }
467
+ }
468
+ in ts_rest let ts_value_implicit_any false
469
+ # No annotation, default or contextual type: TS's implicit `any`. Whether a
470
+ # function expression has a contextual type is read from where it stands:
471
+ # asking for its expected value would re-enter call selection.
472
+ fn ts_value_uncontextual(f) = {
473
+ value holder: if f is ts_method then f.parent.node else f
474
+ value at: last(walk(holder, item => if item.parent is ts_parenthesized then item.parent.node else null))
475
+ value at.parent is (ts_expression_statement, ts_program, ts_block, ts_export)
476
+ || at is ts_declarator.init && at.parent.type == null
477
+ }
478
+ in ts_parameter let ts_value_implicit_any type == null && init == null && name is ts_identifier && (
479
+ parent is (ts_method_signature, ts_function_type, ts_call_signature)
480
+ || parent is ts_method && parent.parent not ts_object
481
+ || parent is (ts_arrow, ts_function, ts_method) && (count(parent.type_parameter.nodes) > 0 || ts_value_uncontextual(parent.node)))
482
+ in ts_parameter, ts_rest {
483
+ let ts_value_pattern_rest node is ts_rest && parent is (ts_array_pattern, ts_object_pattern)
484
+ when !ts_value_pattern_rest { pack ts_tuple }
485
+ # An unannotated parameter of a function expression or object-literal
486
+ # method takes its function's contextual parameter (`expected`; class
487
+ # methods get none), else its default's widened value.
488
+ # Destructured parameters stay out for now: in chained calls their
489
+ # contextual type re-enters call selection after every budget cut.
490
+ let ts_value_contextual_parameter (parent is (ts_arrow, ts_function) || parent is ts_method && parent.parent is ts_object)
491
+ && (name is ts_identifier || name is ts_rest) && count(parent.type_parameter.nodes) == 0
492
+ when parent not ts_tuple_type && ts_value_implicit_any { fits any open }
493
+ when parent not ts_tuple_type && ts_value_contextual_parameter && !ts_value_pattern_rest && !ts_value_implicit_any {
494
+ when optional != null { unwraps (type ?? expected ?? unknown) | trait undefined, nullable, falsy, nonthenable }
495
+ when optional == null { unwraps type ?? expected ?? init.widened ?? unknown }
496
+ }
497
+ when parent not ts_tuple_type && !ts_value_contextual_parameter && !ts_value_pattern_rest && !ts_value_implicit_any {
498
+ when optional != null { unwraps (type ?? unknown) | trait undefined, nullable, falsy, nonthenable }
499
+ when optional == null { unwraps type ?? init.widened ?? unknown }
500
+ }
501
+ }
502
+ in ts_rest {
503
+ let ts_value_rest_array parent is ts_array_pattern && ts_value_array_rest(parent.node)
504
+ when parent is ts_tuple_type { unwraps type }
505
+ when parent is ts_array_pattern {
506
+ when ts_value_has_trait(parent.node, "any") == true { unwraps parent }
507
+ when ts_value_rest_array { unwraps modified parent readonly remove }
508
+ when !ts_value_rest_array && ts_value_has_trait(parent.node, "any") != true { unwraps unknown }
509
+ }
510
+ when parent is ts_object_pattern { unwraps unknown }
511
+ }
512
+ in ts_tuple_member, ts_rest { let ts_value_tuple_optional optional != null }
513
+ in ts_rest, ts_spread { let ts_value_tuple_repeated true }
514
+ in ts_node let ts_value_array_element node not ts_spread
515
+ # Destructuring: a pattern stands for the value it destructures, a property or
516
+ # element for the member it takes, so each bound name gets its own value.
517
+ in ts_array_pattern, ts_object_pattern {
518
+ let ts_value_pattern_slot if parent is ts_array_pattern then index_of(parent.element.nodes, node) else 0
519
+ let ts_value_pattern_any parent is ts_array_pattern && ts_value_has_trait(parent.node, "any") == true
520
+ when parent is (ts_declarator, ts_parameter) { unwraps type ?? parent }
521
+ when ts_value_pattern_any { unwraps parent }
522
+ when parent is ts_array_pattern && !ts_value_pattern_any { unwraps parent.[ts_value_pattern_slot] }
523
+ when parent is (ts_pattern_property, ts_assignment_pattern, ts_rest) { unwraps parent }
524
+ }
525
+ in ts_pattern_property {
526
+ let ts_value_property_key ts_value_key(key.node)
527
+ let ts_value_property_optional value_member_optional(parent.node, ts_value_property_key) == true
528
+ when ts_value_has_trait(parent.node, "any") == true { unwraps parent }
529
+ when value is ts_assignment_pattern { unwraps value }
530
+ when !(value is ts_assignment_pattern) && ts_value_has_trait(parent.node, "any") != true {
531
+ when ts_value_property_optional { unwraps (parent.[ts_value_property_key]) | trait undefined, nullable, falsy, nonthenable }
532
+ when !ts_value_property_optional { unwraps parent.[ts_value_property_key] }
533
+ }
534
+ }
535
+ in ts_assignment_pattern {
536
+ let ts_value_assignment_slot if parent is ts_array_pattern then index_of(parent.element.nodes, node) else 0
537
+ let ts_value_assignment_key if parent is ts_pattern_property then ts_value_key(parent.key.node) else null
538
+ let ts_value_assignment_source if parent is ts_array_pattern then parent.node
539
+ else if parent is ts_pattern_property then parent.parent.node else null
540
+ let ts_value_assignment_any ts_value_assignment_source != null && ts_value_has_trait(ts_value_assignment_source, "any") == true
541
+ when ts_value_assignment_any { unwraps ts_value_assignment_source }
542
+ when parent is ts_array_pattern && !ts_value_assignment_any { unwraps (parent.[ts_value_assignment_slot]) \ undefined | right.widened }
543
+ when parent is ts_pattern_property && !ts_value_assignment_any { unwraps (parent.parent.[ts_value_assignment_key]) \ undefined | right.widened }
544
+ when parent not (ts_array_pattern, ts_pattern_property) { unwraps left ?? right }
545
+ }
546
+ in ts_import, ts_import_specifier, ts_meta_property, ts_import_call {
547
+ unwraps unknown
548
+ }
549
+ in ts_import_equals { when value.node is ts_call { unwraps unknown } }
550
+ in ts_super {
551
+ let ts_value_super_context ts_value_this_context(node)
552
+ let ts_value_super_base ts_value_super_context.container.extends.node
553
+ when ts_value_super_context.static || node is ts_call.callee { unwraps ts_value_super_base }
554
+ when !ts_value_super_context.static && node not ts_call.callee { unwraps ts_value_super_base.["prototype"] }
555
+ }
556
+ in ts_this {
557
+ let ts_value_this ts_value_this_context(node)
558
+ let ts_value_this_explicit ts_value_this.explicit
559
+ let ts_value_this_owner ts_value_this.owner
560
+ let ts_value_this_container ts_value_this.container
561
+ when ts_value_this.owner is ts_function && ts_value_this.container not ts_object {
562
+ fits any open
563
+ unwraps ts_value_this_explicit ?? ts_value_this_owner.expected.receiver ?? self
564
+ }
565
+ # A class member's `this` is the class (TypeScript gives class members no
566
+ # contextual receiver); only an explicit `this` parameter changes it.
567
+ when ts_value_this.container is ts_class {
568
+ when ts_value_this.static {
569
+ unwraps ts_value_this_explicit ?? (scope statics from ts_value_this_container)
570
+ }
571
+ when !ts_value_this.static {
572
+ unwraps ts_value_this_explicit ?? (scope members from ts_value_this_container)
573
+ }
574
+ }
575
+ when ts_value_this.container is ts_object {
576
+ unwraps ts_value_this_explicit ?? ts_value_this_owner.expected.receiver ?? (scope members from ts_value_this_container)
577
+ }
578
+ when ts_value_this.owner not ts_function && ts_value_this.container not (ts_class, ts_object) {
579
+ unwraps unknown
580
+ }
581
+ }
582
+
583
+ in ts_parenthesized, ts_satisfies, ts_instantiation { unwraps expression }
584
+ in ts_as, ts_type_assertion {
585
+ when type.text == "const" { unwraps expression }
586
+ when type.text != "const" { unwraps type }
587
+ }
588
+ in ts_non_null {
589
+ when expression always any { unwraps expression }
590
+ when !(expression always any) { unwraps expression \ nullable }
591
+ }
592
+ in ts_computed_key { unwraps expression }
593
+ in ts_spread { unwraps argument }
594
+ in ts_assignment {
595
+ when operator == "=" { unwraps right }
596
+ when operator == "&&=" { unwraps (left \ truthy) | right }
597
+ when operator == "||=" { unwraps (left \ falsy) | right }
598
+ when operator == "??=" { unwraps (left \ nullable) | right }
599
+ }
600
+ in ts_binary, ts_assignment {
601
+ let ts_value_numeric_kind if contains([">>>", ">>>="], operator) then "number"
602
+ else if left always number || right always number then "number"
603
+ else if left always bigint && right always bigint then "bigint"
604
+ else if (left could bigint) != false && (right could bigint) != false then "both"
605
+ else "number"
606
+ when contains(["-", "*", "/", "%", "**", "&", "|", "^", "<<", ">>", ">>>",
607
+ "-=", "*=", "/=", "%=", "**=", "&=", "|=", "^=", "<<=", ">>=", ">>>="], operator) {
608
+ when ts_value_numeric_kind == "number" {
609
+ unwraps (trait number, truthy) | (trait number, falsy)
610
+ }
611
+ when ts_value_numeric_kind == "bigint" {
612
+ unwraps (trait bigint, truthy) | (trait bigint, falsy)
613
+ }
614
+ when ts_value_numeric_kind == "both" {
615
+ unwraps (trait number, truthy) | (trait number, falsy) | (trait bigint, truthy) | (trait bigint, falsy)
616
+ }
617
+ }
618
+ when operator == "+" || operator == "+=" {
619
+ when ts_value_has_trait(left.node, "any") == true { unwraps left }
620
+ when ts_value_has_trait(right.node, "any") == true { unwraps right }
621
+ when left always number && right always number {
622
+ unwraps (trait number, truthy) | (trait number, falsy)
623
+ }
624
+ when left always bigint && right always bigint {
625
+ unwraps (trait bigint, truthy) | (trait bigint, falsy)
626
+ }
627
+ when left always string || right always string {
628
+ unwraps (trait string, truthy) | (trait string, falsy)
629
+ }
630
+ unwraps left.widened | right.widened
631
+ }
632
+ }
633
+ in ts_conditional { unwraps consequent | alternate }
634
+ in ts_logical {
635
+ let ts_value_logical_choice ts_value_logical_choice(node)
636
+ when ts_value_logical_choice != null { unwraps ts_value_logical_choice }
637
+ when ts_value_logical_choice == null {
638
+ when operator == "&&" { unwraps (left \ truthy) | right }
639
+ when operator == "||" { unwraps (left \ falsy) | right }
640
+ when operator == "??" { unwraps (left \ nullable) | right }
641
+ }
642
+ }
643
+ in ts_sequence {
644
+ let ts_value_last_expression last(expression.nodes)
645
+ unwraps ts_value_last_expression
646
+ }
647
+
648
+ in ts_new, ts_tagged_template { unwraps it.result }
649
+ in ts_call {
650
+ let ts_value_call_nullish in_optional_chain == true && callee could nullable
651
+ when callee always any { unwraps callee }
652
+ when !(callee always any) {
653
+ when ts_value_call_nullish { unwraps it.result | trait undefined, nullable, falsy, nonthenable }
654
+ when !ts_value_call_nullish { unwraps it.result }
655
+ }
656
+ }
657
+ in ts_call, ts_new { arguments type_argument }
658
+ in ts_await {
659
+ # The library's own `Promise<T>` awaits to `Awaited<T>` without relating
660
+ # it structurally to `{ then(onfulfilled: infer F) }`.
661
+ let ts_value_await_promise ts_value_lib_promise(argument.node)
662
+ when argument always any { unwraps argument }
663
+ when !(argument always any) && ts_value_await_promise { unwraps instance "Awaited" of (argument 0 of argument) }
664
+ when !(argument always any) && !ts_value_await_promise { unwraps instance "Awaited" of argument }
665
+ }
666
+ in ts_yield { unwraps unknown }
667
+ in ts_unary {
668
+ when operator == "void" {
669
+ fits undefined, nullable, falsy, nonthenable
670
+ identity { type: "undefined" }
671
+ }
672
+ when operator == "typeof" { fits string, truthy }
673
+ when operator == "delete" { fits boolean }
674
+ when operator == "!" {
675
+ fits boolean
676
+ fits falsy when argument always truthy
677
+ fits truthy when argument always falsy
678
+ when argument always truthy { identity false }
679
+ when argument always falsy { identity true }
680
+ }
681
+ when operator == "+" { fits number }
682
+ when operator == "~" {
683
+ when argument always bigint {
684
+ unwraps (trait bigint, truthy) | (trait bigint, falsy)
685
+ }
686
+ when !(argument always bigint) { fits number }
687
+ }
688
+ when operator == "-" && argument is ts_number {
689
+ fits number, literal
690
+ widens (trait number, truthy) | (trait number, falsy)
691
+ fits falsy when argument always falsy
692
+ fits truthy when argument always truthy
693
+ identity -argument.ts_value_number
694
+ }
695
+ when operator == "-" && argument is ts_bigint {
696
+ fits bigint, literal
697
+ widens (trait bigint, truthy) | (trait bigint, falsy)
698
+ fits falsy when argument always falsy
699
+ fits truthy when argument always truthy
700
+ identity { type: "bigint", value: ts_value_negated_bigint(argument.text) }
701
+ }
702
+ when operator == "-" && !(argument is (ts_number, ts_bigint)) {
703
+ when argument always number {
704
+ unwraps (trait number, truthy) | (trait number, falsy)
705
+ }
706
+ when argument always bigint {
707
+ unwraps (trait bigint, truthy) | (trait bigint, falsy)
708
+ }
709
+ when !(argument always number) && !(argument always bigint) { unwraps unknown }
710
+ }
711
+ }
712
+ in ts_update { unwraps argument.widened }
713
+ in ts_binary {
714
+ when operator == "===" || operator == "!==" || operator == "==" || operator == "!="
715
+ || operator == "<" || operator == "<=" || operator == ">" || operator == ">="
716
+ || operator == "in" || operator == "instanceof" { fits boolean }
717
+ }
718
+
719
+ in ts_hole { unwraps trait undefined, nullable, falsy, nonthenable }
720
+ in ts_array {
721
+ let ts_value_tuple_readonly ts_value_const_context(node)
722
+ let ts_value_is_tuple ts_value_tuple_context(node)
723
+ when !ts_value_is_tuple {
724
+ unwraps product ts_array of element using (ts_value_preserved_element ?? it.widened) repeated values ts_value_array_element repeated ts_value_tuple_repeated
725
+ }
726
+ when ts_value_is_tuple {
727
+ unwraps product ts_tuple of element using (ts_value_preserved_element ?? it.widened) optional ts_value_tuple_optional repeated ts_value_tuple_repeated readonly ts_value_tuple_readonly
728
+ }
729
+ }
730
+ in ts_object, ts_interface, ts_type_literal {
731
+ scope members
732
+ members members
733
+ fits structural in members
734
+ }
735
+ in ts_interface, ts_type_literal { unwraps scope members from it }
736
+ in ts_type_literal { fits object_literal in members }
737
+ in ts_interface, ts_type_literal { fits shaped in members when count(member.nodes) > 0 }
738
+ in ts_object {
739
+ let ts_value_any_spreads filter(property.nodes, item => item is ts_spread && item.argument always any)
740
+ when len(ts_value_any_spreads) > 0 { unwraps ...ts_value_any_spreads.argument }
741
+ when len(ts_value_any_spreads) == 0 { unwraps scope members from it }
742
+ }
743
+ in ts_object { fits object, truthy in members }
744
+ in ts_interface {
745
+ when extends != null { members members inherits &...extends }
746
+ fits builtin_function in members when
747
+ contains(ts_value_library_type_sites(node, "Function"), declaration_identity(node))
748
+ || any(extends.nodes, base => base always builtin_function)
749
+ }
750
+ in ts_interface, ts_type_literal {
751
+ when ts_value_closed_type_members(member.nodes) { members members closed }
752
+ }
753
+ in ts_interface, ts_type_literal, ts_class {
754
+ let ts_value_computed_members filter(member.nodes, item => item.key is ts_computed_key && ts_value_key(item.key.node) == null
755
+ && !ts_value_static(item.modifier.nodes))
756
+ let ts_value_index_signatures filter(member.nodes, item => item is ts_index_signature)
757
+ let ts_value_text_indexers filter(ts_value_index_signatures, item => item.parameter.type always string)
758
+ let ts_value_ordinal_indexers filter(ts_value_index_signatures, item => item.parameter.type always number)
759
+ let ts_value_symbol_indexers filter(ts_value_index_signatures, item => item.parameter.type always symbol)
760
+ indexed keyed ts_value_computed_members by key.expression using it in members
761
+ indexed text ts_value_text_indexers using it.type in members
762
+ indexed ordinal ts_value_ordinal_indexers using it.type in members
763
+ indexed symbol ts_value_symbol_indexers using it.type in members
764
+ }
765
+ in ts_class {
766
+ let ts_value_computed_statics filter(member.nodes, item => item.key is ts_computed_key && ts_value_key(item.key.node) == null
767
+ && ts_value_static(item.modifier.nodes))
768
+ indexed keyed ts_value_computed_statics by key.expression using it in statics
769
+ }
770
+ in ts_object {
771
+ let ts_value_computed_properties filter(property.nodes, item => item.key is ts_computed_key && ts_value_key(item.key.node) == null)
772
+ indexed keyed ts_value_computed_properties by key.expression using it in members
773
+ }
774
+ in ts_object {
775
+ let ts_value_then_member find(property.nodes, member => ts_value_key(member.key.node) == "then")
776
+ let ts_value_closed_object_members all(property.nodes, member => member not ts_spread
777
+ && ts_value_key(member.key.node) != null)
778
+ when ts_value_closed_object_members { members members closed }
779
+ let ts_value_then_shape ts_value_then_contract(ts_value_then_member)
780
+ fits thenable in members when ts_value_then_shape.thenable
781
+ }
782
+ in ts_interface, ts_type_literal, ts_class {
783
+ let ts_value_then_shape {
784
+ value if node is ts_type_literal && all(member.nodes, member => member.key is ts_identifier
785
+ && !contains(member.key.text, "\\") && member.key.text != "then")
786
+ then { known: true, thenable: false }
787
+ else if node is ts_type_literal && ts_value_closed_type_members(member.nodes)
788
+ && value_has_member(node, "then", null) == false
789
+ then { known: true, thenable: false }
790
+ else {
791
+ value signatures: member_call_parameters(node, "then")
792
+ value ts_value_then_headers(signatures)
793
+ }
794
+ }
795
+ fits thenable in members when ts_value_then_shape.thenable
796
+ }
797
+ in ts_property {
798
+ declares member key keyed ts_value_key(it) in nearest members hoisted
799
+ when ts_value_const_context(node) { unwraps value ?? key }
800
+ when !ts_value_const_context(node) { unwraps value.widened ?? key.widened }
801
+ }
802
+ in ts_property_signature {
803
+ declares member key keyed ts_value_key(it) in nearest members hoisted
804
+ optional when optional != null
805
+ when optional == null { unwraps type ?? unknown }
806
+ when optional != null { unwraps (type ?? unknown) | trait undefined, nullable, falsy, nonthenable }
807
+ }
808
+ in ts_method_signature {
809
+ declares member key keyed ts_value_key(it) in nearest members hoisted
810
+ }
811
+ fn ts_value_optional_member(n, key) = value_member_optional(n.object.node, key) == true
812
+ in ts_member {
813
+ refers object.[ts_value_member_key]
814
+ let ts_value_member_key ts_value_key(property.node)
815
+ let ts_value_member_nullish in_optional_chain == true && object could nullable
816
+ || ts_value_optional_member(node, ts_value_member_key)
817
+ # `always any` stays open for a type parameter; one whose parts are
818
+ # certainly not `any` (its constraint's) reads its members.
819
+ let ts_value_member_any if ts_value_has_trait(object.node, "any") == false then false else object always any
820
+ when ts_value_member_any { unwraps object }
821
+ when !ts_value_member_any {
822
+ when !ts_value_member_nullish { unwraps (object \ null, undefined).[ts_value_member_key] }
823
+ when ts_value_member_nullish { unwraps (object \ null, undefined).[ts_value_member_key] | trait undefined, nullable, falsy, nonthenable }
824
+ }
825
+ }
826
+ fn ts_value_unchecked_index(n) = {
827
+ value lookup: value_lookup(n.object.node, n.index.node)
828
+ value keys: value_constituents(n.index.node)
829
+ value finite: keys != null && count(keys) > 0 && all(keys, arm =>
830
+ any(arm, part => part.identity_known && part.identity.kind == "value"))
831
+ value lookup != null && any(lookup.alternatives, row => row.matched && any(row.sources, source =>
832
+ row.required != true && contains(["indexer", "repeated"], source.kind)
833
+ || source.kind == "ordinal" && !finite))
834
+ }
835
+ in ts_index {
836
+ refers object.[index]
837
+ let ts_value_index_nullish in_optional_chain == true && object could nullable
838
+ || ts_value_optional_member(node, index.node)
839
+ || (if ts_compiler_option(node, "noUncheckedIndexedAccess") == true then ts_value_unchecked_index(node) else false)
840
+ when object always any { unwraps object }
841
+ when !(object always any) {
842
+ when !ts_value_index_nullish { unwraps (object \ nullable, undefined).[index] }
843
+ when ts_value_index_nullish { unwraps (object \ nullable, undefined).[index] | trait undefined, nullable, falsy, nonthenable }
844
+ }
845
+ }
846
+ in ts_function, ts_arrow, ts_function_type, ts_call_signature {
847
+ fits callable, object, truthy
848
+ }
849
+ in ts_function { fits constructible when !any(modifier.nodes, modifier => modifier.text == "async") }
850
+ in ts_method, ts_method_signature {
851
+ optional when optional != null
852
+ when kind != "get" && kind != "set" { fits callable, object, truthy }
853
+ when kind == "get" { unwraps it.result }
854
+ when kind == "set" { unwraps parameter.type }
855
+ }
856
+ in ts_class {
857
+ scope statics
858
+ scope members
859
+ members statics
860
+ fits constructible, object, truthy in statics
861
+ fits structural, object, truthy in members
862
+ unwraps scope statics from it
863
+ unwraps scope members from it for type
864
+ member statics "prototype" unwraps scope members from it
865
+ when ts_value_closed_type_members(member.nodes) {
866
+ members members closed
867
+ members statics closed
868
+ }
869
+ when extends != null {
870
+ members statics inherits extends
871
+ members members inherits extends.["prototype"]
872
+ }
873
+ # An interface of the same name merges into the instance (`implements`
874
+ # adds nothing).
875
+ let ts_value_merged_interfaces if name == null then [] else
876
+ map(filter(name_binding(name.node).declarations ?? [], item => item.parent is ts_interface), item => item.parent)
877
+ when count(ts_value_merged_interfaces) > 0 { members members inherits &...ts_value_merged_interfaces }
878
+ }
879
+ in ts_method, ts_class_property {
880
+ flag static when ts_value_static(modifier.nodes)
881
+ when ts_value_static(modifier.nodes) { declares member key keyed ts_value_key(it) in nearest statics hoisted }
882
+ when !ts_value_static(modifier.nodes) { declares member key keyed ts_value_key(it) in nearest members hoisted }
883
+ }
884
+ in ts_parameter {
885
+ when parent is ts_method && parent.calls_constructor
886
+ && any(modifier.nodes, modifier => contains(["private", "protected", "public", "readonly", "override"], modifier.text)) {
887
+ declares member name keyed ts_value_key(it) in nearest members hoisted
888
+ }
889
+ }
890
+ in ts_property { flag readonly when ts_value_const_context(node) }
891
+ in ts_property_signature, ts_index_signature, ts_class_property, ts_parameter {
892
+ flag readonly when any([] modifier, modifier => modifier == "readonly")
893
+ }
894
+ in ts_method, ts_method_signature, ts_property_signature, ts_class_property, ts_parameter {
895
+ flag private_access when any([] modifier, modifier => modifier == "private")
896
+ flag protected_access when any([] modifier, modifier => modifier == "protected")
897
+ }
898
+ in ts_method, ts_method_signature { let ts_value_receiver_function node }
899
+ in ts_property { let ts_value_receiver_function value.node }
900
+ in ts_class_property { let ts_value_receiver_function init.node }
901
+ in ts_property, ts_class_property {
902
+ flag function_initializer when ts_value_receiver_function is ts_function
903
+ }
904
+ in ts_method, ts_method_signature, ts_property, ts_class_property {
905
+ let ts_value_written_receiver if ts_value_receiver_function is (ts_method, ts_method_signature, ts_function)
906
+ then find(ts_value_receiver_function.parameter.nodes, parameter => parameter.name is ts_this || parameter.name == "this")
907
+ else null
908
+ flag explicit_receiver when ts_value_written_receiver != null
909
+ flag void_receiver when ts_value_written_receiver.type.node is ts_type_keyword
910
+ && ts_value_written_receiver.type.text == "void"
911
+ }
912
+ in ts_method, ts_method_signature {
913
+ flag method when kind != "get" && kind != "set"
914
+ flag array_constructor_member when parent is ts_interface && parent.name == "ArrayConstructor"
915
+ flag private when key is ts_private_name
916
+ flag readonly when kind == "get" && !any(parent.member.nodes, member =>
917
+ member.kind == "set" && ts_value_key(member.key.node) == ts_value_key(key.node)
918
+ && ts_value_static(member.modifier.nodes) == ts_value_static(modifier.nodes))
919
+ }
920
+ in ts_class_property { flag private when key is ts_private_name }
921
+ in ts_class_property {
922
+ optional when optional != null
923
+ when optional == null { unwraps type ?? init.widened ?? unknown }
924
+ when optional != null { unwraps (type ?? init.widened ?? unknown) | trait undefined, nullable, falsy, nonthenable }
925
+ }
926
+
927
+ in ts_type_alias { unwraps type }
928
+ in ts_class, ts_interface, ts_type_alias { generics type_parameter }
929
+ in ts_class, ts_interface {
930
+ carries type_parameter as { kind: "named", name: name.text }
931
+ when (name.text == "Array" || name.text == "ReadonlyArray") && ts_value_global_declaration(node, name.text) {
932
+ indexed repeated type_parameter strings
933
+ fits object, truthy in members
934
+ }
935
+ }
936
+ in ts_type_reference {
937
+ let ts_value_unresolved_type name is ts_identifier && ts_value_missing_name(name.node, "type")
938
+ # `Promise` without its required arguments is an error type, read as any.
939
+ # Merged declarations share their parameters: one declaring a default is enough.
940
+ let ts_value_missing_arguments name is ts_identifier && count(type_argument.nodes) == 0 && {
941
+ value local: map(filter(name_binding(name.node).declarations ?? [], item => item.parent is (ts_class, ts_interface, ts_type_alias)),
942
+ item => map(item.parent.type_parameter.nodes, parameter => parameter.default != null))
943
+ value remote: map(filter(value_sources(name.node).sites ?? [], site => contains(["ts_class", "ts_interface", "ts_type_alias"], site.kind)),
944
+ site => map(site_field(node, site, "type_parameter") ?? [], parameter => count(site_field(node, parameter, "default") ?? []) > 0))
945
+ value declarations: flat_map([local, remote], group => group)
946
+ value any(declarations, parameters => any(parameters, (parameter, index) =>
947
+ all(declarations, other => !any(other, (defaulted, at) => at == index && defaulted))))
948
+ }
949
+ arguments type_argument
950
+ when ts_value_unresolved_type || ts_value_missing_arguments { fits any, error open }
951
+ when name is ts_qualified_name { unwraps name }
952
+ when !ts_value_unresolved_type && !ts_value_missing_arguments && name not ts_qualified_name { unwraps &...name.definition }
953
+ }
954
+ in ts_type_query {
955
+ # `typeof f<T>` instantiates `f`'s own type parameters.
956
+ arguments type_argument
957
+ when name is ts_qualified_name { unwraps name }
958
+ when name not ts_qualified_name { unwraps name.definition }
959
+ }
960
+ in ts_type_predicate {
961
+ carries as {
962
+ kind: "predicate",
963
+ parameter_index: find(map(parent.calls_parameters, (parameter, index) =>
964
+ {index: index, name: parameter.name.text}), item => item.name == name.text).index,
965
+ asserts: modifier != null,
966
+ has_target: type != null,
967
+ target_union: type is ts_union,
968
+ target_source: if type != null then value_sources(type.node) else null,
969
+ }
970
+ when modifier == null { fits boolean }
971
+ when modifier != null {
972
+ fits void, undefined, nullable, falsy, nonthenable
973
+ identity { type: "undefined" }
974
+ }
975
+ }
976
+ in ts_type_parameter {
977
+ fits parameter open
978
+ carries as { kind: "parameter", name: name.text }
979
+ when constraint != null { constrains constraint }
980
+ when default != null { defaults default }
981
+ unwraps it.inferred ?? (self & (constraint ?? trait unknown))
982
+ }
983
+ in ts_union { unwraps ...type }
984
+ in ts_intersection {
985
+ let ts_value_intersection ts_value_intersection_parts(type.nodes)
986
+ let ts_value_dynamic_intersection any(ts_value_intersection, item => ts_value_dynamic_part(item))
987
+ when ts_value_dynamic_intersection { unwraps &...ts_value_intersection }
988
+ when !ts_value_dynamic_intersection {
989
+ fits intersection
990
+ members members
991
+ members members closed
992
+ unwraps self & (&...ts_value_intersection)
993
+ }
994
+ }
995
+ in ts_literal_type {
996
+ when !starts_with(text, "-") { unwraps value }
997
+ when starts_with(text, "-") && value is ts_number {
998
+ fits number, literal
999
+ widens (trait number, truthy) | (trait number, falsy)
1000
+ fits falsy when value always falsy
1001
+ fits truthy when value always truthy
1002
+ identity -value.ts_value_number
1003
+ }
1004
+ when starts_with(text, "-") && value is ts_bigint {
1005
+ fits bigint, literal
1006
+ widens (trait bigint, truthy) | (trait bigint, falsy)
1007
+ fits falsy when value always falsy
1008
+ fits truthy when value always truthy
1009
+ identity { type: "bigint", value: ts_value_negated_bigint(value.text) }
1010
+ }
1011
+ }
1012
+ in ts_parenthesized_type { unwraps type }
1013
+ in ts_array_type {
1014
+ let ts_value_array_readonly parent is ts_type_operator && parent.operator == "readonly"
1015
+ unwraps product ts_array of type repeated values ts_value_array_element readonly ts_value_array_readonly
1016
+ }
1017
+ in ts_tuple_type {
1018
+ let ts_value_tuple_readonly parent is ts_type_operator && parent.operator == "readonly"
1019
+ unwraps product ts_tuple of element optional ts_value_tuple_optional repeated ts_value_tuple_repeated readonly ts_value_tuple_readonly
1020
+ }
1021
+ in ts_tuple_member {
1022
+ optional when optional != null
1023
+ unwraps type
1024
+ }
1025
+ in ts_indexed_access { unwraps object.[index] }
1026
+ fn ts_value_conditional_owner(n) = find(walk(n, item => item.parent.node), item => item is ts_conditional_type)
1027
+ in ts_conditional_type {
1028
+ let ts_value_conditional_infers {
1029
+ value descendants(ts_infer)
1030
+ filter (item) item.offset >= extends.offset && item.end_offset <= extends.end_offset
1031
+ && ts_value_conditional_owner(item) == node
1032
+ map (item) item.type_parameter.node
1033
+ }
1034
+ generics ts_value_conditional_infers
1035
+ let ts_value_conditional_formal {
1036
+ value bare: ts_value_bare_type(check.node)
1037
+ value if bare is ts_type_reference && count(bare.type_argument.nodes) == 0 && bare.name is ts_identifier
1038
+ then find(map(name_binding(bare.name.node).declarations, declaration => declaration.parent.node), item => item is ts_type_parameter) else null
1039
+ }
1040
+ let ts_value_conditional_dynamic ts_value_has_trait(check.node, "any") == true || check always any
1041
+ when ts_value_conditional_formal != null {
1042
+ when ts_value_conditional_dynamic { unwraps select check against extends then consequent else alternate distribute ts_value_conditional_formal dynamic both }
1043
+ when !ts_value_conditional_dynamic { unwraps select check against extends then consequent else alternate distribute ts_value_conditional_formal }
1044
+ }
1045
+ when ts_value_conditional_formal == null {
1046
+ when ts_value_conditional_dynamic { unwraps select check against extends then consequent else alternate dynamic both }
1047
+ when !ts_value_conditional_dynamic { unwraps select check against extends then consequent else alternate }
1048
+ }
1049
+ }
1050
+ in ts_type_operator {
1051
+ when operator == "readonly" { unwraps type }
1052
+ when operator == "keyof" { unwraps keys of type }
1053
+ when operator == "unique" && type.text == "symbol" {
1054
+ fits symbol, truthy
1055
+ widens trait symbol, truthy
1056
+ identity node
1057
+ }
1058
+ when operator != "readonly" && operator != "keyof" && !(operator == "unique" && type.text == "symbol") { unwraps unknown }
1059
+ }
1060
+ in ts_template_type {
1061
+ fits string
1062
+ carries type as { kind: "template" }
1063
+ }
1064
+ in ts_infer { unwraps type_parameter }
1065
+ fn ts_value_bare_type(n) = last(walk(n, item => if item is ts_parenthesized_type then item.type.node else null))
1066
+ in ts_mapped_type {
1067
+ scope members
1068
+ members members closed
1069
+ fits structural in members
1070
+ fits shaped in members
1071
+ generics type_parameter
1072
+ let ts_value_mapped_modifiers {
1073
+ value constraint: ts_value_bare_type(type_parameter.constraint.node)
1074
+ value formal: if constraint is ts_type_reference && constraint.name is ts_identifier
1075
+ && count(constraint.type_argument.nodes) == 0
1076
+ then find(map(name_binding(constraint.name.node).declarations, item => item.parent.node), item => item is ts_type_parameter) else null
1077
+ value keys: if formal != null then ts_value_bare_type(formal.constraint.node) else constraint
1078
+ value if keys is ts_type_operator && keys.operator == "keyof" && keys.type.text != "any"
1079
+ then keys.type.node else null
1080
+ }
1081
+ let ts_value_mapped_member if ts_value_mapped_modifiers != null
1082
+ then value_lookup(ts_value_mapped_modifiers, type_parameter.node) else null
1083
+ optional when if optional != null then optional != "-?"
1084
+ else if ts_value_mapped_modifiers == null then false
1085
+ else if ts_value_mapped_member == null then null
1086
+ else all(ts_value_mapped_member.alternatives, item => item.matched && item.required == false)
1087
+ flag readonly when if modifier != null then modifier != "-readonly"
1088
+ else if ts_value_mapped_modifiers == null then false
1089
+ else if ts_value_mapped_member == null then null
1090
+ else all(ts_value_mapped_member.alternatives, item => item.matched
1091
+ && all(item.sources, source => contains(source.flags, "readonly")))
1092
+ when optional != null && optional != "-?" {
1093
+ indexed keys type_parameter.constraint bind type_parameter using (type | trait undefined, nullable, falsy, nonthenable) in members
1094
+ }
1095
+ when optional == null || optional == "-?" {
1096
+ indexed keys type_parameter.constraint bind type_parameter using type in members
1097
+ }
1098
+ let ts_value_mapped_constraint ts_value_bare_type(type_parameter.constraint.node)
1099
+ let ts_value_mapped_template ts_value_bare_type(type.node)
1100
+ let ts_value_mapped_source if name == null && ts_value_mapped_constraint is ts_type_operator
1101
+ && ts_value_mapped_constraint.operator == "keyof" && ts_value_mapped_template is ts_indexed_access then {
1102
+ value source: ts_value_bare_type(ts_value_mapped_constraint.type.node)
1103
+ value object: ts_value_bare_type(ts_value_mapped_template.object.node)
1104
+ value key: ts_value_bare_type(ts_value_mapped_template.index.node)
1105
+ value if source != null && token_list(source) == token_list(object)
1106
+ && key is ts_type_reference && count(key.type_argument.nodes) == 0
1107
+ && key.name is ts_identifier
1108
+ && any(name_binding(key.name.node).declarations, declaration => declaration.parent.node == type_parameter.node)
1109
+ then source else null
1110
+ } else null
1111
+ let ts_value_mapped_optional if optional == null then "keep" else if optional == "-?" then "remove" else "add"
1112
+ let ts_value_mapped_readonly if modifier == null then "keep" else if modifier == "-readonly" then "remove" else "add"
1113
+ when ts_value_mapped_source != null && ts_value_mapped_optional == "keep" {
1114
+ when ts_value_mapped_readonly == "keep" { unwraps ts_value_mapped_source }
1115
+ when ts_value_mapped_readonly == "add" { unwraps modified ts_value_mapped_source readonly add flag readonly add }
1116
+ when ts_value_mapped_readonly == "remove" { unwraps modified ts_value_mapped_source readonly remove flag readonly remove }
1117
+ }
1118
+ when ts_value_mapped_source != null && ts_value_mapped_optional == "add" {
1119
+ when ts_value_mapped_readonly == "keep" { unwraps modified ts_value_mapped_source optional add }
1120
+ when ts_value_mapped_readonly == "add" { unwraps modified ts_value_mapped_source optional add readonly add flag readonly add }
1121
+ when ts_value_mapped_readonly == "remove" { unwraps modified ts_value_mapped_source optional add readonly remove flag readonly remove }
1122
+ }
1123
+ when ts_value_mapped_source != null && ts_value_mapped_optional == "remove" {
1124
+ when ts_value_mapped_readonly == "keep" { unwraps modified ts_value_mapped_source optional remove }
1125
+ when ts_value_mapped_readonly == "add" { unwraps modified ts_value_mapped_source optional remove readonly add flag readonly add }
1126
+ when ts_value_mapped_readonly == "remove" { unwraps modified ts_value_mapped_source optional remove readonly remove flag readonly remove }
1127
+ }
1128
+ when ts_value_mapped_source == null && name == null { unwraps scope members from it }
1129
+ when ts_value_mapped_source == null && name != null { unwraps unknown }
1130
+ }
1131
+ in ts_import_type { unwraps unknown }
1132
+ fn ts_value_intrinsic_text(name, text) = if name == "Uppercase" then upper(text)
1133
+ else if name == "Lowercase" then lower(text)
1134
+ else {
1135
+ value head: slice(text, 0, 1)
1136
+ value if utf16_len(head) > 1 then text
1137
+ else (if name == "Capitalize" then upper(head) else lower(head)) slice(text, 1)
1138
+ }
1139
+ fn ts_value_template_piece(quasi) = {
1140
+ value raw: quasi.text
1141
+ value end: if ends_with(raw, "${{") then len(raw) - 2 else len(raw) - 1
1142
+ value js_string_parts("`" slice(raw, 1, end) "`").text
1143
+ }
1144
+ fn ts_value_template_hole(n) = {
1145
+ value arms: value_parts(n)
1146
+ value parts: if count(arms ?? []) == 1 then first(arms).parts else []
1147
+ value part: if count(parts) == 1 then first(parts) else null
1148
+ value if part.identity_known && part.identity.kind == "value" && contains(["str", "num"], type_of(part.identity.value))
1149
+ then "" part.identity.value else null
1150
+ }
1151
+ fn ts_value_template_text(n) = {
1152
+ value pieces: map(n.quasi.nodes, quasi => ts_value_template_piece(quasi))
1153
+ value holes: map(n.expression.nodes, hole => ts_value_template_hole(hole))
1154
+ value if any(holes, hole => hole == null) then null
1155
+ else first(pieces) join(map(zip(holes, slice(pieces, 1)), pair => first(pair) get(pair, 1)), "")
1156
+ }
1157
+ fn ts_value_intrinsic_literal(n) = {
1158
+ value arms: value_parts(n)
1159
+ value parts: if count(arms ?? []) == 1 then first(arms).parts else []
1160
+ value part: if count(parts) == 1 then first(parts) else null
1161
+ value if part.identity_known && part.identity.kind == "value" && type_of(part.identity.value) == "str"
1162
+ then part.identity.value else null
1163
+ }
1164
+ in ts_type_keyword {
1165
+ let ts_value_never []
1166
+ let ts_value_string_intrinsic text == "intrinsic" && parent is ts_type_alias
1167
+ && count(parent.type_parameter) == 1
1168
+ && contains(["Uppercase", "Lowercase", "Capitalize", "Uncapitalize"], parent.name.text)
1169
+ let ts_value_intrinsic_formal first(parent.type_parameter.nodes)
1170
+ let ts_value_intrinsic_source ts_value_intrinsic_literal(ts_value_intrinsic_formal)
1171
+ let ts_value_intrinsic_result if type_of(ts_value_intrinsic_source) == "str"
1172
+ then ts_value_intrinsic_text(parent.name.text, ts_value_intrinsic_source) else null
1173
+ when ts_value_string_intrinsic {
1174
+ when ts_value_intrinsic_formal always any || ts_value_intrinsic_formal always error {
1175
+ unwraps ts_value_intrinsic_formal
1176
+ }
1177
+ when !(ts_value_intrinsic_formal always any || ts_value_intrinsic_formal always error) {
1178
+ unwraps select ts_value_intrinsic_formal against trait string then self else trait unknown distribute ts_value_intrinsic_formal
1179
+ }
1180
+ fits string
1181
+ when type_of(ts_value_intrinsic_result) == "str" {
1182
+ fits literal
1183
+ fits truthy when ts_value_intrinsic_result != ""
1184
+ fits falsy when ts_value_intrinsic_result == ""
1185
+ identity ts_value_intrinsic_result
1186
+ widens (trait string, truthy) | (trait string, falsy)
1187
+ }
1188
+ when type_of(ts_value_intrinsic_result) != "str" {
1189
+ fits truthy when ts_value_intrinsic_formal always truthy
1190
+ fits falsy when ts_value_intrinsic_formal always falsy
1191
+ }
1192
+ }
1193
+ when text == "string" { unwraps (trait string, truthy) | (trait string, falsy) }
1194
+ when text == "number" { unwraps (trait number, truthy) | (trait number, falsy) }
1195
+ when text == "boolean" { unwraps (trait boolean, truthy) | (trait boolean, falsy) }
1196
+ when text == "bigint" { unwraps (trait bigint, truthy) | (trait bigint, falsy) }
1197
+ fits string when text == "string"
1198
+ fits number when text == "number"
1199
+ fits boolean when text == "boolean"
1200
+ fits bigint when text == "bigint"
1201
+ fits symbol when text == "symbol"
1202
+ fits truthy when text == "symbol"
1203
+ fits object when text == "object"
1204
+ fits nonthenable when text == "null" || text == "undefined" || text == "void"
1205
+ fits null, nullable, falsy when text == "null"
1206
+ fits undefined, nullable, falsy when text == "undefined" || text == "void"
1207
+ fits void when text == "void"
1208
+ when text == "null" { identity null }
1209
+ when text == "undefined" || text == "void" { identity { type: "undefined" } }
1210
+ fits any open when text == "any"
1211
+ fits unknown open when text == "unknown"
1212
+ when text == "never" {
1213
+ unwraps ...ts_value_never
1214
+ }
1215
+ }
1216
+
1217
+ }