@inthedark/dxlint 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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 +29 -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,104 @@
1
+ define {
2
+
3
+ fn ts_value_has_enum(n) = any(value_constituents(n) ?? [], arm => any(arm, part => contains(part.traits, "enum")))
4
+ fn ts_value_enum_literal(n) = {
5
+ value bare: unparenthesised_node(n)
6
+ value if bare is ts_number then bare.ts_value_number
7
+ else if bare is ts_string then js_string(bare.text)
8
+ else if bare is ts_unary && bare.operator == "-" && bare.argument is ts_number
9
+ then -bare.argument.ts_value_number
10
+ else null
11
+ }
12
+ fn ts_value_enum_prior(n) = {
13
+ value bare: unparenthesised_node(n.init.node)
14
+ value if bare not ts_identifier then null else {
15
+ value earlier: take(n.parent.member.nodes, node_ordinal(n))
16
+ value find(earlier, member => ts_value_key(member.name.node) == bare.text)
17
+ }
18
+ }
19
+ in ts_enum {
20
+ let ts_value_enum_literals {
21
+ value members: member.nodes
22
+ value states: {
23
+ value { index: 0, literal: null }
24
+ walk (state) if state.index >= len(members) then null else {
25
+ value member: get(members, state.index)
26
+ value literal: if member.init != null then ts_value_enum_literal(member.init.node)
27
+ else if state.index == 0 then 0
28
+ else if type_of(state.literal) == "num" then state.literal + 1 else null
29
+ value { index: state.index + 1, literal: literal }
30
+ }
31
+ drop 1
32
+ }
33
+ value map(states, state => state.literal)
34
+ }
35
+ scope statics
36
+ scope enum_origin
37
+ members statics closed
38
+ fits object, truthy in statics
39
+ # A uniform (all number or all string) enum type is one value; it spells
40
+ # out its members only where they are enumerated (mapped keys, case arms).
41
+ # A mixed one stays the union of its members.
42
+ let ts_value_enum_backing if count(member.nodes) == 0 then "number"
43
+ else if all(member.nodes, item => item.ts_value_enum_kind == "number") then "number"
44
+ else if all(member.nodes, item => item.ts_value_enum_kind == "string") then "string"
45
+ else null
46
+ let ts_value_enum_numeric any(member.nodes, item => item.ts_value_enum_kind == "number")
47
+ # A numeric member's value is also an ordinal key of the object, mapping back
48
+ # to its name; string members have no reverse mapping. The reverse map is
49
+ # read at runtime but is no key of the type (`keyof typeof E` is the names).
50
+ when ts_value_enum_numeric && !contains(modifier, "const") { indexed runtime ordinal member using trait string in statics }
51
+ fits enum in enum_origin
52
+ fits number in enum_origin when ts_value_enum_backing == "number"
53
+ fits string in enum_origin when ts_value_enum_backing == "string"
54
+ carries as { kind: "enum", name: name.text, first_member_kind: first(member.nodes).ts_value_enum_kind ?? "number" }
55
+ when count(member.nodes) > 0 { enumerates ...member in enum_origin }
56
+ when ts_value_enum_backing != null { unwraps scope enum_origin from it for type }
57
+ when ts_value_enum_backing == null { unwraps ...member for type }
58
+ unwraps scope statics from it
59
+ }
60
+ in ts_namespace {
61
+ let ts_value_namespace_enum_members {
62
+ value name_binding(name.node).declarations
63
+ filter (site) site.parent is ts_enum
64
+ flat_map (site) site.parent.member.nodes
65
+ }
66
+ when count(ts_value_namespace_enum_members) > 0 {
67
+ unwraps ...ts_value_namespace_enum_members for type
68
+ }
69
+ }
70
+ in ts_enum_member {
71
+ declares member name keyed ts_value_key(it) in nearest statics hoisted
72
+ let ts_value_enum_literal if init != null then ts_value_enum_literal(init.node)
73
+ else if node_ordinal(node) == 0 then 0
74
+ else get(parent.ts_value_enum_literals, node_ordinal(node))
75
+ let ts_value_enum_prior ts_value_enum_prior(node)
76
+ let ts_value_enum_kind when {
77
+ type_of(ts_value_enum_literal) == "num" then "number"
78
+ type_of(ts_value_enum_literal) == "str" then "string"
79
+ init == null then "number"
80
+ ts_value_enum_prior != null then ts_value_enum_prior.ts_value_enum_kind
81
+ init always number then "number"
82
+ init always string then "string"
83
+ else then null
84
+ }
85
+ when ts_value_enum_kind != null {
86
+ fits number when ts_value_enum_kind == "number"
87
+ fits string when ts_value_enum_kind == "string"
88
+ fits falsy when ts_value_enum_literal != null && (ts_value_enum_literal == 0 || ts_value_enum_literal == "")
89
+ fits truthy when ts_value_enum_literal != null && ts_value_enum_literal != 0 && ts_value_enum_literal != ""
90
+ when ts_value_enum_literal != null { identity ts_value_enum_literal }
91
+ when ts_value_enum_literal == null { identity node }
92
+ unwraps (scope enum_origin from parent) & self
93
+ widens ...parent.member
94
+ }
95
+ when ts_value_enum_kind == null { unwraps unknown }
96
+ }
97
+ in ts_qualified_name {
98
+ arguments parent.type_argument
99
+ let ts_value_qualified_key ts_value_key(right.node)
100
+ defines (scope module, namespace from left.definition).[ts_value_qualified_key] ?? (scope statics from left.definition).[ts_value_qualified_key]
101
+ unwraps definition
102
+ }
103
+
104
+ }
@@ -0,0 +1,274 @@
1
+ define {
2
+
3
+ fn ts_value_has_trait(n, trait) = {
4
+ value arms: value_trait_parts(n, [trait])
5
+ value if arms == null then null else any(arms, arm => any(arm.parts, part => contains(part.traits, trait)))
6
+ }
7
+ fn ts_value_has_void(n) = ts_value_has_trait(n, "void")
8
+ fn ts_value_callable(n) = {
9
+ value signatures: value_signatures(n)
10
+ value signatures != null && count(signatures) > 0 && all(signatures, arm => count(arm) > 0)
11
+ }
12
+ # The value can't be computed (unsupported or unresolved), unlike explicit `unknown`.
13
+ fn ts_value_opaque(n) = value_parts(n) == null
14
+ fn ts_value_trait(parts, trait) = parts != null && len(parts) > 0
15
+ && all(parts, arm => any(arm, part => contains(part.traits, trait)))
16
+ fn ts_value_category(parts) = if parts != null && any(parts, arm => any(arm, part => contains(part.traits, "error"))) then "error"
17
+ else if ts_value_trait(parts, "any") then "any" else null
18
+ fn ts_value_string_like(n) = {
19
+ value arms: value_trait_parts(n, ["string", "intersection", "parameter", "any", "unknown"])
20
+ value arms != null && len(arms) > 0 && all(arms, arm =>
21
+ any(arm.parts, part => contains(part.traits, "string"))
22
+ && all(arm.parts, part => contains(part.traits, "intersection") || contains(part.traits, "parameter")
23
+ || !contains(part.traits, "any") && !contains(part.traits, "unknown") && !contains(part.uncertain_traits, "string")))
24
+ }
25
+ fn ts_value_parameter(n) = {
26
+ value arms: value_parts(n)
27
+ value arms != null && len(arms) > 0 && all(arms, arm => any(arm.parts, part => part.carrier_known && part.carrier.description.kind == "parameter"))
28
+ }
29
+ fn ts_value_string(text) = {
30
+ value slashes: replace(text, "\\", "\\\\")
31
+ value quotes: replace(slashes, "\"", "\\\"")
32
+ value lines: replace(quotes, "\n", "\\n")
33
+ value "\"" lines "\""
34
+ }
35
+ # A literal's truthy/falsy split prints one label, so parenthesise on distinct
36
+ # printed arms: a union needs brackets, a nested array keeps its own.
37
+ fn ts_value_print_element_mode(parts) = {
38
+ value labels: unique(map(parts ?? [], arm => ts_value_print_arm(arm)))
39
+ value if count(labels) > 1 then "wrap"
40
+ else if count(labels) == 1 && contains(first(labels), " & ") then "wrap" else "plain"
41
+ }
42
+ fn ts_value_print_element(parts, text) = if ts_value_print_element_mode(parts) == "wrap" then "(" text ")" else text
43
+ # TypeScript sorts union constituents by type flags (`stableTypeOrdering`),
44
+ # with `null` and `undefined` always printed last.
45
+ fn ts_value_print_part_rank(part) = {
46
+ value traits: part.traits
47
+ value literal: part.identity_known && part.identity.kind == "value"
48
+ value unique: part.identity_known && part.identity.kind != "value"
49
+ value when {
50
+ contains(traits, "intersection") then 268435456
51
+ contains(traits, "null") then 2000000
52
+ contains(traits, "undefined") then 2000001
53
+ contains(traits, "any") then 1
54
+ contains(traits, "unknown") then 2
55
+ contains(traits, "void") then 16
56
+ contains(traits, "string") then (if literal then 1024 else 32)
57
+ contains(traits, "number") then (if literal then 2048 else 64)
58
+ contains(traits, "bigint") then (if literal then 4096 else 128)
59
+ contains(traits, "boolean") then (if literal then 8192 else 256)
60
+ contains(traits, "symbol") then (if unique then 16384 else 512)
61
+ contains(traits, "enum") then 65536
62
+ contains(traits, "never") then 262144
63
+ contains(traits, "parameter") then 524288
64
+ else then 1048576
65
+ }
66
+ }
67
+ fn ts_value_print_arm_rank(arm) = if any(arm, part => contains(part.traits, "intersection")) then 268435456
68
+ else if count(arm) == 0 then 1048576 else first(map(arm, part => ts_value_print_part_rank(part)))
69
+ fn ts_value_print_part(part) = {
70
+ value carrier: part.carrier
71
+ value args: if carrier.description.kind == "tuple" && part.product_known && part.product != null then
72
+ map(zip(carrier.arguments ?? [], part.product.elements), pair => {
73
+ value flags: last(pair)
74
+ value (if flags.repeated then "..." else "") ts_value_print(first(pair)) (if flags.optional then "?" else "")
75
+ })
76
+ else map(carrier.arguments ?? [], item => ts_value_print(item))
77
+ value joined: { value args; join ", " }
78
+ value elements: zip(carrier.arguments ?? [], args, (item, text) => ts_value_print_element(item, text))
79
+ value joined_elements: { value elements; join ", " }
80
+ value scalar: find(["any", "unknown", "void", "null", "undefined", "string", "number", "boolean", "bigint", "symbol", "object"], trait => contains(part.traits, trait))
81
+ value literal: part.identity.kind == "value" && part.identity_known
82
+ value if carrier.description.kind == "tuple" then (if part.product.readonly then "readonly " else "") "[" joined "]"
83
+ else if carrier.description.kind == "array" then (if part.product.readonly then "readonly " else "") joined_elements "[]"
84
+ else if carrier.description.kind == "named" then carrier.description.name (if len(args) == 0 then "" else "<" joined ">")
85
+ else if carrier.description.kind == "parameter" then carrier.description.name
86
+ else if scalar == "symbol" && part.identity_known && part.identity != null && part.identity.kind != "value" then "unique symbol"
87
+ else if literal && scalar == "string" then ts_value_string(part.identity.value)
88
+ else if literal && scalar == "bigint" then part.identity.value.value "n"
89
+ else if literal && (scalar == "number" || scalar == "boolean") then "" part.identity.value
90
+ else scalar ?? "unknown"
91
+ }
92
+ fn ts_value_print_arm(arm) = {
93
+ value labels: map(arm, part => {
94
+ value text: ts_value_print_part(part)
95
+ value { text: text, parameter: contains(part.traits, "parameter") }
96
+ })
97
+ value definite: filter(labels, item => item.text != "unknown")
98
+ value content: filter(definite, item => !item.parameter)
99
+ value kept: if count(content) > 0 then content else if count(definite) > 0 then definite else [{ text: "unknown" }]
100
+ value { value kept; map (item) item.text; unique; join " & " }
101
+ }
102
+ fn ts_value_print(parts) = if parts == null then "unknown" else if len(parts) == 0 then "never" else {
103
+ value labels: map(parts, arm => ts_value_print_arm(arm))
104
+ value ranked: zip(labels, map(parts, arm => ts_value_print_arm_rank(arm)), (label, rank) => { label: label, rank: rank })
105
+ value { value ranked; sort_by (pair) pair.rank; map (pair) pair.label; unique; join " | " }
106
+ }
107
+ fn ts_value_print_member(edge, seen) = {
108
+ value key: edge.member.key
109
+ value name: if type_of(key) == "num" then "" key
110
+ else if type_of(key) != "str" then null
111
+ else if re_test(key, /^[$A-Za-z_][$A-Za-z_0-9]*$/) then key else ts_value_string(key)
112
+ value prefix: if any(edge.member.sources, source => contains(source.flags, "readonly")) then "readonly " else ""
113
+ value optional: if edge.member.optional then "?" else ""
114
+ value result: ts_value_print_handle_seen(edge.handle, seen)
115
+ value if name == null || edge.member.optional == null
116
+ || any(edge.member.sources, source => source.flags == null) then "unknown"
117
+ else "{prefix}{name}{optional}: {result};"
118
+ }
119
+ fn ts_value_print_handle_part(part, seen) = {
120
+ value structural: part.carrier == null && contains(part.traits, "structural")
121
+ && !contains(part.traits, "callable") && !contains(part.traits, "constructible")
122
+ value children: if structural then value_children(part.handle, true, []) else null
123
+ value members: map(children ?? [], edge => ts_value_print_member(edge, seen))
124
+ value if !structural then ts_value_print(value_constituents(part.handle))
125
+ else if children == null then "unknown"
126
+ else if count(children) == 0 then "{{}}"
127
+ else "{{ " {value members; join " "} " }}"
128
+ }
129
+ fn ts_value_print_handle_arm(arm, seen, n) = if count(arm.parts) == 0 then "unknown" else {
130
+ value printed: map(arm.parts, part => {
131
+ value text: ts_value_print_handle_part(part, [] seen n)
132
+ value { text: text, parameter: contains(part.traits, "parameter") }
133
+ })
134
+ value definite: filter(printed, item => item.text != "unknown")
135
+ value content: filter(definite, item => !item.parameter)
136
+ value kept: if count(content) > 0 then content else if count(definite) > 0 then definite else [{ text: "unknown" }]
137
+ value { value kept; map (item) item.text; unique; join " & " }
138
+ }
139
+ fn ts_value_print_handle_seen(n, seen) = {
140
+ value arms: if contains(seen, n) then null else value_parts(n)
141
+ value labels: map(arms ?? [], arm => ts_value_print_handle_arm(arm, seen, n))
142
+ value ranked: zip(labels, map(arms ?? [], arm => ts_value_print_arm_rank(arm.parts)), (label, rank) => { label: label, rank: rank })
143
+ value if arms == null then "unknown" else if count(arms) == 0 then "never"
144
+ else {value ranked; sort_by (pair) pair.rank; map (pair) pair.label; unique; join " | "}
145
+ }
146
+ fn ts_value_print_handle(n) = ts_value_print_handle_seen(n, [])
147
+ # The type TypeScript prints for an expression: a type parameter stands for its
148
+ # constraint (`any` counts as `unknown`), and a reference keeps its written name.
149
+ fn ts_value_print_annotation(n) = if n is ts_identifier && name_binding(n) != null
150
+ && name_binding(n).node.parent.type != null then name_binding(n).node.parent.type.node else null
151
+ fn ts_value_print_written(n) = {
152
+ value annotation: ts_value_print_annotation(n)
153
+ value named: annotation is ts_type_reference && annotation.name is ts_identifier
154
+ && !ts_value_has_trait(annotation, "parameter")
155
+ && !contains(["Array", "ReadonlyArray"], annotation.name.text)
156
+ value if named then annotation.text else null
157
+ }
158
+ fn ts_value_print_annotation_parameter(annotation) = if annotation is ts_type_reference
159
+ && annotation.name is ts_identifier && count(annotation.type_argument.nodes) == 0
160
+ && name_binding(annotation.name.node) != null
161
+ && name_binding(annotation.name.node).node.parent is ts_type_parameter
162
+ then name_binding(annotation.name.node).node.parent else null
163
+ fn ts_value_print_type(n) = {
164
+ value annotation: ts_value_print_annotation(n)
165
+ value parameter: if annotation != null then ts_value_print_annotation_parameter(annotation) else null
166
+ value written: ts_value_print_written(n)
167
+ value if parameter != null && parameter.constraint != null && parameter.constraint.text == "any" then "unknown"
168
+ else written ?? ts_value_print_handle(n)
169
+ }
170
+ fn ts_value_symbol_key(n, key) = {
171
+ value roots: global_declarations(n, "Symbol", "value")
172
+ value properties: map(roots ?? [], root => value_constituents(value_lookup(declaration_value(n, root), key).member))
173
+ value parts: flat_map(properties, property => flat_map(property ?? [], arm => arm))
174
+ value identity: first(parts).identity
175
+ value if roots != null && count(parts) > 0 && all(properties, property => property != null)
176
+ && all(parts, part => contains(part.traits, "symbol") && part.identity_known
177
+ && part.identity.kind == "declaration" && part.identity == identity)
178
+ then {symbol: {file: identity.file, node: identity.node}} else null
179
+ }
180
+
181
+ fn ts_value_ancestry_links(handle) = {
182
+ value bases: value_bases(handle)
183
+ value projected: map(bases ?? [], base => value_parts(base))
184
+ value parts: flat_map(projected, arms => flat_map(arms ?? [], arm => arm.parts))
185
+ value {parts: parts, complete: bases != null && all(projected, arms => arms != null)}
186
+ }
187
+ fn ts_value_ancestry(part) = {
188
+ value reached: walk([part.handle], handle =>
189
+ map(ts_value_ancestry_links(handle).parts, base => base.handle))
190
+ value parts: flat_map(reached, handle => flat_map(value_parts(handle) ?? [], arm => arm.parts))
191
+ value complete: count(reached) > 0 && all(reached, handle => ts_value_ancestry_links(handle).complete)
192
+ value {parts: parts, complete: complete}
193
+ }
194
+ fn ts_value_builtin_part(part, n, names) = {
195
+ value states: map(names, name => {
196
+ value carrier: part.carrier
197
+ value globals: global_declarations(n, name, "type")
198
+ value site: {file: carrier.file, node: carrier.node}
199
+ value matched: carrier != null && globals != null && contains(globals, site)
200
+ value origin: if matched then declaration_origin(n, site) else null
201
+ value if !part.carrier_known || globals == null then null
202
+ else if !matched then false
203
+ else if origin == null then null
204
+ else origin.kind == "library"
205
+ })
206
+ value if any(states, state => state == true) then true
207
+ else if any(states, state => state == null) then null
208
+ else false
209
+ }
210
+ fn ts_value_builtin_ancestor(part, n, names) = {
211
+ value ancestry: ts_value_ancestry(part)
212
+ value states: map(ancestry.parts, item => ts_value_builtin_part(item, n, names))
213
+ value if any(states, state => state == true) then true
214
+ else if !ancestry.complete || any(states, state => state == null) then null
215
+ else false
216
+ }
217
+ fn ts_value_builtin_values(n, names) = {
218
+ value globals: map(names, name => global_declarations(n, name, "value"))
219
+ value projections: map(flat_map(globals, rows => rows ?? []), site => {
220
+ value origin: declaration_origin(n, site)
221
+ value arms: if origin.kind == "library" then value_parts(declaration_value(n, site)) else []
222
+ value parts: flat_map(arms ?? [], arm => arm.parts)
223
+ value {
224
+ parts: parts,
225
+ complete: origin != null && arms != null && all(parts, part => part.carrier_known)
226
+ }
227
+ })
228
+ value {
229
+ parts: flat_map(projections, projection => projection.parts),
230
+ complete: all(globals, rows => rows != null) && all(projections, projection => projection.complete)
231
+ }
232
+ }
233
+ fn ts_value_builtin_value_ancestor(part, n, names) = {
234
+ value ancestry: ts_value_ancestry(part)
235
+ value canonical: ts_value_builtin_values(n, names)
236
+ value states: flat_map(ancestry.parts, item => map(canonical.parts, target => {
237
+ value matched: item.carrier != null && target.carrier != null
238
+ && item.carrier.file == target.carrier.file && item.carrier.node == target.carrier.node
239
+ value if !item.carrier_known || !target.carrier_known then null
240
+ else if !matched then false
241
+ else if contains(target.uncertain_traits, "constructible") then null
242
+ else if !contains(target.traits, "constructible") then true
243
+ else if contains(item.uncertain_traits, "constructible") then null
244
+ else contains(item.traits, "constructible")
245
+ }))
246
+ value if any(states, state => state == true) then true
247
+ else if !ancestry.complete || !canonical.complete || any(states, state => state == null) then null
248
+ else false
249
+ }
250
+ fn ts_value_builtin_type(n, names) = {
251
+ value arms: value_parts(n)
252
+ value if arms == null then null else if count(arms) != 1 then false else {
253
+ value states: map(first(arms).parts, part => ts_value_builtin_ancestor(part, n, names))
254
+ value if any(states, state => state == true) then true
255
+ else if any(states, state => state == null) then null
256
+ else false
257
+ }
258
+ }
259
+ fn ts_value_builtin_constructor(n, names) = {
260
+ value arms: value_parts(n)
261
+ value if arms == null then null else if count(arms) == 0 then false else {
262
+ value states: map(arms, arm => {
263
+ value parts: map(arm.parts, part => ts_value_builtin_value_ancestor(part, n, names))
264
+ value if any(parts, part => part == true) then true
265
+ else if any(parts, part => part == null) then null
266
+ else false
267
+ })
268
+ value if any(states, state => state == false) then false
269
+ else if any(states, state => state == null) then null
270
+ else true
271
+ }
272
+ }
273
+
274
+ }