@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
|
@@ -1,317 +1,317 @@
|
|
|
1
|
-
# Optional-chain selection, guard semantics and atomic edits.
|
|
2
|
-
import .ts/expression-fixes
|
|
3
|
-
import .ts/values/inspection
|
|
4
|
-
|
|
5
|
-
define {
|
|
6
|
-
|
|
7
|
-
in ts_identifier, ts_private_name, ts_this, ts_super, ts_literal let optional_shape [kind_name(node), text]
|
|
8
|
-
in ts_parenthesized let optional_shape if expression.in_optional_chain == true && parent is (ts_member, ts_index, ts_call) then ["boundary", expression.optional_shape] else expression.optional_shape
|
|
9
|
-
in ts_non_null let optional_shape expression.optional_shape
|
|
10
|
-
in ts_as, ts_type_assertion let optional_shape if expression.optional_shape != null then [kind_name(node), expression.optional_shape, type.token_texts] else null
|
|
11
|
-
in ts_meta_property let optional_shape ["meta", text]
|
|
12
|
-
in ts_await let optional_shape if argument.optional_shape != null then ["await", argument.optional_shape] else null
|
|
13
|
-
in ts_member let optional_shape if object.optional_shape != null && property.optional_shape != null then ["member", object.optional_shape, property.optional_shape] else null
|
|
14
|
-
in ts_index let optional_shape if object.optional_shape != null && index.optional_shape != null then ["index", object.optional_shape, index.optional_shape] else null
|
|
15
|
-
in ts_call let optional_shape {
|
|
16
|
-
value args: { value argument.nodes; map (arg) arg.optional_shape }
|
|
17
|
-
value types: { value type_argument.nodes; map (type) type.text }
|
|
18
|
-
value if callee.optional_shape != null && { value args; all (shape) shape != null }
|
|
19
|
-
then ["call", callee.optional_shape, args, types] else null
|
|
20
|
-
}
|
|
21
|
-
in ts_unary let optional_shape if argument.optional_shape != null then ["unary", operator, argument.optional_shape] else null
|
|
22
|
-
in ts_binary let optional_shape if left.optional_shape != null && right.optional_shape != null then ["binary", operator, left.optional_shape, right.optional_shape] else null
|
|
23
|
-
in ts_template let optional_shape {
|
|
24
|
-
value quasis: { value quasi.nodes; map (quasi) quasi.text }
|
|
25
|
-
value expressions: { value expression.nodes; map (expression) expression.optional_shape }
|
|
26
|
-
value ["template", quasis, expressions]
|
|
27
|
-
}
|
|
28
|
-
in ts_parenthesized let optional_operands expression.optional_operands
|
|
29
|
-
fn optional_side(n, op) = if n is ts_logical && n.operator == op then n.optional_operands else [n]
|
|
30
|
-
in ts_logical let optional_operands {
|
|
31
|
-
value lhs: unparenthesised_node(left.node)
|
|
32
|
-
value rhs: unparenthesised_node(right.node)
|
|
33
|
-
value optional_side(lhs, operator) optional_side(rhs, operator)
|
|
34
|
-
}
|
|
35
|
-
fn optional_base(n) = if n is (ts_member, ts_index) && n.property not ts_private_name then n.object.node
|
|
36
|
-
else if n is ts_call then n.callee.node
|
|
37
|
-
else if n is ts_non_null then n.expression.node
|
|
38
|
-
else if n is ts_parenthesized && n.expression.in_optional_chain != true then n.expression.node else null
|
|
39
|
-
fn optional_same(a, b) = a.optional_shape != null && a.optional_shape == b.optional_shape
|
|
40
|
-
fn optional_extends(a, b) = { value a; walk (n) if n is ts_non_null then n.expression.node else null; last } is (ts_identifier, ts_member, ts_index, ts_call, ts_meta_property)
|
|
41
|
-
&& !optional_same(a, b) && { value b; walk (n) optional_base(n); slice 1; any (n) optional_same(a, n) }
|
|
42
|
-
fn optional_nullish(n) = if n is ts_null then "null" else if n is ts_identifier && n.text == "undefined" then "undefined"
|
|
43
|
-
else if n is ts_string && n.literal_value.value == "undefined" then "typeof" else null
|
|
44
|
-
fn optional_subject(n, value) = if value == "typeof" && n is ts_unary && n.operator == "typeof" && !(n.argument is ts_identifier && n.argument.resolved == "global") then n.argument.node
|
|
45
|
-
else if value != "typeof" then n else null
|
|
46
|
-
fn optional_comparison_kind(value, comparison, op) = if value != "typeof" && starts_with(comparison, "!") != (op == "&&") then "last"
|
|
47
|
-
else if comparison == "==" || comparison == "!=" then "nullish" else if value == "typeof" then "undefined" else value
|
|
48
|
-
fn optional_binary_operand(n, op) = {
|
|
49
|
-
value right: optional_nullish(n.right.node)
|
|
50
|
-
value left: optional_nullish(n.left.node)
|
|
51
|
-
value yoda: right == null && left != null
|
|
52
|
-
value compared: if yoda then n.right.node else n.left.node
|
|
53
|
-
value value_kind: if yoda then left else right
|
|
54
|
-
value {node: n, subject: if value_kind != null then optional_subject(compared, value_kind) else null,
|
|
55
|
-
kind: if value_kind != null then optional_comparison_kind(value_kind, n.operator, op) else "last",
|
|
56
|
-
matches: value_kind != null && starts_with(n.operator, "!") == (op == "&&"), yoda: yoda, terminal: false}
|
|
57
|
-
}
|
|
58
|
-
fn optional_operand(n, op) = if n is ts_binary then optional_binary_operand(n, op)
|
|
59
|
-
else if n is ts_unary && n.operator == "!" then {node: n, subject: unparenthesised_node(n.argument.node), kind: "not", matches: op == "||", terminal: false}
|
|
60
|
-
else {node: n, subject: unparenthesised_node(n), kind: "plain", matches: op == "&&", terminal: false}
|
|
61
|
-
fn optional_falsy_literal(n) = {
|
|
62
|
-
value arms: value_constituents(n)
|
|
63
|
-
value any(arms, arm => any(arm, part => part.identity.kind == "value"
|
|
64
|
-
|| contains(part.traits, "boolean"))
|
|
65
|
-
&& any(arm, part => (contains(part.traits, "falsy") || contains(part.traits, "boolean") && !contains(part.traits, "truthy"))
|
|
66
|
-
&& any(part.traits, trait => contains(["boolean", "number", "string", "bigint"], trait))))
|
|
67
|
-
}
|
|
68
|
-
# A computed type is not needed when the value is an unresolved global or reads
|
|
69
|
-
# as `any`; a call whose callee is understood is likewise treated as `any`.
|
|
70
|
-
# Any other uncomputable operand cannot prove the chain is safe.
|
|
71
|
-
fn optional_type_allowed(n, opts) = {
|
|
72
|
-
value arms: value_trait_parts(n, ["void", "any", "unknown", "string", "number", "boolean", "bigint"])
|
|
73
|
-
value if arms == null then optional_opaque_any(n) || n is ts_call && value_parts(n.callee.node) != null else
|
|
74
|
-
!any(arms, arm => any(arm.parts, part => contains(part.traits, "void")))
|
|
75
|
-
&& all(["any", "unknown", "string", "number", "boolean", "bigint"], trait =>
|
|
76
|
-
get(opts, trait) || !any(arms, arm => any(arm.parts, part => contains(part.traits, trait))))
|
|
77
|
-
}
|
|
78
|
-
fn optional_value_root(n) = if n is (ts_parenthesized, ts_non_null) then optional_value_root(n.expression.node)
|
|
79
|
-
else if n is ts_await then optional_value_root(n.argument.node)
|
|
80
|
-
else if n is (ts_member, ts_index) then optional_value_root(n.object.node)
|
|
81
|
-
else if n is ts_call then optional_value_root(n.callee.node)
|
|
82
|
-
else n
|
|
83
|
-
# Unresolved globals, `this` and `import.meta` read as `any`, which includes every trait.
|
|
84
|
-
fn optional_opaque_any(n) = value_parts(n) == null && {
|
|
85
|
-
value root: optional_value_root(n)
|
|
86
|
-
value root is (ts_this, ts_meta_property) || root is ts_identifier && root.resolved == "global"
|
|
87
|
-
}
|
|
88
|
-
fn optional_uncertain(n, trait) = {
|
|
89
|
-
value arms: value_trait_parts(n, [trait, "any", "unknown"])
|
|
90
|
-
value arms == null || any(arms, arm => any(arm.parts, part =>
|
|
91
|
-
contains(part.traits, trait) || contains(part.traits, "any") || contains(part.traits, "unknown")
|
|
92
|
-
|| contains(part.uncertain_traits, trait)))
|
|
93
|
-
}
|
|
94
|
-
# Like `optional_uncertain`, but an uncomputable answer is only treated as
|
|
95
|
-
# `any` when it comes from an unresolved global.
|
|
96
|
-
fn optional_may(n, trait) = {
|
|
97
|
-
value arms: value_trait_parts(n, [trait, "any", "unknown"])
|
|
98
|
-
value if arms == null then optional_opaque_any(n) else any(arms, arm => any(arm.parts, part =>
|
|
99
|
-
contains(part.traits, trait) || contains(part.traits, "any") || contains(part.traits, "unknown")
|
|
100
|
-
|| contains(part.uncertain_traits, trait)))
|
|
101
|
-
}
|
|
102
|
-
fn optional_arms_trait(arms, trait) = arms == null || any(arms, arm => any(arm, part => contains(part.traits, trait)))
|
|
103
|
-
fn optional_result_trait(call, trait) = {
|
|
104
|
-
value binding: call_binding(call)
|
|
105
|
-
value if binding == null then true else any(binding.choices, choice =>
|
|
106
|
-
choice.result == null || optional_may(choice.result, trait))
|
|
107
|
-
}
|
|
108
|
-
fn optional_hop_trait(hop, trait) = if hop is ts_call then optional_result_trait(hop, trait)
|
|
109
|
-
else if hop is ts_member && hop.property not ts_private_name then
|
|
110
|
-
optional_arms_trait(value_member(hop.object.node, hop.property.text), trait)
|
|
111
|
-
else if hop is ts_index then optional_arms_trait(value_member(hop.object.node, hop.index.node), trait)
|
|
112
|
-
else false
|
|
113
|
-
fn optional_linked(n) = { value n; walk (m) optional_base(m);
|
|
114
|
-
any (m) m is (ts_member, ts_index, ts_call) && m.optional != null }
|
|
115
|
-
# `undefined` that an optional link adds is synthetic: an earlier guard already
|
|
116
|
-
# settled the link's receiver, so TypeScript narrows it away at this operand.
|
|
117
|
-
# Undefined the link's own target declares (a call result, a member) is real.
|
|
118
|
-
fn optional_link_trait(n, trait) = { value n; walk (m) optional_base(m);
|
|
119
|
-
any (m) m is (ts_member, ts_index, ts_call) && m.optional != null && optional_hop_trait(m, trait) }
|
|
120
|
-
fn optional_may_real(n, trait) = optional_may(n, trait)
|
|
121
|
-
&& (!optional_linked(n) || optional_link_trait(n, trait))
|
|
122
|
-
fn optional_nullish_only(n, allow_null) = {
|
|
123
|
-
value arms: value_trait_parts(n, ["undefined", "null"])
|
|
124
|
-
value arms != null && count(arms) > 0 && all(arms, arm => any(arm.parts, part =>
|
|
125
|
-
contains(part.traits, "undefined") || allow_null && contains(part.traits, "null")))
|
|
126
|
-
}
|
|
127
|
-
fn optional_span(items, opts) = {
|
|
128
|
-
value head: first(items).subject
|
|
129
|
-
value tail: last(items).subject
|
|
130
|
-
value if len(items) > 1 && !optional_same(head, tail)
|
|
131
|
-
&& (!opts.requireNullish || { value items; slice 0 -1; any (item) item.subject could nullable }) then {
|
|
132
|
-
start: if { value prefix: first(items).node.offset - opts.boundary.offset; value opts.boundary; byte_slice 0 prefix; re_test /^(?:\s|\(|\/\*[\s\S]*?\*\/|\/\/[^\n]*\n)*$/ }
|
|
133
|
-
then opts.boundary.offset else first(items).node.offset,
|
|
134
|
-
end: last(items).end + { value suffix: last(items).end - opts.boundary.offset; value opts.boundary; byte_slice suffix;
|
|
135
|
-
re_find /^(?:(?:\s|\/\*[\s\S]*?\*\/|\/\/[^\n]*\n)*\))+/; then (closers) byte_len(closers ?? "") }
|
|
136
|
-
} else null
|
|
137
|
-
}
|
|
138
|
-
fn optional_flush(state, opts) = {
|
|
139
|
-
value span: optional_span(state.items, opts)
|
|
140
|
-
value if span != null then state.reports [{span: span, items: state.items}] else state.reports
|
|
141
|
-
}
|
|
142
|
-
fn optional_group(current, next) = current.matches && next.matches && contains(["null", "undefined"], current.kind)
|
|
143
|
-
&& contains(["null", "undefined"], next.kind) && current.kind != next.kind && optional_same(current.subject, next.subject)
|
|
144
|
-
fn optional_valid(current, next, op, last, opts) = current.subject != null && current.matches && (
|
|
145
|
-
if contains(["plain", "not"], current.kind) then last || optional_type_allowed(current.subject, opts)
|
|
146
|
-
&& !(current.kind == (if op == "&&" then "plain" else "not") && optional_falsy_literal(current.subject))
|
|
147
|
-
else current.kind == "nullish" || optional_group(current, next)
|
|
148
|
-
|| current.kind == "null" && (op == "||" || next != null) && !optional_may_real(current.subject, "undefined")
|
|
149
|
-
|| current.kind == "undefined" && !optional_may_real(current.subject, "null"))
|
|
150
|
-
fn optional_terminal_allowed(n, comparison, op) = if comparison == (if op == "&&" then "===" else "!==") then !optional_may(n, "undefined")
|
|
151
|
-
else if comparison == (if op == "&&" then "==" else "!=") then !optional_may(n, "null") && !optional_may(n, "undefined")
|
|
152
|
-
else if comparison == (if op == "&&" then "!==" else "===") then optional_nullish_only(n, false)
|
|
153
|
-
else if comparison == (if op == "&&" then "!=" else "==") then optional_nullish_only(n, true) else false
|
|
154
|
-
fn optional_item(current) = {node: current.node, subject: current.subject, kind: current.kind, yoda: current.yoda,
|
|
155
|
-
terminal: current.terminal, end: current.node.end_offset}
|
|
156
|
-
fn optional_member_based(n) = {
|
|
157
|
-
value bare: unparenthesised_node(n)
|
|
158
|
-
value callee: if bare is ts_call then unparenthesised_node(bare.callee.node) else null
|
|
159
|
-
value bare.in_optional_chain != true && (bare is (ts_member, ts_index) || callee is (ts_member, ts_index))
|
|
160
|
-
}
|
|
161
|
-
fn optional_terminal(state, current, op, opts) = {
|
|
162
|
-
value previous: last(state.items).subject
|
|
163
|
-
value left: optional_member_based(current.node.left.node) && optional_extends(previous, current.node.left.node)
|
|
164
|
-
value right: optional_member_based(current.node.right.node) && optional_extends(previous, current.node.right.node)
|
|
165
|
-
value compared: if right then current.node.right.node else current.node.left.node
|
|
166
|
-
value other: if right then current.node.left.node else current.node.right.node
|
|
167
|
-
value accepted: len(state.items) > 0 && left != right && optional_terminal_allowed(other, current.node.operator, op)
|
|
168
|
-
value if accepted then optional_flush({items: state.items [{node: current.node, subject: compared, kind: "last",
|
|
169
|
-
yoda: right, terminal: true, end: current.node.end_offset}], reports: state.reports}, opts) else optional_flush(state, opts)
|
|
170
|
-
}
|
|
171
|
-
fn optional_accept(state, current, next, opts) = {
|
|
172
|
-
value grouped: optional_group(current, next)
|
|
173
|
-
value extends: len(state.items) > 0 && { value last(state.items).subject; optional_extends current.subject }
|
|
174
|
-
value same: len(state.items) > 0 && { value last(state.items).subject; optional_same current.subject }
|
|
175
|
-
# A paired seed retains both checks. Extending a chain retains only the first;
|
|
176
|
-
# the second check validates the guard but is not part of the replacement.
|
|
177
|
-
value seed: if grouped then [optional_item(current), optional_item(next)] else [optional_item(current)]
|
|
178
|
-
value if extends then {items: state.items [optional_item(current)], reports: state.reports, skip: grouped}
|
|
179
|
-
else if same then {items: state.items, reports: state.reports, skip: grouped}
|
|
180
|
-
else {items: seed, reports: optional_flush(state, opts), skip: grouped}
|
|
181
|
-
}
|
|
182
|
-
fn optional_step(state, current, next, op, last, opts) = if state.skip then {items: state.items, reports: state.reports, skip: false}
|
|
183
|
-
else if current.kind == "last" then {items: [], reports: optional_terminal(state, current, op, opts), skip: false}
|
|
184
|
-
else if optional_valid(current, next, op, last, opts) then optional_accept(state, current, next, opts)
|
|
185
|
-
else {
|
|
186
|
-
value extended: {items: state.items [{node: current.node, subject: current.subject, kind: current.kind,
|
|
187
|
-
yoda: current.yoda, terminal: true, end: current.node.end_offset}], reports: state.reports}
|
|
188
|
-
value {items: [], reports: if current.kind == "undefined" && len(state.items) > 0 && { value last(state.items).subject; optional_extends current.subject }
|
|
189
|
-
then optional_flush(extended, opts) else optional_flush(state, opts), skip: false}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
fn optional_part(n) = if n is (ts_member, ts_index) then {
|
|
193
|
-
text: if n is ts_index then "[" n.index.text "]" else n.property.text,
|
|
194
|
-
dot: n is ts_member, optional: n.optional != null, nonNull: n.object is ts_non_null
|
|
195
|
-
} else if n is ts_call then {
|
|
196
|
-
text: { value start: n.callee.end_offset - n.offset; value n; byte_slice start; trim_start; re_replace /^\?\.\s*/ "" },
|
|
197
|
-
dot: false, optional: n.optional != null, nonNull: false
|
|
198
|
-
} else {text: expression_fix_wrap(n.text, n.precedence < 19), dot: false, optional: false, nonNull: false}
|
|
199
|
-
fn optional_parts(n) = {
|
|
200
|
-
value n
|
|
201
|
-
walk (part) optional_base(part)
|
|
202
|
-
reverse
|
|
203
|
-
filter (part) part not (ts_parenthesized, ts_non_null)
|
|
204
|
-
map (part) optional_part(part)
|
|
205
|
-
}
|
|
206
|
-
fn optional_render_part(part, guarded) = (if guarded || part.optional then "?." else (if part.nonNull then "!" else "") (if part.dot then "." else "")) part.text
|
|
207
|
-
fn optional_render(items) = {
|
|
208
|
-
value items
|
|
209
|
-
fold ({count: 0, text: ""}) (result, item) {
|
|
210
|
-
value parts: optional_parts(item.subject)
|
|
211
|
-
value count: len(parts)
|
|
212
|
-
value appended: { value parts; slice result.count; map (part, index) optional_render_part(part, result.count > 0 && index == 0); join "" }
|
|
213
|
-
value {count: max(result.count, count), text: result.text appended}
|
|
214
|
-
}
|
|
215
|
-
then (result) result.text
|
|
216
|
-
}
|
|
217
|
-
fn optional_output(items) = {
|
|
218
|
-
value text: optional_render(items)
|
|
219
|
-
value terminal: last(items)
|
|
220
|
-
value compared: if terminal.yoda then terminal.node.right.node else terminal.node.left.node
|
|
221
|
-
value unary: if compared is ts_unary then compared.operator " " else ""
|
|
222
|
-
value if terminal.node is ts_binary then
|
|
223
|
-
if terminal.yoda then terminal.node.left.text " " terminal.node.operator " " unary text
|
|
224
|
-
else unary text " " terminal.node.operator " " terminal.node.right.text
|
|
225
|
-
else if terminal.kind == "not" then "!" text else text
|
|
226
|
-
}
|
|
227
|
-
# A call whose signature and receiver the engine cannot read together (lib
|
|
228
|
-
# `Function`, unread imported types) answers TypeScript's error type `any`.
|
|
229
|
-
fn optional_opaque_member_call(n) = n is ts_call && value_parts(n) == null
|
|
230
|
-
&& n.callee.node is (ts_member, ts_index) && value_parts(n.callee.node.object.node) != null
|
|
231
|
-
fn optional_safe(items, op, unsafe) = {
|
|
232
|
-
value terminal: last(items)
|
|
233
|
-
# A fix adds `undefined` to the chain's value; that changes nothing when an
|
|
234
|
-
# operand already carries it (`any` includes `undefined`).
|
|
235
|
-
value already_undefined: any(items, item =>
|
|
236
|
-
optional_may_real(item.node, "undefined") || optional_opaque_member_call(item.node))
|
|
237
|
-
value unsafe || !terminal.terminal && (contains(["nullish", "undefined"], terminal.kind)
|
|
238
|
-
|| op == "||" && terminal.kind == "not" || already_undefined)
|
|
239
|
-
}
|
|
240
|
-
fn optional_tail(source, span, boundary) = {
|
|
241
|
-
# Paired code tokens, not character counts: parentheses in comments and strings
|
|
242
|
-
# cannot consume or manufacture an unmatched delimiter.
|
|
243
|
-
value removed: { value overlapping(ts_close_paren, span.end, boundary.end_offset);
|
|
244
|
-
filter (token) token.paired_token.start >= span.start && token.paired_token.start < span.end }
|
|
245
|
-
value removed
|
|
246
|
-
fold ({offset: span.end, text: ""}) (tail, token) {
|
|
247
|
-
text: tail.text byte_slice(source, tail.offset, token.offset), offset: token.end_offset
|
|
248
|
-
}
|
|
249
|
-
then (tail) tail.text byte_slice(source, tail.offset, boundary.end_offset)
|
|
250
|
-
}
|
|
251
|
-
fn optional_fix_output(boundary, target, text) = {
|
|
252
|
-
value head_node: node_at(target.start)
|
|
253
|
-
value tail_node: node_at(target.end - 1)
|
|
254
|
-
value edges: {offset: target.start, end_offset: target.end,
|
|
255
|
-
token_neighbours: {before: head_node.token_neighbours.before, after: tail_node.token_neighbours.after}}
|
|
256
|
-
value statement: if target.start == boundary.offset then expression_fix_statement(boundary) else null
|
|
257
|
-
value head: { value text; trim_start; slice 0 1 }
|
|
258
|
-
value owners: expression_fix_before_nodes(boundary)
|
|
259
|
-
value asi: if statement != null && contains(["(", "[", "/", "`"], head)
|
|
260
|
-
then expression_fix_asi(statement, boundary.token_neighbours.before, owners, text) else false
|
|
261
|
-
value if asi == null then null else {target: target,
|
|
262
|
-
text: (if asi then ";" else "") expression_fix_edges(edges, text)}
|
|
263
|
-
}
|
|
264
|
-
fn optional_edit(source, report, boundary) = {
|
|
265
|
-
value text: optional_output(report.items)
|
|
266
|
-
value open: { value overlapping(ts_open_paren, report.span.start, report.span.end);
|
|
267
|
-
any (token) token.paired_token.end > report.span.end && token.paired_token.end <= boundary.end_offset }
|
|
268
|
-
value target: if open then {start: report.span.start, end: boundary.end_offset} else report.span
|
|
269
|
-
value replacement: if open then text optional_tail(source, report.span, boundary) else text
|
|
270
|
-
value optional_fix_output(boundary, target, replacement)
|
|
271
|
-
}
|
|
272
|
-
fn optional_report(source, report, boundary, op, unsafe) = {
|
|
273
|
-
span: report.span, edit: if optional_safe(report.items, op, unsafe) then optional_edit(source, report, boundary) else null
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
in ts_logical rule es-prefer-optional-chain {
|
|
277
|
-
option checkAny boolean = true "Check any values"
|
|
278
|
-
option checkUnknown boolean = true "Check unknown values"
|
|
279
|
-
option checkString boolean = true "Check string values"
|
|
280
|
-
option checkNumber boolean = true "Check number values"
|
|
281
|
-
option checkBoolean boolean = true "Check boolean values"
|
|
282
|
-
option checkBigInt boolean = true "Check bigint values"
|
|
283
|
-
option requireNullish boolean = false "Require a nullable checked operand"
|
|
284
|
-
option allowPotentiallyUnsafeFixesThatModifyTheReturnTypeIKnowWhatImDoing boolean = false "Allow fixes that may add undefined to the return type"
|
|
285
|
-
let enclosing { value node; walk (n) if n.parent is ts_parenthesized then n.parent.node else null; last }
|
|
286
|
-
when operator != "??" && !(enclosing.parent is ts_logical && enclosing.parent.operator == operator)
|
|
287
|
-
let opts {any: checkAny, unknown: checkUnknown, string: checkString, number: checkNumber, boolean: checkBoolean,
|
|
288
|
-
bigint: checkBigInt, requireNullish: requireNullish, boundary: node}
|
|
289
|
-
let operands { value optional_operands; map (operand) { value operand; unparenthesised_node; optional_operand operator } }
|
|
290
|
-
let state {
|
|
291
|
-
value following: { value operands; slice 1 } [null]
|
|
292
|
-
value operands
|
|
293
|
-
zip following
|
|
294
|
-
fold ({items: [], reports: [], skip: false}) (state, pair) {
|
|
295
|
-
value current: first(pair)
|
|
296
|
-
value next: last(pair)
|
|
297
|
-
value optional_step(state, current, next, operator, next == null, opts)
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
each chain in { value state; optional_flush opts;
|
|
301
|
-
map (report) optional_report(source, report, node, operator, allowPotentiallyUnsafeFixesThatModifyTheReturnTypeIKnowWhatImDoing) } {
|
|
302
|
-
report at chain.span
|
|
303
|
-
warn "Prefer using an optional chain expression instead, as it's more concise and easier to read."
|
|
304
|
-
fix at chain.edit.target if chain.edit != null as chain.edit.text
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
in ts_logical rule es-prefer-optional-chain:empty-object {
|
|
308
|
-
option requireNullish boolean = false "Require a nullable checked operand"
|
|
309
|
-
when contains(["||", "??"], operator) && unparenthesised_node(right.node) is ts_object && { value unparenthesised_node(right.node).property.nodes; count } == 0
|
|
310
|
-
let enclosing { value node; walk (n) if n.parent is ts_parenthesized then n.parent.node else null; last }
|
|
311
|
-
when enclosing.parent is (ts_member, ts_index) && enclosing.parent.optional == null
|
|
312
|
-
when !requireNullish || left could nullable
|
|
313
|
-
report at enclosing.parent
|
|
314
|
-
warn "Prefer using an optional chain expression instead, as it's more concise and easier to read."
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
}
|
|
1
|
+
# Optional-chain selection, guard semantics and atomic edits.
|
|
2
|
+
import .ts/expression-fixes
|
|
3
|
+
import .ts/values/inspection
|
|
4
|
+
|
|
5
|
+
define {
|
|
6
|
+
|
|
7
|
+
in ts_identifier, ts_private_name, ts_this, ts_super, ts_literal let optional_shape [kind_name(node), text]
|
|
8
|
+
in ts_parenthesized let optional_shape if expression.in_optional_chain == true && parent is (ts_member, ts_index, ts_call) then ["boundary", expression.optional_shape] else expression.optional_shape
|
|
9
|
+
in ts_non_null let optional_shape expression.optional_shape
|
|
10
|
+
in ts_as, ts_type_assertion let optional_shape if expression.optional_shape != null then [kind_name(node), expression.optional_shape, type.token_texts] else null
|
|
11
|
+
in ts_meta_property let optional_shape ["meta", text]
|
|
12
|
+
in ts_await let optional_shape if argument.optional_shape != null then ["await", argument.optional_shape] else null
|
|
13
|
+
in ts_member let optional_shape if object.optional_shape != null && property.optional_shape != null then ["member", object.optional_shape, property.optional_shape] else null
|
|
14
|
+
in ts_index let optional_shape if object.optional_shape != null && index.optional_shape != null then ["index", object.optional_shape, index.optional_shape] else null
|
|
15
|
+
in ts_call let optional_shape {
|
|
16
|
+
value args: { value argument.nodes; map (arg) arg.optional_shape }
|
|
17
|
+
value types: { value type_argument.nodes; map (type) type.text }
|
|
18
|
+
value if callee.optional_shape != null && { value args; all (shape) shape != null }
|
|
19
|
+
then ["call", callee.optional_shape, args, types] else null
|
|
20
|
+
}
|
|
21
|
+
in ts_unary let optional_shape if argument.optional_shape != null then ["unary", operator, argument.optional_shape] else null
|
|
22
|
+
in ts_binary let optional_shape if left.optional_shape != null && right.optional_shape != null then ["binary", operator, left.optional_shape, right.optional_shape] else null
|
|
23
|
+
in ts_template let optional_shape {
|
|
24
|
+
value quasis: { value quasi.nodes; map (quasi) quasi.text }
|
|
25
|
+
value expressions: { value expression.nodes; map (expression) expression.optional_shape }
|
|
26
|
+
value ["template", quasis, expressions]
|
|
27
|
+
}
|
|
28
|
+
in ts_parenthesized let optional_operands expression.optional_operands
|
|
29
|
+
fn optional_side(n, op) = if n is ts_logical && n.operator == op then n.optional_operands else [n]
|
|
30
|
+
in ts_logical let optional_operands {
|
|
31
|
+
value lhs: unparenthesised_node(left.node)
|
|
32
|
+
value rhs: unparenthesised_node(right.node)
|
|
33
|
+
value optional_side(lhs, operator) optional_side(rhs, operator)
|
|
34
|
+
}
|
|
35
|
+
fn optional_base(n) = if n is (ts_member, ts_index) && n.property not ts_private_name then n.object.node
|
|
36
|
+
else if n is ts_call then n.callee.node
|
|
37
|
+
else if n is ts_non_null then n.expression.node
|
|
38
|
+
else if n is ts_parenthesized && n.expression.in_optional_chain != true then n.expression.node else null
|
|
39
|
+
fn optional_same(a, b) = a.optional_shape != null && a.optional_shape == b.optional_shape
|
|
40
|
+
fn optional_extends(a, b) = { value a; walk (n) if n is ts_non_null then n.expression.node else null; last } is (ts_identifier, ts_member, ts_index, ts_call, ts_meta_property)
|
|
41
|
+
&& !optional_same(a, b) && { value b; walk (n) optional_base(n); slice 1; any (n) optional_same(a, n) }
|
|
42
|
+
fn optional_nullish(n) = if n is ts_null then "null" else if n is ts_identifier && n.text == "undefined" then "undefined"
|
|
43
|
+
else if n is ts_string && n.literal_value.value == "undefined" then "typeof" else null
|
|
44
|
+
fn optional_subject(n, value) = if value == "typeof" && n is ts_unary && n.operator == "typeof" && !(n.argument is ts_identifier && n.argument.resolved == "global") then n.argument.node
|
|
45
|
+
else if value != "typeof" then n else null
|
|
46
|
+
fn optional_comparison_kind(value, comparison, op) = if value != "typeof" && starts_with(comparison, "!") != (op == "&&") then "last"
|
|
47
|
+
else if comparison == "==" || comparison == "!=" then "nullish" else if value == "typeof" then "undefined" else value
|
|
48
|
+
fn optional_binary_operand(n, op) = {
|
|
49
|
+
value right: optional_nullish(n.right.node)
|
|
50
|
+
value left: optional_nullish(n.left.node)
|
|
51
|
+
value yoda: right == null && left != null
|
|
52
|
+
value compared: if yoda then n.right.node else n.left.node
|
|
53
|
+
value value_kind: if yoda then left else right
|
|
54
|
+
value {node: n, subject: if value_kind != null then optional_subject(compared, value_kind) else null,
|
|
55
|
+
kind: if value_kind != null then optional_comparison_kind(value_kind, n.operator, op) else "last",
|
|
56
|
+
matches: value_kind != null && starts_with(n.operator, "!") == (op == "&&"), yoda: yoda, terminal: false}
|
|
57
|
+
}
|
|
58
|
+
fn optional_operand(n, op) = if n is ts_binary then optional_binary_operand(n, op)
|
|
59
|
+
else if n is ts_unary && n.operator == "!" then {node: n, subject: unparenthesised_node(n.argument.node), kind: "not", matches: op == "||", terminal: false}
|
|
60
|
+
else {node: n, subject: unparenthesised_node(n), kind: "plain", matches: op == "&&", terminal: false}
|
|
61
|
+
fn optional_falsy_literal(n) = {
|
|
62
|
+
value arms: value_constituents(n)
|
|
63
|
+
value any(arms, arm => any(arm, part => part.identity.kind == "value"
|
|
64
|
+
|| contains(part.traits, "boolean"))
|
|
65
|
+
&& any(arm, part => (contains(part.traits, "falsy") || contains(part.traits, "boolean") && !contains(part.traits, "truthy"))
|
|
66
|
+
&& any(part.traits, trait => contains(["boolean", "number", "string", "bigint"], trait))))
|
|
67
|
+
}
|
|
68
|
+
# A computed type is not needed when the value is an unresolved global or reads
|
|
69
|
+
# as `any`; a call whose callee is understood is likewise treated as `any`.
|
|
70
|
+
# Any other uncomputable operand cannot prove the chain is safe.
|
|
71
|
+
fn optional_type_allowed(n, opts) = {
|
|
72
|
+
value arms: value_trait_parts(n, ["void", "any", "unknown", "string", "number", "boolean", "bigint"])
|
|
73
|
+
value if arms == null then optional_opaque_any(n) || n is ts_call && value_parts(n.callee.node) != null else
|
|
74
|
+
!any(arms, arm => any(arm.parts, part => contains(part.traits, "void")))
|
|
75
|
+
&& all(["any", "unknown", "string", "number", "boolean", "bigint"], trait =>
|
|
76
|
+
get(opts, trait) || !any(arms, arm => any(arm.parts, part => contains(part.traits, trait))))
|
|
77
|
+
}
|
|
78
|
+
fn optional_value_root(n) = if n is (ts_parenthesized, ts_non_null) then optional_value_root(n.expression.node)
|
|
79
|
+
else if n is ts_await then optional_value_root(n.argument.node)
|
|
80
|
+
else if n is (ts_member, ts_index) then optional_value_root(n.object.node)
|
|
81
|
+
else if n is ts_call then optional_value_root(n.callee.node)
|
|
82
|
+
else n
|
|
83
|
+
# Unresolved globals, `this` and `import.meta` read as `any`, which includes every trait.
|
|
84
|
+
fn optional_opaque_any(n) = value_parts(n) == null && {
|
|
85
|
+
value root: optional_value_root(n)
|
|
86
|
+
value root is (ts_this, ts_meta_property) || root is ts_identifier && root.resolved == "global"
|
|
87
|
+
}
|
|
88
|
+
fn optional_uncertain(n, trait) = {
|
|
89
|
+
value arms: value_trait_parts(n, [trait, "any", "unknown"])
|
|
90
|
+
value arms == null || any(arms, arm => any(arm.parts, part =>
|
|
91
|
+
contains(part.traits, trait) || contains(part.traits, "any") || contains(part.traits, "unknown")
|
|
92
|
+
|| contains(part.uncertain_traits, trait)))
|
|
93
|
+
}
|
|
94
|
+
# Like `optional_uncertain`, but an uncomputable answer is only treated as
|
|
95
|
+
# `any` when it comes from an unresolved global.
|
|
96
|
+
fn optional_may(n, trait) = {
|
|
97
|
+
value arms: value_trait_parts(n, [trait, "any", "unknown"])
|
|
98
|
+
value if arms == null then optional_opaque_any(n) else any(arms, arm => any(arm.parts, part =>
|
|
99
|
+
contains(part.traits, trait) || contains(part.traits, "any") || contains(part.traits, "unknown")
|
|
100
|
+
|| contains(part.uncertain_traits, trait)))
|
|
101
|
+
}
|
|
102
|
+
fn optional_arms_trait(arms, trait) = arms == null || any(arms, arm => any(arm, part => contains(part.traits, trait)))
|
|
103
|
+
fn optional_result_trait(call, trait) = {
|
|
104
|
+
value binding: call_binding(call)
|
|
105
|
+
value if binding == null then true else any(binding.choices, choice =>
|
|
106
|
+
choice.result == null || optional_may(choice.result, trait))
|
|
107
|
+
}
|
|
108
|
+
fn optional_hop_trait(hop, trait) = if hop is ts_call then optional_result_trait(hop, trait)
|
|
109
|
+
else if hop is ts_member && hop.property not ts_private_name then
|
|
110
|
+
optional_arms_trait(value_member(hop.object.node, hop.property.text), trait)
|
|
111
|
+
else if hop is ts_index then optional_arms_trait(value_member(hop.object.node, hop.index.node), trait)
|
|
112
|
+
else false
|
|
113
|
+
fn optional_linked(n) = { value n; walk (m) optional_base(m);
|
|
114
|
+
any (m) m is (ts_member, ts_index, ts_call) && m.optional != null }
|
|
115
|
+
# `undefined` that an optional link adds is synthetic: an earlier guard already
|
|
116
|
+
# settled the link's receiver, so TypeScript narrows it away at this operand.
|
|
117
|
+
# Undefined the link's own target declares (a call result, a member) is real.
|
|
118
|
+
fn optional_link_trait(n, trait) = { value n; walk (m) optional_base(m);
|
|
119
|
+
any (m) m is (ts_member, ts_index, ts_call) && m.optional != null && optional_hop_trait(m, trait) }
|
|
120
|
+
fn optional_may_real(n, trait) = optional_may(n, trait)
|
|
121
|
+
&& (!optional_linked(n) || optional_link_trait(n, trait))
|
|
122
|
+
fn optional_nullish_only(n, allow_null) = {
|
|
123
|
+
value arms: value_trait_parts(n, ["undefined", "null"])
|
|
124
|
+
value arms != null && count(arms) > 0 && all(arms, arm => any(arm.parts, part =>
|
|
125
|
+
contains(part.traits, "undefined") || allow_null && contains(part.traits, "null")))
|
|
126
|
+
}
|
|
127
|
+
fn optional_span(items, opts) = {
|
|
128
|
+
value head: first(items).subject
|
|
129
|
+
value tail: last(items).subject
|
|
130
|
+
value if len(items) > 1 && !optional_same(head, tail)
|
|
131
|
+
&& (!opts.requireNullish || { value items; slice 0 -1; any (item) item.subject could nullable }) then {
|
|
132
|
+
start: if { value prefix: first(items).node.offset - opts.boundary.offset; value opts.boundary; byte_slice 0 prefix; re_test /^(?:\s|\(|\/\*[\s\S]*?\*\/|\/\/[^\n]*\n)*$/ }
|
|
133
|
+
then opts.boundary.offset else first(items).node.offset,
|
|
134
|
+
end: last(items).end + { value suffix: last(items).end - opts.boundary.offset; value opts.boundary; byte_slice suffix;
|
|
135
|
+
re_find /^(?:(?:\s|\/\*[\s\S]*?\*\/|\/\/[^\n]*\n)*\))+/; then (closers) byte_len(closers ?? "") }
|
|
136
|
+
} else null
|
|
137
|
+
}
|
|
138
|
+
fn optional_flush(state, opts) = {
|
|
139
|
+
value span: optional_span(state.items, opts)
|
|
140
|
+
value if span != null then state.reports [{span: span, items: state.items}] else state.reports
|
|
141
|
+
}
|
|
142
|
+
fn optional_group(current, next) = current.matches && next.matches && contains(["null", "undefined"], current.kind)
|
|
143
|
+
&& contains(["null", "undefined"], next.kind) && current.kind != next.kind && optional_same(current.subject, next.subject)
|
|
144
|
+
fn optional_valid(current, next, op, last, opts) = current.subject != null && current.matches && (
|
|
145
|
+
if contains(["plain", "not"], current.kind) then last || optional_type_allowed(current.subject, opts)
|
|
146
|
+
&& !(current.kind == (if op == "&&" then "plain" else "not") && optional_falsy_literal(current.subject))
|
|
147
|
+
else current.kind == "nullish" || optional_group(current, next)
|
|
148
|
+
|| current.kind == "null" && (op == "||" || next != null) && !optional_may_real(current.subject, "undefined")
|
|
149
|
+
|| current.kind == "undefined" && !optional_may_real(current.subject, "null"))
|
|
150
|
+
fn optional_terminal_allowed(n, comparison, op) = if comparison == (if op == "&&" then "===" else "!==") then !optional_may(n, "undefined")
|
|
151
|
+
else if comparison == (if op == "&&" then "==" else "!=") then !optional_may(n, "null") && !optional_may(n, "undefined")
|
|
152
|
+
else if comparison == (if op == "&&" then "!==" else "===") then optional_nullish_only(n, false)
|
|
153
|
+
else if comparison == (if op == "&&" then "!=" else "==") then optional_nullish_only(n, true) else false
|
|
154
|
+
fn optional_item(current) = {node: current.node, subject: current.subject, kind: current.kind, yoda: current.yoda,
|
|
155
|
+
terminal: current.terminal, end: current.node.end_offset}
|
|
156
|
+
fn optional_member_based(n) = {
|
|
157
|
+
value bare: unparenthesised_node(n)
|
|
158
|
+
value callee: if bare is ts_call then unparenthesised_node(bare.callee.node) else null
|
|
159
|
+
value bare.in_optional_chain != true && (bare is (ts_member, ts_index) || callee is (ts_member, ts_index))
|
|
160
|
+
}
|
|
161
|
+
fn optional_terminal(state, current, op, opts) = {
|
|
162
|
+
value previous: last(state.items).subject
|
|
163
|
+
value left: optional_member_based(current.node.left.node) && optional_extends(previous, current.node.left.node)
|
|
164
|
+
value right: optional_member_based(current.node.right.node) && optional_extends(previous, current.node.right.node)
|
|
165
|
+
value compared: if right then current.node.right.node else current.node.left.node
|
|
166
|
+
value other: if right then current.node.left.node else current.node.right.node
|
|
167
|
+
value accepted: len(state.items) > 0 && left != right && optional_terminal_allowed(other, current.node.operator, op)
|
|
168
|
+
value if accepted then optional_flush({items: state.items [{node: current.node, subject: compared, kind: "last",
|
|
169
|
+
yoda: right, terminal: true, end: current.node.end_offset}], reports: state.reports}, opts) else optional_flush(state, opts)
|
|
170
|
+
}
|
|
171
|
+
fn optional_accept(state, current, next, opts) = {
|
|
172
|
+
value grouped: optional_group(current, next)
|
|
173
|
+
value extends: len(state.items) > 0 && { value last(state.items).subject; optional_extends current.subject }
|
|
174
|
+
value same: len(state.items) > 0 && { value last(state.items).subject; optional_same current.subject }
|
|
175
|
+
# A paired seed retains both checks. Extending a chain retains only the first;
|
|
176
|
+
# the second check validates the guard but is not part of the replacement.
|
|
177
|
+
value seed: if grouped then [optional_item(current), optional_item(next)] else [optional_item(current)]
|
|
178
|
+
value if extends then {items: state.items [optional_item(current)], reports: state.reports, skip: grouped}
|
|
179
|
+
else if same then {items: state.items, reports: state.reports, skip: grouped}
|
|
180
|
+
else {items: seed, reports: optional_flush(state, opts), skip: grouped}
|
|
181
|
+
}
|
|
182
|
+
fn optional_step(state, current, next, op, last, opts) = if state.skip then {items: state.items, reports: state.reports, skip: false}
|
|
183
|
+
else if current.kind == "last" then {items: [], reports: optional_terminal(state, current, op, opts), skip: false}
|
|
184
|
+
else if optional_valid(current, next, op, last, opts) then optional_accept(state, current, next, opts)
|
|
185
|
+
else {
|
|
186
|
+
value extended: {items: state.items [{node: current.node, subject: current.subject, kind: current.kind,
|
|
187
|
+
yoda: current.yoda, terminal: true, end: current.node.end_offset}], reports: state.reports}
|
|
188
|
+
value {items: [], reports: if current.kind == "undefined" && len(state.items) > 0 && { value last(state.items).subject; optional_extends current.subject }
|
|
189
|
+
then optional_flush(extended, opts) else optional_flush(state, opts), skip: false}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
fn optional_part(n) = if n is (ts_member, ts_index) then {
|
|
193
|
+
text: if n is ts_index then "[" n.index.text "]" else n.property.text,
|
|
194
|
+
dot: n is ts_member, optional: n.optional != null, nonNull: n.object is ts_non_null
|
|
195
|
+
} else if n is ts_call then {
|
|
196
|
+
text: { value start: n.callee.end_offset - n.offset; value n; byte_slice start; trim_start; re_replace /^\?\.\s*/ "" },
|
|
197
|
+
dot: false, optional: n.optional != null, nonNull: false
|
|
198
|
+
} else {text: expression_fix_wrap(n.text, n.precedence < 19), dot: false, optional: false, nonNull: false}
|
|
199
|
+
fn optional_parts(n) = {
|
|
200
|
+
value n
|
|
201
|
+
walk (part) optional_base(part)
|
|
202
|
+
reverse
|
|
203
|
+
filter (part) part not (ts_parenthesized, ts_non_null)
|
|
204
|
+
map (part) optional_part(part)
|
|
205
|
+
}
|
|
206
|
+
fn optional_render_part(part, guarded) = (if guarded || part.optional then "?." else (if part.nonNull then "!" else "") (if part.dot then "." else "")) part.text
|
|
207
|
+
fn optional_render(items) = {
|
|
208
|
+
value items
|
|
209
|
+
fold ({count: 0, text: ""}) (result, item) {
|
|
210
|
+
value parts: optional_parts(item.subject)
|
|
211
|
+
value count: len(parts)
|
|
212
|
+
value appended: { value parts; slice result.count; map (part, index) optional_render_part(part, result.count > 0 && index == 0); join "" }
|
|
213
|
+
value {count: max(result.count, count), text: result.text appended}
|
|
214
|
+
}
|
|
215
|
+
then (result) result.text
|
|
216
|
+
}
|
|
217
|
+
fn optional_output(items) = {
|
|
218
|
+
value text: optional_render(items)
|
|
219
|
+
value terminal: last(items)
|
|
220
|
+
value compared: if terminal.yoda then terminal.node.right.node else terminal.node.left.node
|
|
221
|
+
value unary: if compared is ts_unary then compared.operator " " else ""
|
|
222
|
+
value if terminal.node is ts_binary then
|
|
223
|
+
if terminal.yoda then terminal.node.left.text " " terminal.node.operator " " unary text
|
|
224
|
+
else unary text " " terminal.node.operator " " terminal.node.right.text
|
|
225
|
+
else if terminal.kind == "not" then "!" text else text
|
|
226
|
+
}
|
|
227
|
+
# A call whose signature and receiver the engine cannot read together (lib
|
|
228
|
+
# `Function`, unread imported types) answers TypeScript's error type `any`.
|
|
229
|
+
fn optional_opaque_member_call(n) = n is ts_call && value_parts(n) == null
|
|
230
|
+
&& n.callee.node is (ts_member, ts_index) && value_parts(n.callee.node.object.node) != null
|
|
231
|
+
fn optional_safe(items, op, unsafe) = {
|
|
232
|
+
value terminal: last(items)
|
|
233
|
+
# A fix adds `undefined` to the chain's value; that changes nothing when an
|
|
234
|
+
# operand already carries it (`any` includes `undefined`).
|
|
235
|
+
value already_undefined: any(items, item =>
|
|
236
|
+
optional_may_real(item.node, "undefined") || optional_opaque_member_call(item.node))
|
|
237
|
+
value unsafe || !terminal.terminal && (contains(["nullish", "undefined"], terminal.kind)
|
|
238
|
+
|| op == "||" && terminal.kind == "not" || already_undefined)
|
|
239
|
+
}
|
|
240
|
+
fn optional_tail(source, span, boundary) = {
|
|
241
|
+
# Paired code tokens, not character counts: parentheses in comments and strings
|
|
242
|
+
# cannot consume or manufacture an unmatched delimiter.
|
|
243
|
+
value removed: { value overlapping(ts_close_paren, span.end, boundary.end_offset);
|
|
244
|
+
filter (token) token.paired_token.start >= span.start && token.paired_token.start < span.end }
|
|
245
|
+
value removed
|
|
246
|
+
fold ({offset: span.end, text: ""}) (tail, token) {
|
|
247
|
+
text: tail.text byte_slice(source, tail.offset, token.offset), offset: token.end_offset
|
|
248
|
+
}
|
|
249
|
+
then (tail) tail.text byte_slice(source, tail.offset, boundary.end_offset)
|
|
250
|
+
}
|
|
251
|
+
fn optional_fix_output(boundary, target, text) = {
|
|
252
|
+
value head_node: node_at(target.start)
|
|
253
|
+
value tail_node: node_at(target.end - 1)
|
|
254
|
+
value edges: {offset: target.start, end_offset: target.end,
|
|
255
|
+
token_neighbours: {before: head_node.token_neighbours.before, after: tail_node.token_neighbours.after}}
|
|
256
|
+
value statement: if target.start == boundary.offset then expression_fix_statement(boundary) else null
|
|
257
|
+
value head: { value text; trim_start; slice 0 1 }
|
|
258
|
+
value owners: expression_fix_before_nodes(boundary)
|
|
259
|
+
value asi: if statement != null && contains(["(", "[", "/", "`"], head)
|
|
260
|
+
then expression_fix_asi(statement, boundary.token_neighbours.before, owners, text) else false
|
|
261
|
+
value if asi == null then null else {target: target,
|
|
262
|
+
text: (if asi then ";" else "") expression_fix_edges(edges, text)}
|
|
263
|
+
}
|
|
264
|
+
fn optional_edit(source, report, boundary) = {
|
|
265
|
+
value text: optional_output(report.items)
|
|
266
|
+
value open: { value overlapping(ts_open_paren, report.span.start, report.span.end);
|
|
267
|
+
any (token) token.paired_token.end > report.span.end && token.paired_token.end <= boundary.end_offset }
|
|
268
|
+
value target: if open then {start: report.span.start, end: boundary.end_offset} else report.span
|
|
269
|
+
value replacement: if open then text optional_tail(source, report.span, boundary) else text
|
|
270
|
+
value optional_fix_output(boundary, target, replacement)
|
|
271
|
+
}
|
|
272
|
+
fn optional_report(source, report, boundary, op, unsafe) = {
|
|
273
|
+
span: report.span, edit: if optional_safe(report.items, op, unsafe) then optional_edit(source, report, boundary) else null
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
in ts_logical rule es-prefer-optional-chain {
|
|
277
|
+
option checkAny boolean = true "Check any values"
|
|
278
|
+
option checkUnknown boolean = true "Check unknown values"
|
|
279
|
+
option checkString boolean = true "Check string values"
|
|
280
|
+
option checkNumber boolean = true "Check number values"
|
|
281
|
+
option checkBoolean boolean = true "Check boolean values"
|
|
282
|
+
option checkBigInt boolean = true "Check bigint values"
|
|
283
|
+
option requireNullish boolean = false "Require a nullable checked operand"
|
|
284
|
+
option allowPotentiallyUnsafeFixesThatModifyTheReturnTypeIKnowWhatImDoing boolean = false "Allow fixes that may add undefined to the return type"
|
|
285
|
+
let enclosing { value node; walk (n) if n.parent is ts_parenthesized then n.parent.node else null; last }
|
|
286
|
+
when operator != "??" && !(enclosing.parent is ts_logical && enclosing.parent.operator == operator)
|
|
287
|
+
let opts {any: checkAny, unknown: checkUnknown, string: checkString, number: checkNumber, boolean: checkBoolean,
|
|
288
|
+
bigint: checkBigInt, requireNullish: requireNullish, boundary: node}
|
|
289
|
+
let operands { value optional_operands; map (operand) { value operand; unparenthesised_node; optional_operand operator } }
|
|
290
|
+
let state {
|
|
291
|
+
value following: { value operands; slice 1 } [null]
|
|
292
|
+
value operands
|
|
293
|
+
zip following
|
|
294
|
+
fold ({items: [], reports: [], skip: false}) (state, pair) {
|
|
295
|
+
value current: first(pair)
|
|
296
|
+
value next: last(pair)
|
|
297
|
+
value optional_step(state, current, next, operator, next == null, opts)
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
each chain in { value state; optional_flush opts;
|
|
301
|
+
map (report) optional_report(source, report, node, operator, allowPotentiallyUnsafeFixesThatModifyTheReturnTypeIKnowWhatImDoing) } {
|
|
302
|
+
report at chain.span
|
|
303
|
+
warn "Prefer using an optional chain expression instead, as it's more concise and easier to read."
|
|
304
|
+
fix at chain.edit.target if chain.edit != null as chain.edit.text
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
in ts_logical rule es-prefer-optional-chain:empty-object {
|
|
308
|
+
option requireNullish boolean = false "Require a nullable checked operand"
|
|
309
|
+
when contains(["||", "??"], operator) && unparenthesised_node(right.node) is ts_object && { value unparenthesised_node(right.node).property.nodes; count } == 0
|
|
310
|
+
let enclosing { value node; walk (n) if n.parent is ts_parenthesized then n.parent.node else null; last }
|
|
311
|
+
when enclosing.parent is (ts_member, ts_index) && enclosing.parent.optional == null
|
|
312
|
+
when !requireNullish || left could nullable
|
|
313
|
+
report at enclosing.parent
|
|
314
|
+
warn "Prefer using an optional chain expression instead, as it's more concise and easier to read."
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
}
|