@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.
package/std/ts/modules.dx CHANGED
@@ -11,6 +11,8 @@ import .ts/comments
11
11
  exclude "exclude"
12
12
  extends "extends"
13
13
  references "references[].path"
14
+ # Content mappers make TypeScript read other extensions as its own.
15
+ extensions "contentMappers[].extensions[]"
14
16
  options "compilerOptions"
15
17
  paths "paths"
16
18
  base "baseUrl"
@@ -39,6 +41,7 @@ import .ts/comments
39
41
  scoped_prefix "@"
40
42
  type_directory "@types"
41
43
  type_join "__"
44
+ preserve_links_option "preserveSymlinks"
42
45
  }
43
46
  conditions ["types", condition, "default"]
44
47
  ambient_scopes [ambient_module]
@@ -58,17 +61,44 @@ import .ts/comments
58
61
  ]
59
62
  # `/// <reference lib="x" />` asks for `lib:x`, wherever the release keeps it.
60
63
  library_prefix "lib:"
64
+ # `/// <reference types="x" />` asks for `types:x`: the program's type
65
+ # roots first, then the packages seen from the file.
66
+ type_reference_prefix "types:"
61
67
  library_option "lib"
62
68
  default_libraries ["es2023", "dom"]
63
69
  types_option "types"
64
70
  type_roots_option "typeRoots"
65
71
  type_directories ["node_modules/@types"]
66
- project_files ["**/*.ts", "**/*.tsx", "**/*.mts", "**/*.cts"]
72
+ # Without `include`: every file whose extension the program reads.
73
+ project_files ["**/*"]
67
74
  references true
68
75
  # A file publishes a global only by spelling its name (or escaping it,
69
76
  # or forwarding through a reference); ambient modules need a keyword.
70
77
  name_hints ["\\u", "<reference"]
71
78
  ambient_hints ["module", "namespace"]
79
+ # What pulls another file into the program, as tsc follows imports and
80
+ # references; a cheap text scan, so imports in comments count too.
81
+ pulls {
82
+ "{found}" [
83
+ "(?-u:\\b)(?:from|import)\\s*[\"']([^\"'\\n]+)[\"']",
84
+ "(?-u:\\b)(?:require|import)\\s*\\(\\s*[\"']([^\"'\\n]+)[\"']",
85
+ ]
86
+ }
87
+ # A file only pulls reach joins the program when it may publish globals
88
+ # or ambient modules; a plain module is reached through its importers.
89
+ pulled_publishers ["(?m)^\\s*declare\\s+(?:global(?-u:\\b)|module\\s+[\"'])"]
90
+ pulled_modules ["(?m)^\\s*(?:import|export)(?-u:\\b)"]
91
+ # Global lookups skip modules: a top-level `import`/`export` (column 0;
92
+ # a namespace's indented `export` keeps its script global).
93
+ global_modules ["(?m)^(?:import|export)(?-u:\\b)"]
94
+ # Reference directives count only in the file's leading comments, never
95
+ # inside a block comment (matched whole, so it holds no directive).
96
+ leading "\\A(?:[\\s\\uFEFF]+|//[^\\n]*|/\\*[\\s\\S]*?\\*/)*"
97
+ leading_pulls {
98
+ "./{found}" ["/\\*[\\s\\S]*?\\*/|///\\s*<reference\\s+path\\s*=\\s*[\"']([^\"']+)[\"']"]
99
+ "types:{found}" ["/\\*[\\s\\S]*?\\*/|///\\s*<reference\\s+types\\s*=\\s*[\"']([^\"']+)[\"']"]
100
+ "lib:{found}" ["/\\*[\\s\\S]*?\\*/|///\\s*<reference\\s+lib\\s*=\\s*[\"']([^\"']+)[\"']"]
101
+ }
72
102
  }
73
103
  when resolution.config.module == "commonjs" || resolution.config.module == 1 {
74
104
  conditions ["types", "require", "default"]
@@ -138,6 +168,16 @@ in ts_import {
138
168
  resolves type, query namespace.node in module js_string(from.text) as "*" asking all
139
169
  }
140
170
  }
