@inthedark/dxlint 0.1.13 → 0.1.15

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 (61) hide show
  1. package/README.md +16 -9
  2. package/bin/dxlint-darwin-arm64 +0 -0
  3. package/bin/dxlint-linux-x64 +0 -0
  4. package/bin/dxlint-win32-x64.exe +0 -0
  5. package/esbuild.mjs +106 -0
  6. package/package.json +20 -2
  7. package/std/ts/calls.dx +9 -7
  8. package/std/ts/conditions.dx +2 -0
  9. package/std/ts/constants.dx +2 -1
  10. package/std/ts/content-mapper.dx +48 -0
  11. package/std/ts/deprecation.dx +12 -5
  12. package/std/ts/effects.dx +8 -1
  13. package/std/ts/format.dx +1 -1
  14. package/std/ts/grammar.dx +23 -13
  15. package/std/ts/import-no-deprecated.dx +2 -0
  16. package/std/ts/javascript.dx +496 -0
  17. package/std/ts/layout/literals.dx +14 -7
  18. package/std/ts/layout/separators.dx +1 -1
  19. package/std/ts/layout/shared.dx +36 -37
  20. package/std/ts/layout/types.dx +4 -0
  21. package/std/ts/literals.dx +1 -0
  22. package/std/ts/methods.dx +3 -0
  23. package/std/ts/modules.dx +8 -0
  24. package/std/ts/names.dx +25 -13
  25. package/std/ts/problems/control-flow.dx +1 -0
  26. package/std/ts/problems/numeric-precision.dx +1 -0
  27. package/std/ts/problems/regexp.dx +1 -0
  28. package/std/ts/regex/checks.dx +1 -0
  29. package/std/ts/regex/findings.dx +10 -6
  30. package/std/ts/regex/values.dx +4 -15
  31. package/std/ts/restrictions.dx +76 -0
  32. package/std/ts/scoping/member-uses.dx +5 -3
  33. package/std/ts/scoping/shadowing.dx +1 -0
  34. package/std/ts/scoping/usage.dx +2 -2
  35. package/std/ts/spreads.dx +1 -1
  36. package/std/ts/structure/padding.dx +10 -8
  37. package/std/ts/style/expressions.dx +8 -4
  38. package/std/ts/style/returns.dx +3 -1
  39. package/std/ts/style/signatures.dx +5 -0
  40. package/std/ts/style/type-exports.dx +1 -0
  41. package/std/ts/suggestions/builtins.dx +1 -1
  42. package/std/ts/suggestions/comparisons.dx +1 -0
  43. package/std/ts/suggestions/expressions.dx +1 -0
  44. package/std/ts/suggestions/object-spread.dx +1 -1
  45. package/std/ts/tokens.dx +11 -5
  46. package/std/ts/typed/awaitability.dx +1 -0
  47. package/std/ts/typed/floating-promises.dx +45 -14
  48. package/std/ts/typed/includes.dx +3 -4
  49. package/std/ts/typed/promises.dx +10 -10
  50. package/std/ts/typed/string-boundaries.dx +1 -0
  51. package/std/ts/typed/switches.dx +1 -0
  52. package/std/ts/typed/type-arguments.dx +11 -19
  53. package/std/ts/typed/type-conversions.dx +1 -0
  54. package/std/ts/typed/type-parameters.dx +1 -1
  55. package/std/ts/typed/using-disposable.dx +90 -0
  56. package/std/ts/typed.dx +1 -0
  57. package/std/ts/values/arguments.dx +1 -1
  58. package/std/ts/values/enums.dx +3 -0
  59. package/std/ts/values/inspection.dx +1 -4
  60. package/std/ts/values.dx +41 -26
  61. package/std/ts.dx +10 -4
@@ -116,6 +116,8 @@ in ts_open_angle, ts_close_angle let layout_generic_edge parent is layout_generi
116
116
  when layout_generic_edge
117
117
  }
118
118
 
