@inthedark/dxlint 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +84 -2
  3. package/bin/dxlint +15 -0
  4. package/bin/dxlint-linux-x64 +0 -0
  5. package/bin/dxlint-win32-x64.exe +0 -0
  6. package/package.json +40 -4
  7. package/rules/conventions.dx +34 -0
  8. package/rules/correctness.dx +33 -0
  9. package/rules/es-no-semi.dx +11 -0
  10. package/rules/format.dx +49 -0
  11. package/rules/recommended.dx +51 -0
  12. package/rules/suspicious.dx +31 -0
  13. package/rules/types.dx +21 -0
  14. package/std/c.dx +23 -0
  15. package/std/core/lines.dx +51 -0
  16. package/std/core/lists.dx +76 -0
  17. package/std/core.dx +30 -0
  18. package/std/css.dx +17 -0
  19. package/std/dx.dx +31 -0
  20. package/std/json.dx +104 -0
  21. package/std/markdown.dx +11 -0
  22. package/std/python.dx +21 -0
  23. package/std/rust/format.dx +316 -0
  24. package/std/rust.dx +34 -0
  25. package/std/toml.dx +19 -0
  26. package/std/ts/arithmetic.dx +87 -0
  27. package/std/ts/assertions.dx +477 -0
  28. package/std/ts/calls.dx +246 -0
  29. package/std/ts/comments.dx +10 -0
  30. package/std/ts/conditions.dx +531 -0
  31. package/std/ts/constants.dx +335 -0
  32. package/std/ts/deprecation.dx +185 -0
  33. package/std/ts/directives.dx +60 -0
  34. package/std/ts/effects.dx +219 -0
  35. package/std/ts/enums.dx +233 -0
  36. package/std/ts/expression-fixes.dx +67 -0
  37. package/std/ts/expressions.dx +270 -0
  38. package/std/ts/flow.dx +2 -0
  39. package/std/ts/format.dx +755 -0
  40. package/std/ts/globals.dx +106 -0
  41. package/std/ts/grammar.dx +488 -0
  42. package/std/ts/helpers.dx +102 -0
  43. package/std/ts/import-attributes.dx +88 -0
  44. package/std/ts/import-no-deprecated.dx +73 -0
  45. package/std/ts/indentation.dx +442 -0
  46. package/std/ts/jsx.dx +646 -0
  47. package/std/ts/layout/blocks.dx +281 -0
  48. package/std/ts/layout/calls.dx +79 -0
  49. package/std/ts/layout/expressions.dx +213 -0
  50. package/std/ts/layout/literals.dx +114 -0
  51. package/std/ts/layout/separators.dx +184 -0
  52. package/std/ts/layout/shared.dx +203 -0
  53. package/std/ts/layout/text.dx +142 -0
  54. package/std/ts/layout/types.dx +182 -0
  55. package/std/ts/layout.dx +9 -0
  56. package/std/ts/license.dx +54 -0
  57. package/std/ts/literal-text.dx +3 -0
  58. package/std/ts/literals.dx +95 -0
  59. package/std/ts/methods.dx +69 -0
  60. package/std/ts/modules.dx +268 -0
  61. package/std/ts/names.dx +348 -0
  62. package/std/ts/naming.dx +169 -0
  63. package/std/ts/narrowing.dx +96 -0
  64. package/std/ts/parentheses.dx +262 -0
  65. package/std/ts/precedence.dx +25 -0
  66. package/std/ts/problems/calls.dx +34 -0
  67. package/std/ts/problems/classes.dx +39 -0
  68. package/std/ts/problems/control-flow.dx +257 -0
  69. package/std/ts/problems/declarations.dx +67 -0
  70. package/std/ts/problems/duplicates.dx +70 -0
  71. package/std/ts/problems/expressions.dx +154 -0
  72. package/std/ts/problems/helpers.dx +22 -0
  73. package/std/ts/problems/imports.dx +43 -0
  74. package/std/ts/problems/non-null.dx +78 -0
  75. package/std/ts/problems/numeric-precision.dx +66 -0
  76. package/std/ts/problems/regexp.dx +66 -0
  77. package/std/ts/problems/statements.dx +36 -0
  78. package/std/ts/problems/typescript.dx +55 -0
  79. package/std/ts/problems/whitespace.dx +40 -0
  80. package/std/ts/problems.dx +20 -0
  81. package/std/ts/regex/checks.dx +249 -0
  82. package/std/ts/regex/findings.dx +209 -0
  83. package/std/ts/regex/grammar.dx +80 -0
  84. package/std/ts/regex/suggestions.dx +94 -0
  85. package/std/ts/regex/values.dx +378 -0
  86. package/std/ts/regex.dx +5 -0
  87. package/std/ts/restrictions/comments.dx +76 -0
  88. package/std/ts/restrictions.dx +811 -0
  89. package/std/ts/scopes.dx +68 -0
  90. package/std/ts/scoping/atomic.dx +40 -0
  91. package/std/ts/scoping/bindings.dx +126 -0
  92. package/std/ts/scoping/constness.dx +72 -0
  93. package/std/ts/scoping/globals.dx +52 -0
  94. package/std/ts/scoping/loops.dx +86 -0
  95. package/std/ts/scoping/member-uses.dx +111 -0
  96. package/std/ts/scoping/receivers.dx +107 -0
  97. package/std/ts/scoping/shadowing.dx +202 -0
  98. package/std/ts/scoping/usage.dx +393 -0
  99. package/std/ts/scoping.dx +842 -0
  100. package/std/ts/selectors.dx +10 -0
  101. package/std/ts/spreads.dx +97 -0
  102. package/std/ts/structure/declarations.dx +52 -0
  103. package/std/ts/structure/lines.dx +79 -0
  104. package/std/ts/structure/lists.dx +144 -0
  105. package/std/ts/structure/padding.dx +258 -0
  106. package/std/ts/structure.dx +963 -0
  107. package/std/ts/style/classes.dx +157 -0
  108. package/std/ts/style/comments.dx +75 -0
  109. package/std/ts/style/declarations.dx +82 -0
  110. package/std/ts/style/expressions.dx +125 -0
  111. package/std/ts/style/imports.dx +44 -0
  112. package/std/ts/style/ordering.dx +192 -0
  113. package/std/ts/style/returns.dx +152 -0
  114. package/std/ts/style/signatures.dx +308 -0
  115. package/std/ts/style/type-exports.dx +62 -0
  116. package/std/ts/style/type-imports.dx +166 -0
  117. package/std/ts/style/types.dx +474 -0
  118. package/std/ts/style.dx +29 -0
  119. package/std/ts/suggestions/accessors.dx +138 -0
  120. package/std/ts/suggestions/builtins.dx +110 -0
  121. package/std/ts/suggestions/comparisons.dx +41 -0
  122. package/std/ts/suggestions/computed-keys.dx +20 -0
  123. package/std/ts/suggestions/constructors.dx +54 -0
  124. package/std/ts/suggestions/control-flow.dx +204 -0
  125. package/std/ts/suggestions/curly.dx +124 -0
  126. package/std/ts/suggestions/directives.dx +79 -0
  127. package/std/ts/suggestions/errors.dx +106 -0
  128. package/std/ts/suggestions/expressions.dx +230 -0
  129. package/std/ts/suggestions/functions.dx +260 -0
  130. package/std/ts/suggestions/globals.dx +187 -0
  131. package/std/ts/suggestions/helpers.dx +17 -0
  132. package/std/ts/suggestions/object-shorthand.dx +148 -0
  133. package/std/ts/suggestions/object-spread.dx +96 -0
  134. package/std/ts/suggestions/regexp.dx +59 -0
  135. package/std/ts/suggestions/renames.dx +50 -0
  136. package/std/ts/suggestions/strings.dx +47 -0
  137. package/std/ts/suggestions/templates.dx +104 -0
  138. package/std/ts/suggestions.dx +24 -0
  139. package/std/ts/template-fixes.dx +22 -0
  140. package/std/ts/tokens.dx +124 -0
  141. package/std/ts/type-specifiers.dx +93 -0
  142. package/std/ts/typed/assignments.dx +154 -0
  143. package/std/ts/typed/awaitability.dx +157 -0
  144. package/std/ts/typed/boolean-comparisons.dx +82 -0
  145. package/std/ts/typed/defaults.dx +97 -0
  146. package/std/ts/typed/enum-consistency.dx +68 -0
  147. package/std/ts/typed/enums.dx +52 -0
  148. package/std/ts/typed/errors.dx +32 -0
  149. package/std/ts/typed/floating-promises.dx +147 -0
  150. package/std/ts/typed/for-in-arrays.dx +21 -0
  151. package/std/ts/typed/includes.dx +102 -0
  152. package/std/ts/typed/nullish-coalescing.dx +149 -0
  153. package/std/ts/typed/optional-chains.dx +317 -0
  154. package/std/ts/typed/promises.dx +205 -0
  155. package/std/ts/typed/qualifiers.dx +108 -0
  156. package/std/ts/typed/readonly-members.dx +159 -0
  157. package/std/ts/typed/readonly-parameters.dx +121 -0
  158. package/std/ts/typed/redundant-constituents.dx +171 -0
  159. package/std/ts/typed/regexp-exec.dx +79 -0
  160. package/std/ts/typed/rejection-handlers.dx +91 -0
  161. package/std/ts/typed/return-await.dx +116 -0
  162. package/std/ts/typed/return-this.dx +67 -0
  163. package/std/ts/typed/strict-void-return.dx +121 -0
  164. package/std/ts/typed/string-boundaries.dx +153 -0
  165. package/std/ts/typed/switches.dx +147 -0
  166. package/std/ts/typed/template-expressions.dx +127 -0
  167. package/std/ts/typed/template-values.dx +88 -0
  168. package/std/ts/typed/transfers.dx +106 -0
  169. package/std/ts/typed/type-arguments.dx +337 -0
  170. package/std/ts/typed/type-constituents.dx +143 -0
  171. package/std/ts/typed/type-conversions.dx +82 -0
  172. package/std/ts/typed/type-parameters.dx +280 -0
  173. package/std/ts/typed/unbound-method.dx +167 -0
  174. package/std/ts/typed/unsafe-calls.dx +73 -0
  175. package/std/ts/typed/unsafe-members.dx +36 -0
  176. package/std/ts/typed/void-expressions.dx +101 -0
  177. package/std/ts/typed.dx +585 -0
  178. package/std/ts/values/arguments.dx +13 -0
  179. package/std/ts/values/enums.dx +104 -0
  180. package/std/ts/values/inspection.dx +274 -0
  181. package/std/ts/values.dx +1217 -0
  182. package/std/ts.dx +77 -0
  183. package/std/whitespace.dx +37 -0
