@inthedark/dxlint 0.1.0 → 0.1.2

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.
@@ -1,205 +1,205 @@
1
- import .ts/constants
2
- import .ts/values/inspection
3
- import .ts/typed/awaitability
4
-
5
- define {
6
-
7
- in ts_node let promise_boolean_test
8
- is (ts_if.test, ts_while.test, ts_do_while.test, ts_for.test, ts_conditional.test)
9
- || is ts_unary.argument && node.parent.operator == "!"
10
- || (parent is (ts_parenthesized, ts_logical) || parent is ts_conditional && not ts_conditional.test)
11
- && parent.promise_boolean_test == true
12
- in ts_node let promise_boolean_left
13
- is ts_logical.left && node.parent.operator != "??"
14
- || (parent is ts_parenthesized || is ts_logical.left && node.parent.operator != "??"
15
- || parent is ts_conditional && not ts_conditional.test)
16
- && parent.promise_boolean_left == true
17
-
18
- in ts_parenthesized.expression, ts_logical.left, ts_logical.right, ts_conditional.test, ts_conditional.consequent, ts_conditional.alternate, ts_if.test, ts_while.test, ts_do_while.test, ts_for.test, ts_unary.argument rule es-no-misused-promises:conditionals {
19
- option flagUnions "none" | "strict" | "all" = "none" "Which mixed promise unions to flag"
20
- when not (ts_parenthesized, ts_logical, ts_conditional, ts_null, ts_boolean, ts_number, ts_bigint, ts_string)
21
- when !(is ts_unary && node.operator == "!")
22
- when parent is (ts_parenthesized, ts_logical, ts_conditional)
23
- || is (ts_if.test, ts_while.test, ts_do_while.test, ts_for.test)
24
- || is ts_unary.argument && node.parent.operator == "!"
25
- when promise_boolean_test || promise_boolean_left
26
- # Comparisons and these unary operators make primitives: never thenable.
27
- when !(is ts_binary && contains(["===", "!==", "==", "!=", "<", "<=", ">", ">=", "in", "instanceof"], node.operator))
28
- when !(is ts_unary && contains(["typeof", "void", "delete", "+", "~"], node.operator))
29
- when node could thenable && (flagUnions == "all" || node always thenable || flagUnions == "strict" && ts_await_union_equivalent(node) == true)
30
- warn "Expected non-Promise value in a boolean conditional."
31
- }
32
-
33
- in ts_spread rule es-no-misused-promises:spreads {
34
- when parent is ts_object
35
- when argument could thenable
36
- report at unparenthesised_node(argument.node)
37
- warn "Expected a non-Promise value to be spread in an object."
38
- }
39
-
40
- fn promise_return_arms(signatures) = {
41
- value headers: flat_map(signatures ?? [], arm => arm)
42
- value returns: map(headers, header => if header.result == null then null else value_constituent_traits(header.result, ["intersection", "any", "unknown", "error", "thenable", "void"]))
43
- value if signatures == null || count(headers) == 0 || any(returns, arms => arms == null) then null
44
- else flat_map(returns, arms => arms)
45
- }
46
- fn promise_returns_known(arms) = arms != null && all(arms, arm => all(arm, part =>
47
- contains(part.traits, "intersection") || !any(["any", "unknown", "error"], trait => contains(part.traits, trait))
48
- && !contains(part.uncertain_traits, "thenable") && !contains(part.uncertain_traits, "void")))
49
- fn promise_returns_thenable(arms) = promise_returns_known(arms)
50
- && any(arms, arm => any(arm, part => contains(part.traits, "thenable")))
51
- fn promise_returns_void(arms) = promise_returns_known(arms) && count(arms) > 0
52
- && any(arms, arm => any(arm, part => contains(part.traits, "void")))
53
- && !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
- }
63
- }
64
- # ESLint unions every call signature and drops an argument index when any
65
- # signature's parameter at that index accepts a thenable-returning function.
66
- fn promise_void_overload_accepts(n) = if n.parent.node not (ts_call, ts_new) then false else {
67
- value call: n.parent.node
68
- value signatures: if call is ts_new then value_constructors(call.callee.node) else value_signatures(call.callee.node)
69
- value headers: flat_map(signatures ?? [], arm => arm)
70
- value index: index_of(call.argument.nodes, n)
71
- value chosen: map(headers, header => first(slice(header.parameters ?? [], index, index + 1)))
72
- value found: any(chosen, item => item != null && promise_returns_thenable(promise_return_arms(value_signatures(item))))
73
- }
74
- fn promise_dispose_void_context(n) = {
75
- value key: {global: "Symbol", class: "value", path: ["dispose"]}
76
- # A disposal contract is only asked of `using` bindings and of targets whose
77
- # declared type can hold one; anything else answers without a value lookup.
78
- value using: n is ts_declarator.init && n.parent.parent.kind == "using"
79
- value expected: if using then null else value_expected_handle(n)
80
- value if !using && expected == null then false else {
81
- value source: value_lookup(n, key).member
82
- value source != null && promise_returns_thenable(promise_return_arms(value_signatures(source)))
83
- && (using || {
84
- value target: value_lookup(expected, key).member
85
- value target != null && promise_returns_void(promise_return_arms(value_signatures(target)))
86
- })
87
- }
88
- }
89
- fn promise_property_source(n) = if n is ts_property then n.value.node ?? n else n
90
- fn promise_property_site(n) = {
91
- value n: unparenthesised_node(n)
92
- value if n is (ts_arrow, ts_function, ts_method) then n.return_type.node ?? function_head_site(n) else n
93
- }
94
- in ts_node let promise_builtin_instance ts_value_builtin_type(node, ["Promise"]) == true
95
- fn promise_finally_member(n) = n is (ts_member, ts_index)
96
- && (n.static_name.nonempty ?? ts_runtime_string(n.index.node)) == "finally"
97
- && n.object.promise_builtin_instance
98
- in ts_call let promise_finally_callback { value callee.node; unparenthesised_node; promise_finally_member }
99
-
100
- fn promise_async_policy(n, names) = {
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)
103
- value headers: flat_map(signatures ?? [], arm => arm)
104
- value returns: map(headers, header => {
105
- value arms: if header.result == null then null else value_parts(header.result)
106
- value matches: map(arms ?? [], arm => {
107
- value parts: filter(arm.parts, part => !contains(part.traits, "intersection"))
108
- value named: map(parts, part => part.carrier != null
109
- && any(ts_value_ancestry(part).parts, base => contains(names, base.carrier.description.name)))
110
- value {every: count(named) > 0 && all(named, found => found), some: any(named, found => found)}
111
- })
112
- value {
113
- known: arms != null && all(arms, arm => all(arm.parts, part => part.carrier_known)),
114
- dynamic: any(arms ?? [], arm => any(arm.parts, part =>
115
- contains(part.traits, "any") || contains(part.traits, "unknown"))),
116
- every: count(matches) > 0 && all(matches, result => result.every),
117
- some: any(matches, result => result.some),
118
- mixed: count(arms) > 1 && !all(matches, result => result.every),
119
- }
120
- })
121
- value {
122
- known: signatures != null && count(returns) > 0 && all(returns, result => result.known),
123
- dynamic: any(returns, result => result.dynamic),
124
- required: all(returns, result => if n.return_type != null then result.every else result.some),
125
- mixed: any(returns, result => result.mixed),
126
- }
127
- }
128
-
129
- in ts_call.argument, ts_new.argument, ts_template.expression rule es-no-misused-promises:void-arguments {
130
- 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
- report at unparenthesised_node(node)
134
- warn "Promise returned in function argument where a void return was expected."
135
- }
136
-
137
- in ts_call.argument rule es-no-misused-promises:predicates {
138
- when node.index == 0 && condition_array_callback(parent)
139
- when promise_returns_thenable(promise_return_arms(value_signatures(node)))
140
- report at unparenthesised_node(node)
141
- warn "Expected a non-Promise value to be returned."
142
- }
143
-
144
- in ts_declarator.init, ts_parameter.init, ts_assignment.right rule es-no-misused-promises:void-variables {
145
- # An unannotated declarator gives its initializer no expected type.
146
- when ((!(is ts_declarator.init) || parent.type != null) && promise_void_context(node))
147
- || promise_dispose_void_context(node)
148
- report at unparenthesised_node(node)
149
- warn "Promise-returning function provided to variable where a void return was expected."
150
- }
151
-
152
- in ts_property, ts_method, ts_class_property.init rule es-no-misused-promises:void-properties {
153
- when not ts_method || parent is ts_object
154
- when not ts_class_property.init || node.parent.type != null
155
- let promise_source promise_property_source(node)
156
- when promise_void_context(promise_source)
157
- report at promise_property_site(promise_source)
158
- warn "Promise-returning function provided to property where a void return was expected."
159
- }
160
-
161
- in ts_return.argument, ts_arrow.body rule es-no-misused-promises:void-returns {
162
- when promise_void_context(node)
163
- report at unparenthesised_node(node)
164
- warn "Promise-returning function provided to return value where a void return was expected."
165
- }
166
-
167
- in ts_jsx_expression.expression rule es-no-misused-promises:void-attributes {
168
- when parent.parent is ts_jsx_attribute
169
- when promise_void_context(node)
170
- report at parent.node
171
- warn "Promise-returning function provided to attribute where a void return was expected."
172
- }
173
-
174
- fn promise_inherited_void_targets(n) = {
175
- value owner: n.parent.node
176
- value declared: owner.implements.nodes (if owner is ts_interface then owner.extends.nodes else [])
177
- value constructed: if owner is ts_class && owner.extends != null then {
178
- value headers: value_constructors(owner.extends.node)
179
- value map(flat_map(headers ?? [], arm => arm), header => {heritage: owner.extends.node, value: header.result})
180
- } else []
181
- # An interface extending a constructor type (`typeof C`) is checked
182
- # against the instances it constructs too, as TypeScript ESLint does.
183
- value instances: if owner is ts_interface then flat_map(owner.extends.nodes, heritage => {
184
- value headers: value_constructors(heritage)
185
- value map(flat_map(headers ?? [], arm => arm), header => {heritage: heritage, value: header.result})
186
- }) else []
187
- value targets: map(declared, heritage => {heritage: heritage, value: heritage}) constructed instances
188
- value filter(targets, target => target.value != null && {
189
- value lookup: value_lookup(target.value, n.static_name)
190
- value lookup.member != null && any(lookup.alternatives, alternative => alternative.matched
191
- && any(alternative.sources, source => source.kind == "named"))
192
- && promise_returns_void(promise_return_arms(value_signatures(lookup.member)))
193
- })
194
- }
195
- in ts_method, ts_method_signature, ts_class_property, ts_property_signature rule es-no-misused-promises:void-inherited-methods {
196
- when !is_static
197
- when static_name != null && parent is (ts_class, ts_interface)
198
- when promise_returns_thenable(promise_return_arms(value_signatures(node)))
199
- each target in promise_inherited_void_targets(node) {
200
- report at { start: offset, end: if byte_slice(source, end_offset, end_offset + 1) == ";" then end_offset + 1 else end_offset }
201
- warn "Promise-returning method provided where a void return was expected by extended/implemented type '" target.heritage.text "'."
202
- }
203
- }
204
-
205
- }
1
+ import .ts/constants
2
+ import .ts/values/inspection
3
+ import .ts/typed/awaitability
4
+
5
+ define {
6
+
7
+ in ts_node let promise_boolean_test
8
+ is (ts_if.test, ts_while.test, ts_do_while.test, ts_for.test, ts_conditional.test)
9
+ || is ts_unary.argument && node.parent.operator == "!"
10
+ || (parent is (ts_parenthesized, ts_logical) || parent is ts_conditional && not ts_conditional.test)
11
+ && parent.promise_boolean_test == true
12
+ in ts_node let promise_boolean_left
13
+ is ts_logical.left && node.parent.operator != "??"
14
+ || (parent is ts_parenthesized || is ts_logical.left && node.parent.operator != "??"
15
+ || parent is ts_conditional && not ts_conditional.test)
16
+ && parent.promise_boolean_left == true
17
+
18
+ in ts_parenthesized.expression, ts_logical.left, ts_logical.right, ts_conditional.test, ts_conditional.consequent, ts_conditional.alternate, ts_if.test, ts_while.test, ts_do_while.test, ts_for.test, ts_unary.argument rule es-no-misused-promises:conditionals {
19
+ option flagUnions "none" | "strict" | "all" = "none" "Which mixed promise unions to flag"
20
+ when not (ts_parenthesized, ts_logical, ts_conditional, ts_null, ts_boolean, ts_number, ts_bigint, ts_string)
21
+ when !(is ts_unary && node.operator == "!")
22
+ when parent is (ts_parenthesized, ts_logical, ts_conditional)
23
+ || is (ts_if.test, ts_while.test, ts_do_while.test, ts_for.test)
24
+ || is ts_unary.argument && node.parent.operator == "!"
25
+ when promise_boolean_test || promise_boolean_left
26
+ # Comparisons and these unary operators make primitives: never thenable.
27
+ when !(is ts_binary && contains(["===", "!==", "==", "!=", "<", "<=", ">", ">=", "in", "instanceof"], node.operator))
28
+ when !(is ts_unary && contains(["typeof", "void", "delete", "+", "~"], node.operator))
29
+ when node could thenable && (flagUnions == "all" || node always thenable || flagUnions == "strict" && ts_await_union_equivalent(node) == true)
30
+ warn "Expected non-Promise value in a boolean conditional."
31
+ }
32
+
33
+ in ts_spread rule es-no-misused-promises:spreads {
34
+ when parent is ts_object
35
+ when argument could thenable
36
+ report at unparenthesised_node(argument.node)
37
+ warn "Expected a non-Promise value to be spread in an object."
38
+ }
39
+
40
+ fn promise_return_arms(signatures) = {
41
+ value headers: flat_map(signatures ?? [], arm => arm)
42
+ value returns: map(headers, header => if header.result == null then null else value_constituent_traits(header.result, ["intersection", "any", "unknown", "error", "thenable", "void"]))
43
+ value if signatures == null || count(headers) == 0 || any(returns, arms => arms == null) then null
44
+ else flat_map(returns, arms => arms)
45
+ }
46
+ fn promise_returns_known(arms) = arms != null && all(arms, arm => all(arm, part =>
47
+ contains(part.traits, "intersection") || !any(["any", "unknown", "error"], trait => contains(part.traits, trait))
48
+ && !contains(part.uncertain_traits, "thenable") && !contains(part.uncertain_traits, "void")))
49
+ fn promise_returns_thenable(arms) = promise_returns_known(arms)
50
+ && any(arms, arm => any(arm, part => contains(part.traits, "thenable")))
51
+ fn promise_returns_void(arms) = promise_returns_known(arms) && count(arms) > 0
52
+ && any(arms, arm => any(arm, part => contains(part.traits, "void")))
53
+ && !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
+ }
63
+ }
64
+ # ESLint unions every call signature and drops an argument index when any
65
+ # signature's parameter at that index accepts a thenable-returning function.
66
+ fn promise_void_overload_accepts(n) = if n.parent.node not (ts_call, ts_new) then false else {
67
+ value call: n.parent.node
68
+ value signatures: if call is ts_new then value_constructors(call.callee.node) else value_signatures(call.callee.node)
69
+ value headers: flat_map(signatures ?? [], arm => arm)
70
+ value index: index_of(call.argument.nodes, n)
71
+ value chosen: map(headers, header => first(slice(header.parameters ?? [], index, index + 1)))
72
+ value found: any(chosen, item => item != null && promise_returns_thenable(promise_return_arms(value_signatures(item))))
73
+ }
74
+ fn promise_dispose_void_context(n) = {
75
+ value key: {global: "Symbol", class: "value", path: ["dispose"]}
76
+ # A disposal contract is only asked of `using` bindings and of targets whose
77
+ # declared type can hold one; anything else answers without a value lookup.
78
+ value using: n is ts_declarator.init && n.parent.parent.kind == "using"
79
+ value expected: if using then null else value_expected_handle(n)
80
+ value if !using && expected == null then false else {
81
+ value source: value_lookup(n, key).member
82
+ value source != null && promise_returns_thenable(promise_return_arms(value_signatures(source)))
83
+ && (using || {
84
+ value target: value_lookup(expected, key).member
85
+ value target != null && promise_returns_void(promise_return_arms(value_signatures(target)))
86
+ })
87
+ }
88
+ }
89
+ fn promise_property_source(n) = if n is ts_property then n.value.node ?? n else n
90
+ fn promise_property_site(n) = {
91
+ value n: unparenthesised_node(n)
92
+ value if n is (ts_arrow, ts_function, ts_method) then n.return_type.node ?? function_head_site(n) else n
93
+ }
94
+ in ts_node let promise_builtin_instance ts_value_builtin_type(node, ["Promise"]) == true
95
+ fn promise_finally_member(n) = n is (ts_member, ts_index)
96
+ && (n.static_name.nonempty ?? ts_runtime_string(n.index.node)) == "finally"
97
+ && n.object.promise_builtin_instance
98
+ in ts_call let promise_finally_callback { value callee.node; unparenthesised_node; promise_finally_member }
99
+
100
+ fn promise_async_policy(n, names) = {
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)
103
+ value headers: flat_map(signatures ?? [], arm => arm)
104
+ value returns: map(headers, header => {
105
+ value arms: if header.result == null then null else value_parts(header.result)
106
+ value matches: map(arms ?? [], arm => {
107
+ value parts: filter(arm.parts, part => !contains(part.traits, "intersection"))
108
+ value named: map(parts, part => part.carrier != null
109
+ && any(ts_value_ancestry(part).parts, base => contains(names, base.carrier.description.name)))
110
+ value {every: count(named) > 0 && all(named, found => found), some: any(named, found => found)}
111
+ })
112
+ value {
113
+ known: arms != null && all(arms, arm => all(arm.parts, part => part.carrier_known)),
114
+ dynamic: any(arms ?? [], arm => any(arm.parts, part =>
115
+ contains(part.traits, "any") || contains(part.traits, "unknown"))),
116
+ every: count(matches) > 0 && all(matches, result => result.every),
117
+ some: any(matches, result => result.some),
118
+ mixed: count(arms) > 1 && !all(matches, result => result.every),
119
+ }
120
+ })
121
+ value {
122
+ known: signatures != null && count(returns) > 0 && all(returns, result => result.known),
123
+ dynamic: any(returns, result => result.dynamic),
124
+ required: all(returns, result => if n.return_type != null then result.every else result.some),
125
+ mixed: any(returns, result => result.mixed),
126
+ }
127
+ }
128
+
129
+ in ts_call.argument, ts_new.argument, ts_template.expression rule es-no-misused-promises:void-arguments {
130
+ 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
+ report at unparenthesised_node(node)
134
+ warn "Promise returned in function argument where a void return was expected."
135
+ }
136
+
137
+ in ts_call.argument rule es-no-misused-promises:predicates {
138
+ when node.index == 0 && condition_array_callback(parent)
139
+ when promise_returns_thenable(promise_return_arms(value_signatures(node)))
140
+ report at unparenthesised_node(node)
141
+ warn "Expected a non-Promise value to be returned."
142
+ }
143
+
144
+ in ts_declarator.init, ts_parameter.init, ts_assignment.right rule es-no-misused-promises:void-variables {
145
+ # An unannotated declarator gives its initializer no expected type.
146
+ when ((!(is ts_declarator.init) || parent.type != null) && promise_void_context(node))
147
+ || promise_dispose_void_context(node)
148
+ report at unparenthesised_node(node)
149
+ warn "Promise-returning function provided to variable where a void return was expected."
150
+ }
151
+
152
+ in ts_property, ts_method, ts_class_property.init rule es-no-misused-promises:void-properties {
153
+ when not ts_method || parent is ts_object
154
+ when not ts_class_property.init || node.parent.type != null
155
+ let promise_source promise_property_source(node)
156
+ when promise_void_context(promise_source)
157
+ report at promise_property_site(promise_source)
158
+ warn "Promise-returning function provided to property where a void return was expected."
159
+ }
160
+
161
+ in ts_return.argument, ts_arrow.body rule es-no-misused-promises:void-returns {
162
+ when promise_void_context(node)
163
+ report at unparenthesised_node(node)
164
+ warn "Promise-returning function provided to return value where a void return was expected."
165
+ }
166
+
167
+ in ts_jsx_expression.expression rule es-no-misused-promises:void-attributes {
168
+ when parent.parent is ts_jsx_attribute
169
+ when promise_void_context(node)
170
+ report at parent.node
171
+ warn "Promise-returning function provided to attribute where a void return was expected."
172
+ }
173
+
174
+ fn promise_inherited_void_targets(n) = {
175
+ value owner: n.parent.node
176
+ value declared: owner.implements.nodes (if owner is ts_interface then owner.extends.nodes else [])
177
+ value constructed: if owner is ts_class && owner.extends != null then {
178
+ value headers: value_constructors(owner.extends.node)
179
+ value map(flat_map(headers ?? [], arm => arm), header => {heritage: owner.extends.node, value: header.result})
180
+ } else []
181
+ # An interface extending a constructor type (`typeof C`) is checked
182
+ # against the instances it constructs too, as TypeScript ESLint does.
183
+ value instances: if owner is ts_interface then flat_map(owner.extends.nodes, heritage => {
184
+ value headers: value_constructors(heritage)
185
+ value map(flat_map(headers ?? [], arm => arm), header => {heritage: heritage, value: header.result})
186
+ }) else []
187
+ value targets: map(declared, heritage => {heritage: heritage, value: heritage}) constructed instances
188
+ value filter(targets, target => target.value != null && {
189
+ value lookup: value_lookup(target.value, n.static_name)
190
+ value lookup.member != null && any(lookup.alternatives, alternative => alternative.matched
191
+ && any(alternative.sources, source => source.kind == "named"))
192
+ && promise_returns_void(promise_return_arms(value_signatures(lookup.member)))
193
+ })
194
+ }
195
+ in ts_method, ts_method_signature, ts_class_property, ts_property_signature rule es-no-misused-promises:void-inherited-methods {
196
+ when !is_static
197
+ when static_name != null && parent is (ts_class, ts_interface)
198
+ when promise_returns_thenable(promise_return_arms(value_signatures(node)))
199
+ each target in promise_inherited_void_targets(node) {
200
+ report at { start: offset, end: if byte_slice(source, end_offset, end_offset + 1) == ";" then end_offset + 1 else end_offset }
201
+ warn "Promise-returning method provided where a void return was expected by extended/implemented type '" target.heritage.text "'."
202
+ }
203
+ }
204
+
205
+ }