119
+ # A function declaration, never an expression (an arrow's body is one).
120
+ fn layout_function_declaration(n) = n is ts_function && n is (ts_node.body, ts_node.declaration, ts_if.consequent, ts_if.alternate) && n.parent not ts_arrow
119
121
  in ts_open_angle rule es-type-generic-spacing {
120
122
  @layout_generic_policy
121
123
  when parent not (ts_arrow, ts_function_type, ts_call_signature)
@@ -123,6 +125,8 @@ in ts_open_angle rule es-type-generic-spacing {
123
125
  when previous_token != null
124
126
  let generic_space layout_comma_space(space_before.text, true)
125
127
  when before != (generic_space != "")
128
+ # Without `before`, a function declaration may space its `<` either way.
129
+ when before || !layout_function_declaration(parent.node)
126
130
  report at { start: offset - byte_len(generic_space), end: offset }
127
131
  fix at { start: offset - byte_len(generic_space), end: offset } as if before then " " else ""
128
132
  warn "Generic spaces mismatch"
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  import .ts/names
2
3
  import .ts/import-attributes
3
4
 
package/std/ts/methods.dx CHANGED
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # String and collection method policies.
2
3
  import .ts/constants
3
4
 
@@ -18,6 +19,8 @@ fn collection_value(n) =
18
19
  fn collection_static_value(n) = { value n; collection_value_node; collection_value }
19
20
  fn collection_method(n, name) = n is ts_member && n.property == name || n is ts_index && collection_static_value(n.index.node).type == "string" && collection_static_value(n.index.node).value == name
20
21
 
22
+ in ts_call let collection_test_call count(argument) == 1 && collection_method(unparenthesised_node(callee.node), "test")
23
+
21
24
  fn collection_arrayish(n) = {
22
25
  value arms: value_parts(n)
23
26
  value parts: map(arms ?? [], arm => filter(arm.parts, part => !contains(part.traits, "parameter")
package/std/ts/modules.dx CHANGED
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  import .ts/comments
2
3
 
3
4
  @macro ts_module_profile(profile, order, condition) {
@@ -22,6 +23,13 @@ import .ts/comments
22
23
  ".cjs" [".cts", ".d.cts"]
23
24
  }
24
25
  candidates ["{{dir}}/{{name}}{{ext}}", "{{dir}}/{{name}}/index{{ext}}"]
26
+ references {
27
+ ts_import ["from"]
28
+ ts_export ["from"]
29
+ ts_import_equals ["value.argument"]
30
+ ts_import_call ["argument"]
31
+ ts_import_type ["argument"]
32
+ }
25
33
  packages {
26
34
  directories ["node_modules"]
27
35
  metadata "package.json"
package/std/ts/names.dx CHANGED
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # .ts/names: JavaScript and TypeScript scopes, declarations and references.
2
3
  import .ts/directives
3
4
  import .ts/effects
@@ -85,9 +86,9 @@ in ts_rest { targets argument }
85
86
  in ts_function, ts_arrow, ts_method, ts_function_type, ts_method_signature, ts_call_signature {
86
87
  declares value, query parameter in nearest function hoisted
87
88
  }
88
- # Other files see these bodies only by entering them: the declared return
89
- # type (or a constructor's or setter's role) stands for what they do, so an
90
- # edit inside one concerns only the files that stepped in.
89
+ # Other files never read inside these bodies: the declared return type (or a
90
+ # constructor's or setter's role) stands for what they do. An edit inside one
91
+ # concerns nobody else, and other files' view of this file leaves it empty.
91
92
  in ts_function, ts_arrow { opaque body when return_type != null }
92
93
  in ts_method { opaque body when return_type != null || kind == "constructor" || kind == "set" }
93
94
  in ts_parameter { targets name }
@@ -138,22 +139,24 @@ in ts_decorator let ts_name_decorator_method when {
138
139
  }
139
140
  in ts_identifier {
140
141
  let ts_name_outside_method containing(ts_decorator).ts_name_decorator_method
141
- when !(is (ts_member.property, ts_method.key, ts_class_property.key, ts_property_signature.key, ts_method_signature.key,
142
+ let ts_name_resolution if is (ts_member.property, ts_method.key, ts_class_property.key, ts_property_signature.key, ts_method_signature.key,
142
143
  ts_enum_member.name, ts_pattern_property.key, ts_import_specifier.imported, ts_export_specifier.local,
143
144
  ts_export_specifier.exported, ts_labeled.label, ts_break.label, ts_continue.label, ts_jsx_attribute.name,
144
145
  ts_tuple_member.name, ts_qualified_name.right, ts_type_parameter.name, ts_type_alias.name,
145
146
  ts_interface.name, ts_import.default_import, ts_import.namespace, ts_export.namespace, ts_import_specifier.local, ts_import_equals.name)
146
147
  || is ts_property.key && parent.value != null && !parent.flow_shorthand_default
147
148
  # `[key: string]: T` names nothing.
148
- || is ts_parameter.name && parent is ts_index_signature.parameter) {
149
- when ts_name_outside_method != null { resolves value outside ts_name_outside_method }
150
- when ts_name_outside_method == null && !ts_name_type_key { resolves value }
149
+ || is ts_parameter.name && parent is ts_index_signature.parameter then null
150
+ else if ts_name_outside_method != null then "outside"
151
151
  # A computed key inside type syntax reads like `typeof`: type-only
152
152
  # imports count.
153
- when ts_name_outside_method == null && ts_name_type_key { resolves query }
154
- }
153
+ else if ts_name_type_key then "query"
154
+ else "value"
155
+ when ts_name_resolution == "outside" { resolves value outside ts_name_outside_method }
156
+ when ts_name_resolution == "value" { resolves value }
157
+ when ts_name_resolution == "query" { resolves query }
155
158
  }
156
- in ts_identifier let ts_name_type_key name_type_access && containing(ts_computed_key) != null
159
+ in ts_identifier let ts_name_type_key containing(ts_computed_key) != null && name_type_access
157
160
  in ts_type_reference { resolves type name }
158
161
  # `export { x }` uses `x`; `export { x } from "m"` does not.
159
162
  in ts_export_specifier { when parent.from == null { resolves value, type local } }
@@ -178,6 +181,13 @@ group name_type_syntax ts_type_reference, ts_type_keyword, ts_type_parameter, ts
178
181
  ts_type_query, ts_import_type, ts_literal_type, ts_template_type, ts_type_predicate, ts_mapped_type
179
182
  in ts_node let name_type_access is name_type_syntax || parent.name_type_access == true
180
183
  in ts_node let name_write_owner when {
184
+ # Most nodes sit in no field that can write.
185
+ !(is (ts_for_in.left, ts_for_of.left, ts_declarator.name, ts_assignment.left, ts_update.argument, ts_unary.argument,
186
+ ts_array.element, ts_array_pattern.element, ts_object.property, ts_object_pattern.property,
187
+ ts_object_pattern.rest, ts_rest.argument, ts_spread.argument, ts_assignment_pattern.left,
188
+ ts_parenthesized.expression, ts_as.expression, ts_satisfies.expression, ts_non_null.expression,
189
+ ts_type_assertion.expression, ts_instantiation.expression, ts_property.value, ts_pattern_property.value,
190
+ ts_property.key, ts_pattern_property.key)) then null
181
191
  is (ts_for_in.left, ts_for_of.left) then parent.node
182
192
  is ts_declarator.name then when {
183
193
  parent.init != null then parent.node
@@ -272,7 +282,7 @@ in ts_node let reference_kind null
272
282
  in ts_identifier let declaration ts_name_binding.node
273
283
  in ts_identifier let declarations ts_name_binding.declarations ?? []
274
284
  in ts_identifier let references
275
- if ts_name_declared && ts_name_binding.node == node then ts_name_binding.references else []
285
+ if ts_name_declared && ts_name_binding.node == node then name_references(node) else []
276
286
  in ts_identifier let scope when {
277
287
  ts_name_declared then first(name_scopes(node, "declaration")).owner
278
288
  ts_name_referenced then {
@@ -310,8 +320,10 @@ fn ts_name_self_owner(n) = when {
310
320
  }
311
321
  in ts_identifier let ts_name_usage_references {
312
322
  value binding: if ts_name_declared then ts_name_binding else null
313
- value refs: filter(binding.references, reference => reference is ts_identifier)
314
- value if binding == null || binding.node != node || any(refs, reference => reference.access == null) then null else refs
323
+ value if binding == null || binding.node != node then null else {
324
+ value refs: filter(name_references(node), reference => reference is ts_identifier)
325
+ value if any(refs, reference => reference.access == null) then null else refs
326
+ }
315
327
  }
316
328
  in ts_node {
317
329
  let uses null
@@ -82,6 +82,7 @@ fn problem_duplicate_condition(branch) = {
82
82
  && any(problem_condition_parts(condition, "&&"), part => problem_condition_covered(part, previous))
83
83
  }
84
84
  in ts_if.test rule es-no-dupe-else-if {
85
+ when parent.parent is ts_if && parent.parent.alternate.node == parent.node
85
86
  when problem_duplicate_condition(parent.node)
86
87
  report at unparenthesised_node(node)
87
88
  warn "This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain."
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  define {
2
3
 
3
4
  fn problem_number_significand(text, as_float) = {
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # Regular expression validation rules.
2
3
 
3
4
  define {
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  import .ts/constants
2
3
 
3
4
  define {
@@ -186,17 +186,21 @@ fn regexp_checked_by_call(n) = {
186
186
  value first: first(arguments)
187
187
  value n is ts_regex && owner is (ts_call, ts_new) && first == n && len(arguments) > 1 && regexp_input(owner) != null
188
188
  }
189
- in regexp_site let regexp_control_findings if regexp_tree.ok
189
+ # What a literal's own spelling rules out, before anything parses it.
190
+ in regexp_site let regexp_may_hold_control node not ts_regex || re_test(text, /\\[xuc0-9]|[\u{0}-\u{1f}]/)
191
+ in regexp_site let regexp_may_hold_spaces node not ts_regex || contains(text, " ")
192
+ in regexp_site let regexp_may_mislead node not ts_regex || contains(text, "[") && re_test(text, /[^\u{0}-\u{7f}]|\\[uxc0-9]/)
193
+ in regexp_site let regexp_control_findings if regexp_may_hold_control && regexp_tree.ok
190
194
  then regexp_controls(regexp_nodes, regexp_input) else []
191
- in regexp_site let regexp_empty_class_findings if node is ts_regex && regexp_tree.ok && regexp_error == null
195
+ in regexp_site let regexp_empty_class_findings if node is ts_regex && contains(text, "[]") && regexp_tree.ok && regexp_error == null
192
196
  then regexp_empty_classes(regexp_view_nodes, regexp_input) else []
193
- in regexp_site let regexp_space_findings if regexp_tree.ok && regexp_error == null
197
+ in regexp_site let regexp_space_findings if regexp_may_hold_spaces && regexp_tree.ok && regexp_error == null
194
198
  then regexp_spaces(regexp_view_nodes, regexp_input) else []
195
- in regexp_site let regexp_backreference_findings if re_test(regexp_input.text, /\\(?:[1-9]|k)/)
199
+ in regexp_site let regexp_backreference_findings if re_test(if node is ts_regex then text else regexp_input.text, /\\(?:[1-9]|k)/)
196
200
  && regexp_tree.ok && regexp_error == null then regexp_backreferences(regexp_nodes, regexp_input) else []
197
- in regexp_site let regexp_misleading_findings if regexp_tree.ok && regexp_error == null && !regexp_checked_by_call(node)
201
+ in regexp_site let regexp_misleading_findings if regexp_may_mislead && regexp_tree.ok && regexp_error == null && !regexp_checked_by_call(node)
198
202
  then regexp_misleading(regexp_view_nodes, regexp_input, false) else []
199
- in regexp_site let regexp_misleading_escaped_findings if regexp_tree.ok && regexp_error == null && !regexp_checked_by_call(node)
203
+ in regexp_site let regexp_misleading_escaped_findings if regexp_may_mislead && regexp_tree.ok && regexp_error == null && !regexp_checked_by_call(node)
200
204
  then regexp_misleading(regexp_view_nodes, regexp_input, true) else []
201
205
  in regexp_site let regexp_findings if regexp_tree.ok != true then null else {
202
206
  value flat([
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  import .ts/expression-fixes
2
3
  import .ts/constants
3
4
  import .ts/suggestions/globals
@@ -35,21 +36,9 @@ fn regexp_literal_input(n, override_flags) = {
35
36
  value flags: if override_flags == null then written_flags else override_flags.value
36
37
  value end: byte_len(n.text) - byte_len(written_flags) - 1
37
38
  value pattern: byte_slice(n.text, 1, end)
38
- value characters: split(pattern, "")
39
- value decoded: js_string_parts(json_string(pattern))
40
- value positions: regexp_positions(characters, n.offset + 1)
41
- value spans: map(positions, position => {
42
- offset: position.offset, end_offset: position.offset + byte_len(get(characters, position.index))
43
- })
44
- value units: flat(map(positions, position => {
45
- value span: get(spans, position.index)
46
- value count: if span.end_offset - span.offset == 4 then 2 else 1
47
- value map(slice(decoded.units, position.unit, position.unit + count), unit => {
48
- value: unit.value, offset: span.offset, end_offset: span.end_offset
49
- })
50
- }))
39
+ value mapped: text_units(pattern, n.offset + 1)
51
40
  value {
52
- text: pattern, source: pattern, flags: flags, spans: spans, units: units,
41
+ text: pattern, source: pattern, flags: flags, spans: mapped.spans, units: mapped.units,
53
42
  offset: n.offset + 1, end_offset: n.offset + end, written: "regex", pattern_node: n
54
43
  }
55
44
  }
@@ -91,7 +80,7 @@ fn regexp_initialised_binding(n) = {
91
80
  in ts_program let regexp_mentioned contains(source, "RegExp")
92
81
  in ts_call, ts_new let regexp_candidate {
93
82
  value callee: unparenthesised_node(node.callee.node)
94
- value callee is ts_identifier && (callee.text == "RegExp" || ts_module_program(node).regexp_mentioned && regexp_initialised_binding(callee))
83
+ value callee is ts_identifier && (callee.text == "RegExp" || ts_program.regexp_mentioned && regexp_initialised_binding(callee))
95
84
  || callee is (ts_member, ts_index) && ts_global_method_name(callee) == "RegExp"
96
85
  || callee is (ts_non_null, ts_as, ts_satisfies, ts_type_assertion, ts_conditional, ts_logical)
97
86
  }
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # .ts/restrictions: eslint core rules that limit or forbid things by
2
3
  # configuration: sizes (max-*, complexity), restricted names, imports,
3
4
  # properties and syntax, identifier shapes, sort orders, comment and
@@ -5,9 +6,84 @@
5
6
 
6
7
  import .ts/restrictions/comments
7
8
  import .ts/helpers
9
+ import .ts/modules
8
10
 
9
11
  define {
10
12
 
13
+ in ts_program rule es-module-only-used-from {
14
+ default option from string[] = [] "the files that alone may use a module this way: globs, relative to the config"
15
+ option ignore string[] = [] "files whose imports don't count: globs, relative to the config"
16
+ when len(from) > 0
17
+ let use module_importers(node, from, ignore)
18
+ when use.reaches_stop && !use.reaches_entry && !use.is_stop
19
+ report at { start: 0, end: 1 }
20
+ warn "Only {join(from, ', ')} use this module: move it there."
21
+ }
22
+
23
+ # One name's sites (overloads, merges) decide together, and report once.
24
+ fn export_usage_site(owner, name, at, before) = { owner: owner, name: name, at: at, before: before }
25
+ fn export_usage_tagged(text, tags) = text != null && any(tags, tag => re_test(text, "(?:^|[\\s*/])" re_escape(tag) "(?:$|[^\\w-])"))
26
+ fn export_usage_bound(declarator) = if declarator.name.node is ts_identifier then [declarator.name.node] else {
27
+ value [declarator.name.node]
28
+ walk (current) current.children.nodes
29
+ filter (item) item is ts_identifier && item.declaration_kind != null
30
+ }
31
+ in ts_export_specifier let export_usage_before byte_slice(source, previous.end_offset ?? parent.offset, offset)
32
+ fn export_usage_statement_sites(statement, source_text) = {
33
+ # From the code before it: comments are children only under some configs.
34
+ value lead: token(statement, "before")
35
+ value before: byte_slice(source_text, if lead == null then 0 else lead.end, statement.offset)
36
+ value when {
37
+ statement is ts_export_assignment then [export_usage_site(statement, "=", statement, before)]
38
+ statement.kind == "default" then [export_usage_site(statement, "default", statement.declaration.name.node ?? statement, before)]
39
+ statement.operator == "*" then if statement.namespace == null then [] else [export_usage_site(statement, ts_module_name(statement.namespace.node), statement.namespace.node, before)]
40
+ count(statement.specifier) > 0 then map(statement.specifier.nodes, item => export_usage_site(item, ts_module_name(item.exported.node ?? item.local.node), item.exported.node ?? item.local.node, before item.export_usage_before))
41
+ statement.declaration.node is ts_variable_declaration then flat_map(statement.declaration.declaration.nodes, declarator => map(export_usage_bound(declarator), bound => export_usage_site(declarator, bound.text, bound, before)))
42
+ statement.declaration != null then [export_usage_site(statement, "{statement.declaration.name}", statement.declaration.name.node, before)]
43
+ else then []
44
+ }
45
+ }
46
+ in ts_program let export_usage_sites {
47
+ value body.nodes
48
+ filter (statement) statement is (ts_export, ts_export_assignment)
49
+ flat_map (statement) export_usage_statement_sites(statement, source)
50
+ }
51
+ fn export_usage_reports(sites, names, tags) = {
52
+ value sites
53
+ reported: filter (site) contains(names, site.name)
54
+ && find(sites, other => other.name == site.name).owner == site.owner
55
+ && !any(sites, other => other.name == site.name && export_usage_tagged(other.before, tags))
56
+ unique_by (site) site.owner
57
+ map (site) { at: site.at, names: map(filter(reported, other => other.owner == site.owner), other => other.name) }
58
+ }
59
+ @macro export_usage_options {
60
+ option ignore string[] = [] "files whose imports don't count: globs, relative to the config"
61
+ option keepTags string[] = [] "comment tags before an export that keep it, such as @public"
62
+ }
63
+ # Per program, not per export: the graph answers with the whole file's table.
64
+ in ts_program rule es-no-unused-exports {
65
+ @export_usage_options
66
+ let use module_export_users(node, [], ignore)
67
+ when use != null && !use.is_entry && !use.is_ignored
68
+ let unused { value use.exports; filter (row) count(row.importers) == 0; map (row) row.name }
69
+ each found in export_usage_reports(export_usage_sites, unused, keepTags) {
70
+ report at found.at
71
+ warn "Nothing imports this export: {join(found.names, ', ')}."
72
+ }
73
+ }
74
+ in ts_program rule es-export-only-used-from {
75
+ default option from string[] = [] "the files that alone may use an export this way: globs, relative to the config"
76
+ @export_usage_options
77
+ when len(from) > 0
78
+ let use module_export_users(node, from, ignore)
79
+ when use != null && !use.is_stop && !use.is_ignored
80
+ let only { value use.exports; filter (row) row.reaches_stop && !row.reaches_entry; map (row) row.name }
81
+ each found in export_usage_reports(export_usage_sites, only, keepTags) {
82
+ report at found.at
83
+ warn "Only {join(from, ', ')} use this export: {join(found.names, ', ')}. Move it there."
84
+ }
85
+ }
86
+
11
87
  schema restriction_import_path {
12
88
  name: string
13
89
  message?: string
@@ -2,6 +2,7 @@ define {
2
2
 
3
3
  group member_usage_access ts_member, ts_index, ts_pattern_property, ts_property
4
4
  in ts_class let member_usage_accesses descendants(member_usage_access)
5
+ in ts_class let member_usage_access_groups { value member_usage_accesses; filter (access) access.member_usage_name != null; group_by (access) access.member_usage_name }
5
6
  fn member_usage_class(n) = find(walk(n, item => item.parent.node), item => item is ts_class)
6
7
  fn member_usage_key(n) = if n is ts_parameter then n.name.node else n.key.node
7
8
  in ts_class let member_usage_members {
@@ -64,7 +65,7 @@ fn member_usage_receiver(access) = {
64
65
  else if named != null then { class: named, static: true }
65
66
  else if declaration is ts_declarator then
66
67
  if declaration.init is ts_this
67
- && all(binding.references, reference => reference.name_write_owner == null || reference.name_write_owner == declaration)
68
+ && all(name_references(object), reference => reference.name_write_owner == null || reference.name_write_owner == declaration)
68
69
  then member_usage_this(access) else null
69
70
  else if declaration is ts_parameter then member_usage_annotation(declaration.type.node, access)
70
71
  else null
@@ -80,8 +81,9 @@ fn member_usage_used(declaration) = {
80
81
  value class: member_usage_class(declaration)
81
82
  value accessor: declaration is ts_method && contains(["get", "set"], declaration.kind)
82
83
  || contains([] declaration.modifier, "accessor")
83
- value any(class.member_usage_accesses, access =>
84
- access.member_usage_name == name && (accessor || access.member_usage_read)
84
+ value named: find(class.member_usage_access_groups, group => group.key == name).items ?? []
85
+ value any(named, access =>
86
+ (accessor || access.member_usage_read)
85
87
  && (if access is ts_member && access.property is ts_private_name
86
88
  then member_usage_hash_owner(access) == class
87
89
  else {
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  define {
2
3
 
3
4
  fn scoping_typed(program) = re_test(program.file, /\.(ts|tsx|mts|cts)$/)
@@ -204,7 +204,7 @@ fn ts_usage_before_parameter(n, typed, declaration_file) = {
204
204
  slice (index + 1)
205
205
  any (later) {
206
206
  value binding: name_binding(later)
207
- value binding != null && (count(binding.references) > 0
207
+ value binding != null && (count(name_references(later)) > 0
208
208
  || any(binding.declarations, site => ts_usage_initialized(site) || ts_usage_marked(site, typed, declaration_file)))
209
209
  }
210
210
  }
@@ -269,7 +269,7 @@ fn ts_usage_summary(n) = if !n.scoping_lexical_declaration || n.parent is ts_qua
269
269
  value site: ts_usage_site(n)
270
270
  value typed: n.ts_usage_typed
271
271
  value declaration_file: n.ts_usage_declaration_file
272
- value real: map(binding.references, reference => ts_usage_reference(reference))
272
+ value real: map(name_references(n), reference => ts_usage_reference(reference))
273
273
  value initial: {
274
274
  value sites
275
275
  filter (site) ts_usage_initialized(site)
package/std/ts/spreads.dx CHANGED
@@ -14,7 +14,7 @@ fn spread_written_properties(n) = {
14
14
  value binding: name_binding(n)
15
15
  value if binding == null then if n is (ts_function, ts_arrow) then false else null
16
16
  else if spread_declared_contract(n) then false
17
- else any(binding.references, reference => reference.parent is (ts_member, ts_index)
17
+ else any(name_references(n), reference => reference.parent is (ts_member, ts_index)
18
18
  && reference.parent.object.node == reference && reference.parent.write_site != null)
19
19
  }
20
20
  fn spread_class_value(n, arms) = {
@@ -169,7 +169,8 @@ in ts_node let structure_padding_categories structure_padding_unlabel.nonempty ?
169
169
  in ts_node let structure_padding_multiline position(offset).js_line != position(end_offset).js_line
170
170
  fn structure_padding_receiver(n) = n.structure_padding_receiver.nonempty ?? n.structure_unwrapped
171
171
  fn structure_padding_is_require(n) = n.callee is ts_identifier && n.callee.text == "require"
172
- fn structure_padding_blank(text) = { value text; replace "\r\n" "\n"; re_test /[\n\r\u2028\u2029]\s*[\n\r\u2028\u2029]/ }
172
+ fn structure_padding_blank(text) = { value if contains(text, "\r") then replace(text, "\r\n", "\n") else text; re_test /[\n\r\u2028\u2029]\s*[\n\r\u2028\u2029]/ }
173
+ fn padding_plainly_blank(gap) = !contains(gap, "/") && structure_padding_blank(gap)
173
174
  fn structure_padding_gap_end(previous, next, source) = {
174
175
  value end: if previous is ts_method_signature then previous.member_terminal_token.end else previous.end_offset
175
176
  value semicolon: if previous is ts_method_signature then previous.member_terminal_token.text == ";" else ends_with(previous.text, ";")
@@ -222,25 +223,26 @@ in structure_padding_owner rule es-padding-line-between-statements {
222
223
  option options { blankLine: "any" | "always" | "never"; prev: string | { selector: string; lineMode?: "any" | "singleline" | "multiline" } | (string | { selector: string; lineMode?: "any" | "singleline" | "multiline" })[]; next: string | { selector: string; lineMode?: "any" | "singleline" | "multiline" } | (string | { selector: string; lineMode?: "any" | "singleline" | "multiline" })[] }[] = [] "ordered statement padding policies"
223
224
  option typescriptBodies boolean = true "also pad namespace bodies, interfaces and type literals (false: only the statement lists ESLint core checks)"
224
225
  when any(options, policy => policy.blankLine != "any")
226
+ when count(structure_padding_items) > 1
225
227
  when typescriptBodies || !(node is (ts_interface, ts_type_literal) || node is ts_block && parent is ts_namespace)
226
228
  let prepared_options map(options, config => {
227
229
  blankLine: config.blankLine,
228
230
  prev: padding_prepare_selectors(config.prev),
229
231
  next: padding_prepare_selectors(config.next),
230
232
  })
233
+ let only_required all(options, policy => policy.blankLine != "never")
231
234
  let policies {
232
235
  value zip(structure_padding_items, slice(structure_padding_items, 1))
236
+ map (pair) { prior: first(pair), following: last(pair), end: structure_padding_gap_end(first(pair), last(pair), source) }
237
+ # A blank line already there breaks no policy unless one forbids it.
238
+ filter (pair) !only_required || !padding_plainly_blank(byte_slice(source, pair.end, pair.following.offset))
233
239
  map (pair) {
234
- prior: first(pair),
235
- following: last(pair),
240
+ end: pair.end,
241
+ start: pair.following.offset,
236
242
  policy: find_last(prepared_options, config =>
237
- padding_side_matches(first(pair), config.prev) && padding_side_matches(last(pair), config.next)).blankLine ?? "any",
243
+ padding_side_matches(pair.following, config.next) && padding_side_matches(pair.prior, config.prev)).blankLine ?? "any",
238
244
  }
239
245
  filter (pair) pair.policy != "any"
240
- map (pair) {
241
- end: structure_padding_gap_end(pair.prior, pair.following, source),
242
- start: pair.following.offset, policy: pair.policy,
243
- }
244
246
  filter (pair) pair.end <= pair.start
245
247
  && (pair.policy != "never" || structure_padding_blank(byte_slice(source, pair.end, pair.start)))
246
248
  }
@@ -95,20 +95,23 @@ in ts_call rule es-no-var-requires {
95
95
  warn "Require statement not part of import statement."
96
96
  }
97
97
 
98
- in ts_array, ts_arrow, ts_binary, ts_class, ts_function, ts_identifier, ts_member, ts_index, ts_meta_property, ts_object, ts_sequence, ts_template, ts_this, ts_number, ts_string, ts_boolean, ts_bigint, ts_null, ts_regex let statement_effect false
98
+ in ts_array, ts_arrow, ts_binary, ts_class, ts_function, ts_identifier, ts_member, ts_index, ts_meta_property, ts_object, ts_template, ts_this, ts_number, ts_string, ts_boolean, ts_bigint, ts_null, ts_regex let statement_effect false
99
99
  in ts_unary let statement_effect contains(["delete", "void"], operator)
100
100
  in ts_logical let statement_effect { kind: "logical", right: right.statement_effect }
101
101
  in ts_conditional let statement_effect { kind: "conditional", left: consequent.statement_effect, right: alternate.statement_effect }
102
+ in ts_sequence let statement_effect { kind: "sequence", items: [] expression.statement_effect }
102
103
  in ts_tagged_template let statement_effect { kind: "tagged" }
103
104
  in ts_jsx_element, ts_jsx_fragment let statement_effect { kind: "jsx" }
104
105
  in ts_parenthesized, ts_as, ts_type_assertion, ts_non_null, ts_instantiation forward statement_effect from expression
105
- fn expression_allowed(shape, short, ternary, tagged, jsx) = {
106
+ fn expression_allowed(shape, short, ternary, tagged, jsx, sequences) = {
106
107
  value shape
107
108
  walk (part) if part.kind == "logical" && short then part.right
108
- else if part.kind == "conditional" && ternary then [part.left, part.right] else []
109
+ else if part.kind == "conditional" && ternary then [part.left, part.right]
110
+ else if part.kind == "sequence" && sequences then part.items else []
109
111
  all (part) if type_of(part) == "bool" then part
110
112
  else if part.kind == "logical" then short
111
113
  else if part.kind == "conditional" then ternary
114
+ else if part.kind == "sequence" then sequences
112
115
  else if part.kind == "tagged" then tagged
113
116
  else if part.kind == "jsx" then !jsx else true
114
117
  }
@@ -116,9 +119,10 @@ in ts_expression_statement rule es-no-unused-expressions {
116
119
  option allowShortCircuit boolean = false "Allow effectful short circuit expressions."
117
120
  option allowTernary boolean = false "Allow ternaries with effectful branches."
118
121
  option allowTaggedTemplates boolean = false "Allow tagged templates."
122
+ option allowSequences boolean = false "Allow comma sequences whose every item has an effect."
119
123
  option enforceForJSX boolean = false "Report unused JSX expressions."
120
124
  option ignoreDirectives boolean = false "Ignore directive prologues."
121
- when !(directive_statement && (ecma_version >= 5 || ignoreDirectives)) && !expression_allowed(expression.statement_effect, allowShortCircuit, allowTernary, allowTaggedTemplates, enforceForJSX)
125
+ when !(directive_statement && (ecma_version >= 5 || ignoreDirectives)) && !expression_allowed(expression.statement_effect, allowShortCircuit, allowTernary, allowTaggedTemplates, enforceForJSX, allowSequences)
122
126
  warn "Expected an assignment or function call and instead saw an expression."
123
127
  }
124
128
 
@@ -68,6 +68,7 @@ in ts_function, ts_arrow, ts_method let explicit_return_shape {
68
68
  option allowIIFEs boolean = false "Allow functions directly within calls."
69
69
  option allowTypedFunctionExpressions boolean = true "Allow contextually typed function expressions."
70
70
  when ts_program.typescript_source
71
+ when !return_annotation && return_method_valid != true
71
72
  let shape explicit_return_shape
72
73
  when !shape.valid && !contains(allowedNames, shape.name)
73
74
  when !(allowFunctionsWithoutTypeParameters && count(type_parameter) == 0 || allowIIFEs && shape.iife || allowHigherOrderFunctions && shape.higher_order || allowConciseArrowFunctionExpressionsStartingWithVoid && shape.void_arrow)
@@ -135,11 +136,12 @@ in ts_parameter let boundary_argument {
135
136
  option allowTypedFunctionExpressions boolean = true "Allow contextually typed functions."
136
137
  option allowOverloadFunctions boolean = false "Allow implementations with overload signatures."
137
138
  when ts_program.typescript_source
139
+ when if parameter_check then boundary_argument.eligible else !return_annotation && return_method_valid != true
138
140
  let boundary_shape if parameter_check then parent.explicit_return_shape else explicit_return_shape
139
141
  let boundary_name if parameter_check then parent.boundary_function_name else boundary_function_name
140
142
  let empty_method if parameter_check then parent.boundary_empty_method else boundary_empty_method
141
143
  let overload if parameter_check then parent.boundary_has_overload else boundary_has_overload
142
- when if parameter_check then parent.boundary_function_reachable && boundary_argument.eligible else boundary_function_reachable
144
+ when if parameter_check then parent.boundary_function_reachable else boundary_function_reachable
143
145
  when empty_method == true || !(contains(allowedNames, boundary_name) || boundary_shape.ancestor_typed || allowTypedFunctionExpressions && boundary_shape.context_typed || allowOverloadFunctions && overload)
144
146
  when if parameter_check then !(allowArgumentsExplicitlyTypedAsAny && boundary_argument.any) else !boundary_shape.valid && !(allowHigherOrderFunctions && boundary_shape.higher_order || allowDirectConstAssertionInArrowFunctions && boundary_shape.const_assertion)
145
147
  report at if parameter_check then { start: boundary_argument.offset, end: boundary_argument.end } else boundary_report_site
@@ -14,6 +14,10 @@ in ts_call_signature let overload_identity { name: if kind == "new" then "new" e
14
14
  in ts_export forward overload_identity from declaration
15
15
  in ts_function, ts_method, ts_method_signature, ts_call_signature, ts_export let overload_entry { identity: overload_identity, start: offset }
16
16
  in ts_program, ts_block, ts_class, ts_interface, ts_type_literal let overload_entries [] children.overload_entry
17
+ in ts_program, ts_block, ts_class, ts_interface, ts_type_literal let overload_repeated_names {
18
+ value names: map(overload_entries, entry => entry.identity.name)
19
+ value filter(names, (name, index) => name != null && index_of(names, name) != index)
20
+ }
17
21
  fn earlier_overload(entries, identity, start) = any(entries, e => e.start < start && e.identity == identity)
18
22
  fn overload_report_site(n) = {
19
23
  value terminator: n.token_neighbours.after
@@ -23,6 +27,7 @@ fn overload_report_site(n) = {
23
27
  }
24
28
  in ts_function, ts_method, ts_method_signature, ts_call_signature, ts_export rule es-adjacent-overload-signatures {
25
29
  when parent is (ts_program, ts_block, ts_class, ts_interface, ts_type_literal)
30
+ when count(parent.overload_repeated_names) > 0 && contains(parent.overload_repeated_names, overload_identity.name)
26
31
  when overload_identity != null && overload_identity != previous.overload_identity
27
32
  when earlier_overload(parent.overload_entries, overload_identity, offset)
28
33
  let identity overload_identity
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  import .ts/expression-fixes
2
3
  import .ts/style/type-imports
3
4
  import .ts/modules
@@ -22,7 +22,7 @@ in ts_declarator rule es-no-undef-init {
22
22
  }
23
23
 
24
24
  fn parse_int_target(n) = if enabled_global_identifier(n, "parseInt") || n is (ts_member, ts_index) && n.static_name == "parseInt" && { value n.object.node; unparenthesised_node; enabled_global_identifier "Number" } then n.text else null
25
- in ts_call let parse_int_name { value callee.node; unparenthesised_node; parse_int_target }
25
+ in ts_call let parse_int_name if contains(callee.text, "parseInt") || contains(callee.text, "\\") then { value callee.node; unparenthesised_node; parse_int_target } else null
26
26
  fn valid_radix(value) = value.type == "number" && value.value >= 2 && value.value <= 36 && value.value % 1 == 0
27
27
  fn invalid_radix(n) = n is ts_identifier && n.text == "undefined" && n.resolved == "global" || (n is (ts_number, ts_string, ts_boolean, ts_null, ts_regex, ts_bigint) || n is ts_unary && contains(["+", "-"], n.operator) && n.argument is ts_number) && !valid_radix(n.literal_value)
28
28
 
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # Literal-side comparison policy and source-preserving operand swaps.
2
3
  import .ts/expression-fixes
3
4
 
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # Expression preferences: sequences, comparisons, concatenation, ternaries and coercions.
2
3
 
3
4
  define {
@@ -82,9 +82,9 @@ fn object_spread_fix(source, n) = {
82
82
  }
83
83
 
84
84
  in ts_call rule es-prefer-object-spread {
85
- when ts_global_method_candidate(node, "assign")
86
85
  let first_argument { value argument.nodes; first; unparenthesised_node }
87
86
  when first_argument is ts_object
87
+ when ts_global_method_candidate(node, "assign")
88
88
  when global_method_callee == "Object.assign"
89
89
  when !any(argument.nodes, arg => unparenthesised_node(arg) is ts_spread)
90
90
  when count(argument) == 1 || !any([] argument.assign_source_accessor, a => a == true) && !{ value [] argument.assign_source_proto; slice 1; any (p) p == true }
package/std/ts/tokens.dx CHANGED
@@ -48,21 +48,27 @@ fn ts_token_gaps(source, start, end, text) = if !contains(text, "/") then [text]
48
48
 
49
49
  fn ts_token_gap(source, start, end) = {
50
50
  value text: byte_slice(source, start, end)
51
- value breaks: if text == "" then 0 else ts_token_breaks(text)
52
- value gaps: if text == "" then [] else ts_token_gaps(source, start, end, text)
51
+ value if text == "" || text == " " then {
52
+ text: text, start: start, end: end, spaced: text == " ", line_break: false,
53
+ blank_lines: 0, blank_gaps: 0, comments: false,
54
+ } else {
55
+ value breaks: ts_token_breaks(text)
56
+ value plain: !contains(text, "/")
57
+ value gaps: if plain then [] else ts_token_gaps(source, start, end, text)
53
58
  value {
54
59
  text: text,
55
60
  start: start,
56
61
  end: end,
57
- spaced: any(gaps, gap => re_test(gap, /\s/)),
62
+ spaced: if plain then re_test(text, /\s/) else any(gaps, gap => re_test(gap, /\s/)),
58
63
  line_break: breaks > 0,
59
64
  blank_lines: if breaks < 2 then 0 else {
60
65
  value lines: split(re_replace(text, /\r\n|[\r\u{2028}\u{2029}]/, "\n"), "\n")
61
66
  value count(filter(slice(lines, 1, -1), line => trim(line) == ""))
62
67
  },
63
- blank_gaps: if breaks < 2 then 0 else if !contains(text, "/") then breaks - 1
68
+ blank_gaps: if breaks < 2 then 0 else if plain then breaks - 1
64
69
  else sum(map(gaps, gap => max(ts_token_breaks(gap) - 1, 0))),
65
- comments: text != "" && trim(text) != "",
70
+ comments: trim(text) != "",
71
+ }
66
72
  }
67
73
  }
68
74
 
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  import .ts/scoping/usage
2
3
 
3
4
  define {