@inthedark/dxlint 0.1.14 → 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.
Files changed (71) 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 +134 -0
  6. package/package.json +20 -2
  7. package/rules/bugs.dx +1 -0
  8. package/std/ts/arithmetic.dx +10 -0
  9. package/std/ts/assertions.dx +56 -4
  10. package/std/ts/calls.dx +45 -15
  11. package/std/ts/conditions.dx +2 -0
  12. package/std/ts/constants.dx +2 -1
  13. package/std/ts/content-mapper.dx +48 -0
  14. package/std/ts/deprecation.dx +38 -18
  15. package/std/ts/effects.dx +8 -1
  16. package/std/ts/enums.dx +32 -4
  17. package/std/ts/format.dx +1 -1
  18. package/std/ts/grammar.dx +29 -15
  19. package/std/ts/import-no-deprecated.dx +4 -2
  20. package/std/ts/javascript.dx +496 -0
  21. package/std/ts/layout/literals.dx +14 -7
  22. package/std/ts/layout/separators.dx +6 -1
  23. package/std/ts/layout/shared.dx +36 -37
  24. package/std/ts/layout/types.dx +4 -0
  25. package/std/ts/literals.dx +1 -0
  26. package/std/ts/methods.dx +3 -0
  27. package/std/ts/modules.dx +60 -2
  28. package/std/ts/names.dx +30 -13
  29. package/std/ts/narrowing.dx +20 -14
  30. package/std/ts/parentheses.dx +27 -0
  31. package/std/ts/problems/control-flow.dx +1 -0
  32. package/std/ts/problems/numeric-precision.dx +1 -0
  33. package/std/ts/problems/regexp.dx +1 -0
  34. package/std/ts/regex/checks.dx +1 -0
  35. package/std/ts/regex/findings.dx +10 -6
  36. package/std/ts/regex/values.dx +4 -15
  37. package/std/ts/restrictions.dx +76 -0
  38. package/std/ts/scoping/member-uses.dx +5 -3
  39. package/std/ts/scoping/shadowing.dx +1 -0
  40. package/std/ts/scoping/usage.dx +3 -3
  41. package/std/ts/spreads.dx +1 -1
  42. package/std/ts/structure/padding.dx +10 -8
  43. package/std/ts/style/expressions.dx +8 -4
  44. package/std/ts/style/returns.dx +3 -1
  45. package/std/ts/style/signatures.dx +5 -0
  46. package/std/ts/style/type-exports.dx +1 -0
  47. package/std/ts/suggestions/builtins.dx +1 -1
  48. package/std/ts/suggestions/comparisons.dx +1 -0
  49. package/std/ts/suggestions/expressions.dx +1 -0
  50. package/std/ts/suggestions/object-spread.dx +1 -1
  51. package/std/ts/tokens.dx +14 -5
  52. package/std/ts/type-specifiers.dx +19 -3
  53. package/std/ts/typed/awaitability.dx +1 -0
  54. package/std/ts/typed/collection-iteration.dx +24 -0
  55. package/std/ts/typed/floating-promises.dx +45 -14
  56. package/std/ts/typed/includes.dx +3 -4
  57. package/std/ts/typed/promises.dx +16 -13
  58. package/std/ts/typed/readonly-parameters.dx +11 -2
  59. package/std/ts/typed/string-boundaries.dx +1 -0
  60. package/std/ts/typed/switches.dx +1 -0
  61. package/std/ts/typed/transfers.dx +54 -7
  62. package/std/ts/typed/type-arguments.dx +19 -20
  63. package/std/ts/typed/type-conversions.dx +1 -0
  64. package/std/ts/typed/type-parameters.dx +1 -1
  65. package/std/ts/typed/using-disposable.dx +98 -0
  66. package/std/ts/typed.dx +22 -9
  67. package/std/ts/values/arguments.dx +1 -1
  68. package/std/ts/values/enums.dx +5 -1
  69. package/std/ts/values/inspection.dx +26 -7
  70. package/std/ts/values.dx +98 -39
  71. package/std/ts.dx +10 -4
