@inthedark/dxlint 0.1.13 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -9
- package/bin/dxlint-darwin-arm64 +0 -0
- package/bin/dxlint-linux-x64 +0 -0
- package/bin/dxlint-win32-x64.exe +0 -0
- package/esbuild.mjs +106 -0
- package/package.json +20 -2
- package/std/ts/calls.dx +9 -7
- package/std/ts/conditions.dx +2 -0
- package/std/ts/constants.dx +2 -1
- package/std/ts/content-mapper.dx +48 -0
- package/std/ts/deprecation.dx +12 -5
- package/std/ts/effects.dx +8 -1
- package/std/ts/format.dx +1 -1
- package/std/ts/grammar.dx +23 -13
- package/std/ts/import-no-deprecated.dx +2 -0
- package/std/ts/javascript.dx +496 -0
- package/std/ts/layout/literals.dx +14 -7
- package/std/ts/layout/separators.dx +1 -1
- package/std/ts/layout/shared.dx +36 -37
- package/std/ts/layout/types.dx +4 -0
- package/std/ts/literals.dx +1 -0
- package/std/ts/methods.dx +3 -0
- package/std/ts/modules.dx +8 -0
- package/std/ts/names.dx +25 -13
- package/std/ts/problems/control-flow.dx +1 -0
- package/std/ts/problems/numeric-precision.dx +1 -0
- package/std/ts/problems/regexp.dx +1 -0
- package/std/ts/regex/checks.dx +1 -0
- package/std/ts/regex/findings.dx +10 -6
- package/std/ts/regex/values.dx +4 -15
- package/std/ts/restrictions.dx +76 -0
- package/std/ts/scoping/member-uses.dx +5 -3
- package/std/ts/scoping/shadowing.dx +1 -0
- package/std/ts/scoping/usage.dx +2 -2
- package/std/ts/spreads.dx +1 -1
- package/std/ts/structure/padding.dx +10 -8
- package/std/ts/style/expressions.dx +8 -4
- package/std/ts/style/returns.dx +3 -1
- package/std/ts/style/signatures.dx +5 -0
- package/std/ts/style/type-exports.dx +1 -0
- package/std/ts/suggestions/builtins.dx +1 -1
- package/std/ts/suggestions/comparisons.dx +1 -0
- package/std/ts/suggestions/expressions.dx +1 -0
- package/std/ts/suggestions/object-spread.dx +1 -1
- package/std/ts/tokens.dx +11 -5
- package/std/ts/typed/awaitability.dx +1 -0
- package/std/ts/typed/floating-promises.dx +45 -14
- package/std/ts/typed/includes.dx +3 -4
- package/std/ts/typed/promises.dx +10 -10
- package/std/ts/typed/string-boundaries.dx +1 -0
- package/std/ts/typed/switches.dx +1 -0
- package/std/ts/typed/type-arguments.dx +11 -19
- package/std/ts/typed/type-conversions.dx +1 -0
- package/std/ts/typed/type-parameters.dx +1 -1
- package/std/ts/typed/using-disposable.dx +90 -0
- package/std/ts/typed.dx +1 -0
- package/std/ts/values/arguments.dx +1 -1
- package/std/ts/values/enums.dx +3 -0
- package/std/ts/values/inspection.dx +1 -4
- package/std/ts/values.dx +41 -26
- package/std/ts.dx +10 -4
|
@@ -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
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
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
|
|
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."
|
package/std/ts/typed/includes.dx
CHANGED
|
@@ -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
|
|
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)
|
package/std/ts/typed/promises.dx
CHANGED
|
@@ -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
|
|
56
|
-
value
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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.
|
|
@@ -135,7 +135,7 @@ in ts_call.argument, ts_new.argument, ts_template.expression rule es-no-misused-
|
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
in ts_call.argument rule es-no-misused-promises:predicates {
|
|
138
|
-
when node.index == 0 &&
|
|
138
|
+
when parent.condition_array_call && node.index == 0 && strict_boolean_array(parent.callee.object.node)
|
|
139
139
|
when promise_returns_thenable(promise_return_arms(value_signatures(node)))
|
|
140
140
|
report at unparenthesised_node(node)
|
|
141
141
|
warn "Expected a non-Promise value to be returned."
|
|
@@ -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."
|
package/std/ts/typed/switches.dx
CHANGED
|
@@ -79,21 +79,21 @@ fn type_argument_signature(n) = if n is (ts_call, ts_new) then {
|
|
|
79
79
|
value if binding != null && count(binding.choices) == 1 && first(binding.choices).site != null
|
|
80
80
|
then first(binding.choices) else null
|
|
81
81
|
} else null
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
group type_argument_owner ts_call, ts_new, ts_type_reference, ts_class, ts_jsx_opening, ts_tagged_template
|
|
83
|
+
in type_argument_owner let type_argument_named_site type_argument_named(node)
|
|
84
|
+
fn type_argument_site(n, signature) = if signature != null then signature.site else {
|
|
85
85
|
value local: {
|
|
86
86
|
value declaration: type_argument_local(n)
|
|
87
87
|
value if declaration != null then node_site(declaration) else null
|
|
88
88
|
}
|
|
89
|
-
value if
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
89
|
+
value if local != null then local else {
|
|
90
|
+
value named: n.type_argument_named_site
|
|
91
|
+
value if named != null then named else type_argument_member(n)
|
|
92
|
+
}
|
|
93
93
|
}
|
|
94
94
|
fn type_argument_formals(n, site) = {
|
|
95
95
|
value formals: if site != null then site_field(n, site, "type_parameter") else null
|
|
96
|
-
value named: if formals != null && count(formals) > 0 then null else
|
|
96
|
+
value named: if formals != null && count(formals) > 0 then null else n.type_argument_named_site
|
|
97
97
|
value if formals != null && count(formals) > 0 then formals
|
|
98
98
|
else if named != null then site_field(n, named, "type_parameter")
|
|
99
99
|
else formals
|
|
@@ -208,15 +208,6 @@ fn type_argument_same(n, other, widened) = if widened then {
|
|
|
208
208
|
value source: value_union(n, [])
|
|
209
209
|
value source != null && other != null && type_argument_same_handles(source, other)
|
|
210
210
|
}
|
|
211
|
-
fn type_argument_default_site(n, site) = {
|
|
212
|
-
value formal: get(type_argument_formals(n, site), count(n.type_argument.nodes) - 1)
|
|
213
|
-
value defaults: if formal != null then site_field(n, formal, "default") else null
|
|
214
|
-
value if defaults != null && count(defaults) == 1 then first(defaults) else null
|
|
215
|
-
}
|
|
216
|
-
fn type_argument_default(n, site) = {
|
|
217
|
-
value default: type_argument_default_site(n, site)
|
|
218
|
-
value if default != null then declaration_value(n, default) else null
|
|
219
|
-
}
|
|
220
211
|
# An unresolved type reference (`F<number>` from a missing module) says
|
|
221
212
|
# nothing about which arguments it carries; only its written ones can tell
|
|
222
213
|
# it apart from a default written with another shape.
|
|
@@ -284,8 +275,9 @@ fn type_argument_redundancy(n) = {
|
|
|
284
275
|
value signature: type_argument_signature(owner)
|
|
285
276
|
value site: type_argument_site(owner, signature)
|
|
286
277
|
value formal: get(type_argument_formals(owner, site), count(arguments) - 1)
|
|
287
|
-
value
|
|
288
|
-
value default_site:
|
|
278
|
+
value defaults: if formal != null then site_field(owner, formal, "default") else null
|
|
279
|
+
value default_site: if defaults != null && count(defaults) == 1 then first(defaults) else null
|
|
280
|
+
value default: if default_site != null then declaration_value(owner, default_site) else null
|
|
289
281
|
value if last(arguments) != n then null
|
|
290
282
|
else if default != null && type_argument_same(n, default, false)
|
|
291
283
|
&& (!type_argument_unresolved(n) || type_argument_written_same(n, default_site)) then "default"
|
|
@@ -257,7 +257,7 @@ in ts_type_parameter rule es-no-unnecessary-type-parameters warn "Type parameter
|
|
|
257
257
|
then type_parameter_owner.end_offset
|
|
258
258
|
else type_parameter_owner.body.offset ?? type_parameter_owner.return_type.end_offset ?? type_parameter_owner.end_offset
|
|
259
259
|
let type_parameter_references {
|
|
260
|
-
value
|
|
260
|
+
value name_references(name.node)
|
|
261
261
|
filter (reference) contains(name_role_classes(reference, "reference"), "type")
|
|
262
262
|
&& reference.offset <= type_parameter_boundary
|
|
263
263
|
&& (reference.offset >= node.end_offset || reference.end_offset <= node.offset)
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Disposable values held without `using`.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
# A statement or initializer whose value cannot be an object never reaches a
|
|
6
|
+
# typed question.
|
|
7
|
+
fn using_disposable_primitive(bare) = bare is (ts_null, ts_boolean, ts_number, ts_bigint, ts_string, ts_template, ts_array, ts_arrow, ts_function, ts_regex)
|
|
8
|
+
|| bare is ts_update
|
|
9
|
+
|| bare is ts_unary && contains(["void", "typeof", "delete", "!", "+", "-", "~"], bare.operator)
|
|
10
|
+
|| bare is ts_binary && contains(["<", ">", "<=", ">=", "==", "!=", "===", "!==", "in", "instanceof",
|
|
11
|
+
"+", "-", "*", "/", "%", "**", "&", "|", "^", "<<", ">>", ">>>"], bare.operator)
|
|
12
|
+
|| bare is ts_assignment && contains(["+=", "-=", "*=", "/=", "%=", "**=", "<<=", ">>=", ">>>=", "&=", "|=", "^="], bare.operator)
|
|
13
|
+
# An object literal without computed keys or spreads declares only named
|
|
14
|
+
# members, so it cannot carry Symbol.dispose or Symbol.asyncDispose.
|
|
15
|
+
fn using_disposable_plain_object(bare) = bare is ts_object
|
|
16
|
+
&& !any(bare.property.nodes, member => member is ts_spread || member.key is ts_computed_key)
|
|
17
|
+
fn using_disposable_kind(n) = {
|
|
18
|
+
value bare: unparenthesised_node(n)
|
|
19
|
+
value if bare is ts_assignment && bare.operator == "=" then using_disposable_kind(bare.right.node)
|
|
20
|
+
else if bare == null || using_disposable_primitive(bare) || using_disposable_plain_object(bare) then null else {
|
|
21
|
+
value if !(n could object) then null
|
|
22
|
+
else if value_has_member(n, {global: "Symbol", class: "value", path: ["asyncDispose"]}, null) == true then "async"
|
|
23
|
+
else if value_has_member(n, {global: "Symbol", class: "value", path: ["dispose"]}, null) == true then "sync" else null
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
in ts_node let using_disposable_held using_disposable_kind(node)
|
|
27
|
+
# Iterators inherit dispose only from the bare lib `Disposable` interface;
|
|
28
|
+
# disposing them means nothing.
|
|
29
|
+
fn using_disposable_inherited_by_iterator(n, key) = value_has_member(n, "next", null) == true && {
|
|
30
|
+
value lookup: value_lookup(n, key)
|
|
31
|
+
value lookup != null && all(lookup.alternatives, alternative => all(alternative.sources, source =>
|
|
32
|
+
source.site != null && contains(["Disposable", "AsyncDisposable"], site_parent(n, source.site).name)))
|
|
33
|
+
}
|
|
34
|
+
# Ownership moves when the value leaves the function or goes into something
|
|
35
|
+
# that outlives it, so `using` would dispose a live resource. An argument stays
|
|
36
|
+
# borrowed: only `new` takes ownership.
|
|
37
|
+
fn using_disposable_carriers(read) = {
|
|
38
|
+
value read
|
|
39
|
+
walk (current) if current.parent is (ts_parenthesized, ts_as, ts_satisfies, ts_non_null, ts_type_assertion, ts_logical, ts_array) || (current.parent is ts_conditional && current.parent.test.node != current) then current.parent.node else null
|
|
40
|
+
}
|
|
41
|
+
fn using_disposable_leaves(carrier) = when {
|
|
42
|
+
carrier.parent is (ts_new, ts_return, ts_yield, ts_property) then true
|
|
43
|
+
carrier.parent is ts_arrow then carrier.parent.body.node == carrier
|
|
44
|
+
carrier.parent is ts_assignment then carrier.parent.right.node == carrier && carrier.parent.left is ts_member
|
|
45
|
+
else then false
|
|
46
|
+
}
|
|
47
|
+
fn using_disposable_aliases(binding) = {
|
|
48
|
+
value binding.references
|
|
49
|
+
flat_map (read) using_disposable_carriers(read)
|
|
50
|
+
map (carrier) when {
|
|
51
|
+
carrier.parent is ts_assignment && carrier.parent.right.node == carrier then carrier.parent.left.declaration
|
|
52
|
+
carrier.parent is ts_declarator && carrier.parent.init.node == carrier && carrier.parent.name is ts_identifier then carrier.parent.name.node
|
|
53
|
+
else then null
|
|
54
|
+
}
|
|
55
|
+
filter (alias) alias != null
|
|
56
|
+
}
|
|
57
|
+
fn using_disposable_escapes(binding) = binding != null && {
|
|
58
|
+
value binding
|
|
59
|
+
walk (current) using_disposable_aliases(current)
|
|
60
|
+
any (current) any(current.references, read => any(using_disposable_carriers(read), carrier => using_disposable_leaves(carrier)))
|
|
61
|
+
}
|
|
62
|
+
fn using_disposable_stored(assignment) = assignment.left is ts_member || using_disposable_escapes(assignment.left.declaration)
|
|
63
|
+
fn using_disposable_declared(initializer) = {
|
|
64
|
+
value kind: using_disposable_kind(initializer)
|
|
65
|
+
value if kind == null then null
|
|
66
|
+
else if using_disposable_inherited_by_iterator(initializer, {global: "Symbol", class: "value", path: [if kind == "async" then "asyncDispose" else "dispose"]}) then null
|
|
67
|
+
else kind
|
|
68
|
+
}
|
|
69
|
+
in ts_declarator let using_disposable_initializer {
|
|
70
|
+
value kind: using_disposable_declared(init.node)
|
|
71
|
+
value if kind != null && !using_disposable_escapes(name) then kind else null
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
in ts_declarator.name rule es-use-using-disposable:declare {
|
|
75
|
+
when !re_test(ts_variable_declaration.kind.text ?? "", /using/) && parent.using_disposable_initializer != null
|
|
76
|
+
# `using` cannot bind a pattern: destructuring drops the resource itself.
|
|
77
|
+
warn when {
|
|
78
|
+
node is ts_identifier && parent.using_disposable_initializer == "async" then "Async disposable resources should be declared with `await using`."
|
|
79
|
+
node is ts_identifier then "Disposable resources should be declared with `using`."
|
|
80
|
+
parent.using_disposable_initializer == "async" then "Async disposable resources should be held with `await using` before destructuring."
|
|
81
|
+
else then "Disposable resources should be held with `using` before destructuring."
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
in ts_expression_statement.expression rule es-use-using-disposable:hold {
|
|
85
|
+
let held_assignment unparenthesised_node(node)
|
|
86
|
+
when using_disposable_held != null && !(held_assignment is ts_assignment && using_disposable_stored(held_assignment))
|
|
87
|
+
warn if using_disposable_held == "async" then "Async disposable resources should be held with `await using`." else "Disposable resources should be held with `using`."
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
}
|
package/std/ts/typed.dx
CHANGED
|
@@ -11,6 +11,7 @@ import .ts/typed/errors
|
|
|
11
11
|
import .ts/typed/awaitability
|
|
12
12
|
import .ts/typed/optional-chains
|
|
13
13
|
import .ts/typed/floating-promises
|
|
14
|
+
import .ts/typed/using-disposable
|
|
14
15
|
import .ts/typed/unbound-method
|
|
15
16
|
import .ts/typed/template-expressions
|
|
16
17
|
import .ts/typed/readonly-members
|
package/std/ts/values/enums.dx
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import .ts/javascript
|
|
1
2
|
define {
|
|
2
3
|
|
|
3
4
|
fn ts_value_has_enum(n) = any(value_constituents(n) ?? [], arm => any(arm, part => contains(part.traits, "enum")))
|
|
@@ -17,6 +18,7 @@ fn ts_value_enum_prior(n) = {
|
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
in ts_enum {
|
|
21
|
+
let ts_value_enum_plain all(member.nodes, item => item.init == null)
|
|
20
22
|
let ts_value_enum_literals {
|
|
21
23
|
value members: member.nodes
|
|
22
24
|
value states: {
|
|
@@ -71,6 +73,7 @@ in ts_enum_member {
|
|
|
71
73
|
declares member name keyed ts_value_key(it) in nearest statics hoisted
|
|
72
74
|
let ts_value_enum_literal if init != null then ts_value_enum_literal(init.node)
|
|
73
75
|
else if node_ordinal(node) == 0 then 0
|
|
76
|
+
else if parent.ts_value_enum_plain then node_ordinal(node)
|
|
74
77
|
else get(parent.ts_value_enum_literals, node_ordinal(node))
|
|
75
78
|
let ts_value_enum_prior ts_value_enum_prior(node)
|
|
76
79
|
let ts_value_enum_kind when {
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
define {
|
|
2
2
|
|
|
3
|
-
fn ts_value_has_trait(n, trait) =
|
|
4
|
-
value arms: value_trait_parts(n, [trait])
|
|
5
|
-
value if arms == null then null else any(arms, arm => any(arm.parts, part => contains(part.traits, trait)))
|
|
6
|
-
}
|
|
3
|
+
fn ts_value_has_trait(n, trait) = value_has_trait(n, trait)
|
|
7
4
|
fn ts_value_has_void(n) = ts_value_has_trait(n, "void")
|
|
8
5
|
fn ts_value_callable(n) = {
|
|
9
6
|
value signatures: value_signatures(n)
|
package/std/ts/values.dx
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import .ts/javascript
|
|
1
2
|
import .ts/values/enums
|
|
2
3
|
import .ts/values/arguments
|
|
3
4
|
import .ts/values/inspection
|
|
@@ -42,7 +43,7 @@ in ts_namespace {
|
|
|
42
43
|
when !ts_value_namespace_runtime { unwraps unknown }
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
fn ts_value_key(key) = {
|
|
46
|
+
fn ts_value_key(key) = if key is ts_identifier && !contains(key.text, "\\") then key.text else {
|
|
46
47
|
value literal: if key is ts_computed_key then key.expression.node else key
|
|
47
48
|
value allowed: key not ts_computed_key || literal is (ts_string, ts_number, ts_bigint, ts_boolean, ts_null)
|
|
48
49
|
|| literal is ts_template && count(literal.expression.nodes) == 0
|
|
@@ -376,9 +377,14 @@ in ts_template {
|
|
|
376
377
|
in ts_regex { unwraps instance "RegExp" }
|
|
377
378
|
|
|
378
379
|
in ts_identifier {
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
380
|
+
# One answer for which recipe applies: an identifier's value asks once.
|
|
381
|
+
let ts_value_name_route when {
|
|
382
|
+
text == "undefined" && scope_declaration == null && contains(name_role_classes(node, "reference"), "value") then "undefined"
|
|
383
|
+
ts_value_missing_name(node, "value") then "unresolved"
|
|
384
|
+
ts_value_pattern_name != null then "pattern"
|
|
385
|
+
text == "arguments" && ts_value_implicit_arguments then "arguments"
|
|
386
|
+
else then "definition"
|
|
387
|
+
}
|
|
382
388
|
# A name a destructuring pattern binds takes the value that pattern gives
|
|
383
389
|
# it, not the declarator's whole value.
|
|
384
390
|
let ts_value_pattern_name if declaration != null
|
|
@@ -390,12 +396,12 @@ in ts_identifier {
|
|
|
390
396
|
then index_of(ts_value_pattern_name.parent.element.nodes, ts_value_pattern_name) else 0
|
|
391
397
|
let ts_value_pattern_any ts_value_pattern_name != null && ts_value_pattern_name.parent is ts_array_pattern
|
|
392
398
|
&& ts_value_has_trait(ts_value_pattern_name.parent.node, "any") == true
|
|
393
|
-
when
|
|
399
|
+
when ts_value_name_route == "undefined" {
|
|
394
400
|
fits undefined, nullable, falsy, nonthenable
|
|
395
401
|
identity { type: "undefined" }
|
|
396
402
|
}
|
|
397
|
-
when
|
|
398
|
-
when
|
|
403
|
+
when ts_value_name_route == "unresolved" { fits any, error open }
|
|
404
|
+
when ts_value_name_route == "pattern" {
|
|
399
405
|
when ts_value_pattern_any { unwraps ts_value_pattern_name.parent }
|
|
400
406
|
when ts_value_pattern_name.parent is ts_array_pattern && !ts_value_pattern_any {
|
|
401
407
|
unwraps ts_value_pattern_name.parent.[ts_value_pattern_slot]
|
|
@@ -404,7 +410,7 @@ in ts_identifier {
|
|
|
404
410
|
unwraps ts_value_pattern_name.parent
|
|
405
411
|
}
|
|
406
412
|
}
|
|
407
|
-
when
|
|
413
|
+
when ts_value_name_route == "definition" {
|
|
408
414
|
unwraps definition
|
|
409
415
|
widens definition.widened
|
|
410
416
|
}
|
|
@@ -816,16 +822,16 @@ fn ts_value_optional_member(n, key) = value_member_optional(n.object.node, key)
|
|
|
816
822
|
in ts_member {
|
|
817
823
|
refers object.[ts_value_member_key]
|
|
818
824
|
let ts_value_member_key ts_value_key(property.node)
|
|
819
|
-
let ts_value_member_nullish in_optional_chain == true && object could nullable
|
|
820
|
-
|| ts_value_optional_member(node, ts_value_member_key)
|
|
821
825
|
# `always any` stays open for a type parameter; one whose parts are
|
|
822
826
|
# certainly not `any` (its constraint's) reads its members.
|
|
823
|
-
let
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
when ts_value_member_nullish { unwraps (object \ null, undefined).[ts_value_member_key] | trait undefined, nullable, falsy, nonthenable }
|
|
827
|
+
let ts_value_member_route when {
|
|
828
|
+
ts_value_has_trait(object.node, "any") != false && object always any then "any"
|
|
829
|
+
in_optional_chain == true && object could nullable || ts_value_optional_member(node, ts_value_member_key) then "nullish"
|
|
830
|
+
else then "plain"
|
|
828
831
|
}
|
|
832
|
+
when ts_value_member_route == "any" { unwraps object }
|
|
833
|
+
when ts_value_member_route == "plain" { unwraps (object \ null, undefined).[ts_value_member_key] }
|
|
834
|
+
when ts_value_member_route == "nullish" { unwraps (object \ null, undefined).[ts_value_member_key] | trait undefined, nullable, falsy, nonthenable }
|
|
829
835
|
}
|
|
830
836
|
fn ts_value_unchecked_index(n) = {
|
|
831
837
|
value lookup: value_lookup(n.object.node, n.index.node)
|
|
@@ -937,23 +943,32 @@ in ts_class, ts_interface {
|
|
|
937
943
|
fits truthy in members
|
|
938
944
|
}
|
|
939
945
|
}
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
#
|
|
944
|
-
|
|
945
|
-
value
|
|
946
|
-
|
|
947
|
-
value remote: map(filter(value_sources(name.node).sites ?? [], site => contains(["ts_class", "ts_interface", "ts_type_alias"], site.kind)),
|
|
948
|
-
site => map(site_field(node, site, "type_parameter") ?? [], parameter => count(site_field(node, parameter, "default") ?? []) > 0))
|
|
946
|
+
fn ts_value_name_needs_arguments(n) = {
|
|
947
|
+
value local: map(filter(name_binding(n).declarations ?? [], item => item.parent is (ts_class, ts_interface, ts_type_alias)),
|
|
948
|
+
item => map(item.parent.type_parameter.nodes, parameter => parameter.default != null))
|
|
949
|
+
# A plain local declaration settles it: merged declarations share parameters.
|
|
950
|
+
value if count(local) > 0 && all(local, parameters => count(parameters) == 0) then false else {
|
|
951
|
+
value remote: map(filter(value_sources(n).sites ?? [], site => contains(["ts_class", "ts_interface", "ts_type_alias"], site.kind)),
|
|
952
|
+
site => map(site_field(n, site, "type_parameter") ?? [], parameter => count(site_field(n, parameter, "default") ?? []) > 0))
|
|
949
953
|
value declarations: flat_map([local, remote], group => group)
|
|
950
954
|
value any(declarations, parameters => any(parameters, (parameter, index) =>
|
|
951
955
|
all(declarations, other => !any(other, (defaulted, at) => at == index && defaulted))))
|
|
952
956
|
}
|
|
957
|
+
}
|
|
958
|
+
in ts_identifier let ts_value_needs_arguments ts_value_name_needs_arguments(node)
|
|
959
|
+
in ts_type_reference {
|
|
960
|
+
# `Promise` without its required arguments is an error type, read as any.
|
|
961
|
+
# Merged declarations share their parameters: one declaring a default is enough.
|
|
962
|
+
let ts_value_reference_error name is ts_identifier && (ts_value_missing_name(name.node, "type")
|
|
963
|
+
|| count(type_argument.nodes) == 0 && {
|
|
964
|
+
# Asked of the name's declaration in this file: every use agrees.
|
|
965
|
+
value bound: name.node.scope_declaration
|
|
966
|
+
value if bound is ts_identifier then bound.ts_value_needs_arguments else ts_value_name_needs_arguments(name.node)
|
|
967
|
+
})
|
|
953
968
|
arguments type_argument
|
|
954
|
-
when
|
|
969
|
+
when ts_value_reference_error { fits any, error open }
|
|
955
970
|
when name is ts_qualified_name { unwraps name }
|
|
956
|
-
when !
|
|
971
|
+
when !ts_value_reference_error && name not ts_qualified_name { unwraps &...name.definition }
|
|
957
972
|
}
|
|
958
973
|
in ts_type_query {
|
|
959
974
|
# `typeof f<T>` instantiates `f`'s own type parameters.
|
package/std/ts.dx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
# .ts: JavaScript and TypeScript.
|
|
2
|
-
# every node kind is a `ts_*` layer with ESTree-style fields
|
|
3
|
-
# `ts_call.callee`, `ts_binary.operator`); `dxlint tree file.ts`
|
|
4
|
-
# Textual parts (comments, lines) are pattern layers in the same tree.
|
|
1
|
+
# .ts: JavaScript and TypeScript. The `ts` parser (`.ts/grammar`) owns the
|
|
2
|
+
# syntax: every node kind is a `ts_*` layer with ESTree-style fields
|
|
3
|
+
# (`ts_if.test`, `ts_call.callee`, `ts_binary.operator`); `dxlint tree file.ts`
|
|
4
|
+
# shows them. Textual parts (comments, lines) are pattern layers in the same tree.
|
|
5
5
|
#
|
|
6
6
|
# Brings every JS/TS rule definition, all off: turn on what you want with
|
|
7
7
|
# `rule name` lines, or a ruleset (`import rules:recommended`).
|
|
@@ -15,8 +15,14 @@ language .js {
|
|
|
15
15
|
option sourceType "script" | "module" | "commonjs" | null = null "Root source environment; null infers it from syntax and filename."
|
|
16
16
|
option impliedStrict boolean | null = null "Strict code without an explicit directive."
|
|
17
17
|
option globalReturn boolean | null = null "Wrap script declarations in a function scope, without CommonJS globals."
|
|
18
|
+
option jsx boolean = false "JSX syntax (on for .tsx and .jsx files)."
|
|
19
|
+
}
|
|
20
|
+
language .js {
|
|
21
|
+
files "*.tsx" "*.jsx"
|
|
22
|
+
set jsx true
|
|
18
23
|
}
|
|
19
24
|
|
|
25
|
+
import .ts/grammar
|
|
20
26
|
native ts
|
|
21
27
|
|
|
22
28
|
group ts_function_like ts_function, ts_arrow, ts_method
|