@inthedark/dxlint 0.1.0 → 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/bin/dxlint +15 -15
- package/package.json +15 -4
- package/std/ts/expressions.dx +270 -270
- package/std/ts/indentation.dx +442 -442
- package/std/ts/layout/separators.dx +161 -161
- package/std/ts/restrictions.dx +586 -586
- package/std/ts/typed/floating-promises.dx +147 -147
- package/std/ts/typed/optional-chains.dx +317 -317
- package/std/ts/typed/promises.dx +205 -205
- package/std/ts/typed.dx +585 -585
- package/std/ts/values/enums.dx +104 -104
- package/std/ts/values.dx +1217 -1217
package/std/ts/typed.dx
CHANGED
|
@@ -1,585 +1,585 @@
|
|
|
1
|
-
# Type-aware rules over TypeScript syntax and value queries.
|
|
2
|
-
|
|
3
|
-
import .ts/arithmetic
|
|
4
|
-
import .ts/deprecation
|
|
5
|
-
import .ts/import-no-deprecated
|
|
6
|
-
import .ts/methods
|
|
7
|
-
import .ts/spreads
|
|
8
|
-
import .ts/type-specifiers
|
|
9
|
-
import .ts/typed/promises
|
|
10
|
-
import .ts/typed/errors
|
|
11
|
-
import .ts/typed/awaitability
|
|
12
|
-
import .ts/typed/optional-chains
|
|
13
|
-
import .ts/typed/floating-promises
|
|
14
|
-
import .ts/typed/unbound-method
|
|
15
|
-
import .ts/typed/template-expressions
|
|
16
|
-
import .ts/typed/readonly-members
|
|
17
|
-
import .ts/typed/readonly-parameters
|
|
18
|
-
import .ts/typed/string-boundaries
|
|
19
|
-
import .ts/typed/includes
|
|
20
|
-
import .ts/typed/type-constituents
|
|
21
|
-
import .ts/typed/redundant-constituents
|
|
22
|
-
import .ts/typed/return-await
|
|
23
|
-
import .ts/typed/return-this
|
|
24
|
-
import .ts/typed/type-arguments
|
|
25
|
-
import .ts/typed/nullish-coalescing
|
|
26
|
-
import .ts/typed/boolean-comparisons
|
|
27
|
-
import .ts/typed/void-expressions
|
|
28
|
-
import .ts/typed/strict-void-return
|
|
29
|
-
import .ts/typed/assignments
|
|
30
|
-
import .ts/typed/transfers
|
|
31
|
-
import .ts/typed/defaults
|
|
32
|
-
import .ts/typed/template-values
|
|
33
|
-
import .ts/typed/enum-consistency
|
|
34
|
-
import .ts/typed/type-parameters
|
|
35
|
-
import .ts/typed/switches
|
|
36
|
-
import .ts/typed/enums
|
|
37
|
-
import .ts/typed/qualifiers
|
|
38
|
-
import .ts/typed/regexp-exec
|
|
39
|
-
import .ts/typed/for-in-arrays
|
|
40
|
-
import .ts/typed/type-conversions
|
|
41
|
-
import .ts/typed/rejection-handlers
|
|
42
|
-
import .ts/typed/unsafe-members
|
|
43
|
-
import .ts/typed/unsafe-calls
|
|
44
|
-
import .ts/values/inspection
|
|
45
|
-
import .ts/constants
|
|
46
|
-
|
|
47
|
-
define {
|
|
48
|
-
|
|
49
|
-
fn generated_empty_type(n) = {
|
|
50
|
-
value arms: value_parts(n)
|
|
51
|
-
value arms != null && any(arms, arm =>
|
|
52
|
-
count(arm.parts) > 0 && all(arm.parts, part =>
|
|
53
|
-
contains(part.traits, "structural") && count(part.uncertain_traits) == 0
|
|
54
|
-
&& part.carrier_known && part.carrier == null)
|
|
55
|
-
&& value_children(arm.handle, true, []) == []
|
|
56
|
-
&& value_signatures(arm.handle) == [[]] && value_constructors(arm.handle) == [[]])
|
|
57
|
-
}
|
|
58
|
-
in ts_type_reference, ts_intersection rule es-no-generated-empty-object-type {
|
|
59
|
-
when node is ts_intersection || count(type_argument.nodes) > 0
|
|
60
|
-
&& last(walk(node, item => if item.parent is ts_parenthesized_type then item.parent.node else null)).parent not ts_intersection
|
|
61
|
-
when generated_empty_type(node)
|
|
62
|
-
warn "This type resolves to `{{}}`, the empty object type. This was likely not intentional."
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
in ts_function, ts_arrow, ts_method rule es-require-await {
|
|
66
|
-
when missing_await != null
|
|
67
|
-
report at ts_await_warning_site(node)
|
|
68
|
-
warn missing_await.name " has no 'await' expression."
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
group promise_function_async_function ts_function, ts_arrow, ts_method
|
|
72
|
-
in promise_function_async_function let promise_function_async_kind
|
|
73
|
-
if is ts_arrow then "arrow"
|
|
74
|
-
else if is ts_method then if parent is ts_class then "method" else "expression"
|
|
75
|
-
else if parent is (ts_program, ts_block, ts_export, ts_case, ts_if, ts_labeled) then "declaration"
|
|
76
|
-
else "expression"
|
|
77
|
-
in promise_function_async_function let promise_function_async_checked true
|
|
78
|
-
in ts_method let promise_function_async_exempt
|
|
79
|
-
contains(["get", "set"], kind)
|
|
80
|
-
|| parent is ts_class && !contains([] modifier, "static") && key not ts_computed_key && static_name == "constructor"
|
|
81
|
-
fn promise_function_async_edit(n) = {
|
|
82
|
-
value anchor: if n is ts_method then find(n.modifier.nodes, modifier => modifier.text == "*") ?? n.key.node else n
|
|
83
|
-
value before: anchor.token_neighbours.before
|
|
84
|
-
value separator: if before.end == anchor.offset then expression_fix_separator(before.text, "async") else ""
|
|
85
|
-
value { start: anchor.offset, text: separator "async " }
|
|
86
|
-
}
|
|
87
|
-
in promise_function_async_function rule es-promise-function-async {
|
|
88
|
-
option allowAny boolean = true "Allow any and unknown return types"
|
|
89
|
-
option allowedPromiseNames string[] = [] "Additional promise type names"
|
|
90
|
-
when body != null && !contains([] modifier, "async")
|
|
91
|
-
when !(promise_function_async_exempt.nonempty ?? false)
|
|
92
|
-
when promise_function_async_checked
|
|
93
|
-
let names ("Promise" allowedPromiseNames)
|
|
94
|
-
let policy promise_async_policy(node, names)
|
|
95
|
-
when policy.known
|
|
96
|
-
let disallowed_unknown !allowAny && policy.dynamic
|
|
97
|
-
when disallowed_unknown || policy.required
|
|
98
|
-
report at function_head_site(node)
|
|
99
|
-
warn if !disallowed_unknown && policy.mixed then "Functions that return promises must be async. Consider adding an explicit return type annotation if the function is intended to return a union of promise and non-promise types." else "Functions that return promises must be async."
|
|
100
|
-
let edit promise_function_async_edit(node)
|
|
101
|
-
fix at { start: edit.start, end: edit.start } if !disallowed_unknown as edit.text
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
in ts_node let only_throw_error_checked true
|
|
105
|
-
in ts_throw.argument rule es-only-throw-error {
|
|
106
|
-
option allow (string | type_specifier_file | type_specifier_lib | type_specifier_package)[] = [] "Additional allowed thrown types"
|
|
107
|
-
option allowRethrowing boolean = true "Allow rethrowing catch and Promise rejection parameters"
|
|
108
|
-
option allowThrowingAny boolean = true "Allow any values"
|
|
109
|
-
option allowThrowingUnknown boolean = true "Allow unknown values"
|
|
110
|
-
when ts_error_value.kind != null && ts_error_value.kind != "error"
|
|
111
|
-
when !allowRethrowing || !ts_error_value.rethrown
|
|
112
|
-
when !allowThrowingAny || ts_error_value.kind != "any"
|
|
113
|
-
when !allowThrowingUnknown || ts_error_value.kind != "unknown"
|
|
114
|
-
when only_throw_error_checked
|
|
115
|
-
when type_value_allowed(allow, node) != true
|
|
116
|
-
report at unparenthesised_node(node)
|
|
117
|
-
warn if ts_error_value.kind == "undefined" then "Do not throw undefined." else "Expected an error object to be thrown."
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
in ts_identifier let rejection_global_promise text == "Promise" && resolved == "global" && global != "off"
|
|
121
|
-
in ts_identifier, ts_member, ts_index let rejection_constructor
|
|
122
|
-
if re_test(file, /\.[cm]?tsx?$/) then ts_value_builtin_constructor(node, ["Promise"]) == true else rejection_global_promise.nonempty ?? false
|
|
123
|
-
in ts_identifier, ts_member, ts_index let rejection_receiver
|
|
124
|
-
rejection_constructor || re_test(file, /\.[cm]?tsx?$/) && ts_value_builtin_type(node, ["Promise"]) == true
|
|
125
|
-
in ts_member, ts_index let rejection_member static_name == "reject" && object.rejection_receiver == true
|
|
126
|
-
in ts_parenthesized forward rejection_global_promise, rejection_constructor, rejection_receiver, rejection_member from expression
|
|
127
|
-
in ts_new let rejection_promise_constructor callee.rejection_constructor == true
|
|
128
|
-
in ts_function, ts_arrow, ts_parenthesized let rejection_executor
|
|
129
|
-
if parent is ts_parenthesized then parent.rejection_executor
|
|
130
|
-
else is ts_new.argument && index == 0 && parent.rejection_promise_constructor
|
|
131
|
-
in ts_parameter let rejection_parameter index == 1 && init == null && parent.rejection_executor == true
|
|
132
|
-
in ts_identifier let rejection_binding is ts_parameter.name && parent.rejection_parameter == true
|
|
133
|
-
in ts_call let rejection_call
|
|
134
|
-
callee.rejection_member == true || any(callee.declarations, d => d.rejection_binding == true)
|
|
135
|
-
fn rejection_reason_subject(n) = if n is ts_await then ts_awaited_value(n, n.argument.node) ?? n else n
|
|
136
|
-
in ts_node let rejection_reason
|
|
137
|
-
{possible: could_be_error && error_undefined_global != true, kind: error_kind_at(node, rejection_reason_subject(node)),
|
|
138
|
-
subject: rejection_reason_subject(node)}
|
|
139
|
-
fn rejection_reason_known(n) = value_parts(n) != null || unparenthesised_node(n) is ts_array
|
|
140
|
-
|| unparenthesised_node(n) is ts_assignment && contains(["-=", "*=", "/=", "%=", "**=", "<<=", ">>=", ">>>=", "|=", "&=", "^="],
|
|
141
|
-
unparenthesised_node(n).operator)
|
|
142
|
-
in ts_call let prefer_promise_reject_errors_checked true
|
|
143
|
-
in ts_call rule es-prefer-promise-reject-errors {
|
|
144
|
-
option allowEmptyReject boolean = false "Allow rejection without a reason"
|
|
145
|
-
option allow (string | type_specifier_file | type_specifier_lib | type_specifier_package)[] = [] "Additional allowed rejection types"
|
|
146
|
-
option allowThrowingAny boolean = false "Allow any values"
|
|
147
|
-
option allowThrowingUnknown boolean = false "Allow unknown values"
|
|
148
|
-
when rejection_call
|
|
149
|
-
let reason first([] argument.rejection_reason)
|
|
150
|
-
when reason != null || !allowEmptyReject
|
|
151
|
-
when reason == null || (if re_test(file, /\.[cm]?tsx?$/) then rejection_reason_known(reason.subject) && reason.kind != "error" && !(allowThrowingAny && reason.kind == "any") && !(allowThrowingUnknown && reason.kind == "unknown") && type_value_allowed(allow, reason.subject) == false else !reason.possible)
|
|
152
|
-
when prefer_promise_reject_errors_checked
|
|
153
|
-
warn "Expected the Promise rejection reason to be an Error."
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
fn stringification_union(states) = if count(states) == 0 then 0 else if any(states, state => state == null) then null
|
|
157
|
-
else if all(states, state => state == 2) then 2 else if all(states, state => state == 0) then 0 else 1
|
|
158
|
-
# Elements are all stringified, so one certain element is enough; a union's arms are exclusive.
|
|
159
|
-
fn stringification_elements(states) = if any(states, state => state == 2) then 2
|
|
160
|
-
else if any(states, state => state == null) then null
|
|
161
|
-
else if any(states, state => state == 1) then 1 else 0
|
|
162
|
-
fn stringification_member(n, handle, key) = {
|
|
163
|
-
value roots: global_declarations(n, "Object", "type")
|
|
164
|
-
value defaults: map(roots ?? [], root => value_lookup(declaration_value(n, root), key))
|
|
165
|
-
value lookup: value_lookup(handle, key)
|
|
166
|
-
value known: roots != null && count(roots) > 0
|
|
167
|
-
&& all(defaults, item => item.alternatives != null)
|
|
168
|
-
&& lookup.alternatives != null
|
|
169
|
-
value canonical: flat_map(defaults, item => flat_map(item.alternatives, arm => map(arm.sources, source => source.site)))
|
|
170
|
-
value sources: filter(flat_map(lookup.alternatives ?? [], arm => arm.sources), source => source.kind != "indexer")
|
|
171
|
-
value if !known || any(sources, source => source.site == null) then null
|
|
172
|
-
else if any(sources, source => !contains(canonical, source.site)) then 0
|
|
173
|
-
else 2
|
|
174
|
-
}
|
|
175
|
-
fn stringification_ignored_base(n, carrier, ignored) = if carrier == null then false else {
|
|
176
|
-
value sites: walk([carrier], site => {
|
|
177
|
-
value constructed: if site.kind != "ts_declarator" then [] else flat_map(value_constituents(value_lookup(declaration_value(n, site), "prototype").member) ?? [], arm => map(arm, part => part.carrier))
|
|
178
|
-
value (site_field(n, site, "extends") ?? []) (site_sources(n, site).sites ?? []) filter(constructed, item => item != null)
|
|
179
|
-
})
|
|
180
|
-
value any(sites, site => contains(["ts_class", "ts_interface"], site.kind) && contains(ignored, site.name)
|
|
181
|
-
|| site.description.kind == "named" && contains(ignored, site.description.name))
|
|
182
|
-
}
|
|
183
|
-
fn stringification_ignored_alias(n, ignored) = {
|
|
184
|
-
value arms: value_parts(n)
|
|
185
|
-
value roots: value_sources(n)
|
|
186
|
-
value sites: walk(if count(roots.sites) == 1 then roots.sites else [], site =>
|
|
187
|
-
if count(site_sources(n, site).sites) == 1 then site_sources(n, site).sites else [])
|
|
188
|
-
value arms != null && all(arms, arm => all(arm.parts, part => part.carrier_known && part.carrier == null))
|
|
189
|
-
&& any(sites, site => site.kind == "ts_type_alias" && contains(ignored, site.name))
|
|
190
|
-
}
|
|
191
|
-
fn stringification_part(n, part, ignored, check_unknown, seen) = {
|
|
192
|
-
value primitive: any(["boolean", "string", "number", "bigint", "symbol", "null", "undefined", "any", "callable"],
|
|
193
|
-
trait => contains(part.traits, trait))
|
|
194
|
-
value carrier: part.carrier
|
|
195
|
-
value ignored_name: carrier != null && contains(ignored, carrier.description.name)
|
|
196
|
-
|| stringification_ignored_base(n, carrier, ignored)
|
|
197
|
-
value if primitive || ignored_name then 0
|
|
198
|
-
else if contains(part.traits, "unknown") || contains(part.traits, "parameter") then if check_unknown then 1 else 0
|
|
199
|
-
else if contains(["array", "tuple"], carrier.description.kind) then
|
|
200
|
-
stringification_elements(map(carrier.arguments, argument => stringification_certainty(n, argument, ignored, check_unknown, seen)))
|
|
201
|
-
else if !part.carrier_known then null else {
|
|
202
|
-
value symbol: value_lookup(part.handle, ts_value_symbol_key(n, "toPrimitive"))
|
|
203
|
-
value custom_symbol: symbol.alternatives != null
|
|
204
|
-
&& any(symbol.alternatives, arm => arm.matched && any(arm.sources, source => source.kind != "indexer"))
|
|
205
|
-
value states: map(["toString", "toLocaleString", "valueOf"], key => stringification_member(n, part.handle, key))
|
|
206
|
-
value if custom_symbol || any(states, state => state == 0) then 0
|
|
207
|
-
else if symbol.alternatives == null || any(states, state => state == null) then null
|
|
208
|
-
else if any(states, state => state == 2) then 2 else null
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
fn stringification_parts(arm) = {
|
|
212
|
-
value concrete: filter(arm.parts, part => part.traits != ["intersection"])
|
|
213
|
-
value if count(concrete) > 0 then concrete else arm.parts
|
|
214
|
-
}
|
|
215
|
-
fn stringification_certainty(n, handle, ignored, check_unknown, seen) = {
|
|
216
|
-
value arms: if contains(seen, handle) then null else value_parts(handle)
|
|
217
|
-
value if arms == null then null else if count(arms) == 0 then 0 else stringification_union(map(arms, arm => {
|
|
218
|
-
value concrete: filter(stringification_parts(arm), part => part.traits != ["parameter"])
|
|
219
|
-
value parts: if count(concrete) > 0 then concrete else arm.parts
|
|
220
|
-
value states: map(parts, part => stringification_part(n, part, ignored, check_unknown, seen [handle]))
|
|
221
|
-
value if any(states, state => state == 0) then 0
|
|
222
|
-
else if any(states, state => state == null) then null
|
|
223
|
-
else if all(states, state => state == 1) then 1 else 2
|
|
224
|
-
}))
|
|
225
|
-
}
|
|
226
|
-
fn stringification_join(n, ignored, check_unknown) = {
|
|
227
|
-
value arms: value_parts(n)
|
|
228
|
-
value if arms == null then null else stringification_union(map(arms, arm => {
|
|
229
|
-
value states: map(stringification_parts(arm), part => if contains(["array", "tuple"], part.carrier.description.kind)
|
|
230
|
-
then stringification_elements(map(part.carrier.arguments, argument => stringification_certainty(n, argument, ignored, check_unknown, [])))
|
|
231
|
-
else 0)
|
|
232
|
-
value if any(states, state => state == 0) then 0
|
|
233
|
-
else if any(states, state => state == null) then null else max(states)
|
|
234
|
-
}))
|
|
235
|
-
}
|
|
236
|
-
fn stringification_literal_array(n, ignored, check_unknown) = {
|
|
237
|
-
value elements: n.element.nodes
|
|
238
|
-
value spread: any(elements, element => element is ts_spread)
|
|
239
|
-
value if spread then null
|
|
240
|
-
else stringification_union(map(elements, element => stringification_certainty(n, element, ignored, check_unknown, [])))
|
|
241
|
-
}
|
|
242
|
-
in ts_call let stringification_subjects
|
|
243
|
-
if callee is ts_identifier && global_callee == "String" then [{node: { value argument.nodes; first; unparenthesised_node }, join: false}]
|
|
244
|
-
else if callee is ts_member && callee.property is ts_identifier && contains(["toString", "toLocaleString", "join"], callee.property.text) then [{node: unparenthesised_node(callee.object.node), join: callee.property.text == "join"}]
|
|
245
|
-
else []
|
|
246
|
-
in ts_binary, ts_assignment let stringification_subjects
|
|
247
|
-
if contains(["+", "+="], operator) && ts_value_string_like(left.node) == true then [{node: unparenthesised_node(right.node), join: false}]
|
|
248
|
-
else if contains(["+", "+="], operator) && ts_value_string_like(right.node) == true && left not ts_private_name then [{node: unparenthesised_node(left.node), join: false}]
|
|
249
|
-
else []
|
|
250
|
-
in ts_template let stringification_subjects if parent is ts_tagged_template then [] else map(expression.nodes, n => {node: unparenthesised_node(n), join: false})
|
|
251
|
-
in ts_call, ts_binary, ts_assignment, ts_template rule es-no-base-to-string {
|
|
252
|
-
option ignoredTypeNames string[] = ["Error", "RegExp", "URL", "URLSearchParams"] "Types whose string conversion is permitted"
|
|
253
|
-
option checkUnknown boolean = false "Check explicit unknown and unconstrained type parameters"
|
|
254
|
-
let findings map(stringification_subjects, subject => {
|
|
255
|
-
node: subject.node,
|
|
256
|
-
join: subject.join,
|
|
257
|
-
certainty: if stringification_ignored_alias(subject.node, ignoredTypeNames) then 0 else if subject.join then stringification_join(subject.node, ignoredTypeNames, checkUnknown) else if subject.node is ts_array && value_parts(subject.node) == null then stringification_literal_array(subject.node, ignoredTypeNames, checkUnknown) else stringification_certainty(subject.node, subject.node, ignoredTypeNames, checkUnknown, [])
|
|
258
|
-
})
|
|
259
|
-
each subject in findings {
|
|
260
|
-
when subject.node != null && subject.node not (ts_literal, ts_spread)
|
|
261
|
-
when subject.certainty != null && subject.certainty > 0
|
|
262
|
-
report at {start: subject.node.offset, end: subject.node.end_offset}
|
|
263
|
-
warn if subject.join then "Using `join()` for " subject.node.text " " (if subject.certainty == 2 then "will" else "may") " use Object's default stringification format ('[object Object]') when stringified."
|
|
264
|
-
else "'" subject.node.text "' " (if subject.certainty == 2 then "will" else "may") " use Object's default stringification format ('[object Object]') when stringified."
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
in ts_identifier {
|
|
269
|
-
let eval_name text
|
|
270
|
-
let eval_identifier true
|
|
271
|
-
let eval_core_root if contains(["global", "globalThis", "window", "self"], text) && global != null && global != "off" then text else null
|
|
272
|
-
let eval_timer_global resolved == "global" && (re_test(file, /\.[cm]?tsx?$/) || global != null && global != "off")
|
|
273
|
-
let eval_constructor ts_value_builtin_type(node, ["FunctionConstructor"]) == true || ts_value_has_trait(node, "any") == true || ts_value_opaque(node) && reference_kind == null
|
|
274
|
-
}
|
|
275
|
-
in ts_member, ts_index {
|
|
276
|
-
let eval_name static_name
|
|
277
|
-
let eval_core_root if object.eval_core_root != null && static_name == object.eval_core_root then object.eval_core_root else null
|
|
278
|
-
let eval_timer_global if re_test(file, /\.[cm]?tsx?$/) then object is ts_identifier && contains(["global", "globalThis", "window"], object) else object.eval_core_root != null
|
|
279
|
-
let eval_constructor ts_value_builtin_type(node, ["FunctionConstructor"]) == true || ts_value_has_trait(node, "any") == true || ts_value_opaque(node)
|
|
280
|
-
}
|
|
281
|
-
in ts_member let eval_bind_member property == "bind"
|
|
282
|
-
in ts_parenthesized {
|
|
283
|
-
let eval_name expression.eval_name
|
|
284
|
-
let eval_identifier expression.eval_identifier
|
|
285
|
-
let eval_core_root expression.eval_core_root
|
|
286
|
-
let eval_timer_global expression.eval_timer_global
|
|
287
|
-
let eval_constructor expression.eval_constructor
|
|
288
|
-
}
|
|
289
|
-
group typed_simple_expression ts_identifier, ts_private_name, ts_this, ts_super, ts_null, ts_boolean,
|
|
290
|
-
ts_number, ts_bigint, ts_string, ts_regex, ts_template, ts_tagged_template, ts_array, ts_object,
|
|
291
|
-
ts_member, ts_index, ts_new, ts_unary, ts_update, ts_await, ts_yield,
|
|
292
|
-
ts_logical, ts_assignment, ts_conditional, ts_sequence, ts_as, ts_satisfies,
|
|
293
|
-
ts_non_null, ts_type_assertion, ts_function, ts_arrow, ts_class, ts_instantiation, ts_meta_property,
|
|
294
|
-
ts_import_call
|
|
295
|
-
in typed_simple_expression, ts_binary {
|
|
296
|
-
let eval_callback ts_value_callable(node) || ts_value_builtin_type(node, ["Function"]) == true
|
|
297
|
-
}
|
|
298
|
-
fn eval_constant_string(n) = ts_runtime_string(n) != null && {
|
|
299
|
-
value arms: value_trait_parts(n, ["string", "enum"])
|
|
300
|
-
value part: if count(arms) == 1 && count(first(arms).parts) == 1 then first(first(arms).parts) else null
|
|
301
|
-
value part != null && contains(part.traits, "string") && !contains(part.traits, "enum")
|
|
302
|
-
}
|
|
303
|
-
in typed_simple_expression {
|
|
304
|
-
let eval_string is (ts_string, ts_template)
|
|
305
|
-
|| eval_constant_string(node)
|
|
306
|
-
}
|
|
307
|
-
in ts_call {
|
|
308
|
-
let eval_callback ts_value_callable(node) || ts_value_builtin_type(node, ["Function"]) == true || callee.eval_bind_member == true || callee is ts_identifier && callee == "bind"
|
|
309
|
-
let eval_string eval_constant_string(node) || callee.global_value == "String" && ts_runtime_string(first(argument.nodes)) != null
|
|
310
|
-
}
|
|
311
|
-
in ts_binary let eval_string eval_constant_string(node) || operator == "+" && (left.eval_string == true || right.eval_string == true)
|
|
312
|
-
in ts_parenthesized {
|
|
313
|
-
let eval_callback expression.eval_callback
|
|
314
|
-
let eval_string expression.eval_string
|
|
315
|
-
}
|
|
316
|
-
in ts_call, ts_new let eval_timer (contains(["setTimeout", "setInterval", "execScript"], callee.eval_name) || callee.eval_name == "setImmediate" && re_test(file, /\.[cm]?tsx?$/)) && callee.eval_timer_global == true
|
|
317
|
-
in ts_call.argument, ts_new.argument rule es-no-implied-eval {
|
|
318
|
-
when index == 0 && parent.eval_timer == true && re_test(file, /\.[cm]?tsx?$/) && eval_callback != true
|
|
319
|
-
warn "Implied eval. Consider passing a function."
|
|
320
|
-
}
|
|
321
|
-
in ts_call {
|
|
322
|
-
rule es-no-implied-eval:core {
|
|
323
|
-
when eval_timer && callee.eval_name != "execScript" && !re_test(file, /\.[cm]?tsx?$/) && first([] argument.eval_string) == true
|
|
324
|
-
warn "Implied eval. Consider passing a function instead of a string."
|
|
325
|
-
}
|
|
326
|
-
rule es-no-implied-eval:exec {
|
|
327
|
-
when eval_timer && callee.eval_name == "execScript" && !re_test(file, /\.[cm]?tsx?$/) && first([] argument.eval_string) == true
|
|
328
|
-
warn "Implied eval. Do not use execScript()."
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
in ts_call, ts_new {
|
|
332
|
-
rule es-no-implied-eval:function {
|
|
333
|
-
when callee.eval_name == "Function" && re_test(file, /\.[cm]?tsx?$/) && callee.eval_constructor
|
|
334
|
-
when callee.eval_identifier == true || callee.eval_timer_global == true
|
|
335
|
-
warn "Implied eval. Do not use the Function constructor to create functions."
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
fn unsafe_argument_sender(transfer) = if transfer.error then if transfer.mode == "array" then "error" else "error typed" else if transfer.mode == "tuple" then "of type `{transfer.sender}`" else "`{transfer.sender}`"
|
|
340
|
-
fn unsafe_assignment_sender(transfer) = if transfer.error then "error typed" else "`{transfer.sender}`"
|
|
341
|
-
fn unsafe_this_note() = "\nYou can try to fix this by turning on the `noImplicitThis` compiler option, or adding a `this` parameter to the function."
|
|
342
|
-
|
|
343
|
-
in ts_call, ts_new, ts_tagged_template {
|
|
344
|
-
rule es-no-unsafe-argument {
|
|
345
|
-
each unsafe_arg in argument_value_transfers ?? [] {
|
|
346
|
-
when unsafe_arg.mode == "argument"
|
|
347
|
-
report at unsafe_arg.site
|
|
348
|
-
warn "Unsafe argument of type " unsafe_argument_sender(unsafe_arg) " assigned to a parameter of type `{unsafe_arg.receiver}`."
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
rule es-no-unsafe-argument:spread {
|
|
352
|
-
each unsafe_arg in argument_value_transfers ?? [] {
|
|
353
|
-
when unsafe_arg.mode == "spread"
|
|
354
|
-
report at unsafe_arg.site
|
|
355
|
-
warn "Unsafe spread of an " unsafe_argument_sender(unsafe_arg) " type."
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
rule es-no-unsafe-argument:array {
|
|
359
|
-
each unsafe_arg in argument_value_transfers ?? [] {
|
|
360
|
-
when unsafe_arg.mode == "array"
|
|
361
|
-
report at unsafe_arg.site
|
|
362
|
-
warn "Unsafe spread of an " unsafe_argument_sender(unsafe_arg) " array type."
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
rule es-no-unsafe-argument:tuple {
|
|
366
|
-
each unsafe_arg in argument_value_transfers ?? [] {
|
|
367
|
-
when unsafe_arg.mode == "tuple"
|
|
368
|
-
report at unsafe_arg.site
|
|
369
|
-
warn "Unsafe spread of a tuple type. The argument is " unsafe_argument_sender(unsafe_arg) " and is assigned to a parameter of type `{unsafe_arg.receiver}`."
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
in ts_declarator, ts_parameter, ts_class_property, ts_assignment, ts_assignment_pattern, ts_property, ts_spread, ts_jsx_attribute {
|
|
375
|
-
rule es-no-unsafe-assignment {
|
|
376
|
-
each unsafe_assigned in assignment_value_transfers {
|
|
377
|
-
when unsafe_assigned.mode == "any"
|
|
378
|
-
report at unsafe_assigned.site
|
|
379
|
-
warn when { unsafe_assigned.this_any then "Unsafe assignment of an `any` value. `this` is typed as `any`." unsafe_this_note()
|
|
380
|
-
else then "Unsafe assignment of an " unsafe_assignment_sender(unsafe_assigned) " value." }
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
rule es-no-unsafe-assignment:generic {
|
|
384
|
-
each unsafe_assigned in assignment_value_transfers {
|
|
385
|
-
when unsafe_assigned.mode == "generic"
|
|
386
|
-
report at unsafe_assigned.site
|
|
387
|
-
warn "Unsafe assignment of type " unsafe_assignment_sender(unsafe_assigned) " to a variable of type `{unsafe_assigned.receiver}`."
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
rule es-no-unsafe-assignment:array {
|
|
391
|
-
each unsafe_assigned in assignment_value_transfers {
|
|
392
|
-
when unsafe_assigned.mode == "array"
|
|
393
|
-
report at unsafe_assigned.site
|
|
394
|
-
warn "Unsafe array destructuring of an " unsafe_assignment_sender(unsafe_assigned) " array value."
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
rule es-no-unsafe-assignment:tuple {
|
|
398
|
-
each unsafe_assigned in assignment_value_transfers {
|
|
399
|
-
when unsafe_assigned.mode == "tuple"
|
|
400
|
-
report at unsafe_assigned.site
|
|
401
|
-
warn "Unsafe array destructuring of a tuple element with an " unsafe_assignment_sender(unsafe_assigned) " value."
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
rule es-no-unsafe-assignment:object {
|
|
405
|
-
each unsafe_assigned in assignment_value_transfers {
|
|
406
|
-
when unsafe_assigned.mode == "object"
|
|
407
|
-
report at unsafe_assigned.site
|
|
408
|
-
warn "Unsafe object destructuring of a property with an " unsafe_assignment_sender(unsafe_assigned) " value."
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
rule es-no-unsafe-assignment:spread {
|
|
412
|
-
each unsafe_assigned in assignment_value_transfers {
|
|
413
|
-
when unsafe_assigned.mode == "spread"
|
|
414
|
-
report at unsafe_assigned.site
|
|
415
|
-
warn "Unsafe spread of an " unsafe_assignment_sender(unsafe_assigned) " value in an array."
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
fn unsafe_return_sender(transfer) = if transfer.error then "error" else if transfer.mode == "array" then "`any[]`" else if transfer.mode == "promise" then "`Promise<any>`" else "`any`"
|
|
421
|
-
|
|
422
|
-
in ts_return, ts_arrow.body rule es-no-unsafe-return {
|
|
423
|
-
when return_value_transfer != null && return_value_transfer.mode != "generic"
|
|
424
|
-
warn when { return_value_transfer.this_any then "Unsafe return of a value of type `{unsafe_return_sender(return_value_transfer)}`. `this` is typed as `any`." unsafe_this_note()
|
|
425
|
-
else then "Unsafe return of a value of type " unsafe_return_sender(return_value_transfer) "." }
|
|
426
|
-
}
|
|
427
|
-
in ts_return, ts_arrow.body rule es-no-unsafe-return:assignment {
|
|
428
|
-
when return_value_transfer != null && return_value_transfer.mode == "generic"
|
|
429
|
-
warn "Unsafe return of type `{return_value_transfer.sender}` from function with return type `{return_value_transfer.receiver}`."
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
fn non_null_style_fix(source, n) = {
|
|
433
|
-
value body: assertion_fix_body(source, n)
|
|
434
|
-
value wrapped: body.node is (structure_binary, structure_assertion, ts_sequence, ts_assignment,
|
|
435
|
-
ts_conditional, ts_arrow, ts_yield, ts_unary, ts_await, ts_update)
|
|
436
|
-
value replacement: expression_fix_wrap(body.text, wrapped) "!"
|
|
437
|
-
value if !expression_fix_retains_comments(n, replacement) then null else assertion_fix_output(n, replacement)
|
|
438
|
-
}
|
|
439
|
-
in ts_as, ts_type_assertion rule es-non-nullable-type-assertion-style {
|
|
440
|
-
when type.text != "const" && assertion_non_null_style(node) == true
|
|
441
|
-
warn "Use a ! assertion to more succinctly remove null and undefined from the type."
|
|
442
|
-
let edit non_null_style_fix(source, node)
|
|
443
|
-
fix at edit.target if edit != null as edit.text
|
|
444
|
-
}
|
|
445
|
-
in ts_method.return_type, ts_method_signature.return_type rule es-related-getter-setter-pairs {
|
|
446
|
-
when typed_accessor_type_mismatch(node)
|
|
447
|
-
warn "`get()` type should be assignable to its equivalent `set()` type."
|
|
448
|
-
}
|
|
449
|
-
# `await` of something that is not a promise; promise aggregators of arrays
|
|
450
|
-
# of non-promises.
|
|
451
|
-
in ts_await rule es-await-thenable {
|
|
452
|
-
when ts_value_nonthenable(argument.node) == true
|
|
453
|
-
warn "Unexpected `await` of a non-Promise (non-\"Thenable\") value."
|
|
454
|
-
}
|
|
455
|
-
in ts_member, ts_index let promise_aggregate_member contains(["all", "allSettled", "race", "any"], static_name) && object.global_value == "Promise"
|
|
456
|
-
in ts_call let is_promise_aggregator callee.promise_aggregate_member == true
|
|
457
|
-
in ts_array let is_promise_aggregator_argument parent.is_promise_aggregator == true
|
|
458
|
-
in ts_array.element rule es-await-thenable:aggregate {
|
|
459
|
-
when not ts_hole && parent.is_promise_aggregator_argument
|
|
460
|
-
&& (if is ts_spread then ts_await_nonawaitable_iterable(argument.node) else ts_value_nonthenable(node) == true)
|
|
461
|
-
warn "Unexpected iterable of non-Promise (non-\"Thenable\") values passed to promise aggregator."
|
|
462
|
-
}
|
|
463
|
-
in ts_call.argument rule es-await-thenable:iterable {
|
|
464
|
-
when parent.is_promise_aggregator == true && not ts_array && ts_await_nonawaitable_iterable(node)
|
|
465
|
-
warn "Unexpected iterable of non-Promise (non-\"Thenable\") values passed to promise aggregator."
|
|
466
|
-
}
|
|
467
|
-
in ts_for_of rule es-await-thenable:for-of {
|
|
468
|
-
report at { start: offset, end: last(overlapping(ts_close_paren, right.end_offset, body.offset)).end_offset }
|
|
469
|
-
when contains([] modifier, "await")
|
|
470
|
-
&& value_has_member(right.node, {global: "Symbol", class: "value", path: ["asyncIterator"]}, null) == false
|
|
471
|
-
warn "Unexpected `for await...of` of a value that is not async iterable."
|
|
472
|
-
}
|
|
473
|
-
in ts_declarator.init rule es-await-thenable:using {
|
|
474
|
-
when starts_with(parent.parent.kind.text, "await")
|
|
475
|
-
&& value_has_member(node, {global: "Symbol", class: "value", path: ["asyncDispose"]}, null) == false
|
|
476
|
-
warn "Unexpected `await using` of a value that is not async disposable."
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
# ---- template interpolation ----------------------------------------------------
|
|
480
|
-
in ts_template let tagged_template parent is ts_tagged_template
|
|
481
|
-
in ts_node let restrict_template_expressions_type_checks template_type_checks
|
|
482
|
-
in ts_template.expression rule es-restrict-template-expressions {
|
|
483
|
-
option allowAny boolean = true "Allow any values"
|
|
484
|
-
option allowArray boolean = false "Allow arrays whose elements can be interpolated"
|
|
485
|
-
option allowBoolean boolean = true "Allow booleans"
|
|
486
|
-
option allowNullish boolean = true "Allow null and undefined"
|
|
487
|
-
option allowNumber boolean = true "Allow numbers and bigints"
|
|
488
|
-
option allowRegExp boolean = true "Allow regular expressions"
|
|
489
|
-
option allowNever boolean = false "Allow never values"
|
|
490
|
-
option allow string[] | null = null "Allowed types and values; null keeps standard library Error, URL and URLSearchParams"
|
|
491
|
-
let enabled_template_options ["string"] (if allowAny then ["any"] else []) (if allowArray then ["array"] else []) (if allowBoolean then ["boolean"] else []) (if allowNullish then ["nullish"] else []) (if allowNumber then ["number"] else []) (if allowRegExp then ["regexp"] else []) (if allowNever then ["never"] else [])
|
|
492
|
-
when parent.tagged_template != true
|
|
493
|
-
when template_type_checks != null
|
|
494
|
-
when any(restrict_template_expressions_type_checks, clause => !any(clause, gate => contains(enabled_template_options, gate.option) || gate.name != null && (if allow == null then gate.from == "lib" && contains(["Error", "URL", "URLSearchParams"], gate.name) else contains(allow, gate.name))))
|
|
495
|
-
let template_display_parts value_constituents(node)
|
|
496
|
-
when template_display_parts != null
|
|
497
|
-
warn "Invalid type \"{ts_value_print_type(node)}\" of template literal expression."
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
fn typed_unary_type(n) = if (n is ts_template) && count(n.expression.nodes) == 0
|
|
501
|
-
then ts_value_string(slice(first(n.quasi.nodes).text, 1, -1)) else ts_value_print_type(n)
|
|
502
|
-
|
|
503
|
-
in ts_unary rule es-no-unsafe-unary-minus {
|
|
504
|
-
when operator == "-" && !ts_value_opaque(argument.node)
|
|
505
|
-
&& !any(["any", "number", "bigint", "enum"], trait => ts_value_has_trait(argument.node, trait) == true)
|
|
506
|
-
let operand_type value_constituents(argument.node)
|
|
507
|
-
when operand_type != null && count(operand_type) > 0
|
|
508
|
-
warn "Argument of unary negation should be assignable to number | bigint but is {typed_unary_type(argument.node)} instead."
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
in ts_member, ts_index let object_array_type ts_value_array_like(object.node)
|
|
512
|
-
in ts_parenthesized let object_array_type expression.object_array_type
|
|
513
|
-
in ts_member, ts_index let array_literal_object object is ts_array
|
|
514
|
-
in ts_parenthesized let array_literal_object expression.array_literal_object
|
|
515
|
-
in ts_unary rule es-no-array-delete {
|
|
516
|
-
when operator == "delete" && (argument.array_literal_object == true || argument.object_array_type == true)
|
|
517
|
-
warn "Using the `delete` operator with an array expression is unsafe."
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
in ts_assignment_pattern, ts_parameter rule es-no-useless-default-assignment {
|
|
521
|
-
when typed_default_assignment != null
|
|
522
|
-
report at { start: typed_default_assignment.offset, end: typed_default_assignment.end }
|
|
523
|
-
warn when {
|
|
524
|
-
typed_default_assignment.kind == "optional" then "Using `= undefined` to make a parameter optional adds unnecessary runtime logic."
|
|
525
|
-
typed_default_assignment.kind == "undefined" then "Default value is useless because it is undefined. Optional {typed_default_assignment.owner}s are already undefined by default."
|
|
526
|
-
else then "Default value is useless because the {typed_default_assignment.owner} is not optional."
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
in ts_program rule es-no-useless-default-assignment:strict {
|
|
530
|
-
option allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing boolean = false "Run without strict null checks"
|
|
531
|
-
when ts_compiler_option(node, "strictNullChecks") == false && !allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing
|
|
532
|
-
report at { start: 0, end: 0 }
|
|
533
|
-
warn "This rule requires the `strictNullChecks` compiler option to be turned on to function correctly."
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
in ts_as, ts_type_assertion rule es-no-unsafe-type-assertion {
|
|
537
|
-
when type.text != "const"
|
|
538
|
-
let operand value_constituents(expression.node)
|
|
539
|
-
let annotation value_constituents(type.node)
|
|
540
|
-
when operand != null && annotation != null
|
|
541
|
-
let from_any !assignment_empty_map(expression.node) && (assignment_unsafe(operand, annotation, node) || annotation == [] && ts_value_category(operand) != null)
|
|
542
|
-
let to_any (operand == [] || ts_value_trait(operand, "unknown")) && ts_value_category(annotation) != null || assignment_unsafe(annotation, operand, node)
|
|
543
|
-
let narrowing if from_any || to_any then null else assertion_unsafe_narrowing(node)
|
|
544
|
-
let label if from_any then if ts_value_category(operand) == "error" then "error typed" else "`any`" else if to_any then if ts_value_category(annotation) == "error" then "error typed" else "`any`" else narrowing.label
|
|
545
|
-
when from_any || to_any || narrowing != null && !re_test(label, /\bunknown\b/)
|
|
546
|
-
warn if from_any then "Unsafe assertion from {label} detected: consider using type guards or a safer assertion." else if to_any then "Unsafe assertion to {label} detected: consider using a more specific type to ensure safety." else if narrowing.kind == "narrow" then "Unsafe type assertion: type '{label}' is more narrow than the original type." else if narrowing.kind == "unconstrained" then "Unsafe type assertion: '{label}' could be instantiated with an arbitrary type which could be unrelated to the original type." else "Unsafe type assertion: the original type is assignable to the constraint of type '{label}', but '{label}' could be instantiated with a different subtype of its constraint."
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
in ts_member let object_string_array_type ts_value_string_array(object.node)
|
|
550
|
-
in ts_call rule es-require-array-sort-compare {
|
|
551
|
-
option ignoreStringArrays boolean = true "Ignore arrays whose elements are all strings"
|
|
552
|
-
when count(argument) == 0 && re_test(callee, /\??\.\s*(sort|toSorted)$/)
|
|
553
|
-
when callee.object_array_type == true && (!ignoreStringArrays || callee.object_string_array_type != true)
|
|
554
|
-
warn "Require 'compare' argument."
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
in ts_unary rule es-no-meaningless-void-operator {
|
|
559
|
-
option checkNever boolean = false "Also check calls returning never"
|
|
560
|
-
when operator == "void" && argument != "0"
|
|
561
|
-
let underlying {
|
|
562
|
-
value argument.node
|
|
563
|
-
walk (current) if current is (ts_parenthesized, ts_as, ts_satisfies, ts_non_null, ts_type_assertion)
|
|
564
|
-
then current.expression.node
|
|
565
|
-
else if current is ts_sequence then last(current.expression.nodes) else null
|
|
566
|
-
last
|
|
567
|
-
}
|
|
568
|
-
when underlying not ts_assignment
|
|
569
|
-
let call underlying is (ts_call, ts_new)
|
|
570
|
-
when if call then ts_value_trait(value_constituent_traits(argument.node, ["undefined"]), "undefined")
|
|
571
|
-
|| checkNever && value_parts(argument.node) == [] else value_has_member(argument.node, "then", null) != true
|
|
572
|
-
let operand_type if call then value_constituents(argument.node) else []
|
|
573
|
-
when !call || operand_type != null
|
|
574
|
-
warn if call then "void operator shouldn't be used on {ts_value_print(operand_type)}; it should convey that a return value is being ignored" else "void operator is useless here; it should only discard a call's return value"
|
|
575
|
-
let operand argument.node
|
|
576
|
-
let parens expression_fix_parens(source, node, operand)
|
|
577
|
-
let replacement expression_fix_wrap(operand.text, parens > 0)
|
|
578
|
-
let statement expression_fix_statement(node)
|
|
579
|
-
let semicolon if statement != null && expression_fix_continuation(replacement)
|
|
580
|
-
then expression_fix_asi(statement, token_neighbours.before, expression_fix_before_nodes(node), replacement) else false
|
|
581
|
-
fix if parens != null && semicolon != null && expression_fix_clean({ offset: offset, end_offset: operand.offset }) as
|
|
582
|
-
(if semicolon then ";" else "") expression_fix_edges(node, replacement)
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
}
|
|
1
|
+
# Type-aware rules over TypeScript syntax and value queries.
|
|
2
|
+
|
|
3
|
+
import .ts/arithmetic
|
|
4
|
+
import .ts/deprecation
|
|
5
|
+
import .ts/import-no-deprecated
|
|
6
|
+
import .ts/methods
|
|
7
|
+
import .ts/spreads
|
|
8
|
+
import .ts/type-specifiers
|
|
9
|
+
import .ts/typed/promises
|
|
10
|
+
import .ts/typed/errors
|
|
11
|
+
import .ts/typed/awaitability
|
|
12
|
+
import .ts/typed/optional-chains
|
|
13
|
+
import .ts/typed/floating-promises
|
|
14
|
+
import .ts/typed/unbound-method
|
|
15
|
+
import .ts/typed/template-expressions
|
|
16
|
+
import .ts/typed/readonly-members
|
|
17
|
+
import .ts/typed/readonly-parameters
|
|
18
|
+
import .ts/typed/string-boundaries
|
|
19
|
+
import .ts/typed/includes
|
|
20
|
+
import .ts/typed/type-constituents
|
|
21
|
+
import .ts/typed/redundant-constituents
|
|
22
|
+
import .ts/typed/return-await
|
|
23
|
+
import .ts/typed/return-this
|
|
24
|
+
import .ts/typed/type-arguments
|
|
25
|
+
import .ts/typed/nullish-coalescing
|
|
26
|
+
import .ts/typed/boolean-comparisons
|
|
27
|
+
import .ts/typed/void-expressions
|
|
28
|
+
import .ts/typed/strict-void-return
|
|
29
|
+
import .ts/typed/assignments
|
|
30
|
+
import .ts/typed/transfers
|
|
31
|
+
import .ts/typed/defaults
|
|
32
|
+
import .ts/typed/template-values
|
|
33
|
+
import .ts/typed/enum-consistency
|
|
34
|
+
import .ts/typed/type-parameters
|
|
35
|
+
import .ts/typed/switches
|
|
36
|
+
import .ts/typed/enums
|
|
37
|
+
import .ts/typed/qualifiers
|
|
38
|
+
import .ts/typed/regexp-exec
|
|
39
|
+
import .ts/typed/for-in-arrays
|
|
40
|
+
import .ts/typed/type-conversions
|
|
41
|
+
import .ts/typed/rejection-handlers
|
|
42
|
+
import .ts/typed/unsafe-members
|
|
43
|
+
import .ts/typed/unsafe-calls
|
|
44
|
+
import .ts/values/inspection
|
|
45
|
+
import .ts/constants
|
|
46
|
+
|
|
47
|
+
define {
|
|
48
|
+
|
|
49
|
+
fn generated_empty_type(n) = {
|
|
50
|
+
value arms: value_parts(n)
|
|
51
|
+
value arms != null && any(arms, arm =>
|
|
52
|
+
count(arm.parts) > 0 && all(arm.parts, part =>
|
|
53
|
+
contains(part.traits, "structural") && count(part.uncertain_traits) == 0
|
|
54
|
+
&& part.carrier_known && part.carrier == null)
|
|
55
|
+
&& value_children(arm.handle, true, []) == []
|
|
56
|
+
&& value_signatures(arm.handle) == [[]] && value_constructors(arm.handle) == [[]])
|
|
57
|
+
}
|
|
58
|
+
in ts_type_reference, ts_intersection rule es-no-generated-empty-object-type {
|
|
59
|
+
when node is ts_intersection || count(type_argument.nodes) > 0
|
|
60
|
+
&& last(walk(node, item => if item.parent is ts_parenthesized_type then item.parent.node else null)).parent not ts_intersection
|
|
61
|
+
when generated_empty_type(node)
|
|
62
|
+
warn "This type resolves to `{{}}`, the empty object type. This was likely not intentional."
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
in ts_function, ts_arrow, ts_method rule es-require-await {
|
|
66
|
+
when missing_await != null
|
|
67
|
+
report at ts_await_warning_site(node)
|
|
68
|
+
warn missing_await.name " has no 'await' expression."
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
group promise_function_async_function ts_function, ts_arrow, ts_method
|
|
72
|
+
in promise_function_async_function let promise_function_async_kind
|
|
73
|
+
if is ts_arrow then "arrow"
|
|
74
|
+
else if is ts_method then if parent is ts_class then "method" else "expression"
|
|
75
|
+
else if parent is (ts_program, ts_block, ts_export, ts_case, ts_if, ts_labeled) then "declaration"
|
|
76
|
+
else "expression"
|
|
77
|
+
in promise_function_async_function let promise_function_async_checked true
|
|
78
|
+
in ts_method let promise_function_async_exempt
|
|
79
|
+
contains(["get", "set"], kind)
|
|
80
|
+
|| parent is ts_class && !contains([] modifier, "static") && key not ts_computed_key && static_name == "constructor"
|
|
81
|
+
fn promise_function_async_edit(n) = {
|
|
82
|
+
value anchor: if n is ts_method then find(n.modifier.nodes, modifier => modifier.text == "*") ?? n.key.node else n
|
|
83
|
+
value before: anchor.token_neighbours.before
|
|
84
|
+
value separator: if before.end == anchor.offset then expression_fix_separator(before.text, "async") else ""
|
|
85
|
+
value { start: anchor.offset, text: separator "async " }
|
|
86
|
+
}
|
|
87
|
+
in promise_function_async_function rule es-promise-function-async {
|
|
88
|
+
option allowAny boolean = true "Allow any and unknown return types"
|
|
89
|
+
option allowedPromiseNames string[] = [] "Additional promise type names"
|
|
90
|
+
when body != null && !contains([] modifier, "async")
|
|
91
|
+
when !(promise_function_async_exempt.nonempty ?? false)
|
|
92
|
+
when promise_function_async_checked
|
|
93
|
+
let names ("Promise" allowedPromiseNames)
|
|
94
|
+
let policy promise_async_policy(node, names)
|
|
95
|
+
when policy.known
|
|
96
|
+
let disallowed_unknown !allowAny && policy.dynamic
|
|
97
|
+
when disallowed_unknown || policy.required
|
|
98
|
+
report at function_head_site(node)
|
|
99
|
+
warn if !disallowed_unknown && policy.mixed then "Functions that return promises must be async. Consider adding an explicit return type annotation if the function is intended to return a union of promise and non-promise types." else "Functions that return promises must be async."
|
|
100
|
+
let edit promise_function_async_edit(node)
|
|
101
|
+
fix at { start: edit.start, end: edit.start } if !disallowed_unknown as edit.text
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
in ts_node let only_throw_error_checked true
|
|
105
|
+
in ts_throw.argument rule es-only-throw-error {
|
|
106
|
+
option allow (string | type_specifier_file | type_specifier_lib | type_specifier_package)[] = [] "Additional allowed thrown types"
|
|
107
|
+
option allowRethrowing boolean = true "Allow rethrowing catch and Promise rejection parameters"
|
|
108
|
+
option allowThrowingAny boolean = true "Allow any values"
|
|
109
|
+
option allowThrowingUnknown boolean = true "Allow unknown values"
|
|
110
|
+
when ts_error_value.kind != null && ts_error_value.kind != "error"
|
|
111
|
+
when !allowRethrowing || !ts_error_value.rethrown
|
|
112
|
+
when !allowThrowingAny || ts_error_value.kind != "any"
|
|
113
|
+
when !allowThrowingUnknown || ts_error_value.kind != "unknown"
|
|
114
|
+
when only_throw_error_checked
|
|
115
|
+
when type_value_allowed(allow, node) != true
|
|
116
|
+
report at unparenthesised_node(node)
|
|
117
|
+
warn if ts_error_value.kind == "undefined" then "Do not throw undefined." else "Expected an error object to be thrown."
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
in ts_identifier let rejection_global_promise text == "Promise" && resolved == "global" && global != "off"
|
|
121
|
+
in ts_identifier, ts_member, ts_index let rejection_constructor
|
|
122
|
+
if re_test(file, /\.[cm]?tsx?$/) then ts_value_builtin_constructor(node, ["Promise"]) == true else rejection_global_promise.nonempty ?? false
|
|
123
|
+
in ts_identifier, ts_member, ts_index let rejection_receiver
|
|
124
|
+
rejection_constructor || re_test(file, /\.[cm]?tsx?$/) && ts_value_builtin_type(node, ["Promise"]) == true
|
|
125
|
+
in ts_member, ts_index let rejection_member static_name == "reject" && object.rejection_receiver == true
|
|
126
|
+
in ts_parenthesized forward rejection_global_promise, rejection_constructor, rejection_receiver, rejection_member from expression
|
|
127
|
+
in ts_new let rejection_promise_constructor callee.rejection_constructor == true
|
|
128
|
+
in ts_function, ts_arrow, ts_parenthesized let rejection_executor
|
|
129
|
+
if parent is ts_parenthesized then parent.rejection_executor
|
|
130
|
+
else is ts_new.argument && index == 0 && parent.rejection_promise_constructor
|
|
131
|
+
in ts_parameter let rejection_parameter index == 1 && init == null && parent.rejection_executor == true
|
|
132
|
+
in ts_identifier let rejection_binding is ts_parameter.name && parent.rejection_parameter == true
|
|
133
|
+
in ts_call let rejection_call
|
|
134
|
+
callee.rejection_member == true || any(callee.declarations, d => d.rejection_binding == true)
|
|
135
|
+
fn rejection_reason_subject(n) = if n is ts_await then ts_awaited_value(n, n.argument.node) ?? n else n
|
|
136
|
+
in ts_node let rejection_reason
|
|
137
|
+
{possible: could_be_error && error_undefined_global != true, kind: error_kind_at(node, rejection_reason_subject(node)),
|
|
138
|
+
subject: rejection_reason_subject(node)}
|
|
139
|
+
fn rejection_reason_known(n) = value_parts(n) != null || unparenthesised_node(n) is ts_array
|
|
140
|
+
|| unparenthesised_node(n) is ts_assignment && contains(["-=", "*=", "/=", "%=", "**=", "<<=", ">>=", ">>>=", "|=", "&=", "^="],
|
|
141
|
+
unparenthesised_node(n).operator)
|
|
142
|
+
in ts_call let prefer_promise_reject_errors_checked true
|
|
143
|
+
in ts_call rule es-prefer-promise-reject-errors {
|
|
144
|
+
option allowEmptyReject boolean = false "Allow rejection without a reason"
|
|
145
|
+
option allow (string | type_specifier_file | type_specifier_lib | type_specifier_package)[] = [] "Additional allowed rejection types"
|
|
146
|
+
option allowThrowingAny boolean = false "Allow any values"
|
|
147
|
+
option allowThrowingUnknown boolean = false "Allow unknown values"
|
|
148
|
+
when rejection_call
|
|
149
|
+
let reason first([] argument.rejection_reason)
|
|
150
|
+
when reason != null || !allowEmptyReject
|
|
151
|
+
when reason == null || (if re_test(file, /\.[cm]?tsx?$/) then rejection_reason_known(reason.subject) && reason.kind != "error" && !(allowThrowingAny && reason.kind == "any") && !(allowThrowingUnknown && reason.kind == "unknown") && type_value_allowed(allow, reason.subject) == false else !reason.possible)
|
|
152
|
+
when prefer_promise_reject_errors_checked
|
|
153
|
+
warn "Expected the Promise rejection reason to be an Error."
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
fn stringification_union(states) = if count(states) == 0 then 0 else if any(states, state => state == null) then null
|
|
157
|
+
else if all(states, state => state == 2) then 2 else if all(states, state => state == 0) then 0 else 1
|
|
158
|
+
# Elements are all stringified, so one certain element is enough; a union's arms are exclusive.
|
|
159
|
+
fn stringification_elements(states) = if any(states, state => state == 2) then 2
|
|
160
|
+
else if any(states, state => state == null) then null
|
|
161
|
+
else if any(states, state => state == 1) then 1 else 0
|
|
162
|
+
fn stringification_member(n, handle, key) = {
|
|
163
|
+
value roots: global_declarations(n, "Object", "type")
|
|
164
|
+
value defaults: map(roots ?? [], root => value_lookup(declaration_value(n, root), key))
|
|
165
|
+
value lookup: value_lookup(handle, key)
|
|
166
|
+
value known: roots != null && count(roots) > 0
|
|
167
|
+
&& all(defaults, item => item.alternatives != null)
|
|
168
|
+
&& lookup.alternatives != null
|
|
169
|
+
value canonical: flat_map(defaults, item => flat_map(item.alternatives, arm => map(arm.sources, source => source.site)))
|
|
170
|
+
value sources: filter(flat_map(lookup.alternatives ?? [], arm => arm.sources), source => source.kind != "indexer")
|
|
171
|
+
value if !known || any(sources, source => source.site == null) then null
|
|
172
|
+
else if any(sources, source => !contains(canonical, source.site)) then 0
|
|
173
|
+
else 2
|
|
174
|
+
}
|
|
175
|
+
fn stringification_ignored_base(n, carrier, ignored) = if carrier == null then false else {
|
|
176
|
+
value sites: walk([carrier], site => {
|
|
177
|
+
value constructed: if site.kind != "ts_declarator" then [] else flat_map(value_constituents(value_lookup(declaration_value(n, site), "prototype").member) ?? [], arm => map(arm, part => part.carrier))
|
|
178
|
+
value (site_field(n, site, "extends") ?? []) (site_sources(n, site).sites ?? []) filter(constructed, item => item != null)
|
|
179
|
+
})
|
|
180
|
+
value any(sites, site => contains(["ts_class", "ts_interface"], site.kind) && contains(ignored, site.name)
|
|
181
|
+
|| site.description.kind == "named" && contains(ignored, site.description.name))
|
|
182
|
+
}
|
|
183
|
+
fn stringification_ignored_alias(n, ignored) = {
|
|
184
|
+
value arms: value_parts(n)
|
|
185
|
+
value roots: value_sources(n)
|
|
186
|
+
value sites: walk(if count(roots.sites) == 1 then roots.sites else [], site =>
|
|
187
|
+
if count(site_sources(n, site).sites) == 1 then site_sources(n, site).sites else [])
|
|
188
|
+
value arms != null && all(arms, arm => all(arm.parts, part => part.carrier_known && part.carrier == null))
|
|
189
|
+
&& any(sites, site => site.kind == "ts_type_alias" && contains(ignored, site.name))
|
|
190
|
+
}
|
|
191
|
+
fn stringification_part(n, part, ignored, check_unknown, seen) = {
|
|
192
|
+
value primitive: any(["boolean", "string", "number", "bigint", "symbol", "null", "undefined", "any", "callable"],
|
|
193
|
+
trait => contains(part.traits, trait))
|
|
194
|
+
value carrier: part.carrier
|
|
195
|
+
value ignored_name: carrier != null && contains(ignored, carrier.description.name)
|
|
196
|
+
|| stringification_ignored_base(n, carrier, ignored)
|
|
197
|
+
value if primitive || ignored_name then 0
|
|
198
|
+
else if contains(part.traits, "unknown") || contains(part.traits, "parameter") then if check_unknown then 1 else 0
|
|
199
|
+
else if contains(["array", "tuple"], carrier.description.kind) then
|
|
200
|
+
stringification_elements(map(carrier.arguments, argument => stringification_certainty(n, argument, ignored, check_unknown, seen)))
|
|
201
|
+
else if !part.carrier_known then null else {
|
|
202
|
+
value symbol: value_lookup(part.handle, ts_value_symbol_key(n, "toPrimitive"))
|
|
203
|
+
value custom_symbol: symbol.alternatives != null
|
|
204
|
+
&& any(symbol.alternatives, arm => arm.matched && any(arm.sources, source => source.kind != "indexer"))
|
|
205
|
+
value states: map(["toString", "toLocaleString", "valueOf"], key => stringification_member(n, part.handle, key))
|
|
206
|
+
value if custom_symbol || any(states, state => state == 0) then 0
|
|
207
|
+
else if symbol.alternatives == null || any(states, state => state == null) then null
|
|
208
|
+
else if any(states, state => state == 2) then 2 else null
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
fn stringification_parts(arm) = {
|
|
212
|
+
value concrete: filter(arm.parts, part => part.traits != ["intersection"])
|
|
213
|
+
value if count(concrete) > 0 then concrete else arm.parts
|
|
214
|
+
}
|
|
215
|
+
fn stringification_certainty(n, handle, ignored, check_unknown, seen) = {
|
|
216
|
+
value arms: if contains(seen, handle) then null else value_parts(handle)
|
|
217
|
+
value if arms == null then null else if count(arms) == 0 then 0 else stringification_union(map(arms, arm => {
|
|
218
|
+
value concrete: filter(stringification_parts(arm), part => part.traits != ["parameter"])
|
|
219
|
+
value parts: if count(concrete) > 0 then concrete else arm.parts
|
|
220
|
+
value states: map(parts, part => stringification_part(n, part, ignored, check_unknown, seen [handle]))
|
|
221
|
+
value if any(states, state => state == 0) then 0
|
|
222
|
+
else if any(states, state => state == null) then null
|
|
223
|
+
else if all(states, state => state == 1) then 1 else 2
|
|
224
|
+
}))
|
|
225
|
+
}
|
|
226
|
+
fn stringification_join(n, ignored, check_unknown) = {
|
|
227
|
+
value arms: value_parts(n)
|
|
228
|
+
value if arms == null then null else stringification_union(map(arms, arm => {
|
|
229
|
+
value states: map(stringification_parts(arm), part => if contains(["array", "tuple"], part.carrier.description.kind)
|
|
230
|
+
then stringification_elements(map(part.carrier.arguments, argument => stringification_certainty(n, argument, ignored, check_unknown, [])))
|
|
231
|
+
else 0)
|
|
232
|
+
value if any(states, state => state == 0) then 0
|
|
233
|
+
else if any(states, state => state == null) then null else max(states)
|
|
234
|
+
}))
|
|
235
|
+
}
|
|
236
|
+
fn stringification_literal_array(n, ignored, check_unknown) = {
|
|
237
|
+
value elements: n.element.nodes
|
|
238
|
+
value spread: any(elements, element => element is ts_spread)
|
|
239
|
+
value if spread then null
|
|
240
|
+
else stringification_union(map(elements, element => stringification_certainty(n, element, ignored, check_unknown, [])))
|
|
241
|
+
}
|
|
242
|
+
in ts_call let stringification_subjects
|
|
243
|
+
if callee is ts_identifier && global_callee == "String" then [{node: { value argument.nodes; first; unparenthesised_node }, join: false}]
|
|
244
|
+
else if callee is ts_member && callee.property is ts_identifier && contains(["toString", "toLocaleString", "join"], callee.property.text) then [{node: unparenthesised_node(callee.object.node), join: callee.property.text == "join"}]
|
|
245
|
+
else []
|
|
246
|
+
in ts_binary, ts_assignment let stringification_subjects
|
|
247
|
+
if contains(["+", "+="], operator) && ts_value_string_like(left.node) == true then [{node: unparenthesised_node(right.node), join: false}]
|
|
248
|
+
else if contains(["+", "+="], operator) && ts_value_string_like(right.node) == true && left not ts_private_name then [{node: unparenthesised_node(left.node), join: false}]
|
|
249
|
+
else []
|
|
250
|
+
in ts_template let stringification_subjects if parent is ts_tagged_template then [] else map(expression.nodes, n => {node: unparenthesised_node(n), join: false})
|
|
251
|
+
in ts_call, ts_binary, ts_assignment, ts_template rule es-no-base-to-string {
|
|
252
|
+
option ignoredTypeNames string[] = ["Error", "RegExp", "URL", "URLSearchParams"] "Types whose string conversion is permitted"
|
|
253
|
+
option checkUnknown boolean = false "Check explicit unknown and unconstrained type parameters"
|
|
254
|
+
let findings map(stringification_subjects, subject => {
|
|
255
|
+
node: subject.node,
|
|
256
|
+
join: subject.join,
|
|
257
|
+
certainty: if stringification_ignored_alias(subject.node, ignoredTypeNames) then 0 else if subject.join then stringification_join(subject.node, ignoredTypeNames, checkUnknown) else if subject.node is ts_array && value_parts(subject.node) == null then stringification_literal_array(subject.node, ignoredTypeNames, checkUnknown) else stringification_certainty(subject.node, subject.node, ignoredTypeNames, checkUnknown, [])
|
|
258
|
+
})
|
|
259
|
+
each subject in findings {
|
|
260
|
+
when subject.node != null && subject.node not (ts_literal, ts_spread)
|
|
261
|
+
when subject.certainty != null && subject.certainty > 0
|
|
262
|
+
report at {start: subject.node.offset, end: subject.node.end_offset}
|
|
263
|
+
warn if subject.join then "Using `join()` for " subject.node.text " " (if subject.certainty == 2 then "will" else "may") " use Object's default stringification format ('[object Object]') when stringified."
|
|
264
|
+
else "'" subject.node.text "' " (if subject.certainty == 2 then "will" else "may") " use Object's default stringification format ('[object Object]') when stringified."
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
in ts_identifier {
|
|
269
|
+
let eval_name text
|
|
270
|
+
let eval_identifier true
|
|
271
|
+
let eval_core_root if contains(["global", "globalThis", "window", "self"], text) && global != null && global != "off" then text else null
|
|
272
|
+
let eval_timer_global resolved == "global" && (re_test(file, /\.[cm]?tsx?$/) || global != null && global != "off")
|
|
273
|
+
let eval_constructor ts_value_builtin_type(node, ["FunctionConstructor"]) == true || ts_value_has_trait(node, "any") == true || ts_value_opaque(node) && reference_kind == null
|
|
274
|
+
}
|
|
275
|
+
in ts_member, ts_index {
|
|
276
|
+
let eval_name static_name
|
|
277
|
+
let eval_core_root if object.eval_core_root != null && static_name == object.eval_core_root then object.eval_core_root else null
|
|
278
|
+
let eval_timer_global if re_test(file, /\.[cm]?tsx?$/) then object is ts_identifier && contains(["global", "globalThis", "window"], object) else object.eval_core_root != null
|
|
279
|
+
let eval_constructor ts_value_builtin_type(node, ["FunctionConstructor"]) == true || ts_value_has_trait(node, "any") == true || ts_value_opaque(node)
|
|
280
|
+
}
|
|
281
|
+
in ts_member let eval_bind_member property == "bind"
|
|
282
|
+
in ts_parenthesized {
|
|
283
|
+
let eval_name expression.eval_name
|
|
284
|
+
let eval_identifier expression.eval_identifier
|
|
285
|
+
let eval_core_root expression.eval_core_root
|
|
286
|
+
let eval_timer_global expression.eval_timer_global
|
|
287
|
+
let eval_constructor expression.eval_constructor
|
|
288
|
+
}
|
|
289
|
+
group typed_simple_expression ts_identifier, ts_private_name, ts_this, ts_super, ts_null, ts_boolean,
|
|
290
|
+
ts_number, ts_bigint, ts_string, ts_regex, ts_template, ts_tagged_template, ts_array, ts_object,
|
|
291
|
+
ts_member, ts_index, ts_new, ts_unary, ts_update, ts_await, ts_yield,
|
|
292
|
+
ts_logical, ts_assignment, ts_conditional, ts_sequence, ts_as, ts_satisfies,
|
|
293
|
+
ts_non_null, ts_type_assertion, ts_function, ts_arrow, ts_class, ts_instantiation, ts_meta_property,
|
|
294
|
+
ts_import_call
|
|
295
|
+
in typed_simple_expression, ts_binary {
|
|
296
|
+
let eval_callback ts_value_callable(node) || ts_value_builtin_type(node, ["Function"]) == true
|
|
297
|
+
}
|
|
298
|
+
fn eval_constant_string(n) = ts_runtime_string(n) != null && {
|
|
299
|
+
value arms: value_trait_parts(n, ["string", "enum"])
|
|
300
|
+
value part: if count(arms) == 1 && count(first(arms).parts) == 1 then first(first(arms).parts) else null
|
|
301
|
+
value part != null && contains(part.traits, "string") && !contains(part.traits, "enum")
|
|
302
|
+
}
|
|
303
|
+
in typed_simple_expression {
|
|
304
|
+
let eval_string is (ts_string, ts_template)
|
|
305
|
+
|| eval_constant_string(node)
|
|
306
|
+
}
|
|
307
|
+
in ts_call {
|
|
308
|
+
let eval_callback ts_value_callable(node) || ts_value_builtin_type(node, ["Function"]) == true || callee.eval_bind_member == true || callee is ts_identifier && callee == "bind"
|
|
309
|
+
let eval_string eval_constant_string(node) || callee.global_value == "String" && ts_runtime_string(first(argument.nodes)) != null
|
|
310
|
+
}
|
|
311
|
+
in ts_binary let eval_string eval_constant_string(node) || operator == "+" && (left.eval_string == true || right.eval_string == true)
|
|
312
|
+
in ts_parenthesized {
|
|
313
|
+
let eval_callback expression.eval_callback
|
|
314
|
+
let eval_string expression.eval_string
|
|
315
|
+
}
|
|
316
|
+
in ts_call, ts_new let eval_timer (contains(["setTimeout", "setInterval", "execScript"], callee.eval_name) || callee.eval_name == "setImmediate" && re_test(file, /\.[cm]?tsx?$/)) && callee.eval_timer_global == true
|
|
317
|
+
in ts_call.argument, ts_new.argument rule es-no-implied-eval {
|
|
318
|
+
when index == 0 && parent.eval_timer == true && re_test(file, /\.[cm]?tsx?$/) && eval_callback != true
|
|
319
|
+
warn "Implied eval. Consider passing a function."
|
|
320
|
+
}
|
|
321
|
+
in ts_call {
|
|
322
|
+
rule es-no-implied-eval:core {
|
|
323
|
+
when eval_timer && callee.eval_name != "execScript" && !re_test(file, /\.[cm]?tsx?$/) && first([] argument.eval_string) == true
|
|
324
|
+
warn "Implied eval. Consider passing a function instead of a string."
|
|
325
|
+
}
|
|
326
|
+
rule es-no-implied-eval:exec {
|
|
327
|
+
when eval_timer && callee.eval_name == "execScript" && !re_test(file, /\.[cm]?tsx?$/) && first([] argument.eval_string) == true
|
|
328
|
+
warn "Implied eval. Do not use execScript()."
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
in ts_call, ts_new {
|
|
332
|
+
rule es-no-implied-eval:function {
|
|
333
|
+
when callee.eval_name == "Function" && re_test(file, /\.[cm]?tsx?$/) && callee.eval_constructor
|
|
334
|
+
when callee.eval_identifier == true || callee.eval_timer_global == true
|
|
335
|
+
warn "Implied eval. Do not use the Function constructor to create functions."
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
fn unsafe_argument_sender(transfer) = if transfer.error then if transfer.mode == "array" then "error" else "error typed" else if transfer.mode == "tuple" then "of type `{transfer.sender}`" else "`{transfer.sender}`"
|
|
340
|
+
fn unsafe_assignment_sender(transfer) = if transfer.error then "error typed" else "`{transfer.sender}`"
|
|
341
|
+
fn unsafe_this_note() = "\nYou can try to fix this by turning on the `noImplicitThis` compiler option, or adding a `this` parameter to the function."
|
|
342
|
+
|
|
343
|
+
in ts_call, ts_new, ts_tagged_template {
|
|
344
|
+
rule es-no-unsafe-argument {
|
|
345
|
+
each unsafe_arg in argument_value_transfers ?? [] {
|
|
346
|
+
when unsafe_arg.mode == "argument"
|
|
347
|
+
report at unsafe_arg.site
|
|
348
|
+
warn "Unsafe argument of type " unsafe_argument_sender(unsafe_arg) " assigned to a parameter of type `{unsafe_arg.receiver}`."
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
rule es-no-unsafe-argument:spread {
|
|
352
|
+
each unsafe_arg in argument_value_transfers ?? [] {
|
|
353
|
+
when unsafe_arg.mode == "spread"
|
|
354
|
+
report at unsafe_arg.site
|
|
355
|
+
warn "Unsafe spread of an " unsafe_argument_sender(unsafe_arg) " type."
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
rule es-no-unsafe-argument:array {
|
|
359
|
+
each unsafe_arg in argument_value_transfers ?? [] {
|
|
360
|
+
when unsafe_arg.mode == "array"
|
|
361
|
+
report at unsafe_arg.site
|
|
362
|
+
warn "Unsafe spread of an " unsafe_argument_sender(unsafe_arg) " array type."
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
rule es-no-unsafe-argument:tuple {
|
|
366
|
+
each unsafe_arg in argument_value_transfers ?? [] {
|
|
367
|
+
when unsafe_arg.mode == "tuple"
|
|
368
|
+
report at unsafe_arg.site
|
|
369
|
+
warn "Unsafe spread of a tuple type. The argument is " unsafe_argument_sender(unsafe_arg) " and is assigned to a parameter of type `{unsafe_arg.receiver}`."
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
in ts_declarator, ts_parameter, ts_class_property, ts_assignment, ts_assignment_pattern, ts_property, ts_spread, ts_jsx_attribute {
|
|
375
|
+
rule es-no-unsafe-assignment {
|
|
376
|
+
each unsafe_assigned in assignment_value_transfers {
|
|
377
|
+
when unsafe_assigned.mode == "any"
|
|
378
|
+
report at unsafe_assigned.site
|
|
379
|
+
warn when { unsafe_assigned.this_any then "Unsafe assignment of an `any` value. `this` is typed as `any`." unsafe_this_note()
|
|
380
|
+
else then "Unsafe assignment of an " unsafe_assignment_sender(unsafe_assigned) " value." }
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
rule es-no-unsafe-assignment:generic {
|
|
384
|
+
each unsafe_assigned in assignment_value_transfers {
|
|
385
|
+
when unsafe_assigned.mode == "generic"
|
|
386
|
+
report at unsafe_assigned.site
|
|
387
|
+
warn "Unsafe assignment of type " unsafe_assignment_sender(unsafe_assigned) " to a variable of type `{unsafe_assigned.receiver}`."
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
rule es-no-unsafe-assignment:array {
|
|
391
|
+
each unsafe_assigned in assignment_value_transfers {
|
|
392
|
+
when unsafe_assigned.mode == "array"
|
|
393
|
+
report at unsafe_assigned.site
|
|
394
|
+
warn "Unsafe array destructuring of an " unsafe_assignment_sender(unsafe_assigned) " array value."
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
rule es-no-unsafe-assignment:tuple {
|
|
398
|
+
each unsafe_assigned in assignment_value_transfers {
|
|
399
|
+
when unsafe_assigned.mode == "tuple"
|
|
400
|
+
report at unsafe_assigned.site
|
|
401
|
+
warn "Unsafe array destructuring of a tuple element with an " unsafe_assignment_sender(unsafe_assigned) " value."
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
rule es-no-unsafe-assignment:object {
|
|
405
|
+
each unsafe_assigned in assignment_value_transfers {
|
|
406
|
+
when unsafe_assigned.mode == "object"
|
|
407
|
+
report at unsafe_assigned.site
|
|
408
|
+
warn "Unsafe object destructuring of a property with an " unsafe_assignment_sender(unsafe_assigned) " value."
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
rule es-no-unsafe-assignment:spread {
|
|
412
|
+
each unsafe_assigned in assignment_value_transfers {
|
|
413
|
+
when unsafe_assigned.mode == "spread"
|
|
414
|
+
report at unsafe_assigned.site
|
|
415
|
+
warn "Unsafe spread of an " unsafe_assignment_sender(unsafe_assigned) " value in an array."
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
fn unsafe_return_sender(transfer) = if transfer.error then "error" else if transfer.mode == "array" then "`any[]`" else if transfer.mode == "promise" then "`Promise<any>`" else "`any`"
|
|
421
|
+
|
|
422
|
+
in ts_return, ts_arrow.body rule es-no-unsafe-return {
|
|
423
|
+
when return_value_transfer != null && return_value_transfer.mode != "generic"
|
|
424
|
+
warn when { return_value_transfer.this_any then "Unsafe return of a value of type `{unsafe_return_sender(return_value_transfer)}`. `this` is typed as `any`." unsafe_this_note()
|
|
425
|
+
else then "Unsafe return of a value of type " unsafe_return_sender(return_value_transfer) "." }
|
|
426
|
+
}
|
|
427
|
+
in ts_return, ts_arrow.body rule es-no-unsafe-return:assignment {
|
|
428
|
+
when return_value_transfer != null && return_value_transfer.mode == "generic"
|
|
429
|
+
warn "Unsafe return of type `{return_value_transfer.sender}` from function with return type `{return_value_transfer.receiver}`."
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
fn non_null_style_fix(source, n) = {
|
|
433
|
+
value body: assertion_fix_body(source, n)
|
|
434
|
+
value wrapped: body.node is (structure_binary, structure_assertion, ts_sequence, ts_assignment,
|
|
435
|
+
ts_conditional, ts_arrow, ts_yield, ts_unary, ts_await, ts_update)
|
|
436
|
+
value replacement: expression_fix_wrap(body.text, wrapped) "!"
|
|
437
|
+
value if !expression_fix_retains_comments(n, replacement) then null else assertion_fix_output(n, replacement)
|
|
438
|
+
}
|
|
439
|
+
in ts_as, ts_type_assertion rule es-non-nullable-type-assertion-style {
|
|
440
|
+
when type.text != "const" && assertion_non_null_style(node) == true
|
|
441
|
+
warn "Use a ! assertion to more succinctly remove null and undefined from the type."
|
|
442
|
+
let edit non_null_style_fix(source, node)
|
|
443
|
+
fix at edit.target if edit != null as edit.text
|
|
444
|
+
}
|
|
445
|
+
in ts_method.return_type, ts_method_signature.return_type rule es-related-getter-setter-pairs {
|
|
446
|
+
when typed_accessor_type_mismatch(node)
|
|
447
|
+
warn "`get()` type should be assignable to its equivalent `set()` type."
|
|
448
|
+
}
|
|
449
|
+
# `await` of something that is not a promise; promise aggregators of arrays
|
|
450
|
+
# of non-promises.
|
|
451
|
+
in ts_await rule es-await-thenable {
|
|
452
|
+
when ts_value_nonthenable(argument.node) == true
|
|
453
|
+
warn "Unexpected `await` of a non-Promise (non-\"Thenable\") value."
|
|
454
|
+
}
|
|
455
|
+
in ts_member, ts_index let promise_aggregate_member contains(["all", "allSettled", "race", "any"], static_name) && object.global_value == "Promise"
|
|
456
|
+
in ts_call let is_promise_aggregator callee.promise_aggregate_member == true
|
|
457
|
+
in ts_array let is_promise_aggregator_argument parent.is_promise_aggregator == true
|
|
458
|
+
in ts_array.element rule es-await-thenable:aggregate {
|
|
459
|
+
when not ts_hole && parent.is_promise_aggregator_argument
|
|
460
|
+
&& (if is ts_spread then ts_await_nonawaitable_iterable(argument.node) else ts_value_nonthenable(node) == true)
|
|
461
|
+
warn "Unexpected iterable of non-Promise (non-\"Thenable\") values passed to promise aggregator."
|
|
462
|
+
}
|
|
463
|
+
in ts_call.argument rule es-await-thenable:iterable {
|
|
464
|
+
when parent.is_promise_aggregator == true && not ts_array && ts_await_nonawaitable_iterable(node)
|
|
465
|
+
warn "Unexpected iterable of non-Promise (non-\"Thenable\") values passed to promise aggregator."
|
|
466
|
+
}
|
|
467
|
+
in ts_for_of rule es-await-thenable:for-of {
|
|
468
|
+
report at { start: offset, end: last(overlapping(ts_close_paren, right.end_offset, body.offset)).end_offset }
|
|
469
|
+
when contains([] modifier, "await")
|
|
470
|
+
&& value_has_member(right.node, {global: "Symbol", class: "value", path: ["asyncIterator"]}, null) == false
|
|
471
|
+
warn "Unexpected `for await...of` of a value that is not async iterable."
|
|
472
|
+
}
|
|
473
|
+
in ts_declarator.init rule es-await-thenable:using {
|
|
474
|
+
when starts_with(parent.parent.kind.text, "await")
|
|
475
|
+
&& value_has_member(node, {global: "Symbol", class: "value", path: ["asyncDispose"]}, null) == false
|
|
476
|
+
warn "Unexpected `await using` of a value that is not async disposable."
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
# ---- template interpolation ----------------------------------------------------
|
|
480
|
+
in ts_template let tagged_template parent is ts_tagged_template
|
|
481
|
+
in ts_node let restrict_template_expressions_type_checks template_type_checks
|
|
482
|
+
in ts_template.expression rule es-restrict-template-expressions {
|
|
483
|
+
option allowAny boolean = true "Allow any values"
|
|
484
|
+
option allowArray boolean = false "Allow arrays whose elements can be interpolated"
|
|
485
|
+
option allowBoolean boolean = true "Allow booleans"
|
|
486
|
+
option allowNullish boolean = true "Allow null and undefined"
|
|
487
|
+
option allowNumber boolean = true "Allow numbers and bigints"
|
|
488
|
+
option allowRegExp boolean = true "Allow regular expressions"
|
|
489
|
+
option allowNever boolean = false "Allow never values"
|
|
490
|
+
option allow string[] | null = null "Allowed types and values; null keeps standard library Error, URL and URLSearchParams"
|
|
491
|
+
let enabled_template_options ["string"] (if allowAny then ["any"] else []) (if allowArray then ["array"] else []) (if allowBoolean then ["boolean"] else []) (if allowNullish then ["nullish"] else []) (if allowNumber then ["number"] else []) (if allowRegExp then ["regexp"] else []) (if allowNever then ["never"] else [])
|
|
492
|
+
when parent.tagged_template != true
|
|
493
|
+
when template_type_checks != null
|
|
494
|
+
when any(restrict_template_expressions_type_checks, clause => !any(clause, gate => contains(enabled_template_options, gate.option) || gate.name != null && (if allow == null then gate.from == "lib" && contains(["Error", "URL", "URLSearchParams"], gate.name) else contains(allow, gate.name))))
|
|
495
|
+
let template_display_parts value_constituents(node)
|
|
496
|
+
when template_display_parts != null
|
|
497
|
+
warn "Invalid type \"{ts_value_print_type(node)}\" of template literal expression."
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
fn typed_unary_type(n) = if (n is ts_template) && count(n.expression.nodes) == 0
|
|
501
|
+
then ts_value_string(slice(first(n.quasi.nodes).text, 1, -1)) else ts_value_print_type(n)
|
|
502
|
+
|
|
503
|
+
in ts_unary rule es-no-unsafe-unary-minus {
|
|
504
|
+
when operator == "-" && !ts_value_opaque(argument.node)
|
|
505
|
+
&& !any(["any", "number", "bigint", "enum"], trait => ts_value_has_trait(argument.node, trait) == true)
|
|
506
|
+
let operand_type value_constituents(argument.node)
|
|
507
|
+
when operand_type != null && count(operand_type) > 0
|
|
508
|
+
warn "Argument of unary negation should be assignable to number | bigint but is {typed_unary_type(argument.node)} instead."
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
in ts_member, ts_index let object_array_type ts_value_array_like(object.node)
|
|
512
|
+
in ts_parenthesized let object_array_type expression.object_array_type
|
|
513
|
+
in ts_member, ts_index let array_literal_object object is ts_array
|
|
514
|
+
in ts_parenthesized let array_literal_object expression.array_literal_object
|
|
515
|
+
in ts_unary rule es-no-array-delete {
|
|
516
|
+
when operator == "delete" && (argument.array_literal_object == true || argument.object_array_type == true)
|
|
517
|
+
warn "Using the `delete` operator with an array expression is unsafe."
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
in ts_assignment_pattern, ts_parameter rule es-no-useless-default-assignment {
|
|
521
|
+
when typed_default_assignment != null
|
|
522
|
+
report at { start: typed_default_assignment.offset, end: typed_default_assignment.end }
|
|
523
|
+
warn when {
|
|
524
|
+
typed_default_assignment.kind == "optional" then "Using `= undefined` to make a parameter optional adds unnecessary runtime logic."
|
|
525
|
+
typed_default_assignment.kind == "undefined" then "Default value is useless because it is undefined. Optional {typed_default_assignment.owner}s are already undefined by default."
|
|
526
|
+
else then "Default value is useless because the {typed_default_assignment.owner} is not optional."
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
in ts_program rule es-no-useless-default-assignment:strict {
|
|
530
|
+
option allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing boolean = false "Run without strict null checks"
|
|
531
|
+
when ts_compiler_option(node, "strictNullChecks") == false && !allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing
|
|
532
|
+
report at { start: 0, end: 0 }
|
|
533
|
+
warn "This rule requires the `strictNullChecks` compiler option to be turned on to function correctly."
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
in ts_as, ts_type_assertion rule es-no-unsafe-type-assertion {
|
|
537
|
+
when type.text != "const"
|
|
538
|
+
let operand value_constituents(expression.node)
|
|
539
|
+
let annotation value_constituents(type.node)
|
|
540
|
+
when operand != null && annotation != null
|
|
541
|
+
let from_any !assignment_empty_map(expression.node) && (assignment_unsafe(operand, annotation, node) || annotation == [] && ts_value_category(operand) != null)
|
|
542
|
+
let to_any (operand == [] || ts_value_trait(operand, "unknown")) && ts_value_category(annotation) != null || assignment_unsafe(annotation, operand, node)
|
|
543
|
+
let narrowing if from_any || to_any then null else assertion_unsafe_narrowing(node)
|
|
544
|
+
let label if from_any then if ts_value_category(operand) == "error" then "error typed" else "`any`" else if to_any then if ts_value_category(annotation) == "error" then "error typed" else "`any`" else narrowing.label
|
|
545
|
+
when from_any || to_any || narrowing != null && !re_test(label, /\bunknown\b/)
|
|
546
|
+
warn if from_any then "Unsafe assertion from {label} detected: consider using type guards or a safer assertion." else if to_any then "Unsafe assertion to {label} detected: consider using a more specific type to ensure safety." else if narrowing.kind == "narrow" then "Unsafe type assertion: type '{label}' is more narrow than the original type." else if narrowing.kind == "unconstrained" then "Unsafe type assertion: '{label}' could be instantiated with an arbitrary type which could be unrelated to the original type." else "Unsafe type assertion: the original type is assignable to the constraint of type '{label}', but '{label}' could be instantiated with a different subtype of its constraint."
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
in ts_member let object_string_array_type ts_value_string_array(object.node)
|
|
550
|
+
in ts_call rule es-require-array-sort-compare {
|
|
551
|
+
option ignoreStringArrays boolean = true "Ignore arrays whose elements are all strings"
|
|
552
|
+
when count(argument) == 0 && re_test(callee, /\??\.\s*(sort|toSorted)$/)
|
|
553
|
+
when callee.object_array_type == true && (!ignoreStringArrays || callee.object_string_array_type != true)
|
|
554
|
+
warn "Require 'compare' argument."
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
in ts_unary rule es-no-meaningless-void-operator {
|
|
559
|
+
option checkNever boolean = false "Also check calls returning never"
|
|
560
|
+
when operator == "void" && argument != "0"
|
|
561
|
+
let underlying {
|
|
562
|
+
value argument.node
|
|
563
|
+
walk (current) if current is (ts_parenthesized, ts_as, ts_satisfies, ts_non_null, ts_type_assertion)
|
|
564
|
+
then current.expression.node
|
|
565
|
+
else if current is ts_sequence then last(current.expression.nodes) else null
|
|
566
|
+
last
|
|
567
|
+
}
|
|
568
|
+
when underlying not ts_assignment
|
|
569
|
+
let call underlying is (ts_call, ts_new)
|
|
570
|
+
when if call then ts_value_trait(value_constituent_traits(argument.node, ["undefined"]), "undefined")
|
|
571
|
+
|| checkNever && value_parts(argument.node) == [] else value_has_member(argument.node, "then", null) != true
|
|
572
|
+
let operand_type if call then value_constituents(argument.node) else []
|
|
573
|
+
when !call || operand_type != null
|
|
574
|
+
warn if call then "void operator shouldn't be used on {ts_value_print(operand_type)}; it should convey that a return value is being ignored" else "void operator is useless here; it should only discard a call's return value"
|
|
575
|
+
let operand argument.node
|
|
576
|
+
let parens expression_fix_parens(source, node, operand)
|
|
577
|
+
let replacement expression_fix_wrap(operand.text, parens > 0)
|
|
578
|
+
let statement expression_fix_statement(node)
|
|
579
|
+
let semicolon if statement != null && expression_fix_continuation(replacement)
|
|
580
|
+
then expression_fix_asi(statement, token_neighbours.before, expression_fix_before_nodes(node), replacement) else false
|
|
581
|
+
fix if parens != null && semicolon != null && expression_fix_clean({ offset: offset, end_offset: operand.offset }) as
|
|
582
|
+
(if semicolon then ";" else "") expression_fix_edges(node, replacement)
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
}
|