@@ -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,30 @@ 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 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,
57
+ } else {
58
+ value breaks: ts_token_breaks(text)
59
+ value plain: !contains(text, "/")
60
+ value gaps: if plain then [] else ts_token_gaps(source, start, end, text)
53
61
  value {
54
62
  text: text,
55
63
  start: start,
56
64
  end: end,
57
- spaced: any(gaps, gap => re_test(gap, /\s/)),
65
+ spaced: if plain then re_test(text, /\s/) else any(gaps, gap => re_test(gap, /\s/)),
58
66
  line_break: breaks > 0,
59
67
  blank_lines: if breaks < 2 then 0 else {
60
68
  value lines: split(re_replace(text, /\r\n|[\r\u{2028}\u{2029}]/, "\n"), "\n")
61
69
  value count(filter(slice(lines, 1, -1), line => trim(line) == ""))
62
70
  },
63
- blank_gaps: if breaks < 2 then 0 else if !contains(text, "/") then breaks - 1
71
+ blank_gaps: if breaks < 2 then 0 else if plain then breaks - 1
64
72
  else sum(map(gaps, gap => max(ts_token_breaks(gap) - 1, 0))),
65
- comments: text != "" && trim(text) != "",
73
+ comments: trim(text) != "",
74
+ }
66
75
  }
67
76
  }
68
77
 
@@ -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
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  import .ts/scoping/usage
2
3
 
3
4
  define {
@@ -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
+ }
@@ -4,9 +4,16 @@ define {
4
4
 
5
5
  in ts_node let floating_promise_arms value_parts(node)
6
6
  in ts_await let floating_promise_arms value_parts(ts_awaited_value(node, argument.node))
7
+ # A promise or an array is an object: anything else needs no describing.
8
+ in ts_node let floating_promise_candidate node could object
9
+ in ts_await let floating_promise_candidate true
7
10
  in ts_node let floating_promise_safe_call false
8
11
  in ts_node let floating_promise_safe_types []
9
12
  in ts_node let floating_promises_checked true
13
+ # A lone value without a carrier can be neither: no leaves to walk or exempt.
14
+ in ts_node let floating_promise_composite is (ts_parenthesized, ts_sequence, ts_logical, ts_conditional, ts_unary)
15
+ || is ts_call && callee.node.static_name == "finally"
16
+ fn floating_promise_subject_possible(n) = n.floating_promise_candidate != false || n.floating_promise_composite
10
17
  fn floating_promise_safe(n, safe_calls) = {
11
18
  value expression: unparenthesised_node(n)
12
19
  value expression.floating_promise_safe_call
@@ -83,23 +90,45 @@ fn floating_promise_arm(arm, n, check_thenables, safe_promises) =
83
90
  value any(headers ?? [], alternatives => any(alternatives, header => count(header.parameters) >= 2
84
91
  && all(slice(header.parameters, 0, 2), parameter => floating_callable_arm(parameter))))
85
92
  })
93
+ fn floating_promise_exempt(part, safe_promises, safe_calls) = part is ts_assignment || floating_promise_safe(part, safe_calls)
94
+ || floating_promise_alias_allowed(part, safe_promises)
95
+ || type_value_allowed(part.floating_promise_safe_types, part) != false
96
+ fn floating_promise_is_promise(part, check_thenables, safe_promises) = part not ts_await
97
+ && (check_thenables || part.floating_promise_candidate != false)
98
+ && part could thenable
99
+ && any(part.floating_promise_arms ?? [], arm => floating_promise_arm(arm, part, check_thenables, safe_promises))
100
+ fn floating_promise_is_array(part, check_thenables, safe_promises) = part.floating_promise_candidate != false
101
+ && floating_promise_array(part.floating_promise_arms, part, check_thenables, safe_promises)
102
+ fn floating_promise_unhandled(part) = if part is ts_call then {
103
+ value handler: floating_rejection_handler(part)
104
+ value if handler == "handled" then null else if handler == "useless" then "useless" else "promise"
105
+ } else "promise"
86
106
  fn floating_promise_failure(n, check_thenables, ignore_void, safe_promises, safe_calls) = {
87
107
  value floating_promise_leaves(n, ignore_void)
88
- map (part) if part is ts_assignment || floating_promise_safe(part, safe_calls)
89
- || floating_promise_alias_allowed(part, safe_promises)
90
- || type_value_allowed(part.floating_promise_safe_types, part) != false then null else {
91
- value arms: part.floating_promise_arms
92
- value if floating_promise_array(arms, part, check_thenables, safe_promises) then "array"
93
- else if part not ts_await
94
- && any(arms ?? [], arm => floating_promise_arm(arm, part, check_thenables, safe_promises))
95
- then if part is ts_call then {
96
- value handler: floating_rejection_handler(part)
97
- value if handler == "handled" then null else if handler == "useless" then "useless" else "promise"
98
- } else "promise" else null
99
- }
108
+ map (part) if floating_promise_exempt(part, safe_promises, safe_calls) then null
109
+ else if floating_promise_is_array(part, check_thenables, safe_promises) then "array"
110
+ else if floating_promise_is_promise(part, check_thenables, safe_promises) then floating_promise_unhandled(part) else null
100
111
  filter (failure) failure != null
101
112
  first
102
113
  }
