@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.
- package/LICENSE +21 -0
- package/README.md +84 -2
- package/bin/dxlint +15 -0
- package/bin/dxlint-linux-x64 +0 -0
- package/bin/dxlint-win32-x64.exe +0 -0
- package/package.json +40 -4
- package/rules/conventions.dx +34 -0
- package/rules/correctness.dx +33 -0
- package/rules/es-no-semi.dx +11 -0
- package/rules/format.dx +49 -0
- package/rules/recommended.dx +51 -0
- package/rules/suspicious.dx +31 -0
- package/rules/types.dx +21 -0
- package/std/c.dx +23 -0
- package/std/core/lines.dx +51 -0
- package/std/core/lists.dx +76 -0
- package/std/core.dx +30 -0
- package/std/css.dx +17 -0
- package/std/dx.dx +31 -0
- package/std/json.dx +104 -0
- package/std/markdown.dx +11 -0
- package/std/python.dx +21 -0
- package/std/rust/format.dx +316 -0
- package/std/rust.dx +34 -0
- package/std/toml.dx +19 -0
- package/std/ts/arithmetic.dx +87 -0
- package/std/ts/assertions.dx +477 -0
- package/std/ts/calls.dx +246 -0
- package/std/ts/comments.dx +10 -0
- package/std/ts/conditions.dx +531 -0
- package/std/ts/constants.dx +335 -0
- package/std/ts/deprecation.dx +185 -0
- package/std/ts/directives.dx +60 -0
- package/std/ts/effects.dx +219 -0
- package/std/ts/enums.dx +233 -0
- package/std/ts/expression-fixes.dx +67 -0
- package/std/ts/expressions.dx +270 -0
- package/std/ts/flow.dx +2 -0
- package/std/ts/format.dx +755 -0
- package/std/ts/globals.dx +106 -0
- package/std/ts/grammar.dx +488 -0
- package/std/ts/helpers.dx +102 -0
- package/std/ts/import-attributes.dx +88 -0
- package/std/ts/import-no-deprecated.dx +73 -0
- package/std/ts/indentation.dx +442 -0
- package/std/ts/jsx.dx +646 -0
- package/std/ts/layout/blocks.dx +281 -0
- package/std/ts/layout/calls.dx +79 -0
- package/std/ts/layout/expressions.dx +213 -0
- package/std/ts/layout/literals.dx +114 -0
- package/std/ts/layout/separators.dx +184 -0
- package/std/ts/layout/shared.dx +203 -0
- package/std/ts/layout/text.dx +142 -0
- package/std/ts/layout/types.dx +182 -0
- package/std/ts/layout.dx +9 -0
- package/std/ts/license.dx +54 -0
- package/std/ts/literal-text.dx +3 -0
- package/std/ts/literals.dx +95 -0
- package/std/ts/methods.dx +69 -0
- package/std/ts/modules.dx +268 -0
- package/std/ts/names.dx +348 -0
- package/std/ts/naming.dx +169 -0
- package/std/ts/narrowing.dx +96 -0
- package/std/ts/parentheses.dx +262 -0
- package/std/ts/precedence.dx +25 -0
- package/std/ts/problems/calls.dx +34 -0
- package/std/ts/problems/classes.dx +39 -0
- package/std/ts/problems/control-flow.dx +257 -0
- package/std/ts/problems/declarations.dx +67 -0
- package/std/ts/problems/duplicates.dx +70 -0
- package/std/ts/problems/expressions.dx +154 -0
- package/std/ts/problems/helpers.dx +22 -0
- package/std/ts/problems/imports.dx +43 -0
- package/std/ts/problems/non-null.dx +78 -0
- package/std/ts/problems/numeric-precision.dx +66 -0
- package/std/ts/problems/regexp.dx +66 -0
- package/std/ts/problems/statements.dx +36 -0
- package/std/ts/problems/typescript.dx +55 -0
- package/std/ts/problems/whitespace.dx +40 -0
- package/std/ts/problems.dx +20 -0
- package/std/ts/regex/checks.dx +249 -0
- package/std/ts/regex/findings.dx +209 -0
- package/std/ts/regex/grammar.dx +80 -0
- package/std/ts/regex/suggestions.dx +94 -0
- package/std/ts/regex/values.dx +378 -0
- package/std/ts/regex.dx +5 -0
- package/std/ts/restrictions/comments.dx +76 -0
- package/std/ts/restrictions.dx +811 -0
- package/std/ts/scopes.dx +68 -0
- package/std/ts/scoping/atomic.dx +40 -0
- package/std/ts/scoping/bindings.dx +126 -0
- package/std/ts/scoping/constness.dx +72 -0
- package/std/ts/scoping/globals.dx +52 -0
- package/std/ts/scoping/loops.dx +86 -0
- package/std/ts/scoping/member-uses.dx +111 -0
- package/std/ts/scoping/receivers.dx +107 -0
- package/std/ts/scoping/shadowing.dx +202 -0
- package/std/ts/scoping/usage.dx +393 -0
- package/std/ts/scoping.dx +842 -0
- package/std/ts/selectors.dx +10 -0
- package/std/ts/spreads.dx +97 -0
- package/std/ts/structure/declarations.dx +52 -0
- package/std/ts/structure/lines.dx +79 -0
- package/std/ts/structure/lists.dx +144 -0
- package/std/ts/structure/padding.dx +258 -0
- package/std/ts/structure.dx +963 -0
- package/std/ts/style/classes.dx +157 -0
- package/std/ts/style/comments.dx +75 -0
- package/std/ts/style/declarations.dx +82 -0
- package/std/ts/style/expressions.dx +125 -0
- package/std/ts/style/imports.dx +44 -0
- package/std/ts/style/ordering.dx +192 -0
- package/std/ts/style/returns.dx +152 -0
- package/std/ts/style/signatures.dx +308 -0
- package/std/ts/style/type-exports.dx +62 -0
- package/std/ts/style/type-imports.dx +166 -0
- package/std/ts/style/types.dx +474 -0
- package/std/ts/style.dx +29 -0
- package/std/ts/suggestions/accessors.dx +138 -0
- package/std/ts/suggestions/builtins.dx +110 -0
- package/std/ts/suggestions/comparisons.dx +41 -0
- package/std/ts/suggestions/computed-keys.dx +20 -0
- package/std/ts/suggestions/constructors.dx +54 -0
- package/std/ts/suggestions/control-flow.dx +204 -0
- package/std/ts/suggestions/curly.dx +124 -0
- package/std/ts/suggestions/directives.dx +79 -0
- package/std/ts/suggestions/errors.dx +106 -0
- package/std/ts/suggestions/expressions.dx +230 -0
- package/std/ts/suggestions/functions.dx +260 -0
- package/std/ts/suggestions/globals.dx +187 -0
- package/std/ts/suggestions/helpers.dx +17 -0
- package/std/ts/suggestions/object-shorthand.dx +148 -0
- package/std/ts/suggestions/object-spread.dx +96 -0
- package/std/ts/suggestions/regexp.dx +59 -0
- package/std/ts/suggestions/renames.dx +50 -0
- package/std/ts/suggestions/strings.dx +47 -0
- package/std/ts/suggestions/templates.dx +104 -0
- package/std/ts/suggestions.dx +24 -0
- package/std/ts/template-fixes.dx +22 -0
- package/std/ts/tokens.dx +124 -0
- package/std/ts/type-specifiers.dx +93 -0
- package/std/ts/typed/assignments.dx +154 -0
- package/std/ts/typed/awaitability.dx +157 -0
- package/std/ts/typed/boolean-comparisons.dx +82 -0
- package/std/ts/typed/defaults.dx +97 -0
- package/std/ts/typed/enum-consistency.dx +68 -0
- package/std/ts/typed/enums.dx +52 -0
- package/std/ts/typed/errors.dx +32 -0
- package/std/ts/typed/floating-promises.dx +147 -0
- package/std/ts/typed/for-in-arrays.dx +21 -0
- package/std/ts/typed/includes.dx +102 -0
- package/std/ts/typed/nullish-coalescing.dx +149 -0
- package/std/ts/typed/optional-chains.dx +317 -0
- package/std/ts/typed/promises.dx +205 -0
- package/std/ts/typed/qualifiers.dx +108 -0
- package/std/ts/typed/readonly-members.dx +159 -0
- package/std/ts/typed/readonly-parameters.dx +121 -0
- package/std/ts/typed/redundant-constituents.dx +171 -0
- package/std/ts/typed/regexp-exec.dx +79 -0
- package/std/ts/typed/rejection-handlers.dx +91 -0
- package/std/ts/typed/return-await.dx +116 -0
- package/std/ts/typed/return-this.dx +67 -0
- package/std/ts/typed/strict-void-return.dx +121 -0
- package/std/ts/typed/string-boundaries.dx +153 -0
- package/std/ts/typed/switches.dx +147 -0
- package/std/ts/typed/template-expressions.dx +127 -0
- package/std/ts/typed/template-values.dx +88 -0
- package/std/ts/typed/transfers.dx +106 -0
- package/std/ts/typed/type-arguments.dx +337 -0
- package/std/ts/typed/type-constituents.dx +143 -0
- package/std/ts/typed/type-conversions.dx +82 -0
- package/std/ts/typed/type-parameters.dx +280 -0
- package/std/ts/typed/unbound-method.dx +167 -0
- package/std/ts/typed/unsafe-calls.dx +73 -0
- package/std/ts/typed/unsafe-members.dx +36 -0
- package/std/ts/typed/void-expressions.dx +101 -0
- package/std/ts/typed.dx +585 -0
- package/std/ts/values/arguments.dx +13 -0
- package/std/ts/values/enums.dx +104 -0
- package/std/ts/values/inspection.dx +274 -0
- package/std/ts/values.dx +1217 -0
- package/std/ts.dx +77 -0
- package/std/whitespace.dx +37 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import .ts/scoping/usage
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
fn ts_awaited_callback(callback) = {
|
|
6
|
+
value signatures: value_signatures(callback)
|
|
7
|
+
value headers: flat_map(signatures ?? [], arm => arm)
|
|
8
|
+
value values: map(headers, header => first(header.parameters))
|
|
9
|
+
value {
|
|
10
|
+
known: signatures != null && all(headers, header => header.parameters != null)
|
|
11
|
+
&& all(values, value => value != null),
|
|
12
|
+
values: values,
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
fn ts_awaited_fulfilments(arm) = {
|
|
16
|
+
value member: value_lookup(arm, "then").member
|
|
17
|
+
value signatures: if member != null then value_signatures(member) else null
|
|
18
|
+
value headers: flat_map(signatures ?? [], alternative => alternative)
|
|
19
|
+
value callbacks: map(headers, header => value_alternatives(first(header.parameters)))
|
|
20
|
+
value states: flat_map(callbacks, alternatives => map(alternatives ?? [], callback => ts_awaited_callback(callback)))
|
|
21
|
+
value values: flat_map(states, state => state.values)
|
|
22
|
+
value {
|
|
23
|
+
known: signatures != null && count(headers) > 0 && all(callbacks, alternatives => alternatives != null)
|
|
24
|
+
&& all(states, state => state.known) && count(values) > 0,
|
|
25
|
+
values: values,
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
fn ts_awaited_step(entry) = {
|
|
29
|
+
value arms: if contains(entry.seen, entry.source) then null else value_alternatives(entry.source)
|
|
30
|
+
value states: map(arms ?? [], arm => {
|
|
31
|
+
value parts: value_constituents(arm)
|
|
32
|
+
value terminal: parts != null && count(parts) == 1 && count(first(parts)) == 1
|
|
33
|
+
&& any(first(parts), part => contains(part.traits, "any") || contains(part.traits, "unknown")
|
|
34
|
+
|| contains(part.traits, "object") && all(part.traits, trait => contains(["object", "truthy"], trait))
|
|
35
|
+
&& part.uncertain_traits == [] && part.carrier_known && part.carrier == null
|
|
36
|
+
&& part.product_known && part.product == null && part.identity_known && part.identity == null)
|
|
37
|
+
value ordinary: if terminal then true else ts_value_nonthenable(arm)
|
|
38
|
+
value fulfilment: if ordinary == false then ts_awaited_fulfilments(arm) else null
|
|
39
|
+
value {
|
|
40
|
+
known: ordinary == true || fulfilment.known == true,
|
|
41
|
+
leaves: if ordinary == true then [arm] else [],
|
|
42
|
+
children: if fulfilment.known == true then fulfilment.values else [],
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
value {
|
|
46
|
+
known: arms != null && all(states, state => state.known),
|
|
47
|
+
leaves: if arms == [] then [value_union(entry.source, [])] else flat_map(states, state => state.leaves),
|
|
48
|
+
children: map(flat_map(states, state => state.children), source =>
|
|
49
|
+
{source: source, seen: entry.seen [entry.source]}),
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
fn ts_awaited_union(values) = if count(values) == 0 then null else value_union(first(values), slice(values, 1))
|
|
53
|
+
fn ts_awaited_value(n, source) = {
|
|
54
|
+
value limit: 64
|
|
55
|
+
value states: walk({pending: [{source: source, seen: []}], leaves: [], steps: 0, known: true}, state =>
|
|
56
|
+
if !state.known || count(state.pending) == 0 || state.steps >= limit then [] else {
|
|
57
|
+
value step: ts_awaited_step(first(state.pending))
|
|
58
|
+
value pending: slice(state.pending, 1) step.children
|
|
59
|
+
value [{
|
|
60
|
+
pending: pending,
|
|
61
|
+
leaves: state.leaves step.leaves,
|
|
62
|
+
steps: state.steps + 1,
|
|
63
|
+
known: step.known && count(pending) <= limit,
|
|
64
|
+
}]
|
|
65
|
+
})
|
|
66
|
+
value result: last(states)
|
|
67
|
+
value if !result.known || count(result.pending) > 0 then null else ts_awaited_union(result.leaves)
|
|
68
|
+
}
|
|
69
|
+
fn ts_await_union_equivalent(n) = {
|
|
70
|
+
value arms: value_alternatives(n)
|
|
71
|
+
value states: map(arms ?? [], arm => {handle: arm, ordinary: ts_value_nonthenable(arm)})
|
|
72
|
+
value ordinary: filter(states, state => state.ordinary == true)
|
|
73
|
+
value asynchronous: filter(states, state => state.ordinary == false)
|
|
74
|
+
value if arms == null || any(states, state => state.ordinary == null)
|
|
75
|
+
|| count(ordinary) == 0 || count(asynchronous) == 0 then null else {
|
|
76
|
+
value awaited: ts_awaited_value(n, ts_awaited_union(map(asynchronous, state => state.handle)))
|
|
77
|
+
value other: ts_awaited_union(map(ordinary, state => state.handle))
|
|
78
|
+
value forward: if awaited != null && other != null then value_accepts(other, awaited, "value", "value") else null
|
|
79
|
+
value reverse: if awaited != null && other != null then value_accepts(awaited, other, "value", "value") else null
|
|
80
|
+
value if forward == null || reverse == null then null else forward && reverse
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fn ts_await_warning_site(n) = function_head_site(n)
|
|
85
|
+
fn ts_await_function_name(n) = {
|
|
86
|
+
value name: n.function_name ?? "async function"
|
|
87
|
+
value holder: outer_parenthesised_node(n).parent.node
|
|
88
|
+
value if !n.ts_usage_typed then name
|
|
89
|
+
else if n.name == null && holder is ts_declarator && holder.name is ts_identifier
|
|
90
|
+
then name " '" holder.name.text "'"
|
|
91
|
+
else if n is ts_method && n.key is ts_string && js_string(n.key.text) == ""
|
|
92
|
+
then re_replace(name, / ''$/, "")
|
|
93
|
+
else name
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fn ts_awaitable_result(n) = n != null
|
|
97
|
+
&& unparenthesised_node(n) not (ts_null, ts_boolean, ts_number, ts_bigint, ts_string, ts_template)
|
|
98
|
+
&& n could thenable
|
|
99
|
+
fn ts_await_nonthenable_element(handle) = {
|
|
100
|
+
value arms: value_parts(handle)
|
|
101
|
+
value arms != null && any(arms, arm => ts_value_nonthenable(arm.handle) == true
|
|
102
|
+
&& all(arm.parts, part => !contains(part.traits, "any") && !contains(part.traits, "unknown")))
|
|
103
|
+
}
|
|
104
|
+
fn ts_await_iterable_elements(part, n) = {
|
|
105
|
+
value shape: ts_value_array_part(part, n)
|
|
106
|
+
value if shape.tuple then value_children(part.handle, true, [])
|
|
107
|
+
else if shape.array then value_children(part.handle, false, [0])
|
|
108
|
+
else if part.carrier == null then [] else {
|
|
109
|
+
value ancestry: ts_value_ancestry(part)
|
|
110
|
+
value bases: filter(ancestry.parts, base => base.carrier.description.kind == "named"
|
|
111
|
+
&& contains(["Iterable", "IterableIterator", "Generator", "Set", "ReadonlySet"], base.carrier.description.name)
|
|
112
|
+
&& ts_value_builtin_part(base, n, [base.carrier.description.name]) == true)
|
|
113
|
+
value flat_map(bases, base => value_children(base.handle, false, [0]) ?? [])
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
fn ts_await_nonawaitable_iterable(n) = {
|
|
117
|
+
value arms: value_parts(n)
|
|
118
|
+
value arms != null && any(arms, arm => any(arm.parts, part =>
|
|
119
|
+
any(ts_await_iterable_elements(part, n) ?? [], element => element.member.optional == true
|
|
120
|
+
|| ts_await_nonthenable_element(element.handle))))
|
|
121
|
+
}
|
|
122
|
+
group ts_await_sites ts_await, ts_for_of, ts_variable_declaration, ts_return, ts_yield
|
|
123
|
+
in ts_node let ts_await_owner parent.ts_await_owner
|
|
124
|
+
in ts_function, ts_arrow, ts_method let ts_await_owner node
|
|
125
|
+
in ts_node let ts_await_descendants descendants(ts_await_sites)
|
|
126
|
+
fn ts_missing_await(n) = {
|
|
127
|
+
value if !contains(n.modifier, "async") || n.body == null
|
|
128
|
+
|| n.body.node is ts_block && count(n.body.body.nodes) == 0 then null else {
|
|
129
|
+
value body: n.body.node
|
|
130
|
+
value generator: contains(n.modifier, "*")
|
|
131
|
+
value sites: {
|
|
132
|
+
value [body] body.ts_await_descendants
|
|
133
|
+
filter (current) current is ts_await_sites && current.ts_await_owner == n
|
|
134
|
+
}
|
|
135
|
+
value awaited: any(sites, site => site is ts_await
|
|
136
|
+
|| site is ts_for_of && contains(site.modifier, "await")
|
|
137
|
+
|| site is ts_variable_declaration && site.kind == "await using")
|
|
138
|
+
value if awaited then null else {
|
|
139
|
+
value typed: n.ts_usage_typed
|
|
140
|
+
value if !typed && generator then null else {
|
|
141
|
+
value asynchronous: typed && (body not ts_block && ts_awaitable_result(body)
|
|
142
|
+
|| any(sites, site => site is ts_return && ts_awaitable_result(site.argument.node)
|
|
143
|
+
|| generator && site is ts_yield && site.argument != null
|
|
144
|
+
&& (if site.operator == "*" then value_has_member(site.argument.node,
|
|
145
|
+
{ global: "Symbol", class: "value", path: ["asyncIterator"] }, null) == true
|
|
146
|
+
else ts_awaitable_result(site.argument.node))))
|
|
147
|
+
value if asynchronous then null else {
|
|
148
|
+
value name: ts_await_function_name(n)
|
|
149
|
+
value { name: upper(slice(name, 0, 1)) slice(name, 1) }
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
in ts_function, ts_arrow, ts_method let missing_await ts_missing_await(node)
|
|
156
|
+
|
|
157
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Boolean literal comparisons, nullable options and source-preserving fixes.
|
|
2
|
+
import .ts/expression-fixes
|
|
3
|
+
import .ts/modules
|
|
4
|
+
|
|
5
|
+
define {
|
|
6
|
+
|
|
7
|
+
fn typed_boolean_comparison(n) = {
|
|
8
|
+
value right: unparenthesised_node(n.right.node)
|
|
9
|
+
value left: unparenthesised_node(n.left.node)
|
|
10
|
+
value if right is ts_boolean then {literal: right.text, expression: left}
|
|
11
|
+
else if left is ts_boolean then {literal: left.text, expression: right} else null
|
|
12
|
+
}
|
|
13
|
+
fn typed_boolean_kind(arm) = {
|
|
14
|
+
value parts: filter(arm, part => !contains(part.traits, "parameter"))
|
|
15
|
+
value part: first(parts)
|
|
16
|
+
value if count(parts) != 1 || contains(part.traits, "void") then null
|
|
17
|
+
else find(["boolean", "null", "undefined"], trait => contains(part.traits, trait))
|
|
18
|
+
}
|
|
19
|
+
fn typed_boolean_domain(parts) = parts != null && count(parts) > 0
|
|
20
|
+
&& all(parts, arm => typed_boolean_kind(arm) == "boolean")
|
|
21
|
+
fn typed_nullable_boolean(parts) = {
|
|
22
|
+
value kinds: map(parts ?? [], arm => typed_boolean_kind(arm))
|
|
23
|
+
value contains(kinds, "boolean") && (contains(kinds, "null") || contains(kinds, "undefined"))
|
|
24
|
+
&& !contains(kinds, null)
|
|
25
|
+
}
|
|
26
|
+
fn typed_boolean_conditional(n) = {
|
|
27
|
+
value n
|
|
28
|
+
walk (current) if current.parent is ts_parenthesized
|
|
29
|
+
|| current.parent is ts_logical && (current.parent.operator != "??" || current is ts_logical.right)
|
|
30
|
+
|| current.parent is ts_conditional && current not ts_conditional.test
|
|
31
|
+
|| current.parent is ts_sequence && last(current.parent.expression.nodes) == current
|
|
32
|
+
|| current.parent is ts_unary && current.parent.operator == "!" then current.parent.node else null
|
|
33
|
+
any (current) current is (ts_if.test, ts_while.test, ts_do_while.test, ts_for.test, ts_conditional.test)
|
|
34
|
+
}
|
|
35
|
+
fn typed_boolean_outer(n) = {
|
|
36
|
+
value n
|
|
37
|
+
walk (current) if current.parent is ts_parenthesized then current.parent.node else null
|
|
38
|
+
last
|
|
39
|
+
}
|
|
40
|
+
fn typed_boolean_fix(n, comparison, nullable) = {
|
|
41
|
+
value outside: typed_boolean_outer(n)
|
|
42
|
+
value negated_parent: outside.parent is ts_unary && outside.parent.operator == "!"
|
|
43
|
+
value target: if negated_parent then outside.parent.node else n
|
|
44
|
+
value negative: starts_with(n.operator, "!")
|
|
45
|
+
value overall_negative: (negative != (comparison.literal == "false")) != negated_parent
|
|
46
|
+
value operand: comparison.expression
|
|
47
|
+
value weak_operand: operand.precedence < 18
|
|
48
|
+
value conditional: typed_boolean_conditional(target)
|
|
49
|
+
value direct_default: nullable && comparison.literal == "true" && !overall_negative && !conditional
|
|
50
|
+
value false_default: nullable && comparison.literal == "false" && !direct_default
|
|
51
|
+
value base: expression_fix_wrap(operand.text, weak_operand && (direct_default || false_default || overall_negative))
|
|
52
|
+
value defaulted: base (if direct_default then " ?? false" else if false_default then " ?? true" else "")
|
|
53
|
+
value replacement: if overall_negative then "!" expression_fix_wrap(defaulted, false_default) else defaulted
|
|
54
|
+
value weak_replacement: !overall_negative && (weak_operand || direct_default || false_default)
|
|
55
|
+
value outer: typed_boolean_outer(target)
|
|
56
|
+
value weak_parent: outer.parent is (ts_update, ts_unary, ts_binary, ts_logical, ts_conditional, ts_await)
|
|
57
|
+
|| outer is (ts_member.object, ts_index.object, ts_call.callee, ts_new.callee, ts_tagged_template.tag)
|
|
58
|
+
value wrapped: expression_fix_wrap(replacement, weak_replacement && weak_parent && target.parent not ts_parenthesized)
|
|
59
|
+
value text: expression_fix_edges(target, wrapped)
|
|
60
|
+
value if !expression_fix_clean(target) then null else {start: target.offset, end: target.end_offset, text: text}
|
|
61
|
+
}
|
|
62
|
+
in ts_binary rule es-no-unnecessary-boolean-literal-compare {
|
|
63
|
+
option allowComparingNullableBooleansToTrue boolean = true "Allow nullable boolean comparisons with true"
|
|
64
|
+
option allowComparingNullableBooleansToFalse boolean = true "Allow nullable boolean comparisons with false"
|
|
65
|
+
let comparison typed_boolean_comparison(node)
|
|
66
|
+
when re_test(operator, /^[!=]==?$/) && comparison != null
|
|
67
|
+
let domain value_constituents(comparison.expression)
|
|
68
|
+
let nullable typed_nullable_boolean(domain)
|
|
69
|
+
when typed_boolean_domain(domain) || nullable && !(if comparison.literal == "true" then allowComparingNullableBooleansToTrue else allowComparingNullableBooleansToFalse)
|
|
70
|
+
let negative starts_with(operator, "!")
|
|
71
|
+
warn if nullable then if comparison.literal == "true" then if negative then "This expression unnecessarily compares a nullable boolean value to true instead of negating it." else "This expression unnecessarily compares a nullable boolean value to true instead of using it directly." else "This expression unnecessarily compares a nullable boolean value to false instead of using the ?? operator to provide a default." else if negative then "This expression unnecessarily compares a boolean value to a boolean instead of negating it." else "This expression unnecessarily compares a boolean value to a boolean instead of using it directly."
|
|
72
|
+
let replacement typed_boolean_fix(node, comparison, nullable)
|
|
73
|
+
fix at replacement ?? node if replacement != null as replacement.text
|
|
74
|
+
}
|
|
75
|
+
in ts_program rule es-no-unnecessary-boolean-literal-compare:strict {
|
|
76
|
+
option allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing boolean = false "Run without strict null checks"
|
|
77
|
+
when ts_compiler_option(node, "strictNullChecks") == false && !allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing
|
|
78
|
+
report at {start: 0, end: 0}
|
|
79
|
+
warn "This rule requires the `strictNullChecks` compiler option to be turned on to function correctly."
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import .ts/values/inspection
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
fn typed_default_maybe_undefined(parts) = parts == null || any(parts, arm => any(arm, part =>
|
|
6
|
+
any(["any", "unknown", "undefined", "void", "parameter"], trait =>
|
|
7
|
+
contains(part.traits, trait) || contains(part.uncertain_traits, trait))))
|
|
8
|
+
fn typed_default_context(parameter) = {
|
|
9
|
+
value function: parameter.parent.node
|
|
10
|
+
value declaration: function is ts_function && function.parent is (ts_program, ts_block, ts_export)
|
|
11
|
+
value contextual: value_expected_at(function, [])
|
|
12
|
+
value signatures: if !declaration && function is (ts_arrow, ts_function)
|
|
13
|
+
&& ts_value_trait(contextual, "callable") then expected_call_signatures(function) else null
|
|
14
|
+
value position: index_of(function.parameter.nodes, parameter)
|
|
15
|
+
value headers: flat_map(signatures ?? [], arm => arm)
|
|
16
|
+
value parameters: map(headers, header => {
|
|
17
|
+
value selected: { value header.parameters; drop position; first }
|
|
18
|
+
value optional: { value header.parameter_optional; drop position; first }
|
|
19
|
+
value rest: { value header.parameter_rest; drop position; first }
|
|
20
|
+
value if header.parameters_known && optional == false && rest == false then selected else null
|
|
21
|
+
})
|
|
22
|
+
value if signatures == null || count(parameters) == 0 || any(parameters, parts => parts == null)
|
|
23
|
+
then null else flat_map(parameters, parts => parts)
|
|
24
|
+
}
|
|
25
|
+
fn typed_default_ancestor(n) = if n.parent is (ts_declarator, ts_parameter, ts_assignment_pattern,
|
|
26
|
+
ts_pattern_property, ts_object_pattern, ts_array_pattern) then n.parent.node else null
|
|
27
|
+
fn typed_default_member(handle, child) = {
|
|
28
|
+
value arms: value_parts(handle)
|
|
29
|
+
value choices: map(arms ?? [], arm => {
|
|
30
|
+
value part: find(arm.parts, part => {
|
|
31
|
+
value shape: ts_value_array_part(part, child)
|
|
32
|
+
value shape.array || shape.tuple
|
|
33
|
+
})
|
|
34
|
+
value carrier: part.carrier
|
|
35
|
+
value array: child.parent is ts_array_pattern
|
|
36
|
+
value key: if array then index_of(child.parent.element.nodes, child)
|
|
37
|
+
else ts_value_key(child.key.node)
|
|
38
|
+
value elements: part.product.elements
|
|
39
|
+
value required: count(filter(elements, element => !element.optional && !element.repeated && !element.values))
|
|
40
|
+
value variadic: find(elements, element => element.values == true)
|
|
41
|
+
value fixed: if variadic == null then count(elements) else index_of(elements, variadic)
|
|
42
|
+
value element: if !array || carrier.description.kind != "tuple" || key == null || key < 0
|
|
43
|
+
|| key >= required then null
|
|
44
|
+
else if key < fixed then first(slice(carrier.arguments, key, key + 1))
|
|
45
|
+
else {
|
|
46
|
+
value rest: slice(carrier.arguments, fixed)
|
|
47
|
+
value if count(rest) == 0 then null else value_union(first(rest), drop(rest, 1))
|
|
48
|
+
}
|
|
49
|
+
value member: if key != null then value_lookup(arm.handle, key) else null
|
|
50
|
+
value when {
|
|
51
|
+
element != null then element
|
|
52
|
+
array && child not ts_assignment_pattern
|
|
53
|
+
&& ts_value_array_parts([arm.parts], child).array then first(carrier.arguments)
|
|
54
|
+
!array && key != null && member != null && count(member.alternatives) > 0
|
|
55
|
+
&& all(member.alternatives, alternative => alternative.required == true
|
|
56
|
+
&& any(alternative.sources, source => source.kind == "named")) then member.member
|
|
57
|
+
else then null
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
value if arms == null || any(choices, choice => choice == null) then null else choices
|
|
61
|
+
}
|
|
62
|
+
fn typed_default_pattern(n) = {
|
|
63
|
+
value ancestors: walk(n, current => typed_default_ancestor(current))
|
|
64
|
+
value root: last(ancestors)
|
|
65
|
+
value source: if root is ts_declarator then root.init.node
|
|
66
|
+
else if root is ts_parameter then root.type.node else null
|
|
67
|
+
value state: fold(reverse(ancestors), {known: source != null, handles: [source]}, (state, child) =>
|
|
68
|
+
if !state.known || !(child.parent is ts_array_pattern || child is ts_pattern_property) then state else {
|
|
69
|
+
value choices: map(state.handles, handle => typed_default_member(handle, child))
|
|
70
|
+
value {known: all(choices, choice => choice != null), handles: flat_map(choices, choice => choice ?? [])}
|
|
71
|
+
})
|
|
72
|
+
value results: if state.known then map(state.handles, handle => value_constituent_traits(handle, ["any", "unknown", "undefined", "void", "parameter"])) else null
|
|
73
|
+
value if results != null && all(results, parts => parts != null)
|
|
74
|
+
then flat_map(results, parts => parts) else null
|
|
75
|
+
}
|
|
76
|
+
fn typed_useless_default(n) = {
|
|
77
|
+
value initial: if n is ts_parameter then n.init.node else n.right.node
|
|
78
|
+
value property: n is ts_assignment_pattern && n.parent is (ts_pattern_property, ts_array_pattern)
|
|
79
|
+
value undefined: initial is ts_identifier && initial.text == "undefined"
|
|
80
|
+
value source: if initial == null || undefined then null
|
|
81
|
+
else if n is ts_parameter then typed_default_context(n) else typed_default_pattern(n)
|
|
82
|
+
value optional: n is ts_parameter && n.type != null
|
|
83
|
+
&& typed_default_maybe_undefined(value_constituent_traits(n.type.node, ["any", "unknown", "undefined", "void", "parameter"]))
|
|
84
|
+
value kind: when {
|
|
85
|
+
initial == null then null
|
|
86
|
+
undefined then if optional then "optional" else "undefined"
|
|
87
|
+
!typed_default_maybe_undefined(source) then "required"
|
|
88
|
+
else then null
|
|
89
|
+
}
|
|
90
|
+
value if kind == null then null else {
|
|
91
|
+
kind: kind, owner: if property then "property" else "parameter", offset: initial.offset, end: initial.end_offset
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
in ts_assignment_pattern, ts_parameter let typed_default_assignment typed_useless_default(node)
|
|
96
|
+
|
|
97
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
define {
|
|
2
|
+
|
|
3
|
+
in ts_program {
|
|
4
|
+
let enum_consistency_declarations descendants(ts_enum)
|
|
5
|
+
let enum_consistency_imports descendants(ts_import) descendants(ts_import_specifier) descendants(ts_import_equals)
|
|
6
|
+
}
|
|
7
|
+
fn enum_consistency_root(n) = { value n; walk (current) current.parent.node; last }
|
|
8
|
+
fn enum_consistency_group(n) = {
|
|
9
|
+
value ancestors: { value n.parent.node; walk (current) current.parent.node }
|
|
10
|
+
value state: fold(ancestors, {names: [n.name.text], merge: n.parent is ts_export, stop: null}, (group, owner) =>
|
|
11
|
+
if group.stop != null then group
|
|
12
|
+
else if owner is ts_namespace then
|
|
13
|
+
if !group.merge then {names: group.names, merge: false, stop: owner}
|
|
14
|
+
else {names: group.names [owner.name.text ?? "global"], merge: owner.parent is ts_export, stop: null}
|
|
15
|
+
else if owner is (ts_function, ts_method, ts_arrow)
|
|
16
|
+
|| owner is ts_block && owner.parent not ts_namespace
|
|
17
|
+
then {names: group.names, merge: group.merge, stop: owner}
|
|
18
|
+
else group)
|
|
19
|
+
value {names: state.names, stop: state.stop}
|
|
20
|
+
}
|
|
21
|
+
fn enum_consistency_kind(member) = {
|
|
22
|
+
value init: member.init.node
|
|
23
|
+
value if init == null then "number"
|
|
24
|
+
else if init is (ts_string, ts_template) then "string"
|
|
25
|
+
else if init is (ts_boolean, ts_null, ts_regex, ts_bigint) then null
|
|
26
|
+
else if init always string then "string" else "number"
|
|
27
|
+
}
|
|
28
|
+
fn enum_consistency_import(n) = {
|
|
29
|
+
value root: enum_consistency_root(n)
|
|
30
|
+
value bindings: map(root.enum_consistency_imports, entry =>
|
|
31
|
+
if entry is ts_import then entry.default_import.node
|
|
32
|
+
else if entry is ts_import_specifier then entry.local.node ?? entry.imported.node
|
|
33
|
+
else entry.name.node)
|
|
34
|
+
value local: declaration_identity(n)
|
|
35
|
+
value imported: {
|
|
36
|
+
value filter(bindings, binding => binding != null && binding.text == n.name.text)
|
|
37
|
+
flat_map (binding) {
|
|
38
|
+
value value_sources(binding).sites ?? []
|
|
39
|
+
walk (site) site_sources(n, site).sites ?? []
|
|
40
|
+
filter (site) site.kind == "ts_enum" && site.file != local.file
|
|
41
|
+
}
|
|
42
|
+
first
|
|
43
|
+
}
|
|
44
|
+
value arms: if imported == null then [] else value_parts(declaration_value(n, imported))
|
|
45
|
+
value parts: flat_map(arms ?? [], arm => arm.parts)
|
|
46
|
+
value candidate: find(parts, part => part.carrier_known && part.carrier.description.kind == "enum")
|
|
47
|
+
value candidate.carrier.description.first_member_kind
|
|
48
|
+
}
|
|
49
|
+
fn enum_consistency_mismatch(n) = if count(n.member.nodes) == 0 then null else {
|
|
50
|
+
value group: enum_consistency_group(n)
|
|
51
|
+
value declarations: filter(enum_consistency_root(n).enum_consistency_declarations, declaration =>
|
|
52
|
+
declaration.offset <= n.offset && count(declaration.member.nodes) > 0
|
|
53
|
+
&& enum_consistency_group(declaration) == group)
|
|
54
|
+
value first_declaration: first(sort_by(declarations, declaration => declaration.offset))
|
|
55
|
+
value desired: enum_consistency_import(n) ?? enum_consistency_kind(first(first_declaration.member.nodes))
|
|
56
|
+
value first_problem: if desired == null then null else find(n.member.nodes, member =>
|
|
57
|
+
enum_consistency_kind(member) != desired)
|
|
58
|
+
value if first_problem == null || enum_consistency_kind(first_problem) == null then null
|
|
59
|
+
else first_problem.init.node ?? first_problem
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
in ts_enum rule es-no-mixed-enums {
|
|
63
|
+
let mismatch enum_consistency_mismatch(node)
|
|
64
|
+
when mismatch != null
|
|
65
|
+
report at mismatch
|
|
66
|
+
warn "Mixing number and string enums can be confusing."
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
define {
|
|
2
|
+
fn enum_comparison_arm(arm) = {
|
|
3
|
+
value origin: find(arm, part => part.carrier.description.kind == "enum")
|
|
4
|
+
value scalar: find(arm, part => contains(part.traits, "number") || contains(part.traits, "string"))
|
|
5
|
+
value primitive: if contains(scalar.traits, "number") then "number"
|
|
6
|
+
else if contains(scalar.traits, "string") then "string" else null
|
|
7
|
+
value {
|
|
8
|
+
known: all(arm, part => part.carrier_known && part.identity_known
|
|
9
|
+
&& (!contains(part.uncertain_traits, "enum") || contains(part.traits, "intersection"))),
|
|
10
|
+
enum: if origin == null then null else { file: origin.carrier.file, node: origin.carrier.node },
|
|
11
|
+
primitive: primitive,
|
|
12
|
+
identity: scalar.identity,
|
|
13
|
+
intersection: any(arm, part => contains(part.traits, "intersection")),
|
|
14
|
+
wide: origin == null && count(arm) == 1 && primitive != null && scalar.identity == null,
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
fn enum_comparison_parts(n) = {
|
|
18
|
+
value parts: value_constituents(n)
|
|
19
|
+
value if parts == null then null else {
|
|
20
|
+
value arms: map(parts, arm => enum_comparison_arm(arm))
|
|
21
|
+
value filter(arms, arm => arm.enum == null
|
|
22
|
+
|| !any(arms, other => other.wide && other.primitive == arm.primitive))
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
fn enum_comparison_kind(parts, primitive) =
|
|
26
|
+
count(parts) > 0 && all(parts, part => part.primitive == primitive)
|
|
27
|
+
fn enum_comparison_unsafe(left, right) = {
|
|
28
|
+
value enums: map(filter(left, part => part.enum != null), part => part.enum)
|
|
29
|
+
value others: map(filter(right, part => part.enum != null), part => part.enum)
|
|
30
|
+
value shared: any(enums, origin => contains(others, origin))
|
|
31
|
+
|| any(left, part => part.enum == null && !part.intersection && part.identity != null
|
|
32
|
+
&& any(right, other => other.enum == null && !other.intersection
|
|
33
|
+
&& part.primitive == other.primitive && part.identity == other.identity))
|
|
34
|
+
value if any(left, part => part.intersection) && any(right, part => part.intersection) then null
|
|
35
|
+
else left != null && right != null && all(left, part => part.known) && all(right, part => part.known)
|
|
36
|
+
&& !shared && (
|
|
37
|
+
any(left, part => part.enum != null && part.primitive != null && enum_comparison_kind(right, part.primitive))
|
|
38
|
+
|| any(right, part => part.enum != null && part.primitive != null && enum_comparison_kind(left, part.primitive))
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
in ts_binary, ts_case rule es-no-unsafe-enum-comparison {
|
|
42
|
+
when (node is ts_binary && contains(["<", ">", "<=", ">=", "==", "===", "!=", "!=="], node.operator))
|
|
43
|
+
|| (node is ts_case && node.test != null)
|
|
44
|
+
let enum_comparison_left enum_comparison_parts(if node is ts_case then parent.discriminant.node else node.left.node)
|
|
45
|
+
let enum_comparison_right enum_comparison_parts(if node is ts_case then node.test.node else node.right.node)
|
|
46
|
+
when enum_comparison_unsafe(enum_comparison_left, enum_comparison_right)
|
|
47
|
+
let enum_comparison_message if node is ts_case
|
|
48
|
+
then "The case statement does not have a shared enum type with the switch predicate."
|
|
49
|
+
else "The two values in this comparison do not have a shared enum type."
|
|
50
|
+
warn "{enum_comparison_message}"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import .ts/values/inspection
|
|
2
|
+
import .ts/typed/rejection-handlers
|
|
3
|
+
|
|
4
|
+
define {
|
|
5
|
+
|
|
6
|
+
fn error_rethrown(n) = {
|
|
7
|
+
value bare: unparenthesised_node(n)
|
|
8
|
+
value declaration: if bare is ts_identifier then name_binding(bare).node else null
|
|
9
|
+
value parameter: declaration.parent.node
|
|
10
|
+
value function: parameter.parent.node
|
|
11
|
+
value call: function.parent.node
|
|
12
|
+
value declaration != null && (declaration.declaration_kind == "catch"
|
|
13
|
+
|| parameter is ts_parameter && function is ts_arrow && call is ts_call
|
|
14
|
+
&& first(function.parameter.nodes) == parameter && rejection_handler(call).handler == function)
|
|
15
|
+
}
|
|
16
|
+
fn error_kind(n) = error_kind_at(n, n)
|
|
17
|
+
fn error_kind_at(n, subject) = {
|
|
18
|
+
value arms: value_parts(subject)
|
|
19
|
+
value parts: map(arms ?? [], arm => arm.parts)
|
|
20
|
+
value when {
|
|
21
|
+
arms == null then "unknown"
|
|
22
|
+
ts_value_trait(parts, "any") then "any"
|
|
23
|
+
count(arms) > 0 && all(arms, arm =>
|
|
24
|
+
any(arm.parts, part => ts_value_builtin_ancestor(part, n, ["Error"]) == true)) then "error"
|
|
25
|
+
ts_value_trait(parts, "unknown") then "unknown"
|
|
26
|
+
ts_value_trait(parts, "undefined") && !any(parts, arm => any(arm, part => contains(part.traits, "void"))) then "undefined"
|
|
27
|
+
else then "other"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
in ts_node let ts_error_value {kind: error_kind(node), rethrown: error_rethrown(node)}
|
|
31
|
+
|
|
32
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# Floating promise values and immediate promise elements in arrays or tuples.
|
|
2
|
+
|
|
3
|
+
define {
|
|
4
|
+
|
|
5
|
+
in ts_node let floating_promise_arms value_parts(node)
|
|
6
|
+
in ts_await let floating_promise_arms value_parts(ts_awaited_value(node, argument.node))
|
|
7
|
+
in ts_node let floating_promise_safe_call false
|
|
8
|
+
in ts_node let floating_promise_safe_types []
|
|
9
|
+
in ts_node let floating_promises_checked true
|
|
10
|
+
fn floating_promise_safe(n, safe_calls) = {
|
|
11
|
+
value expression: unparenthesised_node(n)
|
|
12
|
+
value expression.floating_promise_safe_call
|
|
13
|
+
|| len(safe_calls) > 0 && expression is ts_call && {
|
|
14
|
+
value callee: unparenthesised_node(expression.callee.node)
|
|
15
|
+
value callee is ts_identifier && contains(safe_calls, callee.text)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
fn floating_promise_subject_checked(n, ignore_iife) = {
|
|
19
|
+
value n.floating_promises_checked
|
|
20
|
+
&& (!(n.parent.node is ts_arrow) || n is ts_unary)
|
|
21
|
+
&& !(ignore_iife && n.parent.node is ts_expression_statement && {
|
|
22
|
+
value expression: unparenthesised_node(n)
|
|
23
|
+
value expression is ts_call && unparenthesised_node(expression.callee.node) is (ts_function, ts_arrow)
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
fn floating_rejection_handler(n) = {
|
|
27
|
+
value callee: unparenthesised_node(n.callee.node)
|
|
28
|
+
value position: if callee.static_name == "catch" then 0
|
|
29
|
+
else if callee.static_name == "then" then 1 else -1
|
|
30
|
+
value handler: if callee.static_name == "catch" then first(n.argument.nodes)
|
|
31
|
+
else if callee.static_name == "then" then { value n.argument.nodes; slice 1 2; first } else null
|
|
32
|
+
value checked_count: position + 1
|
|
33
|
+
value preceding: slice(n.argument.nodes, 0, checked_count)
|
|
34
|
+
value if handler == null || any(preceding, argument => argument is ts_spread) then "missing"
|
|
35
|
+
else if handler could callable && handler always callable then "handled" else "useless"
|
|
36
|
+
}
|
|
37
|
+
fn floating_promise_nonnull(n) = {
|
|
38
|
+
value arms: value_constituents(n)
|
|
39
|
+
value arms != null && len(arms) > 0
|
|
40
|
+
&& all(arms, arm => any(arm, part => any(part.traits, trait =>
|
|
41
|
+
contains(["boolean", "number", "string", "bigint", "symbol", "object", "truthy"], trait))))
|
|
42
|
+
}
|
|
43
|
+
fn floating_promise_logical_leaves(n) =
|
|
44
|
+
if n.operator == "&&" && n.left.node could falsy && n.left.node always falsy
|
|
45
|
+
|| n.operator == "||" && n.left.node could truthy && n.left.node always truthy
|
|
46
|
+
|| n.operator == "??" && floating_promise_nonnull(n.left.node) then [n.left.node]
|
|
47
|
+
else [n.left.node, n.right.node]
|
|
48
|
+
fn floating_promise_leaves(n, ignore_void) = {
|
|
49
|
+
value unparenthesised_node(n)
|
|
50
|
+
walk (current) if current is ts_parenthesized then current.expression.node
|
|
51
|
+
else if current is ts_sequence then current.expression.nodes
|
|
52
|
+
else if current is ts_logical then floating_promise_logical_leaves(current)
|
|
53
|
+
else if current is ts_conditional then [current.consequent.node, current.alternate.node]
|
|
54
|
+
else if !ignore_void && current is ts_unary && current.operator == "void" then current.argument.node
|
|
55
|
+
else if current is ts_call && current.callee.node.static_name == "finally" then current.callee.node.object.node else null
|
|
56
|
+
filter (part) part not (ts_parenthesized, ts_sequence, ts_logical, ts_conditional)
|
|
57
|
+
&& !(part is ts_call && part.callee.node.static_name == "finally")
|
|
58
|
+
&& !(!ignore_void && part is ts_unary && part.operator == "void")
|
|
59
|
+
}
|
|
60
|
+
fn floating_promise_alias_allowed(n, safe_promises) = len(safe_promises) > 0 && {
|
|
61
|
+
value walk(value_sources(n).sites ?? [], site => {
|
|
62
|
+
value sources: site_sources(n, site)
|
|
63
|
+
value if sources.combine == "union" then [] else sources.sites ?? []
|
|
64
|
+
})
|
|
65
|
+
any (site) site.kind == "ts_type_alias" && contains(safe_promises, site.name)
|
|
66
|
+
&& any(site_sources(n, site).sites, body =>
|
|
67
|
+
contains(["ts_type_literal", "ts_union", "ts_intersection", "ts_array_type", "ts_tuple_type", "ts_readonly_type"], body.kind))
|
|
68
|
+
}
|
|
69
|
+
fn floating_promise_arm_allowed(arm, n, safe_promises) = {
|
|
70
|
+
value entries: n.floating_promise_safe_types
|
|
71
|
+
value directory: if any(entries, entry => entry.from == "file") then program_directory(n) else null
|
|
72
|
+
value len(safe_promises) > 0 && any(arm.parts, part => part.carrier != null
|
|
73
|
+
&& any(ts_value_ancestry(part).parts, base => contains(safe_promises, base.carrier.description.name)))
|
|
74
|
+
|| any(entries, entry => any(arm.parts, part => type_specifier_part(entry, n, part, directory) != false))
|
|
75
|
+
}
|
|
76
|
+
fn floating_promise_arm(arm, n, check_thenables, safe_promises) =
|
|
77
|
+
!floating_promise_arm_allowed(arm, n, safe_promises)
|
|
78
|
+
&& (any(arm.parts, part => part.carrier != null
|
|
79
|
+
&& ts_value_builtin_ancestor(part, n, ["Promise"]) == true)
|
|
80
|
+
|| check_thenables && {
|
|
81
|
+
value member: value_lookup(arm.handle, "then").member
|
|
82
|
+
value headers: value_signatures(member)
|
|
83
|
+
value any(headers ?? [], alternatives => any(alternatives, header => count(header.parameters) >= 2
|
|
84
|
+
&& all(slice(header.parameters, 0, 2), parameter => floating_callable_arm(parameter))))
|
|
85
|
+
})
|
|
86
|
+
fn floating_promise_failure(n, check_thenables, ignore_void, safe_promises, safe_calls) = {
|
|
87
|
+
value floating_promise_leaves(n, ignore_void)
|
|
88
|
+
map (part) if part is ts_assignment || floating_promise_safe(part, safe_calls)
|
|
89
|
+
|| floating_promise_alias_allowed(part, safe_promises)
|
|
90
|
+
|| type_value_allowed(part.floating_promise_safe_types, part) != false then null else {
|
|
91
|
+
value arms: part.floating_promise_arms
|
|
92
|
+
value if floating_promise_array(arms, part, check_thenables, safe_promises) then "array"
|
|
93
|
+
else if part not ts_await
|
|
94
|
+
&& any(arms ?? [], arm => floating_promise_arm(arm, part, check_thenables, safe_promises))
|
|
95
|
+
then if part is ts_call then {
|
|
96
|
+
value handler: floating_rejection_handler(part)
|
|
97
|
+
value if handler == "handled" then null else if handler == "useless" then "useless" else "promise"
|
|
98
|
+
} else "promise" else null
|
|
99
|
+
}
|
|
100
|
+
filter (failure) failure != null
|
|
101
|
+
first
|
|
102
|
+
}
|
|
103
|
+
in ts_expression_statement.expression, ts_arrow.body rule es-no-floating-promises {
|
|
104
|
+
option checkThenables boolean = false "Check promise-like types with fulfilment and rejection callbacks"
|
|
105
|
+
option ignoreVoid boolean = true "Treat the void operator as explicit promise handling"
|
|
106
|
+
option ignoreIIFE boolean = false "Ignore a directly invoked function expression"
|
|
107
|
+
option allowForKnownSafePromises string[] = [] "Promise type names whose rejection is handled elsewhere"
|
|
108
|
+
option allowForKnownSafeCalls string[] = [] "Function names whose returned promise is handled elsewhere"
|
|
109
|
+
when floating_promise_subject_checked(node, ignoreIIFE)
|
|
110
|
+
when node not ts_assignment
|
|
111
|
+
let failure floating_promise_failure(node, checkThenables, ignoreVoid, allowForKnownSafePromises, allowForKnownSafeCalls)
|
|
112
|
+
when contains(["promise", "useless"], failure)
|
|
113
|
+
report at if parent.node is ts_expression_statement then parent.node else node
|
|
114
|
+
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."
|
|
115
|
+
else "Promises must be awaited, end with a call to .catch, or end with a call to .then with a rejection handler.") (if failure == "useless"
|
|
116
|
+
then " A rejection handler that is not a function will be ignored." else "")
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
fn floating_callable_arm(handle) = any(value_alternatives(handle) ?? [], arm => {
|
|
120
|
+
value signatures: value_signatures(arm)
|
|
121
|
+
value signatures != null && any(signatures, headers => len(headers) > 0)
|
|
122
|
+
})
|
|
123
|
+
fn floating_promise_array(arms, n, check_thenables, safe_promises) = any(arms ?? [], arm =>
|
|
124
|
+
!any(arm.parts, part => contains(part.traits, "intersection"))
|
|
125
|
+
&& !floating_promise_arm_allowed(arm, n, safe_promises) && any(arm.parts, part => {
|
|
126
|
+
value shape: ts_value_array_part(part, n)
|
|
127
|
+
value elements: if shape.tuple then value_children(part.handle, true, [])
|
|
128
|
+
else if shape.array then value_children(part.handle, false, [0]) else []
|
|
129
|
+
value any(elements ?? [], element => any(value_parts(element.handle) ?? [],
|
|
130
|
+
item => floating_promise_arm(item, n, check_thenables, safe_promises)))
|
|
131
|
+
}))
|
|
132
|
+
|
|
133
|
+
in ts_expression_statement.expression, ts_arrow.body rule es-no-floating-promises:array {
|
|
134
|
+
option checkThenables boolean = false "Check promise-like types with fulfilment and rejection callbacks"
|
|
135
|
+
option ignoreVoid boolean = true "Treat the void operator as explicit promise handling"
|
|
136
|
+
option ignoreIIFE boolean = false "Ignore a directly invoked function expression"
|
|
137
|
+
option allowForKnownSafePromises string[] = [] "Promise type names whose rejection is handled elsewhere"
|
|
138
|
+
option allowForKnownSafeCalls string[] = [] "Function names whose returned promise is handled elsewhere"
|
|
139
|
+
when floating_promise_subject_checked(node, ignoreIIFE)
|
|
140
|
+
when node not ts_assignment
|
|
141
|
+
when floating_promise_failure(node, checkThenables, ignoreVoid, allowForKnownSafePromises, allowForKnownSafeCalls) == "array"
|
|
142
|
+
report at if parent.node is ts_expression_statement then parent.node else node
|
|
143
|
+
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
|
+
else "An array of Promises may be unintentional. Consider handling the promises' fulfillment or rejection with Promise.all or similar."
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
}
|