@inthedark/dxlint 0.1.15 → 0.1.17

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.
@@ -68,7 +68,8 @@ fn ts_value_print_part(part) = {
68
68
  value args: if carrier.description.kind == "tuple" && part.product_known && part.product != null then
69
69
  map(zip(carrier.arguments ?? [], part.product.elements), pair => {
70
70
  value flags: last(pair)
71
- value (if flags.repeated then "..." else "") ts_value_print(first(pair)) (if flags.optional then "?" else "")
71
+ value element: ts_value_print(first(pair))
72
+ value if flags.optional then "(" element " | undefined)?" else if flags.values then "..." element "[]" else element
72
73
  })
73
74
  else map(carrier.arguments ?? [], item => ts_value_print(item))
74
75
  value joined: { value args; join ", " }
@@ -113,12 +114,27 @@ fn ts_value_print_member(edge, seen) = {
113
114
  || any(edge.member.sources, source => source.flags == null) then "unknown"
114
115
  else "{prefix}{name}{optional}: {result};"
115
116
  }
117
+ # A single call signature prints as its function type: `<T>(v: T) => T`.
118
+ fn ts_value_print_signature(n, header, seen) = {
119
+ value formals: map(site_field(n, header.site, "type_parameter") ?? [], formal => site_text(n, first(site_field(n, formal, "name") ?? [])).text)
120
+ value names: map(site_field(n, header.site, "parameter") ?? [], parameter => site_text(n, first(site_field(n, parameter, "name") ?? [])).text)
121
+ value types: map(header.parameters, parameter => ts_value_print_handle_seen(parameter, seen))
122
+ value params: zip(names, types, (name, text) => "{name}: {text}")
123
+ value generics: if count(formals) > 0 then "<" {value formals; join ", "} ">" else ""
124
+ value generics "(" {value params; join ", "} ") => " ts_value_print_handle_seen(header.result, seen)
125
+ }
126
+ fn ts_value_print_callable(part, seen) = if part.carrier != null || !contains(part.traits, "callable") then null else {
127
+ value headers: flat_map(value_signatures(part.handle) ?? [], arm => arm)
128
+ value if count(headers) == 1 then ts_value_print_signature(last(seen), first(headers), seen) else null
129
+ }
116
130
  fn ts_value_print_handle_part(part, seen) = {
117
131
  value structural: part.carrier == null && contains(part.traits, "structural")
118
132
  && !contains(part.traits, "callable") && !contains(part.traits, "constructible")
119
133
  value children: if structural then value_children(part.handle, true, []) else null
120
134
  value members: map(children ?? [], edge => ts_value_print_member(edge, seen))
121
- value if !structural then ts_value_print(value_constituents(part.handle))
135
+ value callable: ts_value_print_callable(part, seen)
136
+ value if callable != null then callable
137
+ else if !structural then ts_value_print(value_constituents(part.handle))
122
138
  else if children == null then "unknown"
123
139
  else if count(children) == 0 then "{{}}"
124
140
  else "{{ " {value members; join " "} " }}"
@@ -140,7 +156,13 @@ fn ts_value_print_handle_seen(n, seen) = {
140
156
  value if arms == null then "unknown" else if count(arms) == 0 then "never"
141
157
  else {value ranked; sort_by (pair) pair.rank; map (pair) pair.label; unique; join " | "}
142
158
  }
143
- fn ts_value_print_handle(n) = ts_value_print_handle_seen(n, [])
159
+ # A plain (argument-free) written reference keeps its name: tsc keeps alias names
160
+ # for object-literal aliases, but expands instantiated conditional aliases.
161
+ fn ts_value_print_handle(n) = {
162
+ value annotation: ts_value_print_annotation(n)
163
+ value plain: annotation is ts_type_reference && count(annotation.type_argument.nodes) == 0
164
+ value if plain then ts_value_print_written(n) ?? ts_value_print_handle_seen(n, []) else ts_value_print_handle_seen(n, [])
165
+ }
144
166
  # The type TypeScript prints for an expression: a type parameter stands for its
145
167
  # constraint (`any` counts as `unknown`), and a reference keeps its written name.
146
168
  fn ts_value_print_annotation(n) = if n is ts_identifier && name_binding(n) != null
package/std/ts/values.dx CHANGED
@@ -12,8 +12,10 @@ define {
12
12
  exclusive null, undefined, string, number, boolean, bigint, symbol, object
13
13
  exclusive falsy, truthy
14
14
  exclusive thenable, nonthenable
15
- # TypeScript's `object` keyword takes every non-primitive: a shape with members is one.
15
+ # TypeScript's `object` keyword takes every non-primitive: a shape is one,
16
+ # even `{}` (`{} extends object` holds; as a target `{}` still takes `"abc"`).
16
17
  trait shaped implies object
18
+ trait structural implies object
17
19
  trait array implies object
18
20
  trait tuple implies object
19
21
  trait callable implies object
@@ -307,7 +309,11 @@ fn ts_value_nonthenable(n) = {
307
309
  })
308
310
  value if certain then true else {
309
311
  value shape: ts_value_then_headers(member_call_parameters(n, "then"))
310
- value if parts == null || len(parts) == 0 || !shape.known then null else !shape.thenable
312
+ # No arm has a `then` at all (not even through an indexer).
313
+ value then_arms: value_lookup(n, "then").alternatives
314
+ value absent: then_arms != null && count(then_arms) > 0 && all(then_arms, arm => arm.matched == false)
315
+ value if parts == null || len(parts) == 0 then null else if absent then true
316
+ else if !shape.known then null else !shape.thenable
311
317
  }
312
318
  }
313
319
  # Read from certain traits: `always any` stays unknown for an open type
@@ -328,9 +334,11 @@ in ts_null {
328
334
  }
329
335
  in ts_null { fits nonthenable }
330
336
  in ts_boolean, ts_number, ts_bigint, ts_string { fits literal }
337
+ # Only a literal expression is fresh and widens; a written literal type
338
+ # (`(): true`, `x as 1`, inside `ts_literal_type`) keeps its literal.
331
339
  in ts_boolean {
332
340
  fits boolean
333
- widens (trait boolean, truthy) | (trait boolean, falsy)
341
+ when parent not ts_literal_type { widens (trait boolean, truthy) | (trait boolean, falsy) }
334
342
  fits falsy when text == "false"
335
343
  fits truthy when text == "true"
336
344
  identity text == "true"
@@ -339,21 +347,21 @@ in ts_number {
339
347
  let ts_value_number_raw replace(text, "_", "")
340
348
  let ts_value_number js_number(ts_value_number_raw)
341
349
  fits number
342
- widens (trait number, truthy) | (trait number, falsy)
350
+ when parent not ts_literal_type { widens (trait number, truthy) | (trait number, falsy) }
343
351
  fits falsy when ts_value_number == 0
344
352
  fits truthy when ts_value_number != 0
345
353
  identity ts_value_number
346
354
  }
347
355
  in ts_bigint {
348
356
  fits bigint
349
- widens (trait bigint, truthy) | (trait bigint, falsy)
357
+ when parent not ts_literal_type { widens (trait bigint, truthy) | (trait bigint, falsy) }
350
358
  fits falsy when js_bigint(text) == "0"
351
359
  fits truthy when js_bigint(text) != "0"
352
360
  identity { type: "bigint", value: js_bigint(text) }
353
361
  }
354
362
  in ts_string {
355
363
  fits string
356
- widens (trait string, truthy) | (trait string, falsy)
364
+ when parent not ts_literal_type { widens (trait string, truthy) | (trait string, falsy) }
357
365
  fits falsy when js_string(text) == ""
358
366
  fits truthy when js_string(text) != ""
359
367
  identity js_string(text)
@@ -376,6 +384,9 @@ in ts_template {
376
384
  }
377
385
  in ts_regex { unwraps instance "RegExp" }
378
386
 
387
+ # `class X extends B<number>`: the heritage arguments sit on the class and
388
+ # instantiate the base its name reads.
389
+ in ts_identifier { when parent is ts_class && node == parent.extends.node { arguments parent.type_argument } }
379
390
  in ts_identifier {
380
391
  # One answer for which recipe applies: an identifier's value asks once.
381
392
  let ts_value_name_route when {
@@ -553,10 +564,17 @@ in ts_assignment_pattern {
553
564
  when parent is ts_pattern_property && !ts_value_assignment_any { unwraps (parent.parent.[ts_value_assignment_key]) \ undefined | right.widened }
554
565
  when parent not (ts_array_pattern, ts_pattern_property) { unwraps left ?? right }
555
566
  }
556
- in ts_import, ts_import_specifier, ts_meta_property, ts_import_call {
567
+ in ts_import, ts_import_specifier, ts_meta_property {
557
568
  unwraps unknown
558
569
  }
570
+ # A promise of the module's namespace (`await import("./m")`).
571
+ in ts_import_call {
572
+ when ts_module_dynamic_source != null { unwraps instance "Promise" of definition }
573
+ when ts_module_dynamic_source == null { unwraps unknown }
574
+ }
559
575
  in ts_import_equals { when value.node is ts_call { unwraps unknown } }
576
+ # `import Box = ns.Box; Box<number>`: the reference's arguments pass through the alias.
577
+ in ts_import_equals { when value.node not ts_call { arguments through.type_argument } }
560
578
  in ts_super {
561
579
  let ts_value_super_context ts_value_this_context(node)
562
580
  let ts_value_super_base ts_value_super_context.container.extends.node
@@ -590,7 +608,9 @@ in ts_this {
590
608
  }
591
609
  }
592
610
 
593
- in ts_parenthesized, ts_satisfies, ts_instantiation { unwraps expression }
611
+ in ts_parenthesized, ts_satisfies { unwraps expression }
612
+ # `take<Callback>`: `take`'s signature with `T` fixed (no inference at calls).
613
+ in ts_instantiation { unwraps instantiate expression with type_argument }
594
614
  in ts_as, ts_type_assertion {
595
615
  when type.text == "const" { unwraps expression }
596
616
  when type.text != "const" { unwraps type }
@@ -749,8 +769,20 @@ in ts_object {
749
769
  let ts_value_any_spreads filter(property.nodes, item => item is ts_spread && item.argument always any)
750
770
  when len(ts_value_any_spreads) > 0 { unwraps ...ts_value_any_spreads.argument }
751
771
  when len(ts_value_any_spreads) == 0 { unwraps scope members from it }
772
+ # Spread members under the own ones; of two spreads, the later wins.
773
+ let ts_value_spread_sources reverse(filter(property.nodes, item => item is ts_spread))
774
+ let ts_value_last_spread first(ts_value_spread_sources)
775
+ let ts_value_earlier_spreads slice(ts_value_spread_sources, 1)
776
+ when ts_value_last_spread != null { members members inherits ts_value_last_spread.argument }
777
+ when len(ts_value_earlier_spreads) > 0 { members members inherits &...ts_value_earlier_spreads.argument }
752
778
  }
753
779
  in ts_object { fits object, truthy in members }
780
+ # `{ [Symbol.dispose]() {} }`: a computed key is keyed by its value (a real symbol).
781
+ in ts_object {
782
+ let ts_value_object_computed filter(property.nodes, item => item is (ts_property, ts_method)
783
+ && item.key is ts_computed_key && ts_value_key(item.key.node) == null)
784
+ indexed keyed ts_value_object_computed by key.expression using it in members
785
+ }
754
786
  in ts_interface {
755
787
  when extends != null { members members inherits &...extends }
756
788
  fits builtin_function in members when
@@ -847,8 +879,10 @@ in ts_index {
847
879
  let ts_value_index_nullish in_optional_chain == true && object could nullable
848
880
  || ts_value_optional_member(node, index.node)
849
881
  || (if ts_compiler_option(node, "noUncheckedIndexedAccess") == true then ts_value_unchecked_index(node) else false)
850
- when object always any { unwraps object }
851
- when !(object always any) {
882
+ # As for `ts_member`: a type parameter whose parts are certainly not `any` reads its indexes.
883
+ let ts_value_index_dynamic ts_value_has_trait(object.node, "any") != false && object always any
884
+ when ts_value_index_dynamic { unwraps object }
885
+ when !ts_value_index_dynamic {
852
886
  when !ts_value_index_nullish { unwraps (object \ nullable, undefined).[index] }
853
887
  when ts_value_index_nullish { unwraps (object \ nullable, undefined).[index] | trait undefined, nullable, falsy, nonthenable }
854
888
  }
@@ -880,6 +914,11 @@ in ts_class {
880
914
  members statics inherits extends
881
915
  members members inherits extends.["prototype"]
882
916
  }
917
+ # A base class is a function too (`C.name`, `C.length`).
918
+ # (A program without that global has no such layer.)
919
+ when extends == null && count(global_declarations(node, "Function", "type") ?? []) > 0 {
920
+ members statics inherits instance "Function"
921
+ }
883
922
  # An interface of the same name merges into the instance (`implements`
884
923
  # adds nothing).
885
924
  let ts_value_merged_interfaces if name == null then [] else
@@ -898,6 +937,8 @@ in ts_parameter {
898
937
  }
899
938
  }
900
939
  in ts_property { flag readonly when ts_value_const_context(node) }
940
+ # `{ old: 1, ...a }`: a later spread's member of the same key wins.
941
+ in ts_property { flag yields when parent is ts_object && any(parent.property.nodes, item => item is ts_spread && item.offset > offset) }
901
942
  in ts_property_signature, ts_index_signature, ts_class_property, ts_parameter {
902
943
  flag readonly when any([] modifier, modifier => modifier == "readonly")
903
944
  }
@@ -974,7 +1015,9 @@ in ts_type_query {
974
1015
  # `typeof f<T>` instantiates `f`'s own type parameters.
975
1016
  arguments type_argument
976
1017
  when name is ts_qualified_name { unwraps name }
977
- when name not ts_qualified_name { unwraps name.definition }
1018
+ when name not ts_qualified_name && count(type_argument.nodes) == 0 { unwraps name.definition }
1019
+ # A value whose type is generic (`declare const take: <T>(fn: T) => T`) too.
1020
+ when name not ts_qualified_name && count(type_argument.nodes) > 0 { unwraps instantiate name.definition with type_argument }
978
1021
  }
979
1022
  in ts_type_predicate {
980
1023
  carries as {
@@ -1015,14 +1058,12 @@ in ts_literal_type {
1015
1058
  when !starts_with(text, "-") { unwraps value }
1016
1059
  when starts_with(text, "-") && value is ts_number {
1017
1060
  fits number, literal
1018
- widens (trait number, truthy) | (trait number, falsy)
1019
1061
  fits falsy when value always falsy
1020
1062
  fits truthy when value always truthy
1021
1063
  identity -value.ts_value_number
1022
1064
  }
1023
1065
  when starts_with(text, "-") && value is ts_bigint {
1024
1066
  fits bigint, literal
1025
- widens (trait bigint, truthy) | (trait bigint, falsy)
1026
1067
  fits falsy when value always falsy
1027
1068
  fits truthy when value always truthy
1028
1069
  identity { type: "bigint", value: ts_value_negated_bigint(value.text) }
@@ -1209,6 +1250,9 @@ in ts_type_keyword {
1209
1250
  fits falsy when ts_value_intrinsic_formal always falsy
1210
1251
  }
1211
1252
  }
1253
+ # The `this` type (`on(): this`) is the receiver of the call reading the
1254
+ # signature (`store.on(1)`: `store`'s type); elsewhere it stays unknown.
1255
+ when text == "this" { unwraps through.callee.object }
1212
1256
  when text == "string" { unwraps (trait string, truthy) | (trait string, falsy) }
1213
1257
  when text == "number" { unwraps (trait number, truthy) | (trait number, falsy) }
1214
1258
  when text == "boolean" { unwraps (trait boolean, truthy) | (trait boolean, falsy) }