114
+ # One leaf can fail one way: each rule asks only its own question first.
115
+ fn floating_promise_problem(n, check_thenables, ignore_void, safe_promises, safe_calls) = {
116
+ value leaves: floating_promise_leaves(n, ignore_void)
117
+ value if count(leaves) != 1 then floating_promise_failure(n, check_thenables, ignore_void, safe_promises, safe_calls) else {
118
+ value part: first(leaves)
119
+ value if floating_promise_exempt(part, safe_promises, safe_calls) || !floating_promise_is_promise(part, check_thenables, safe_promises)
120
+ || floating_promise_is_array(part, check_thenables, safe_promises) then null
121
+ else floating_promise_unhandled(part)
122
+ }
123
+ }
124
+ fn floating_promise_array_problem(n, check_thenables, ignore_void, safe_promises, safe_calls) = {
125
+ value leaves: floating_promise_leaves(n, ignore_void)
126
+ value if count(leaves) != 1 then floating_promise_failure(n, check_thenables, ignore_void, safe_promises, safe_calls) == "array" else {
127
+ value part: first(leaves)
128
+ value !floating_promise_exempt(part, safe_promises, safe_calls)
129
+ && floating_promise_is_array(part, check_thenables, safe_promises)
130
+ }
131
+ }
103
132
  in ts_expression_statement.expression, ts_arrow.body rule es-no-floating-promises {
104
133
  option checkThenables boolean = false "Check promise-like types with fulfilment and rejection callbacks"
105
134
  option ignoreVoid boolean = true "Treat the void operator as explicit promise handling"
@@ -108,7 +137,8 @@ in ts_expression_statement.expression, ts_arrow.body rule es-no-floating-promise
108
137
  option allowForKnownSafeCalls string[] = [] "Function names whose returned promise is handled elsewhere"
109
138
  when floating_promise_subject_checked(node, ignoreIIFE)
110
139
  when node not ts_assignment
111
- let failure floating_promise_failure(node, checkThenables, ignoreVoid, allowForKnownSafePromises, allowForKnownSafeCalls)
140
+ when checkThenables || floating_promise_subject_possible(node)
141
+ let failure floating_promise_problem(node, checkThenables, ignoreVoid, allowForKnownSafePromises, allowForKnownSafeCalls)
112
142
  when contains(["promise", "useless"], failure)
113
143
  report at if parent.node is ts_expression_statement then parent.node else node
114
144
  warn (if ignoreVoid then "Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator."
@@ -138,7 +168,8 @@ in ts_expression_statement.expression, ts_arrow.body rule es-no-floating-promise
138
168
  option allowForKnownSafeCalls string[] = [] "Function names whose returned promise is handled elsewhere"
139
169
  when floating_promise_subject_checked(node, ignoreIIFE)
140
170
  when node not ts_assignment
141
- when floating_promise_failure(node, checkThenables, ignoreVoid, allowForKnownSafePromises, allowForKnownSafeCalls) == "array"
171
+ when floating_promise_subject_possible(node)
172
+ when floating_promise_array_problem(node, checkThenables, ignoreVoid, allowForKnownSafePromises, allowForKnownSafeCalls)
142
173
  report at if parent.node is ts_expression_statement then parent.node else node
143
174
  warn if ignoreVoid then "An array of Promises may be unintentional. Consider handling the promises' fulfillment or rejection with Promise.all or similar, or explicitly marking the expression as ignored with the `void` operator."
144
175
  else "An array of Promises may be unintentional. Consider handling the promises' fulfillment or rejection with Promise.all or similar."
@@ -76,18 +76,17 @@ fn includes_quote(text) = "'" {
76
76
  } "'"
77
77
 
78
78
  in ts_binary rule es-prefer-includes {
79
- let negative includes_negative(operator.text, ts_runtime_number(right.node))
80
- when negative != null
81
79
  let info includes_index_call(left.node)
82
80
  when info != null
81
+ let negative includes_negative(operator.text, ts_runtime_number(right.node))
82
+ when negative != null
83
83
  warn "Use 'includes()' method instead."
84
84
  let replacement includes_index_replacement(source, node, info, negative)
85
85
  fix if replacement != null as replacement
86
86
  }
87
87
  in ts_call rule es-prefer-includes:regex {
88
- when count(argument) == 1
88
+ when collection_test_call
89
89
  let method unparenthesised_node(callee.node)
90
- when collection_method(method, "test")
91
90
  let receiver first(argument.nodes)
92
91
  when string_boundary_receiver(receiver, false)
93
92
  let pattern_text includes_regex(method.object.node)
@@ -26,6 +26,7 @@ in ts_parenthesized.expression, ts_logical.left, ts_logical.right, ts_conditiona
26
26
  # Comparisons and these unary operators make primitives: never thenable.
27
27
  when !(is ts_binary && contains(["===", "!==", "==", "!=", "<", "<=", ">", ">=", "in", "instanceof"], node.operator))
28
28
  when !(is ts_unary && contains(["typeof", "void", "delete", "+", "~"], node.operator))
29
+ when value_may_have_carrier(node) != false
29
30
  when node could thenable && (flagUnions == "all" || node always thenable || flagUnions == "strict" && ts_await_union_equivalent(node) == true)
30
31
  warn "Expected non-Promise value in a boolean conditional."
31
32
  }
@@ -51,15 +52,14 @@ fn promise_returns_thenable(arms) = promise_returns_known(arms)
51
52
  fn promise_returns_void(arms) = promise_returns_known(arms) && count(arms) > 0
52
53
  && any(arms, arm => any(arm, part => contains(part.traits, "void")))
53
54
  && !any(arms, arm => any(arm, part => contains(part.traits, "thenable")))
54
- fn promise_void_context(n) = {
55
- value source: promise_return_arms(value_signatures(n))
56
- value if !promise_returns_thenable(source) then false else {
57
- value targets: expected_call_signatures(n)
58
- value headers: flat_map(targets ?? [], arm => arm)
59
- value target: flat_map(headers, header => header.result)
60
- value targets != null && count(headers) > 0
61
- && all(headers, header => header.result_known) && promise_returns_void(target)
62
- }
55
+ fn promise_void_context(n) = !(unparenthesised_node(n) is (ts_string, ts_number, ts_boolean, ts_null, ts_bigint, ts_template, ts_regex, ts_array, ts_object, ts_binary, ts_update)) && {
56
+ value targets: expected_call_signatures(n)
57
+ value headers: flat_map(targets ?? [], arm => arm)
58
+ value target: flat_map(headers, header => header.result)
59
+ # Few places expect a function: asked first, it spares the value of the rest.
60
+ value targets != null && count(headers) > 0
61
+ && all(headers, header => header.result_known) && promise_returns_void(target)
62
+ && promise_returns_thenable(promise_return_arms(value_signatures(n)))
63
63
  }
64
64
  # ESLint unions every call signature and drops an argument index when any
65
65
  # signature's parameter at that index accepts a thenable-returning function.
@@ -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,14 +130,15 @@ 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
  }
136
139
 
137
140
  in ts_call.argument rule es-no-misused-promises:predicates {
138
- when node.index == 0 && condition_array_callback(parent)
141
+ when parent.condition_array_call && node.index == 0 && strict_boolean_array(parent.callee.object.node)
139
142
  when promise_returns_thenable(promise_return_arms(value_signatures(node)))
140
143
  report at unparenthesised_node(node)
141
144
  warn "Expected a non-Promise value to be returned."
@@ -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
@@ -143,6 +143,7 @@ in ts_binary rule es-prefer-string-starts-ends-with {
143
143
  fix at node if replacement != null as replacement
144
144
  }
145
145
  in ts_call rule es-prefer-string-starts-ends-with:regex {
146
+ when collection_test_call
146
147
  let info string_boundary_regex_call(node, "test")
147
148
  when info != null
148
149
  warn if info.kind == "end" then "Use the 'String#endsWith' method instead." else "Use 'String#startsWith' method instead."
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  define {
2
3
  fn switch_part_identity(part) =
3
4
  when {
@@ -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