171
+ # `import("./m")` (a written specifier) reads the module's namespace, as `import * as`.
172
+ fn ts_module_dynamic_specifier(n) = {
173
+ value bare: last(walk([first(n.argument.nodes)], item => if item is ts_parenthesized then [item.expression.node] else []))
174
+ value if bare is ts_string then js_string(bare.text)
175
+ else if bare is ts_template && count(bare.expression.nodes) == 0 then re_replace(bare.text, /^`|`$/, "") else null
176
+ }
177
+ in ts_import_call {
178
+ let ts_module_dynamic_source ts_module_dynamic_specifier(node)
179
+ when ts_module_dynamic_source != null { resolves value node in module ts_module_dynamic_source as "*" }
180
+ }
141
181
  in ts_import_specifier {
142
182
  when kind != "type" && parent.kind != "type" {
143
183
  resolves value, type, query local.node ?? imported.node in module js_string(parent.from.text) as ts_module_name(imported.node)
@@ -206,6 +246,16 @@ in ts_export {
206
246
  }
207
247
  }
208
248
  in ts_export_assignment { exports value, type, query expression.node as "=" }
249
+ # `export = Outer.Public`: a qualified target reads the namespace member, as
250
+ # `import X = Outer.Public` does.
251
+ fn ts_module_export_target(n) = n.parent is ts_export_assignment
252
+ || n.parent is ts_member && n == n.parent.object.node && ts_module_export_target(n.parent.node)
253
+ in ts_member {
254
+ let ts_module_member_key ts_module_name(property.node)
255
+ when ts_module_export_target(node) {
256
+ defines (scope module, namespace from object.definition).[ts_module_member_key]
257
+ }
258
+ }
209
259
 
210
260
  in ts_variable_declaration {
211
261
  when ts_module_public(node) { exports value, query declaration.nodes }
@@ -275,7 +325,7 @@ in ts_reference_path let ts_module_path_reference {
275
325
  value if starts_with(path, "./") || starts_with(path, "../") || starts_with(path, "/")
276
326
  || re_test(path, /^[A-Za-z]:\//) then path else "./{path}"
277
327
  }
278
- in ts_reference_types let ts_module_types_reference "@types/{ts_module_reference}"
328
+ in ts_reference_types let ts_module_types_reference "types:{ts_module_reference}"
279
329
  in ts_reference_lib let ts_module_lib_reference "lib:{ts_module_reference}"
280
330
  in ts_reference_path {
281
331
  globals all value, type, query from module ts_module_path_reference
package/std/ts/names.dx CHANGED
@@ -148,6 +148,8 @@ in ts_identifier {
148
148
  # `[key: string]: T` names nothing.
149
149
  || is ts_parameter.name && parent is ts_index_signature.parameter then null
150
150
  else if ts_name_outside_method != null then "outside"
151
+ # `import X = A.B`, `export = A.B`: `A` names a namespace in every meaning.
152
+ else if is ts_member.object && ts_name_entity_path(parent.node) then "entity"
151
153
  # A computed key inside type syntax reads like `typeof`: type-only
152
154
  # imports count.
153
155
  else if ts_name_type_key then "query"
@@ -155,7 +157,10 @@ in ts_identifier {
155
157
  when ts_name_resolution == "outside" { resolves value outside ts_name_outside_method }
156
158
  when ts_name_resolution == "value" { resolves value }
157
159
  when ts_name_resolution == "query" { resolves query }
160
+ when ts_name_resolution == "entity" { resolves value, type }
158
161
  }
162
+ fn ts_name_entity_path(n) = n is (ts_export_assignment.expression, ts_import_equals.value)
163
+ || n is ts_member.object && ts_name_entity_path(n.parent.node)
159
164
  in ts_identifier let ts_name_type_key containing(ts_computed_key) != null && name_type_access
160
165
  in ts_type_reference { resolves type name }
161
166
  # `export { x }` uses `x`; `export { x } from "m"` does not.
@@ -33,8 +33,10 @@ in ts_conditional {
33
33
  }
34
34
  in ts_type_predicate {
35
35
  resolves value name
36
- when modifier == null { unwraps truthy includes type from name }
36
+ when modifier == null { unwraps truthy includes type from name dynamic }
37
37
  }
38
+ # Equality keeps `any` as it is (TypeScript); predicates, typeof and
39
+ # instanceof narrow it (`dynamic`), except to `Object`/`Function`.
38
40
  in ts_binary {
39
41
  when operator == "===" && (right always literal || right always undefined || right.structure_unwrapped is ts_null) { unwraps truthy includes right from left }
40
42
  when operator == "===" && (left always literal || left always undefined || left.structure_unwrapped is ts_null) { unwraps truthy includes left from right }
@@ -58,16 +60,20 @@ in ts_binary {
58
60
  unwraps truthy right \ nullable
59
61
  }
60
62
  when operator == "in" { unwraps truthy has left from right }
61
- when operator == "instanceof" {
63
+ when operator == "instanceof" && (right not ts_identifier || right.text != "Object" && right.text != "Function") {
64
+ unwraps truthy includes right.["prototype"] from left dynamic
65
+ }
66
+ when operator == "instanceof" && right is ts_identifier && (right.text == "Object" || right.text == "Function") {
62
67
  unwraps truthy includes right.["prototype"] from left
63
68
  }
64
69
  }
65
70
 
66
- @macro narrowing_typeof(tag, expected, available) {
71
+ # `mode`: `dynamic` when the test narrows `any` (all but "function"/"object").
72
+ @macro narrowing_typeof(tag, expected, available, mode) {
67
73
  in ts_binary {
68
74
  when left.structure_unwrapped is ts_unary && left.structure_unwrapped.operator == "typeof" && right.literal_value.value == tag && available {
69
75
  when operator == "===" || operator == "==" {
70
- unwraps truthy includes expected from left.structure_unwrapped.argument
76
+ unwraps truthy includes expected from left.structure_unwrapped.argument mode
71
77
  }
72
78
  when operator == "!==" || operator == "!=" {
73
79
  unwraps truthy !includes expected from left.structure_unwrapped.argument
@@ -75,7 +81,7 @@ in ts_binary {
75
81
  }
76
82
  when right.structure_unwrapped is ts_unary && right.structure_unwrapped.operator == "typeof" && left.literal_value.value == tag && available {
77
83
  when operator == "===" || operator == "==" {
78
- unwraps truthy includes expected from right.structure_unwrapped.argument
84
+ unwraps truthy includes expected from right.structure_unwrapped.argument mode
79
85
  }
80
86
  when operator == "!==" || operator == "!=" {
81
87
  unwraps truthy !includes expected from right.structure_unwrapped.argument
@@ -83,14 +89,14 @@ in ts_binary {
83
89
  }
84
90
  }
85
91
  }
86
- @narrowing_typeof("string", (trait string, truthy | trait string, falsy), true)
87
- @narrowing_typeof("number", (trait number, truthy | trait number, falsy), true)
88
- @narrowing_typeof("boolean", (trait boolean, truthy | trait boolean, falsy), true)
89
- @narrowing_typeof("bigint", (trait bigint, truthy | trait bigint, falsy), true)
90
- @narrowing_typeof("symbol", (trait symbol, truthy), true)
91
- @narrowing_typeof("undefined", (trait undefined, nullable, falsy, nonthenable), true)
92
- @narrowing_typeof("function", (instance "Function"), global_declarations(node, "Function", "type") != null && count(global_declarations(node, "Function", "type")) > 0)
93
- @narrowing_typeof("function", (trait callable, object, truthy), global_declarations(node, "Function", "type") == [])
94
- @narrowing_typeof("object", (trait object, truthy | trait null, nullable, falsy, nonthenable), true)
92
+ @narrowing_typeof("string", (trait string, truthy | trait string, falsy), true, dynamic)
93
+ @narrowing_typeof("number", (trait number, truthy | trait number, falsy), true, dynamic)
94
+ @narrowing_typeof("boolean", (trait boolean, truthy | trait boolean, falsy), true, dynamic)
95
+ @narrowing_typeof("bigint", (trait bigint, truthy | trait bigint, falsy), true, dynamic)
96
+ @narrowing_typeof("symbol", (trait symbol, truthy), true, dynamic)
97
+ @narrowing_typeof("undefined", (trait undefined, nullable, falsy, nonthenable), true, dynamic)
98
+ @narrowing_typeof("function", (instance "Function"), global_declarations(node, "Function", "type") != null && count(global_declarations(node, "Function", "type")) > 0, static)
99
+ @narrowing_typeof("function", (trait callable, object, truthy), global_declarations(node, "Function", "type") == [], static)
100
+ @narrowing_typeof("object", (trait object, truthy | trait null, nullable, falsy, nonthenable), true, static)
95
101
 
96
102
  }
@@ -31,6 +31,32 @@ in ts_call rule es-wrap-iife {
31
31
  warn if !structure_iife_wrapped && !info.wrapped then "Wrap an immediate function invocation in parentheses." else if mode == "inside" then "Wrap only the function expression in parens." else "Move the invocation into the parens that contain the function."
32
32
  }
33
33
 
34
+ fn wrap_breaks(source, from, to) = re_test(byte_slice(source, from, to), /[\n\r\u{2028}\u{2029}]/)
35
+ # A line break between the parts an operator joins, never inside a bracketed child.
36
+ fn wrap_multiline(source, n) =
37
+ if n is structure_binary then wrap_breaks(source, n.left.end_offset, n.right.offset) || wrap_multiline(source, n.left.node) || wrap_multiline(source, n.right.node)
38
+ else if n is ts_conditional then wrap_breaks(source, n.test.end_offset, n.consequent.offset) || wrap_breaks(source, n.consequent.end_offset, n.alternate.offset)
39
+ || wrap_multiline(source, n.test.node) || wrap_multiline(source, n.consequent.node) || wrap_multiline(source, n.alternate.node)
40
+ else if n is structure_member then re_test(n.structure_chain_info.gap ?? "", /[\n\r\u{2028}\u{2029}]/) || wrap_multiline(source, n.object.node)
41
+ else if n is ts_call then wrap_multiline(source, n.callee.node)
42
+ else if n is (ts_as, ts_satisfies, ts_non_null) then wrap_multiline(source, n.expression.node)
43
+ else if n is (ts_unary, ts_await) then wrap_multiline(source, n.argument.node)
44
+ else false
45
+ # An operand of an expression `wrap_multiline` looks into.
46
+ fn wrap_joined(n) =
47
+ n.parent is (ts_binary, ts_logical, ts_conditional, ts_as, ts_satisfies, ts_non_null, ts_unary, ts_await)
48
+ || n.parent is structure_member && n.parent.object.node == n
49
+ || n.parent is ts_call && n.parent.callee.node == n
50
+
51
+ in ts_binary, ts_logical, ts_conditional, ts_member, ts_index, ts_call, ts_as, ts_satisfies, ts_non_null, ts_unary, ts_await rule es-wrap-multiline-expression {
52
+ when !wrap_joined(node) && !(parent is (ts_expression_statement, ts_parenthesized))
53
+ when wrap_multiline(source, node)
54
+ when next_token != null && !space_after.line_break
55
+ let indent { value byte_slice(source, position(offset).js_line_start, offset); re_find /^[ \t]*/; nonempty } ?? ""
56
+ fix at node as "(" text "\n" indent ")"
57
+ warn "Wrap this multiline expression in parentheses, with `)` on its own line."
58
+ }
59
+
34
60
  group structure_parens ts_parenthesized, ts_parenthesized_type
35
61
  group structure_assertion ts_as, ts_satisfies, ts_type_assertion
36
62
  group structure_binary ts_binary, ts_logical
@@ -206,6 +232,7 @@ in structure_parens rule es-no-extra-parens {
206
232
  || outer.structure_parens_for_init && inner.structure_parens_has_in
207
233
  || owner is ts_update && { value source; byte_slice inner.end_offset owner.end_offset; re_test /[\n\r]/ }
208
234
  || owner is ts_await && inner is ts_non_null
235
+ || wrap_multiline(source, inner) && wrap_breaks(source, inner.end_offset, outer.end_offset)
209
236
  when structure_parens_count > (if protected && required < 1 then 1 else required)
210
237
  let comment structure_parens_comment(outer, source)
211
238
  let typed_inner {
@@ -252,7 +252,7 @@ fn ts_usage_used(binding, sites, references, typed, variable, value_binding, dec
252
252
  }
253
253
  in ts_identifier let ts_usage_binding name_binding(node)
254
254
  in ts_program let ts_usage_typed
255
- any([".ts", ".tsx", ".mts", ".cts"], extension => ends_with(file, extension))
255
+ !any([".js", ".jsx", ".mjs", ".cjs"], extension => ends_with(file, extension))
256
256
  in ts_program let ts_usage_declaration_file ts_usage_typed && contains(last(split(file, "/")), ".d.")
257
257
  in ts_node let ts_usage_typed scoping_program.ts_usage_typed
258
258
  in ts_node let ts_usage_declaration_file scoping_program.ts_usage_declaration_file
package/std/ts/tokens.dx CHANGED
@@ -51,6 +51,9 @@ fn ts_token_gap(source, start, end) = {
51
51
  value if text == "" || text == " " then {
52
52
  text: text, start: start, end: end, spaced: text == " ", line_break: false,
53
53
  blank_lines: 0, blank_gaps: 0, comments: false,
54
+ } else if re_test(text, /^\r?\n[ \t]*$/) then {
55
+ text: text, start: start, end: end, spaced: true, line_break: true,
56
+ blank_lines: 0, blank_gaps: 0, comments: false,
54
57
  } else {
55
58
  value breaks: ts_token_breaks(text)
56
59
  value plain: !contains(text, "/")
@@ -42,13 +42,21 @@ fn type_is_allowed(entries, groups, node) = {
42
42
  value count(groups) > 0 && any(entries, entry =>
43
43
  all(groups, group => any(group, gate => type_specifier_matches(entry, gate, directory))))
44
44
  }
45
+ # The `declare module "pkg"` a declaration sits in, if any: it declares that package.
46
+ fn type_specifier_ambient_package(n, site) = {
47
+ value owner: find(walk([site], current => site_parent(n, current)), current => current.kind == "ts_namespace"
48
+ && re_test(site_text(n, current).text, /^\s*(?:export\s+)?(?:declare\s+)?module\s*["']/))
49
+ value name: if owner == null then null else first(site_field(n, owner, "name") ?? [])
50
+ value if name == null then null else replace(replace(site_text(n, name).text, "\"", ""), "'", "")
51
+ }
45
52
  fn type_specifier_site(entry, n, site, name, directory) = {
46
53
  value origin: declaration_origin(n, site)
54
+ value ambient: if entry.from == "package" then type_specifier_ambient_package(n, site) else null
47
55
  value if origin == null || name == null || entry.from == "file" && directory == null then null else type_specifier_matches(entry, {
48
56
  name: name,
49
- from: if origin.kind == "library" then "lib" else if origin.kind == "package" then "package" else "file",
57
+ from: if ambient != null || origin.kind == "package" then "package" else if origin.kind == "library" then "lib" else "file",
50
58
  path: origin.path,
51
- package: origin.package
59
+ package: ambient ?? origin.package
52
60
  }, directory)
53
61
  }
54
62
  fn type_specifier_part(entry, n, part, directory) = {
@@ -72,8 +80,16 @@ fn type_value_allowed(entries, n) = if count(entries) == 0 then false else {
72
80
  value named: if count(arms) == 1 || !any(sites, site => site.kind == "ts_union") then filter(sites, site => site.name != null
73
81
  && contains(["ts_type_alias", "ts_function", "ts_method", "ts_method_signature"], site.kind)) else []
74
82
  value directory: if any(entries, entry => entry.from == "file") then program_directory(n) else null
83
+ # A function is also allowed by the name it is called through, as
84
+ # TypeScript ESLint compares the reference (`import { it }` of `test as it`).
85
+ value reference: if n is ts_identifier then n.text else if n is ts_member then n.property.text else null
75
86
  value states: map(entries, entry => {
76
- value named_states: map(named, site => type_specifier_site(entry, n, site, site.name, directory))
87
+ value named_states: map(named, site => {
88
+ value own: type_specifier_site(entry, n, site, site.name, directory)
89
+ value referenced: if reference != null && reference != site.name && site.kind != "ts_type_alias"
90
+ then type_specifier_site(entry, n, site, reference, directory) else null
91
+ value if referenced == true then true else own
92
+ })
77
93
  value alternatives: map(arms ?? [], arm => {
78
94
  value parts: map(arm.parts, part => type_specifier_part(entry, n, part, directory))
79
95
  value if any(parts, state => state == true) || any(named_states, state => state == true) then true
@@ -0,0 +1,24 @@
1
+ # Object-property iteration over keyed collections, whose entries are never own properties.
2
+
3
+ import .ts/suggestions/globals
4
+
5
+ define {
6
+
7
+ fn collection_iteration_subject(n) = {
8
+ value arms: value_parts(n)
9
+ value arms != null && count(arms) > 0 && all(arms, arm => any(arm.parts, part =>
10
+ ts_value_builtin_ancestor(part, n, ["Map", "ReadonlyMap", "WeakMap", "Set", "ReadonlySet", "WeakSet"]) == true))
11
+ }
12
+
13
+ in ts_call, ts_for_in rule es-no-object-iteration-of-collections {
14
+ let method if node is ts_call && contains(["Object.keys", "Object.values", "Object.entries"], global_method_callee)
15
+ then global_method_callee else null
16
+ let subject if node is ts_for_in then right.node
17
+ else if method != null && count(argument.nodes) > 0 && first(argument.nodes) not ts_spread then first(argument.nodes) else null
18
+ when subject != null && collection_iteration_subject(subject)
19
+ report at if node is ts_for_in then { start: offset, end: opener.ts_open_paren.paired_token.end } else node
20
+ warn if node is ts_call then "`" method "` on a Map or Set is always empty: its entries are not own properties. Iterate it with `for...of` or its own `.keys()`, `.values()` or `.entries()` instead."
21
+ else "`for...in` over a Map or Set visits none of its entries. Use `for...of` instead."
22
+ }
23
+
24
+ }
@@ -99,7 +99,9 @@ in ts_call let promise_finally_callback { value callee.node; unparenthesised_nod
99
99
 
100
100
  fn promise_async_policy(n, names) = {
101
101
  value source: if n is ts_function && n.name != null then name_binding(n.name.node).node ?? n.name.node else n
102
- value signatures: value_signatures(source)
102
+ # A getter is no callable: it returns its declared (or body's) value.
103
+ value getter: n is ts_method && n.kind == "get"
104
+ value signatures: if getter then [[{result: n.return_type.node ?? n}]] else value_signatures(source)
103
105
  value headers: flat_map(signatures ?? [], arm => arm)
104
106
  value returns: map(headers, header => {
105
107
  value arms: if header.result == null then null else value_parts(header.result)
@@ -128,8 +130,9 @@ fn promise_async_policy(n, names) = {
128
130
 
129
131
  in ts_call.argument, ts_new.argument, ts_template.expression rule es-no-misused-promises:void-arguments {
130
132
  when parent not ts_call || node.index != 0 || !parent.promise_finally_callback
131
- when promise_void_context(node)
132
- when !promise_void_overload_accepts(node)
133
+ # `f(...[1, async () => {}])` passes the elements: reported at the spread.
134
+ let passed if node is ts_spread && node.argument is ts_array then node.argument.element.nodes else [node]
135
+ when any(passed, item => promise_void_context(item) && !promise_void_overload_accepts(item))
133
136
  report at unparenthesised_node(node)
134
137
  warn "Promise returned in function argument where a void return was expected."
135
138
  }
@@ -53,12 +53,21 @@ fn readonly_parameter_children(handle, members, arguments, unchecked) = {
53
53
  else {kind: "edge", edge: edge}),
54
54
  }
55
55
  }
56
+ fn readonly_parameter_deferred_branches(source) = if source is ts_conditional_type then [source.consequent.node, source.alternate.node] else {
57
+ value sources: value_sources(source)
58
+ value alias: if count(sources.sites) == 1 then first(sources.sites) else null
59
+ value body: if alias.kind == "ts_type_alias" then first(site_field(source, alias, "type") ?? []) else null
60
+ value if body.kind != "ts_conditional_type" then []
61
+ else map(flat_map(["consequent", "alternate"], field => site_field(source, body, field) ?? []), site => declaration_value(source, site))
62
+ }
56
63
  fn readonly_parameter_step(entry, settings) = if entry.kind == "source" then {
57
64
  value arms: value_parts(entry.source)
65
+ value deferred: if arms == null && entry.root then readonly_parameter_deferred_branches(entry.source) else []
58
66
  value skipped: entry.root && count(arms) > 0 && all(arms, arm => readonly_parameter_branded(arm))
59
67
  value {
60
- answer: if arms == null then null else true,
61
- children: if skipped then [] else map(arms ?? [], arm => {kind: "arm", arm: arm}),
68
+ answer: if arms == null && count(deferred) == 0 then null else true,
69
+ children: if count(deferred) > 0 then map(deferred, branch => {kind: "source", source: branch, root: false})
70
+ else if skipped then [] else map(arms ?? [], arm => {kind: "arm", arm: arm}),
62
71
  }
63
72
  } else if entry.kind == "edge" then {
64
73
  value edge: entry.edge
@@ -13,27 +13,74 @@ fn argument_value_finding(site, mode, sender, receiver, display) = {
13
13
  fn assignment_may_be_unsafe(parts) = parts != null && any(parts, arm => any(arm, part =>
14
14
  contains(part.traits, "any")
15
15
  || part.carrier_known && part.carrier != null && count(part.carrier.arguments ?? []) > 0))
16
+ # The lone signature a call with spread arguments relates to, and whether its
17
+ # last parameter is a rest parameter.
18
+ fn argument_header(call) = {
19
+ value arms: if call is ts_new then value_constructors(call.callee.node) else value_signatures(call.callee.node)
20
+ value header: if arms != null && count(arms) == 1 && count(first(arms)) == 1 then first(first(arms)) else null
21
+ value parameters: if header != null then site_field(call, header.site, "parameter") ?? [] else []
22
+ value names: if count(parameters) > 0 then site_field(call, last(parameters), "name") ?? [] else []
23
+ value if header == null || header.parameters == null then null
24
+ else {parameters: header.parameters, rest: count(names) == 1 && first(names).kind == "ts_rest"}
25
+ }
26
+ # The parameter an argument at `position` meets (a rest parameter's element past the others).
27
+ fn argument_parameter_at(header, position) = {
28
+ value fixed: if header.rest then count(header.parameters) - 1 else count(header.parameters)
29
+ value if position < fixed then get(header.parameters, position)
30
+ else if header.rest then value_lookup(last(header.parameters), position - fixed).member else null
31
+ }
32
+ # A spread tuple's element handles (`f(...pair)` passes each); null for anything else.
33
+ fn argument_tuple_elements(source) = {
34
+ value arms: value_parts(source)
35
+ value part: if arms != null && count(arms) == 1 && count(first(arms).parts) == 1 then first(first(arms).parts) else null
36
+ value tuple: part != null && part.product_known && part.product != null && part.carrier.description.kind == "tuple"
37
+ && !any(part.product.elements, element => element.values || element.optional)
38
+ value if tuple then map(part.product.elements, (element, index) => value_lookup(part.handle, index).member) else null
39
+ }
40
+ fn argument_tuple_findings(argument, header, elements, position) = filter(map(elements, (element, index) => {
41
+ value sender: value_constituents(element)
42
+ value parameter: argument_parameter_at(header, position + index)
43
+ value receiver: if parameter != null then value_constituents(parameter) else null
44
+ value if parameter != null && ts_value_category(sender) != null && receiver != null
45
+ && !ts_value_trait(receiver, "unknown") && !ts_value_trait(receiver, "any")
46
+ then argument_value_finding(argument, "tuple", sender, receiver, null) else null
47
+ }), finding => finding != null)
48
+ # Arguments in order: a spread tuple passes its elements to the next parameters
49
+ # (TypeScript ESLint), so later arguments meet the parameters after them.
16
50
  fn argument_value_findings(call) = if call not (ts_call, ts_new, ts_tagged_template) then [] else {
17
- value if call is ts_tagged_template then call.quasi.expression.nodes else call.argument.nodes
18
- flat_map (argument) {
51
+ value arguments: if call is ts_tagged_template then call.quasi.expression.nodes else call.argument.nodes
52
+ value header: if any(arguments, argument => argument is ts_spread) then argument_header(call) else null
53
+ value walked: fold(arguments, {position: 0, shifted: false, lost: false, findings: []}, (state, argument) => {
19
54
  value spread: argument is ts_spread
20
55
  value source: if spread then argument.argument.node else argument
21
56
  value sender: value_constituents(source)
22
- value expected: if spread || !assignment_may_be_unsafe(sender) then null else value_expected(argument)
23
- value receiver: expected.value
57
+ value tuple: if spread && header != null && !state.lost && ts_value_category(sender) == null
58
+ then argument_tuple_elements(source) else null
59
+ value parameter: if !spread && state.shifted && !state.lost && header != null && assignment_may_be_unsafe(sender)
60
+ then argument_parameter_at(header, state.position) else null
61
+ value expected: if spread || parameter != null || !assignment_may_be_unsafe(sender) then null else value_expected(argument)
62
+ value receiver: if parameter != null then value_constituents(parameter) else expected.value
24
63
  value carrier: assignment_carrier(sender)
25
64
  value element: first(carrier.arguments)
26
- value when {
65
+ value found: when {
27
66
  spread && ts_value_category(sender) != null then
28
67
  [argument_value_finding(argument, "spread", sender, null, null)]
68
+ tuple != null then argument_tuple_findings(argument, header, tuple, state.position)
29
69
  spread && assignment_array(sender, source) && ts_value_category(element) != null then
30
70
  [argument_value_finding(argument, "array", element, null, sender)]
31
- !spread && expected.present == true && receiver != null
71
+ !spread && (expected.present == true || parameter != null) && receiver != null
32
72
  && !assignment_empty_map(source) && assignment_unsafe(sender, receiver, call) then
33
73
  [argument_value_finding(argument, "argument", sender, receiver, null)]
34
74
  else then []
35
75
  }
36
- }
76
+ value {
77
+ position: state.position + (if tuple != null then count(tuple) else 1),
78
+ shifted: state.shifted || tuple != null,
79
+ lost: state.lost || spread && tuple == null,
80
+ findings: concat(state.findings, found),
81
+ }
82
+ })
83
+ value walked.findings
37
84
  }
38
85
  fn return_value_owner(n) = {
39
86
  value n.parent.node
@@ -269,6 +269,13 @@ fn type_argument_inferred(n, site, signature, formal) = count(n.parent.type_argu
269
269
  && !any(parameters, parameter => parameter != null && parameter != first(pair) && type_argument_mentions(n, parameter, formal))
270
270
  value pair != null && !literal_only && type_argument_same(n, last(pair), true)
271
271
  }
272
+ # A call's argument whose parameter mentions the type parameter infers it:
273
+ # the default then applies only where nothing is written.
274
+ fn type_argument_infers(n, site, signature, formal) = n.parent is (ts_call, ts_new) && {
275
+ value parameters: type_argument_parameter_sites(n, site, signature)
276
+ value any(zip(parameters, n.parent.argument.nodes), pair => pair != null && first(pair) != null && last(pair) != null
277
+ && type_argument_mentions(n, first(pair), formal))
278
+ }
272
279
  fn type_argument_redundancy(n) = {
273
280
  value owner: n.parent.node
274
281
  value arguments: owner.type_argument.nodes
@@ -279,7 +286,7 @@ fn type_argument_redundancy(n) = {
279
286
  value default_site: if defaults != null && count(defaults) == 1 then first(defaults) else null
280
287
  value default: if default_site != null then declaration_value(owner, default_site) else null
281
288
  value if last(arguments) != n then null
282
- else if default != null && type_argument_same(n, default, false)
289
+ else if default != null && type_argument_same(n, default, false) && !type_argument_infers(n, site, signature, formal)
283
290
  && (!type_argument_unresolved(n) || type_argument_written_same(n, default_site)) then "default"
284
291
  else if formal != null && type_argument_inferred(n, site, signature, formal) then "inferred"
285
292
  else null
@@ -14,13 +14,21 @@ fn using_disposable_primitive(bare) = bare is (ts_null, ts_boolean, ts_number, t
14
14
  # members, so it cannot carry Symbol.dispose or Symbol.asyncDispose.
15
15
  fn using_disposable_plain_object(bare) = bare is ts_object
16
16
  && !any(bare.property.nodes, member => member is ts_spread || member.key is ts_computed_key)
17
+ # A method returning its receiver (`server.listen(): this`) hands back a
18
+ # resource its receiver already holds.
19
+ fn using_disposable_returns_receiver(bare) = bare is ts_call && bare.callee is ts_member && {
20
+ value selection: call_selection(bare)
21
+ value selection != null && count(selection.selected) > 0 && all(selection.selected, site =>
22
+ any(site_field(bare, site, "return_type") ?? [], type => site_text(bare, type).text == "this"))
23
+ }
17
24
  fn using_disposable_kind(n) = {
18
25
  value bare: unparenthesised_node(n)
19
26
  value if bare is ts_assignment && bare.operator == "=" then using_disposable_kind(bare.right.node)
20
27
  else if bare == null || using_disposable_primitive(bare) || using_disposable_plain_object(bare) then null else {
21
- value if !(n could object) then null
28
+ value kind: if !(n could object) then null
22
29
  else if value_has_member(n, {global: "Symbol", class: "value", path: ["asyncDispose"]}, null) == true then "async"
23
30
  else if value_has_member(n, {global: "Symbol", class: "value", path: ["dispose"]}, null) == true then "sync" else null
31
+ value if kind == null || using_disposable_returns_receiver(bare) then null else kind
24
32
  }
25
33
  }
26
34
  in ts_node let using_disposable_held using_disposable_kind(node)
package/std/ts/typed.dx CHANGED
@@ -38,6 +38,7 @@ import .ts/typed/enums
38
38
  import .ts/typed/qualifiers
39
39
  import .ts/typed/regexp-exec
40
40
  import .ts/typed/for-in-arrays
41
+ import .ts/typed/collection-iteration
41
42
  import .ts/typed/type-conversions
42
43
  import .ts/typed/rejection-handlers
43
44
  import .ts/typed/unsafe-members
@@ -49,12 +50,16 @@ define {
49
50
 
50
51
  fn generated_empty_type(n) = {
51
52
  value arms: value_parts(n)
52
- value arms != null && any(arms, arm =>
53
- count(arm.parts) > 0 && all(arm.parts, part =>
53
+ value arms != null && any(arms, arm => {
54
+ # An intersection node kept as its own part (`T & {}`) adds no members.
55
+ value concrete: filter(arm.parts, part => part.traits != ["intersection"])
56
+ value handles: if count(concrete) == count(arm.parts) then [arm.handle] else map(concrete, part => part.handle)
57
+ value count(concrete) > 0 && all(concrete, part =>
54
58
  contains(part.traits, "structural") && count(part.uncertain_traits) == 0
55
59
  && part.carrier_known && part.carrier == null)
56
- && value_children(arm.handle, true, []) == []
57
- && value_signatures(arm.handle) == [[]] && value_constructors(arm.handle) == [[]])
60
+ && all(handles, handle => value_children(handle, true, []) == []
61
+ && value_signatures(handle) == [[]] && value_constructors(handle) == [[]])
62
+ })
58
63
  }
59
64
  in ts_type_reference, ts_intersection rule es-no-generated-empty-object-type {
60
65
  when node is ts_intersection || count(type_argument.nodes) > 0
@@ -186,7 +191,9 @@ fn stringification_ignored_alias(n, ignored) = {
186
191
  value roots: value_sources(n)
187
192
  value sites: walk(if count(roots.sites) == 1 then roots.sites else [], site =>
188
193
  if count(site_sources(n, site).sites) == 1 then site_sources(n, site).sites else [])
189
- value arms != null && all(arms, arm => all(arm.parts, part => part.carrier_known && part.carrier == null))
194
+ # TypeScript keeps an alias's name for unions and structural shapes, not
195
+ # for a lone class or interface (`type ClassAlias = Plain` reads as `Plain`).
196
+ value arms != null && (count(arms) > 1 || all(arms, arm => all(arm.parts, part => part.carrier_known && part.carrier == null)))
190
197
  && any(sites, site => site.kind == "ts_type_alias" && contains(ignored, site.name))
191
198
  }
192
199
  fn stringification_part(n, part, ignored, check_unknown, seen) = {
@@ -465,15 +472,20 @@ in ts_call.argument rule es-await-thenable:iterable {
465
472
  when parent.is_promise_aggregator == true && not ts_array && ts_await_nonawaitable_iterable(node)
466
473
  warn "Unexpected iterable of non-Promise (non-\"Thenable\") values passed to promise aggregator."
467
474
  }
475
+ # No arm declares `Symbol[name]` (an open library interface leaves
476
+ # value_has_member undecided, though its declarations lack the member).
477
+ fn typed_lacks_symbol(n, name) = value_has_member(n, {global: "Symbol", class: "value", path: [name]}, null) == false || {
478
+ value key: ts_value_symbol_key(n, name)
479
+ value arms: if key == null then null else value_lookup(n, key).alternatives
480
+ value arms != null && count(arms) > 0 && all(arms, arm => arm.matched == false)
481
+ }
468
482
  in ts_for_of rule es-await-thenable:for-of {
469
483
  report at { start: offset, end: last(overlapping(ts_close_paren, right.end_offset, body.offset)).end_offset }
470
- when contains([] modifier, "await")
471
- && value_has_member(right.node, {global: "Symbol", class: "value", path: ["asyncIterator"]}, null) == false
484
+ when contains([] modifier, "await") && typed_lacks_symbol(right.node, "asyncIterator")
472
485
  warn "Unexpected `for await...of` of a value that is not async iterable."
473
486
  }
474
487
  in ts_declarator.init rule es-await-thenable:using {
475
- when starts_with(parent.parent.kind.text, "await")
476
- && value_has_member(node, {global: "Symbol", class: "value", path: ["asyncDispose"]}, null) == false
488
+ when starts_with(parent.parent.kind.text, "await") && typed_lacks_symbol(node, "asyncDispose")
477
489
  warn "Unexpected `await using` of a value that is not async disposable."
478
490
  }
479
491
 
@@ -100,7 +100,8 @@ in ts_enum_member {
100
100
  in ts_qualified_name {
101
101
  arguments parent.type_argument
102
102
  let ts_value_qualified_key ts_value_key(right.node)
103
- defines (scope module, namespace from left.definition).[ts_value_qualified_key] ?? (scope statics from left.definition).[ts_value_qualified_key]
103
+ # `import Alias = ns.E; Alias.Zero`: the alias stands for what it defines.
104
+ defines (scope module, namespace from left.definition).[ts_value_qualified_key] ?? (scope statics from left.definition).[ts_value_qualified_key] ?? (scope module, namespace from left.definition.definition).[ts_value_qualified_key] ?? (scope statics from left.definition.definition).[ts_value_qualified_key]
104
105
  unwraps definition
105
106
  }
106
107