@@ -0,0 +1,531 @@
1
+ # .ts/conditions: type-aware conditional policy.
2
+
3
+ import .ts/values/inspection
4
+
5
+ define {
6
+
7
+ in ts_switch let switch_default_comment {
8
+ value case_end: last([] case.end_offset)
9
+ value if count(case) == 0 then null else {
10
+ value descendants(ts_comment)
11
+ filter (comment) comment.offset >= case_end && comment.end_offset < end_offset
12
+ last
13
+ }
14
+ }
15
+
16
+ fn condition_domains(parts) = {
17
+ value arms: parts ?? []
18
+ value known: parts != null && all(arms, arm => all(arm, part =>
19
+ !any(part.traits, trait => contains(["any", "unknown", "error"], trait))))
20
+ value {
21
+ known: known,
22
+ truthy: any(arms, arm => !any(arm, part => contains(part.traits, "falsy"))),
23
+ falsy: any(arms, arm => !any(arm, part => contains(part.traits, "truthy"))
24
+ && !condition_structural_truthy([arm])),
25
+ nullish: any(arms, arm => any(arm, part =>
26
+ any(part.traits, trait => contains(["null", "undefined", "any", "unknown"], trait)))
27
+ || any(arm, part => contains(part.traits, "parameter")) && !any(arm, part =>
28
+ any(part.traits, trait => contains(["boolean", "number", "string", "bigint", "symbol",
29
+ "object", "structural", "callable", "constructible"], trait)))),
30
+ non_nullish: any(arms, arm => !any(arm, part =>
31
+ contains(part.traits, "null") || contains(part.traits, "undefined"))),
32
+ }
33
+ }
34
+ fn condition_domain(n) = condition_domains(value_constituent_traits(n, ["any", "unknown", "error", "falsy", "truthy", "null", "undefined", "parameter", "boolean", "number", "string", "bigint", "symbol", "object", "structural", "callable", "constructible"]))
35
+ fn condition_known(d) = d.known == true
36
+ fn condition_never(d) = !d.truthy && !d.falsy
37
+ fn condition_returns(n) = {
38
+ value signatures: value_signatures(n)
39
+ value if signatures == null then null else flat_map(signatures, headers =>
40
+ map(headers, header => condition_domains(value_constituent_traits(header.result, ["any", "unknown", "error", "falsy", "truthy", "null", "undefined", "parameter", "boolean", "number", "string", "bigint", "symbol", "object", "structural", "callable", "constructible"]))))
41
+ }
42
+ fn condition_base(n) = when {
43
+ n is (ts_member, ts_index) then n.object.node
44
+ n is ts_call then n.callee.node
45
+ else then null
46
+ }
47
+ fn condition_unchecked_index(n) = if n not ts_index then false else {
48
+ value shape: ts_value_array_shape(n.object.node)
49
+ value if !shape.known then null else (shape.array || shape.tuple)
50
+ && !(shape.tuple && unparenthesised_node(n.index.node) is ts_literal)
51
+ }
52
+ fn condition_array_origin(n) = {
53
+ value candidates: {
54
+ value n
55
+ walk (current) condition_base(current)
56
+ filter (current) current.optional != null
57
+ map (current) { value current; condition_base; condition_unchecked_index }
58
+ }
59
+ value when {
60
+ contains(candidates, true) then true
61
+ contains(candidates, null) then null
62
+ else then false
63
+ }
64
+ }
65
+ fn condition_member_access(n, arms) = {
66
+ value keys: if n is ts_member then [ts_value_key(n.property.node)] else value_keys(n.index.node)
67
+ value receivers: filter(arms ?? [], arm => !all(arm.parts, part =>
68
+ contains(part.traits, "null") || contains(part.traits, "undefined")))
69
+ value lookups: flat_map(receivers, arm => map(keys ?? [], key =>
70
+ {key: key, result: value_lookup(arm.handle, key)}))
71
+ value complete: arms != null && keys != null && count(keys) > 0
72
+ && all(keys, key => key != null) && all(lookups, lookup => lookup.result != null)
73
+ value projected: map(lookups, lookup => value_parts(lookup.result.member))
74
+ value {
75
+ arms: if complete && all(projected, parts => parts != null)
76
+ then flat_map(projected, parts => parts) else null,
77
+ from_index: if !complete then null else any(lookups, lookup =>
78
+ any(lookup.result.alternatives, alternative =>
79
+ !any(alternative.sources, source => source.kind == "named")
80
+ && any(alternative.sources, source => contains(["indexer", "repeated"], source.kind)
81
+ || source.kind == "ordinal" && lookup.key.domain == "ordinal"))),
82
+ optional: if !complete then null else any(lookups, lookup =>
83
+ any(lookup.result.alternatives, alternative => alternative.required == false
84
+ && any(alternative.sources, source => source.kind == "named"))),
85
+ }
86
+ }
87
+ fn condition_call_access(n) = {
88
+ value binding: call_binding(n)
89
+ value projected: map(binding.choices ?? [], choice => value_parts(choice.result))
90
+ value {
91
+ arms: if binding != null && !binding.uncertain && count(projected) > 0 && all(projected, parts => parts != null)
92
+ then flat_map(projected, parts => parts) else null,
93
+ from_index: false,
94
+ optional: false,
95
+ }
96
+ }
97
+ fn condition_evaluated(n) = if n.in_optional_chain != true then
98
+ {arms: value_parts(n), from_index: false, optional: false}
99
+ else {
100
+ value steps: reverse(walk(n, current => condition_base(current)))
101
+ value fold(slice(steps, 1), {arms: value_parts(first(steps)), from_index: false, optional: false}, (state, step) =>
102
+ if step.in_optional_chain != true then {arms: value_parts(step), from_index: false, optional: false}
103
+ else if step is ts_call then condition_call_access(step)
104
+ else condition_member_access(step, state.arms))
105
+ }
106
+ fn condition_access(n) = if n not (ts_member, ts_index) then null
107
+ else if n.in_optional_chain == true then condition_evaluated(n)
108
+ else condition_member_access(n, value_parts(n.object.node))
109
+ fn condition_index_fallback(n, unchecked) = condition_access(n).from_index == true
110
+ && (n is ts_index || unchecked || condition_access_domains(condition_base(n)).nullish)
111
+ fn condition_access_domains(n) = {
112
+ value arms: condition_evaluated(n).arms
113
+ value condition_domains(if arms == null then null else map(arms, arm => arm.parts))
114
+ }
115
+ fn condition_predicate_union(n, descriptor, parts) = {
116
+ value sources: walk([descriptor.target_source], source =>
117
+ map(filter(source.sites ?? [], site => site.kind != "ts_type_parameter"), site => site_sources(n, site)))
118
+ value kinds: unique(map(parts ?? [], arm =>
119
+ find(["null", "undefined", "boolean", "number", "string", "bigint", "symbol"], trait =>
120
+ any(arm, part => contains(part.traits, trait)))))
121
+ value descriptor.target_union || any(sources, source => source.combine == "union")
122
+ || count(kinds) > 1 || count(parts) > 1
123
+ && !all(kinds, kind => kind == "boolean")
124
+ && all(parts, arm => any(arm, part => part.identity_known && part.identity.kind == "value"))
125
+ && !any(parts, arm => any(arm, part => contains(part.traits, "enum")))
126
+ }
127
+ fn condition_predicate_choice(n, choice, target_choice) = {
128
+ value descriptions: flat_map(value_parts(choice.at) ?? [], arm =>
129
+ map(arm.parts, part => part.carrier.description))
130
+ value descriptor: first(filter(descriptions, description => description.kind == "predicate"))
131
+ value index: descriptor.parameter_index
132
+ value if descriptor == null || index == null then null
133
+ else {
134
+ value parameter: first(slice(choice.parameters, index, index + 1))
135
+ value if count(parameter.arguments) != 1
136
+ || any(slice(n.argument.nodes, 0, index + 1), argument => argument is ts_spread) then null
137
+ else {
138
+ value actual: declaration_value(n, first(parameter.arguments))
139
+ value target: if descriptor.has_target then target_choice.at else null
140
+ value parts: if descriptor.has_target then value_constituents(target) else null
141
+ value union: condition_predicate_union(n, descriptor, parts)
142
+ value {
143
+ argument_index: index,
144
+ asserts: descriptor.asserts,
145
+ argument: condition_domains(value_constituent_traits(actual, ["any", "unknown", "error", "falsy", "truthy", "null", "undefined", "parameter", "boolean", "number", "string", "bigint", "symbol", "object", "structural", "callable", "constructible"])),
146
+ target: if descriptor.has_target then condition_domains(parts) else null,
147
+ to_target: if descriptor.has_target then value_accepts(target, actual, null, null) else null,
148
+ from_target: if descriptor.has_target then value_accepts(actual, target, null, null) else null,
149
+ target_union: union,
150
+ }
151
+ }
152
+ }
153
+ }
154
+ fn condition_predicate(n) = {
155
+ value binding: call_binding_at(n, ["return_type"])
156
+ value targets: call_binding_at(n, ["return_type", "type"])
157
+ value predicates: map(binding.choices ?? [], (choice, index) =>
158
+ condition_predicate_choice(n, choice, first(slice(targets.choices ?? [], index, index + 1))))
159
+ value predicate: first(predicates)
160
+ value if binding == null || binding.uncertain || predicate == null
161
+ || !all(predicates, other => other == predicate) then null else predicate
162
+ }
163
+ in ts_call let condition_predicate condition_predicate(node)
164
+ fn condition_boolean_use(n) = {
165
+ value n
166
+ walk (current) if current.parent is (ts_logical, ts_parenthesized) then current.parent else null
167
+ any (current) current.parent is (ts_if, ts_while, ts_do_while, ts_for, ts_conditional) && current.parent.test.node == current || current.parent is ts_unary && current.parent.operator == "!" || current.parent is ts_logical && current.parent.operator == "&&" && current.parent.left.node == current
168
+ }
169
+ fn condition_steps(n) = {
170
+ value n
171
+ unparenthesised_node
172
+ walk (current) when {
173
+ current is ts_unary && current.operator == "!" then unparenthesised_node(current.argument.node)
174
+ current is ts_logical && current.operator != "??" && condition_boolean_use(current) then unparenthesised_node(current.right.node)
175
+ else then null
176
+ }
177
+ }
178
+ fn condition_boolean(n) = { value n; steps: condition_steps; value condition_boolean_branch(n, steps) }
179
+ fn condition_boolean_branch(n, steps) = {
180
+ value if any(steps, current => current is ts_logical && current.operator != "??" && condition_boolean_use(current)) then {
181
+ value {
182
+ value steps
183
+ filter (current) current is ts_logical && current.operator != "??" && condition_boolean_use(current)
184
+ last
185
+ }.right.node
186
+ unparenthesised_node
187
+ } else unparenthesised_node(n)
188
+ condition_boolean_leaf
189
+ }
190
+ fn condition_boolean_leaf(n) = { node: n, subject: {
191
+ value n
192
+ walk (current) if current is ts_unary && current.operator == "!" then unparenthesised_node(current.argument.node) else null
193
+ last
194
+ }, negated: {
195
+ value n
196
+ walk (current) if current is ts_unary && current.operator == "!" then unparenthesised_node(current.argument.node) else null
197
+ filter (current) current is ts_unary && current.operator == "!"
198
+ count
199
+ } % 2 == 1 }
200
+ fn condition_loop_allowed(n, mode) = if mode == "always" then {
201
+ value literal: condition_literal(n)
202
+ value literal.type == "boolean" && literal.value == true
203
+ } else mode == "only-allowed-literals" && n is ts_literal && (n.literal_value.type == "boolean" || n.literal_value.type == "number" && contains([0, 1], n.literal_value.value))
204
+ fn condition_callback(n) = if n is (ts_arrow, ts_function) then when {
205
+ n.body not ts_block then n.body.node
206
+ count(n.body.body.node) == 1 && first(n.body.body.node) is ts_return then first(n.body.body.node).argument.node
207
+ else then null
208
+ } else null
209
+ fn condition_array_method(n) = n.callee is (ts_member, ts_index) && contains(["every", "filter", "find", "findIndex", "findLast", "findLastIndex", "some"], n.callee.static_name)
210
+ fn condition_array_callback(n) = condition_array_method(n) && strict_boolean_array(n.callee.object.node)
211
+ fn condition_literal_arm(arm) = {
212
+ value parts: filter(arm, part => part.identity.kind == "value")
213
+ value part: first(parts)
214
+ value kind: when {
215
+ contains(part.traits, "null") then "null"
216
+ contains(part.traits, "undefined") then "undefined"
217
+ contains(part.traits, "boolean") then "boolean"
218
+ contains(part.traits, "number") then "number"
219
+ contains(part.traits, "string") then "string"
220
+ contains(part.traits, "bigint") then "bigint"
221
+ else then null
222
+ }
223
+ value if kind == null || !all(parts, other => other.identity == part.identity) then null else {
224
+ type: kind,
225
+ value: when {
226
+ kind == "bigint" then part.identity.value.value "n"
227
+ kind == "undefined" then "undefined"
228
+ else then part.identity.value
229
+ }
230
+ }
231
+ }
232
+ fn condition_literal(n) = {
233
+ value arms: value_constituents(n)
234
+ value literals: map(arms, arm => condition_literal_arm(arm))
235
+ value first_literal: first(literals)
236
+ value if arms != null && count(arms) > 0 && first_literal != null && all(literals, literal => literal == first_literal)
237
+ then first_literal else null
238
+ }
239
+ # A static value as TypeScript prints its literal type: `"a"`, `1`, `2n`, `true`, `null`.
240
+ fn condition_literal_text(literal) = when {
241
+ literal.type == "string" then ts_value_string(literal.value)
242
+ literal.type == "null" then "null"
243
+ else then str(literal.value)
244
+ }
245
+ fn condition_equal(left, right, strict) = when {
246
+ left.type == right.type then contains(["null", "undefined"], left.type) || js_compare(left, right) == 0
247
+ strict then false
248
+ contains(["null", "undefined"], left.type) || contains(["null", "undefined"], right.type) then contains(["null", "undefined"], left.type) && contains(["null", "undefined"], right.type)
249
+ else then js_compare(left, right) == 0
250
+ }
251
+ fn condition_compare(left, right, op) = when {
252
+ contains(["==", "==="], op) then condition_equal(left, right, op == "===")
253
+ contains(["!=", "!=="], op) then !condition_equal(left, right, op == "!==")
254
+ op == "<" then js_compare(left, right) == -1
255
+ op == ">" then js_compare(left, right) == 1
256
+ op == "<=" then js_compare(left, right) != null && js_compare(left, right) <= 0
257
+ else then js_compare(left, right) != null && js_compare(left, right) >= 0
258
+ }
259
+ fn condition_comparable_nullish(literal, other, loose) = !contains(["null", "undefined"], literal.type)
260
+ || !condition_known(condition_domain(other)) || ts_value_has_trait(other, literal.type) == true
261
+ || any(value_constituent_traits(other, ["null", "undefined", "boolean", "number", "string", "bigint"]) ?? [], arm =>
262
+ all(arm, part => contains(part.traits, literal.type) || contains(part.uncertain_traits, literal.type)))
263
+ || literal.type == "undefined" && ts_value_has_void(other) == true
264
+ || loose && condition_domain(other).nullish
265
+ fn condition_structural_truthy(arms) = arms != null && count(arms) > 0 && all(arms, arm =>
266
+ any(arm, part => contains(part.traits, "truthy") || contains(part.traits, "structural")
267
+ || contains(part.traits, "object")) && all(arm, part =>
268
+ !any(part.traits, trait => contains(["any", "unknown", "error", "null", "undefined", "falsy"], trait))
269
+ && (!any(part.traits, trait => contains(["boolean", "number", "bigint", "string"], trait))
270
+ || contains(part.traits, "truthy"))))
271
+
272
+ in ts_binary let condition_comparison { left: left.node, right: right.node, operator: operator, report: node }
273
+ in ts_case let condition_comparison { left: parent.discriminant.node, right: test.node, operator: "===", report: test.node }
274
+
275
+ @macro condition_boolean_policy {
276
+ when checked.node != null && (checked.subject not ts_logical || checked.subject.operator == "??")
277
+ let values value_constituent_traits(checked.subject, ["any", "unknown", "error", "null", "undefined", "falsy", "boolean", "number", "bigint", "string", "truthy", "structural", "object"])
278
+ let never_value values != null && count(values) == 0
279
+ let certainly_truthy condition_structural_truthy(values) || checked.subject always truthy
280
+ let certainly_falsy if certainly_truthy then false else checked.subject always falsy
281
+ when condition_unchecked_index(checked.subject) == false && !(ts_compiler_option(node, "noUncheckedIndexedAccess") == true && condition_access(checked.subject).from_index)
282
+ when never_value || certainly_truthy || certainly_falsy
283
+ report at { start: checked.node.offset, end: checked.node.end_offset }
284
+ warn when {
285
+ never_value then "Unnecessary conditional, value is `never`."
286
+ certainly_falsy != checked.negated then "Unnecessary conditional, value is always falsy."
287
+ else then "Unnecessary conditional, value is always truthy."
288
+ }
289
+ }
290
+
291
+ in ts_if.test, ts_conditional.test rule es-no-unnecessary-condition {
292
+ let checked condition_boolean(node)
293
+ @condition_boolean_policy
294
+ }
295
+ in ts_while.test, ts_do_while.test, ts_for.test rule es-no-unnecessary-condition:loop {
296
+ option allowConstantLoopConditions ("always" | "never" | "only-allowed-literals") = "never" "Allow selected constant loop conditions"
297
+ when !{ value node; unparenthesised_node; condition_loop_allowed allowConstantLoopConditions }
298
+ let checked condition_boolean(node)
299
+ @condition_boolean_policy
300
+ }
301
+ in ts_logical, ts_assignment rule es-no-unnecessary-condition:logical {
302
+ when contains(["&&", "||", "&&=", "||="], operator)
303
+ let checked condition_boolean(left.node)
304
+ @condition_boolean_policy
305
+ }
306
+ in ts_logical, ts_assignment rule es-no-unnecessary-condition:nullish {
307
+ when contains(["??", "??="], operator)
308
+ let subject unparenthesised_node(left.node)
309
+ let domain condition_domain(subject)
310
+ when condition_known(domain)
311
+ when condition_never(domain) || !domain.non_nullish && ts_value_has_void(subject) == false || !domain.nullish && condition_unchecked_index(subject) == false && condition_array_origin(subject) == false && !condition_index_fallback(subject, ts_compiler_option(node, "noUncheckedIndexedAccess") == true) && !condition_access(subject).optional
312
+ report at { start: subject.offset, end: subject.end_offset }
313
+ warn when {
314
+ condition_never(domain) then "Unnecessary conditional, value is `never`."
315
+ !domain.non_nullish then "Unnecessary conditional, left-hand side of `??` operator is always `null` or `undefined`."
316
+ else then "Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined."
317
+ }
318
+ }
319
+ in ts_member, ts_index, ts_call rule es-no-unnecessary-condition:optional {
320
+ when optional != null
321
+ let subject condition_base(node)
322
+ let domain condition_access_domains(subject)
323
+ when condition_known(domain) && !domain.nullish && condition_unchecked_index(subject) == false && (condition_array_origin(node) == false || ts_compiler_option(node, "noUncheckedIndexedAccess") == true) && !condition_index_fallback(subject, ts_compiler_option(node, "noUncheckedIndexedAccess") == true)
324
+ report at { start: optional.offset, end: optional.end_offset }
325
+ warn "Unnecessary optional chain on a non-nullish value."
326
+ }
327
+ in ts_call let no_unnecessary_condition_truthiness_assertion_checked false
328
+ in ts_call let no_unnecessary_condition_predicate_checked false
329
+ in ts_call rule es-no-unnecessary-condition:truthiness-assertion {
330
+ when no_unnecessary_condition_truthiness_assertion_checked
331
+ when condition_predicate.asserts && condition_predicate.target == null
332
+ let checked {
333
+ value argument.nodes
334
+ slice condition_predicate.argument_index (condition_predicate.argument_index + 1)
335
+ first
336
+ condition_boolean
337
+ }
338
+ @condition_boolean_policy
339
+ }
340
+ in ts_call rule es-no-unnecessary-condition:predicate {
341
+ when no_unnecessary_condition_predicate_checked
342
+ let predicate condition_predicate
343
+ when predicate.target != null && condition_known(predicate.argument) && predicate.to_target && (predicate.from_target || predicate.target_union)
344
+ let subject { value argument.nodes; slice predicate.argument_index (predicate.argument_index + 1); first }
345
+ report at { start: subject.offset, end: subject.end_offset }
346
+ warn "Unnecessary conditional, expression already has the type being checked by the " (if predicate.asserts then "assertion function" else "type guard") "."
347
+ }
348
+ in ts_call rule es-no-unnecessary-condition:callback {
349
+ when condition_array_callback(node) && count(argument.nodes) > 0
350
+ let checked {
351
+ value argument.nodes
352
+ first
353
+ condition_callback
354
+ condition_boolean
355
+ }
356
+ @condition_boolean_policy
357
+ }
358
+ in ts_call rule es-no-unnecessary-condition:callback-return {
359
+ when condition_array_callback(node) && count(argument.nodes) > 0 && { value argument.nodes; first; condition_callback } == null
360
+ let subject first(argument.nodes)
361
+ let returns condition_returns(subject)
362
+ when count(returns) > 0 && all(returns, domain => condition_known(domain))
363
+ when all(returns, domain => !domain.falsy) || all(returns, domain => !domain.truthy)
364
+ report at { start: subject.offset, end: subject.end_offset }
365
+ warn if all(returns, domain => !domain.falsy) then "This callback should return a conditional, but return is always truthy." else "This callback should return a conditional, but return is always falsy."
366
+ }
367
+ in ts_program rule es-no-unnecessary-condition:strict {
368
+ option allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing boolean = false "Run without strict null checks"
369
+ when ts_compiler_option(node, "strictNullChecks") == false && !allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing
370
+ report at { start: 0, end: 0 }
371
+ warn "This rule requires the `strictNullChecks` compiler option to be turned on to function correctly."
372
+ }
373
+ in ts_binary, ts_case rule es-no-unnecessary-condition:comparison {
374
+ let comparison condition_comparison
375
+ when comparison.right != null && contains(["==", "===", "!=", "!==", "<", ">", "<=", ">="], comparison.operator)
376
+ let left_value condition_literal(comparison.left)
377
+ let right_value condition_literal(comparison.right)
378
+ when left_value != null && right_value != null
379
+ let answer condition_compare(left_value, right_value, comparison.operator)
380
+ report at { start: comparison.report.offset, end: comparison.report.end_offset }
381
+ warn "Unnecessary conditional, comparison is always " answer ", since `" condition_literal_text(left_value) " " comparison.operator " " condition_literal_text(right_value) "` is " answer "."
382
+ }
383
+ in ts_binary, ts_case rule es-no-unnecessary-condition:overlap {
384
+ let comparison condition_comparison
385
+ when ts_compiler_option(node, "strictNullChecks") != false && comparison.right != null && contains(["==", "===", "!=", "!==", "<", ">", "<=", ">="], comparison.operator)
386
+ let left_value condition_literal(comparison.left)
387
+ let right_value condition_literal(comparison.right)
388
+ when left_value == null || right_value == null
389
+ let loose contains(["==", "!="], comparison.operator)
390
+ when !condition_comparable_nullish(left_value, comparison.right, loose) || !condition_comparable_nullish(right_value, comparison.left, loose)
391
+ report at { start: comparison.report.offset, end: comparison.report.end_offset }
392
+ warn "Unnecessary conditional, the types have no overlap."
393
+ }
394
+
395
+ @macro strict_boolean_options {
396
+ option allowAny boolean = false "Allow any and unknown"
397
+ option allowNullableBoolean boolean = false "Allow nullable booleans"
398
+ option allowNullableEnum boolean = false "Allow nullable enums"
399
+ option allowNullableNumber boolean = false "Allow nullable numbers"
400
+ option allowNullableObject boolean = true "Allow nullable objects"
401
+ option allowNullableString boolean = false "Allow nullable strings"
402
+ option allowNumber boolean = true "Allow numbers and bigints"
403
+ option allowString boolean = true "Allow strings"
404
+ option allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing boolean = false "Run without strict null checks"
405
+ let strict_boolean_settings {
406
+ any: allowAny, boolean: allowNullableBoolean, enum: allowNullableEnum,
407
+ number: allowNullableNumber, object: allowNullableObject, string: allowNullableString,
408
+ primitive_number: allowNumber, primitive_string: allowString
409
+ }
410
+ }
411
+
412
+ fn strict_boolean_exact(types, expected) = count(types) == count(expected) && all(types, variant => contains(expected, variant))
413
+ fn strict_boolean_report(types, options) = when {
414
+ strict_boolean_exact(types, ["boolean"]) || strict_boolean_exact(types, ["truthy boolean"]) || strict_boolean_exact(types, ["never"]) || strict_boolean_exact(types, ["nullish", "truthy boolean"]) then null
415
+ strict_boolean_exact(types, ["nullish"]) then "nullish"
416
+ strict_boolean_exact(types, ["nullish", "boolean"]) then if options.boolean then null else "nullable boolean"
417
+ options.primitive_number && strict_boolean_exact(types, ["nullish", "truthy number"]) || options.primitive_string && strict_boolean_exact(types, ["nullish", "truthy string"]) then null
418
+ strict_boolean_exact(types, ["string"]) || strict_boolean_exact(types, ["truthy string"]) then if options.primitive_string then null else "string"
419
+ strict_boolean_exact(types, ["nullish", "string"]) then if options.string then null else "nullable string"
420
+ strict_boolean_exact(types, ["number"]) || strict_boolean_exact(types, ["truthy number"]) then if options.primitive_number then null else "number"
421
+ strict_boolean_exact(types, ["nullish", "number"]) then if options.number then null else "nullable number"
422
+ strict_boolean_exact(types, ["object"]) then "object"
423
+ strict_boolean_exact(types, ["nullish", "object"]) then if options.object then null else "nullable object"
424
+ contains(types, "nullish") && contains(types, "enum") && count(types) >= 3 && all(types, variant => contains(["nullish", "enum", "number", "truthy number", "string", "truthy string"], variant)) then if options.enum then null else "nullable enum"
425
+ strict_boolean_exact(types, ["any"]) then if options.any then null else "any"
426
+ else then "other"
427
+ }
428
+ fn strict_boolean_message(kind, context) = when {
429
+ kind == "nullish" then "Unexpected nullish value in conditional. The condition is always false."
430
+ kind == "other" then "Unexpected value in conditional. A boolean expression is required."
431
+ kind == "object" then "Unexpected object value in " context ". The condition is always true."
432
+ else then "Unexpected " kind " value in " context ". " (when {
433
+ kind == "any" then "An explicit comparison or type conversion is required."
434
+ kind == "nullable boolean" then "Please handle the nullish case explicitly."
435
+ kind == "nullable enum" || kind == "nullable number" then "Please handle the nullish/zero/NaN cases explicitly."
436
+ kind == "nullable object" then "An explicit null check is required."
437
+ kind == "nullable string" then "Please handle the nullish/empty cases explicitly."
438
+ kind == "number" then "An explicit zero/NaN check is required."
439
+ else then "An explicit empty string check is required."
440
+ })
441
+ }
442
+ fn strict_boolean_asserted(n) = n.parent is ts_call && n.parent.condition_predicate.asserts && n.parent.condition_predicate.target == null && {
443
+ value n.parent.argument.nodes
444
+ slice n.parent.condition_predicate.argument_index (n.parent.condition_predicate.argument_index + 1)
445
+ first
446
+ } == n
447
+ fn strict_boolean_use(n) = {
448
+ value n
449
+ walk (current) if current.parent is ts_parenthesized || current.parent is ts_logical && current.parent.operator != "??" && current.parent.right.node == current then current.parent else null
450
+ any (current) current.parent is (ts_if, ts_while, ts_do_while, ts_for, ts_conditional) && current.parent.test.node == current || current.parent is ts_unary && current.parent.operator == "!" || current.parent is ts_logical && current.parent.operator != "??" && current.parent.left.node == current || strict_boolean_asserted(current)
451
+ }
452
+ fn strict_boolean_array(n) = {
453
+ value shape: ts_value_array_shape(n)
454
+ value shape.known && (shape.array || shape.tuple)
455
+ }
456
+ fn strict_boolean_value_has(arm, trait) = any(arm, part => contains(part.traits, trait))
457
+ fn strict_boolean_value_primitive(arm) = when {
458
+ strict_boolean_value_has(arm, "intersection") then if strict_boolean_value_has(arm, "boolean") then "boolean" else "object"
459
+ strict_boolean_value_has(arm, "any") || strict_boolean_value_has(arm, "unknown") then "any"
460
+ strict_boolean_value_has(arm, "null") || strict_boolean_value_has(arm, "undefined") then "nullish"
461
+ strict_boolean_value_has(arm, "boolean") then "boolean"
462
+ strict_boolean_value_has(arm, "string") then "string"
463
+ strict_boolean_value_has(arm, "number") || strict_boolean_value_has(arm, "bigint") then "number"
464
+ strict_boolean_value_has(arm, "parameter") && !any(arm, part =>
465
+ any(part.traits, trait => contains(["object", "symbol", "callable", "constructible", "structural"], trait))) then "any"
466
+ else then "object"
467
+ }
468
+ fn strict_boolean_value_complete(arms) = arms != null && all(arms, arm =>
469
+ (!strict_boolean_value_has(arm, "error") || strict_boolean_value_has(arm, "any")) && any(arm, part =>
470
+ any(part.traits, trait => contains(["any", "unknown", "parameter", "intersection", "null", "undefined", "boolean", "string", "number", "bigint", "object", "symbol", "callable", "constructible", "structural"], trait))))
471
+ fn strict_boolean_value_group(arms, primitive) = filter(arms, arm => strict_boolean_value_primitive(arm) == primitive)
472
+ fn strict_boolean_value_variant(arms, primitive) = {
473
+ value group: strict_boolean_value_group(arms, primitive)
474
+ value when {
475
+ count(group) == 0 then null
476
+ contains(["boolean", "string", "number"], primitive) && all(group, arm => strict_boolean_value_has(arm, "truthy"))
477
+ then "truthy " primitive
478
+ else then primitive
479
+ }
480
+ }
481
+ fn strict_boolean_value_variants(arms) = when {
482
+ !strict_boolean_value_complete(arms) then null
483
+ count(arms) == 0 then ["never"]
484
+ else then {
485
+ value variants: {
486
+ value ["nullish", "boolean", "string", "number", "object", "any"]
487
+ map (primitive) strict_boolean_value_variant(arms, primitive)
488
+ filter (variant) variant != null
489
+ }
490
+ value variants (if any(arms, arm => strict_boolean_value_has(arm, "enum")) then ["enum"] else [])
491
+ }
492
+ }
493
+ fn strict_boolean_callback_variants(n) = {
494
+ value signatures: call_results(n)
495
+ value headers: { value signatures; flat_map (arm) arm }
496
+ value parts: if signatures != null && count(headers) > 0 && all(headers, header => header.result_known)
497
+ then { value headers; flat_map (header) header.result } else null
498
+ value strict_boolean_value_variants(parts)
499
+ }
500
+
501
+ in ts_if.test, ts_while.test, ts_do_while.test, ts_for.test, ts_conditional.test, ts_unary.argument, ts_logical.left, ts_logical.right, ts_call.argument rule es-strict-boolean-expressions {
502
+ @strict_boolean_options
503
+ let subject unparenthesised_node(node)
504
+ when strict_boolean_use(subject) && (subject not ts_logical || subject.operator == "??")
505
+ let variants { value subject; value_constituents; strict_boolean_value_variants }
506
+ when variants != null
507
+ let failure strict_boolean_report(variants, strict_boolean_settings)
508
+ when failure != null
509
+ report at { start: subject.offset, end: subject.end_offset }
510
+ warn strict_boolean_message(failure, "conditional")
511
+ }
512
+ in ts_call rule es-strict-boolean-expressions:callback {
513
+ @strict_boolean_options
514
+ when condition_array_method(node) && strict_boolean_array(callee.object.node) && count(argument.nodes) > 0
515
+ let subject { value argument.nodes; first; unparenthesised_node }
516
+ let asynchronous subject is (ts_arrow, ts_function) && contains(subject.modifier, "async")
517
+ let variants strict_boolean_callback_variants(subject)
518
+ when asynchronous || variants != null
519
+ let failure if asynchronous then "async" else strict_boolean_report(variants, strict_boolean_settings)
520
+ when failure != null
521
+ report at { start: subject.offset, end: subject.end_offset }
522
+ warn if asynchronous then "Predicate function should not be 'async'; expected a boolean return type." else strict_boolean_message(failure, "array predicate return type")
523
+ }
524
+ in ts_program rule es-strict-boolean-expressions:strict {
525
+ @strict_boolean_options
526
+ when ts_compiler_option(node, "strictNullChecks") == false && !allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing
527
+ report at { start: 0, end: 0 }
528
+ warn "This rule requires the `strictNullChecks` compiler option to be turned on to function correctly."
529
+ }
530
+
531
+ }