@inthedark/dxlint 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +84 -2
- package/bin/dxlint +15 -0
- package/bin/dxlint-linux-x64 +0 -0
- package/bin/dxlint-win32-x64.exe +0 -0
- package/package.json +40 -4
- package/rules/conventions.dx +34 -0
- package/rules/correctness.dx +33 -0
- package/rules/es-no-semi.dx +11 -0
- package/rules/format.dx +49 -0
- package/rules/recommended.dx +51 -0
- package/rules/suspicious.dx +31 -0
- package/rules/types.dx +21 -0
- package/std/c.dx +23 -0
- package/std/core/lines.dx +51 -0
- package/std/core/lists.dx +76 -0
- package/std/core.dx +30 -0
- package/std/css.dx +17 -0
- package/std/dx.dx +31 -0
- package/std/json.dx +104 -0
- package/std/markdown.dx +11 -0
- package/std/python.dx +21 -0
- package/std/rust/format.dx +316 -0
- package/std/rust.dx +34 -0
- package/std/toml.dx +19 -0
- package/std/ts/arithmetic.dx +87 -0
- package/std/ts/assertions.dx +477 -0
- package/std/ts/calls.dx +246 -0
- package/std/ts/comments.dx +10 -0
- package/std/ts/conditions.dx +531 -0
- package/std/ts/constants.dx +335 -0
- package/std/ts/deprecation.dx +185 -0
- package/std/ts/directives.dx +60 -0
- package/std/ts/effects.dx +219 -0
- package/std/ts/enums.dx +233 -0
- package/std/ts/expression-fixes.dx +67 -0
- package/std/ts/expressions.dx +270 -0
- package/std/ts/flow.dx +2 -0
- package/std/ts/format.dx +755 -0
- package/std/ts/globals.dx +106 -0
- package/std/ts/grammar.dx +488 -0
- package/std/ts/helpers.dx +102 -0
- package/std/ts/import-attributes.dx +88 -0
- package/std/ts/import-no-deprecated.dx +73 -0
- package/std/ts/indentation.dx +442 -0
- package/std/ts/jsx.dx +646 -0
- package/std/ts/layout/blocks.dx +281 -0
- package/std/ts/layout/calls.dx +79 -0
- package/std/ts/layout/expressions.dx +213 -0
- package/std/ts/layout/literals.dx +114 -0
- package/std/ts/layout/separators.dx +184 -0
- package/std/ts/layout/shared.dx +203 -0
- package/std/ts/layout/text.dx +142 -0
- package/std/ts/layout/types.dx +182 -0
- package/std/ts/layout.dx +9 -0
- package/std/ts/license.dx +54 -0
- package/std/ts/literal-text.dx +3 -0
- package/std/ts/literals.dx +95 -0
- package/std/ts/methods.dx +69 -0
- package/std/ts/modules.dx +268 -0
- package/std/ts/names.dx +348 -0
- package/std/ts/naming.dx +169 -0
- package/std/ts/narrowing.dx +96 -0
- package/std/ts/parentheses.dx +262 -0
- package/std/ts/precedence.dx +25 -0
- package/std/ts/problems/calls.dx +34 -0
- package/std/ts/problems/classes.dx +39 -0
- package/std/ts/problems/control-flow.dx +257 -0
- package/std/ts/problems/declarations.dx +67 -0
- package/std/ts/problems/duplicates.dx +70 -0
- package/std/ts/problems/expressions.dx +154 -0
- package/std/ts/problems/helpers.dx +22 -0
- package/std/ts/problems/imports.dx +43 -0
- package/std/ts/problems/non-null.dx +78 -0
- package/std/ts/problems/numeric-precision.dx +66 -0
- package/std/ts/problems/regexp.dx +66 -0
- package/std/ts/problems/statements.dx +36 -0
- package/std/ts/problems/typescript.dx +55 -0
- package/std/ts/problems/whitespace.dx +40 -0
- package/std/ts/problems.dx +20 -0
- package/std/ts/regex/checks.dx +249 -0
- package/std/ts/regex/findings.dx +209 -0
- package/std/ts/regex/grammar.dx +80 -0
- package/std/ts/regex/suggestions.dx +94 -0
- package/std/ts/regex/values.dx +378 -0
- package/std/ts/regex.dx +5 -0
- package/std/ts/restrictions/comments.dx +76 -0
- package/std/ts/restrictions.dx +811 -0
- package/std/ts/scopes.dx +68 -0
- package/std/ts/scoping/atomic.dx +40 -0
- package/std/ts/scoping/bindings.dx +126 -0
- package/std/ts/scoping/constness.dx +72 -0
- package/std/ts/scoping/globals.dx +52 -0
- package/std/ts/scoping/loops.dx +86 -0
- package/std/ts/scoping/member-uses.dx +111 -0
- package/std/ts/scoping/receivers.dx +107 -0
- package/std/ts/scoping/shadowing.dx +202 -0
- package/std/ts/scoping/usage.dx +393 -0
- package/std/ts/scoping.dx +842 -0
- package/std/ts/selectors.dx +10 -0
- package/std/ts/spreads.dx +97 -0
- package/std/ts/structure/declarations.dx +52 -0
- package/std/ts/structure/lines.dx +79 -0
- package/std/ts/structure/lists.dx +144 -0
- package/std/ts/structure/padding.dx +258 -0
- package/std/ts/structure.dx +963 -0
- package/std/ts/style/classes.dx +157 -0
- package/std/ts/style/comments.dx +75 -0
- package/std/ts/style/declarations.dx +82 -0
- package/std/ts/style/expressions.dx +125 -0
- package/std/ts/style/imports.dx +44 -0
- package/std/ts/style/ordering.dx +192 -0
- package/std/ts/style/returns.dx +152 -0
- package/std/ts/style/signatures.dx +308 -0
- package/std/ts/style/type-exports.dx +62 -0
- package/std/ts/style/type-imports.dx +166 -0
- package/std/ts/style/types.dx +474 -0
- package/std/ts/style.dx +29 -0
- package/std/ts/suggestions/accessors.dx +138 -0
- package/std/ts/suggestions/builtins.dx +110 -0
- package/std/ts/suggestions/comparisons.dx +41 -0
- package/std/ts/suggestions/computed-keys.dx +20 -0
- package/std/ts/suggestions/constructors.dx +54 -0
- package/std/ts/suggestions/control-flow.dx +204 -0
- package/std/ts/suggestions/curly.dx +124 -0
- package/std/ts/suggestions/directives.dx +79 -0
- package/std/ts/suggestions/errors.dx +106 -0
- package/std/ts/suggestions/expressions.dx +230 -0
- package/std/ts/suggestions/functions.dx +260 -0
- package/std/ts/suggestions/globals.dx +187 -0
- package/std/ts/suggestions/helpers.dx +17 -0
- package/std/ts/suggestions/object-shorthand.dx +148 -0
- package/std/ts/suggestions/object-spread.dx +96 -0
- package/std/ts/suggestions/regexp.dx +59 -0
- package/std/ts/suggestions/renames.dx +50 -0
- package/std/ts/suggestions/strings.dx +47 -0
- package/std/ts/suggestions/templates.dx +104 -0
- package/std/ts/suggestions.dx +24 -0
- package/std/ts/template-fixes.dx +22 -0
- package/std/ts/tokens.dx +124 -0
- package/std/ts/type-specifiers.dx +93 -0
- package/std/ts/typed/assignments.dx +154 -0
- package/std/ts/typed/awaitability.dx +157 -0
- package/std/ts/typed/boolean-comparisons.dx +82 -0
- package/std/ts/typed/defaults.dx +97 -0
- package/std/ts/typed/enum-consistency.dx +68 -0
- package/std/ts/typed/enums.dx +52 -0
- package/std/ts/typed/errors.dx +32 -0
- package/std/ts/typed/floating-promises.dx +147 -0
- package/std/ts/typed/for-in-arrays.dx +21 -0
- package/std/ts/typed/includes.dx +102 -0
- package/std/ts/typed/nullish-coalescing.dx +149 -0
- package/std/ts/typed/optional-chains.dx +317 -0
- package/std/ts/typed/promises.dx +205 -0
- package/std/ts/typed/qualifiers.dx +108 -0
- package/std/ts/typed/readonly-members.dx +159 -0
- package/std/ts/typed/readonly-parameters.dx +121 -0
- package/std/ts/typed/redundant-constituents.dx +171 -0
- package/std/ts/typed/regexp-exec.dx +79 -0
- package/std/ts/typed/rejection-handlers.dx +91 -0
- package/std/ts/typed/return-await.dx +116 -0
- package/std/ts/typed/return-this.dx +67 -0
- package/std/ts/typed/strict-void-return.dx +121 -0
- package/std/ts/typed/string-boundaries.dx +153 -0
- package/std/ts/typed/switches.dx +147 -0
- package/std/ts/typed/template-expressions.dx +127 -0
- package/std/ts/typed/template-values.dx +88 -0
- package/std/ts/typed/transfers.dx +106 -0
- package/std/ts/typed/type-arguments.dx +337 -0
- package/std/ts/typed/type-constituents.dx +143 -0
- package/std/ts/typed/type-conversions.dx +82 -0
- package/std/ts/typed/type-parameters.dx +280 -0
- package/std/ts/typed/unbound-method.dx +167 -0
- package/std/ts/typed/unsafe-calls.dx +73 -0
- package/std/ts/typed/unsafe-members.dx +36 -0
- package/std/ts/typed/void-expressions.dx +101 -0
- package/std/ts/typed.dx +585 -0
- package/std/ts/values/arguments.dx +13 -0
- package/std/ts/values/enums.dx +104 -0
- package/std/ts/values/inspection.dx +274 -0
- package/std/ts/values.dx +1217 -0
- package/std/ts.dx +77 -0
- package/std/whitespace.dx +37 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
define {
|
|
2
|
+
|
|
3
|
+
fn for_in_arraylike_value(handle) = {
|
|
4
|
+
value index: value_lookup(handle, 0)
|
|
5
|
+
value any(index.alternatives, alternative => alternative.matched && any(alternative.sources, source =>
|
|
6
|
+
source.kind == "indexer" && source.domain == "ordinal" || source.kind == "repeated" || source.kind == "ordinal"))
|
|
7
|
+
&& {
|
|
8
|
+
value length: value_constituent_traits(value_lookup(handle, "length").member, ["number"])
|
|
9
|
+
value length != null && count(length) > 0
|
|
10
|
+
&& all(length, parts => any(parts, part => contains(part.traits, "number")))
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
fn for_in_arraylike(arm) = any(arm.parts, part => contains(part.traits, "object") || contains(part.traits, "structural"))
|
|
14
|
+
&& (for_in_arraylike_value(arm.handle) || any(arm.parts, part => for_in_arraylike_value(part.handle)))
|
|
15
|
+
|
|
16
|
+
in ts_for_in rule es-no-for-in-array {
|
|
17
|
+
when any(value_trait_parts(right.node, ["object", "structural"]), arm => for_in_arraylike(arm))
|
|
18
|
+
report at { start: offset, end: opener.ts_open_paren.paired_token.end }
|
|
19
|
+
warn "For-in loops over arrays skips holes, returns indices as strings, and may visit the prototype chain or other enumerable properties. Use a more robust iteration method such as for-of or array.forEach instead."
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import .ts/typed/string-boundaries
|
|
2
|
+
import .ts/constants
|
|
3
|
+
|
|
4
|
+
define {
|
|
5
|
+
|
|
6
|
+
fn includes_compatible(receiver, optional) = if string_boundary_receiver(receiver, optional) then true else {
|
|
7
|
+
value index_arms: member_call_parameters(receiver, "indexOf")
|
|
8
|
+
value includes_arms: member_call_parameters(receiver, "includes")
|
|
9
|
+
value index: flat_map(index_arms, arm => arm)
|
|
10
|
+
value includes: flat_map(includes_arms, arm => arm)
|
|
11
|
+
value index_arms != null && includes_arms != null && len(index) > 0 && len(includes) > 0
|
|
12
|
+
&& all(index_arms, arm => len(arm) > 0) && all(includes_arms, arm => len(arm) > 0)
|
|
13
|
+
&& all(index, signature => signature.parameters_known)
|
|
14
|
+
&& all(includes, signature => signature.parameters_known)
|
|
15
|
+
&& all(index, signature => all(includes, other => signature.parameters == other.parameters))
|
|
16
|
+
}
|
|
17
|
+
fn includes_index_call(n) = {
|
|
18
|
+
value call: unparenthesised_node(n)
|
|
19
|
+
value method: unparenthesised_node(call.callee.node)
|
|
20
|
+
value optional: method.optional != null || call.optional != null
|
|
21
|
+
value if call not ts_call || !collection_method(method, "indexOf") || count(call.argument) < 1
|
|
22
|
+
|| !includes_compatible(method.object.node, optional) then null else {
|
|
23
|
+
call: call, method: method, optional: optional
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
fn includes_negative(operator, threshold) =
|
|
27
|
+
if threshold == -1 && contains(["!==", "!=", ">"], operator) || threshold == 0 && operator == ">=" then false
|
|
28
|
+
else if threshold == -1 && contains(["===", "==", "<="], operator) || threshold == 0 && operator == "<" then true
|
|
29
|
+
else null
|
|
30
|
+
fn includes_replacement(source, n, result, negative, leading) = {
|
|
31
|
+
value shape: {
|
|
32
|
+
kind: if negative then "ts_unary" else "ts_call",
|
|
33
|
+
precedence: if negative then 15 else 19,
|
|
34
|
+
offset: n.offset, structure_parens_hazard: !negative && leading.structure_parens_hazard,
|
|
35
|
+
structure_parens_iife: false, structure_parens_has_in: false,
|
|
36
|
+
structure_parens_has_call: true, structure_parens_optional: false
|
|
37
|
+
}
|
|
38
|
+
value required: expression_fix_parens(source, n, shape)
|
|
39
|
+
value if required == null || !expression_fix_retains_comments(n, result) then null
|
|
40
|
+
else expression_fix_edges(n, expression_fix_wrap(result, required > 0))
|
|
41
|
+
}
|
|
42
|
+
fn includes_index_replacement(source, n, info, negative) = {
|
|
43
|
+
value property: if info.method is ts_member then info.method.property.node else info.method.index.node
|
|
44
|
+
value call: expression_fix_edits(info.call.text, info.call.offset, [{
|
|
45
|
+
start: property.offset, end: property.end_offset,
|
|
46
|
+
text: if info.method is ts_member then "includes" else "'includes'"
|
|
47
|
+
}])
|
|
48
|
+
value result: (if negative then "!" else "") call
|
|
49
|
+
value if info.optional then null else includes_replacement(source, n, result, negative, info.call)
|
|
50
|
+
}
|
|
51
|
+
fn includes_regex(n) = {
|
|
52
|
+
value pattern: collection_value_node(n)
|
|
53
|
+
value syntax: pattern.regexp_syntax
|
|
54
|
+
value root: first(syntax)
|
|
55
|
+
value flags: pattern.regexp_input.flags
|
|
56
|
+
value alternatives: filter(syntax, part => part.kind == "alternative" && part.parent == root.id)
|
|
57
|
+
value characters: filter(syntax, term => term.parent == first(alternatives).id)
|
|
58
|
+
value if syntax == null || type_of(flags) != "str" || re_test(flags, /[gimy]/)
|
|
59
|
+
|| len(alternatives) != 1 || len(characters) == 0
|
|
60
|
+
|| !all(characters, term => term.kind == "character" && term.character != null) then null
|
|
61
|
+
else { value characters; map (term) term.character; join "" }
|
|
62
|
+
}
|
|
63
|
+
fn includes_quote(text) = "'" {
|
|
64
|
+
value text
|
|
65
|
+
re_replace /\\/ "\\\\"
|
|
66
|
+
re_replace /'/ "\\'"
|
|
67
|
+
re_replace /\u{0}([0-9])/ "\\x00$1"
|
|
68
|
+
re_replace /\u{0}/ "\\0"
|
|
69
|
+
re_replace /\n/ "\\n"
|
|
70
|
+
re_replace /\r/ "\\r"
|
|
71
|
+
re_replace /\u{b}/ "\\v"
|
|
72
|
+
re_replace /\t/ "\\t"
|
|
73
|
+
re_replace /\u{c}/ "\\f"
|
|
74
|
+
re_replace /\u{2028}/ "\\u2028"
|
|
75
|
+
re_replace /\u{2029}/ "\\u2029"
|
|
76
|
+
} "'"
|
|
77
|
+
|
|
78
|
+
in ts_binary rule es-prefer-includes {
|
|
79
|
+
let negative includes_negative(operator.text, ts_runtime_number(right.node))
|
|
80
|
+
when negative != null
|
|
81
|
+
let info includes_index_call(left.node)
|
|
82
|
+
when info != null
|
|
83
|
+
warn "Use 'includes()' method instead."
|
|
84
|
+
let replacement includes_index_replacement(source, node, info, negative)
|
|
85
|
+
fix if replacement != null as replacement
|
|
86
|
+
}
|
|
87
|
+
in ts_call rule es-prefer-includes:regex {
|
|
88
|
+
when count(argument) == 1
|
|
89
|
+
let method unparenthesised_node(callee.node)
|
|
90
|
+
when collection_method(method, "test")
|
|
91
|
+
let receiver first(argument.nodes)
|
|
92
|
+
when string_boundary_receiver(receiver, false)
|
|
93
|
+
let pattern_text includes_regex(method.object.node)
|
|
94
|
+
when pattern_text != null
|
|
95
|
+
warn "Use `String#includes()` method with a string instead."
|
|
96
|
+
let result expression_fix_wrap(receiver.text, receiver.precedence < 19) ".includes(" includes_quote(pattern_text) ")"
|
|
97
|
+
let replacement if method.optional != null || optional != null then null
|
|
98
|
+
else includes_replacement(source, node, result, false, method)
|
|
99
|
+
fix if replacement != null as replacement
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Nullish replacements, stable references and selection policy.
|
|
2
|
+
import .ts/values/inspection
|
|
3
|
+
import .ts/modules
|
|
4
|
+
import .ts/conditions
|
|
5
|
+
|
|
6
|
+
define {
|
|
7
|
+
|
|
8
|
+
group nullish_coalescing_expression ts_logical, ts_assignment, ts_conditional, ts_if
|
|
9
|
+
|
|
10
|
+
fn nullish_reference_chain(n) = {
|
|
11
|
+
value unparenthesised_node(n)
|
|
12
|
+
walk (current) if current is (ts_member, ts_index) then unparenthesised_node(current.object.node) else null
|
|
13
|
+
}
|
|
14
|
+
fn nullish_reference_parts(chain) = {
|
|
15
|
+
value chain
|
|
16
|
+
map (part) if part is (ts_identifier, ts_this, ts_super) then { kind: "root", value: part.text }
|
|
17
|
+
else if part.static_name != null then { kind: "static", value: part.static_name }
|
|
18
|
+
else { kind: "computed", value: part.index.token_texts }
|
|
19
|
+
}
|
|
20
|
+
fn nullish_same_reference(a, b) = {
|
|
21
|
+
value chain_a: nullish_reference_chain(a)
|
|
22
|
+
value chain_b: nullish_reference_chain(b)
|
|
23
|
+
value { value chain_a; all (part) part is (ts_identifier, ts_this, ts_super, ts_index) || part is ts_member && part.static_name != null }
|
|
24
|
+
&& { value chain_b; all (part) part is (ts_identifier, ts_this, ts_super, ts_index) || part is ts_member && part.static_name != null }
|
|
25
|
+
&& nullish_reference_parts(chain_a) == nullish_reference_parts(chain_b)
|
|
26
|
+
}
|
|
27
|
+
fn nullish_unknown(n) = {
|
|
28
|
+
value ts_value_opaque(n) || ts_value_has_trait(n, "any") == true || ts_value_has_trait(n, "unknown") == true
|
|
29
|
+
}
|
|
30
|
+
fn nullish_comparison(n) = if n is ts_binary && contains(["==", "===", "!=", "!=="], n.operator)
|
|
31
|
+
then { operator: n.operator, operands: [unparenthesised_node(n.left.node), unparenthesised_node(n.right.node)] } else null
|
|
32
|
+
fn nullish_comparisons(n) = {
|
|
33
|
+
value direct: nullish_comparison(n)
|
|
34
|
+
value if direct != null then direct else if n is ts_logical && contains(["&&", "||"], n.operator) then {
|
|
35
|
+
value a: { value n.left.node; unparenthesised_node; nullish_comparison }
|
|
36
|
+
value b: { value n.right.node; unparenthesised_node; nullish_comparison }
|
|
37
|
+
value if a == null || b == null
|
|
38
|
+
|| { value a.operands; all (part) logical_nullish(part) != null }
|
|
39
|
+
|| { value b.operands; all (part) logical_nullish(part) != null } then null
|
|
40
|
+
else if n.operator == "||" && contains(["==", "==="], a.operator) && contains(["==", "==="], b.operator)
|
|
41
|
+
then { operator: if a.operator == "==" || b.operator == "==" then "==" else "===", operands: a.operands b.operands }
|
|
42
|
+
else if n.operator == "&&" && contains(["!=", "!=="], a.operator) && contains(["!=", "!=="], b.operator)
|
|
43
|
+
then { operator: if a.operator == "!=" || b.operator == "!=" then "!=" else "!==", operands: a.operands b.operands }
|
|
44
|
+
else null
|
|
45
|
+
} else null
|
|
46
|
+
}
|
|
47
|
+
fn nullish_replacement(test, branch) = {
|
|
48
|
+
value comparison: nullish_comparisons(test)
|
|
49
|
+
value if comparison == null then {
|
|
50
|
+
value subject: if test is ts_unary && test.operator == "!" then unparenthesised_node(test.argument.node) else test
|
|
51
|
+
value if nullish_same_reference(subject, branch) then { subject: subject, truthiness: true } else null
|
|
52
|
+
} else {
|
|
53
|
+
value subject: { value comparison.operands; find (part) logical_nullish(part) == null }
|
|
54
|
+
value checks_null: { value comparison.operands; any (part) logical_nullish(part) == false }
|
|
55
|
+
value checks_undefined: { value comparison.operands; any (part) logical_nullish(part) == true }
|
|
56
|
+
value uncovered: if checks_null then "undefined" else "null"
|
|
57
|
+
value if subject == null || !(checks_null || checks_undefined)
|
|
58
|
+
|| !{ value comparison.operands; all (part) logical_nullish(part) != null || nullish_same_reference(part, branch) }
|
|
59
|
+
then null
|
|
60
|
+
else if checks_null && checks_undefined || contains(["==", "!="], comparison.operator)
|
|
61
|
+
then { subject: subject, truthiness: false }
|
|
62
|
+
else if nullish_unknown(subject) || ts_value_has_trait(subject, uncovered) == true
|
|
63
|
+
then null else { subject: subject, truthiness: false }
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
fn nullish_subject(n) = if n is ts_logical && n.operator == "||" || n is ts_assignment && n.operator == "||="
|
|
67
|
+
then { subject: n.left.node, truthiness: true, kind: if n is ts_assignment then "assignment" else "logical" }
|
|
68
|
+
else if n is ts_conditional then {
|
|
69
|
+
value test: unparenthesised_node(n.test.node)
|
|
70
|
+
value branch: if contains(["!", "==", "===", "||"], test.operator) then n.alternate.node else n.consequent.node
|
|
71
|
+
value replacement: nullish_replacement(test, branch)
|
|
72
|
+
value if replacement == null then null else { subject: replacement.subject, truthiness: replacement.truthiness, kind: "ternary" }
|
|
73
|
+
} else if n is ts_if && n.alternate == null then {
|
|
74
|
+
value body: logical_if_body(n)
|
|
75
|
+
value assignment: unparenthesised_node(body.expression.node)
|
|
76
|
+
value test: unparenthesised_node(n.test.node)
|
|
77
|
+
value replacement: if body is ts_expression_statement && assignment is ts_assignment && contains(["=", "||=", "??="], assignment.operator)
|
|
78
|
+
&& contains(["!", "==", "===", "||"], test.operator) then nullish_replacement(test, assignment.left.node) else null
|
|
79
|
+
value if replacement == null then null else { subject: replacement.subject, truthiness: replacement.truthiness, kind: "if" }
|
|
80
|
+
} else null
|
|
81
|
+
fn nullish_context_chain(n, boolean) = {
|
|
82
|
+
value n
|
|
83
|
+
walk (current) if current.parent is ts_parenthesized || current.parent is ts_logical
|
|
84
|
+
|| current.parent is ts_conditional && current.parent.test.node != current
|
|
85
|
+
|| current.parent is ts_sequence && last(current.parent.expression.nodes) == current
|
|
86
|
+
|| !boolean && current.parent is ts_unary && current.parent.operator == "!"
|
|
87
|
+
then current.parent.node else null
|
|
88
|
+
}
|
|
89
|
+
fn nullish_context(n, boolean) = {
|
|
90
|
+
value chain: nullish_context_chain(n, boolean)
|
|
91
|
+
value outer: last(chain)
|
|
92
|
+
value if boolean then outer.parent is ts_call && outer.parent.global_callee == "Boolean"
|
|
93
|
+
&& (n not ts_conditional || { value chain; any (part) part != n && part not ts_parenthesized })
|
|
94
|
+
else outer.parent is (ts_conditional, ts_if, ts_while, ts_do_while, ts_for) && outer.parent.test.node == outer
|
|
95
|
+
}
|
|
96
|
+
fn nullish_mixed(n) = {
|
|
97
|
+
value n
|
|
98
|
+
walk (current) if current is ts_parenthesized then [current.expression.node, current.parent.node]
|
|
99
|
+
else if current is ts_logical && current.operator == "||" then [current.parent.node, current.left.node, current.right.node] else null
|
|
100
|
+
any (part) part is ts_logical && part.operator == "&&"
|
|
101
|
+
}
|
|
102
|
+
fn nullish_information(n) = {
|
|
103
|
+
value replacement: nullish_subject(n)
|
|
104
|
+
value if replacement == null then null else {
|
|
105
|
+
value {
|
|
106
|
+
kind: replacement.kind,
|
|
107
|
+
subject: replacement.subject,
|
|
108
|
+
truthiness: replacement.truthiness,
|
|
109
|
+
unknown: nullish_unknown(replacement.subject),
|
|
110
|
+
conditional: nullish_context(n, false),
|
|
111
|
+
boolean: nullish_context(n, true),
|
|
112
|
+
mixed: replacement.kind == "logical" && nullish_mixed(n),
|
|
113
|
+
report: if contains(["logical", "assignment"], replacement.kind)
|
|
114
|
+
then { start: n.operator.offset, end: n.operator.end_offset }
|
|
115
|
+
else { start: n.offset, end: n.end_offset }
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
in nullish_coalescing_expression let nullish_coalescing nullish_information(node)
|
|
121
|
+
in ts_program rule es-prefer-nullish-coalescing:strict {
|
|
122
|
+
option allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing boolean = false "Run without strict null checks"
|
|
123
|
+
when ts_compiler_option(node, "strictNullChecks") == false && !allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing
|
|
124
|
+
report at { start: 0, end: 0 }
|
|
125
|
+
warn "This rule requires the `strictNullChecks` compiler option to be turned on to function correctly."
|
|
126
|
+
}
|
|
127
|
+
in nullish_coalescing_expression rule es-prefer-nullish-coalescing {
|
|
128
|
+
option ignoreConditionalTests boolean = true "Ignore truthiness in conditional tests"
|
|
129
|
+
option ignoreBooleanCoercion boolean = false "Ignore truthiness passed to Boolean"
|
|
130
|
+
option ignoreMixedLogicalExpressions boolean = false "Ignore mixed logical expressions"
|
|
131
|
+
option ignoreTernaryTests boolean = false "Ignore ternary replacements"
|
|
132
|
+
option ignoreIfStatements boolean = false "Ignore conditional assignments"
|
|
133
|
+
option ignoreBigint boolean = false "Ignore bigint truthiness"
|
|
134
|
+
option ignoreBoolean boolean = false "Ignore boolean truthiness"
|
|
135
|
+
option ignoreNumber boolean = false "Ignore number truthiness"
|
|
136
|
+
option ignoreString boolean = false "Ignore string truthiness"
|
|
137
|
+
option ignoreUnknown boolean = false "Ignore unknown truthiness"
|
|
138
|
+
let replacement nullish_coalescing
|
|
139
|
+
when replacement != null
|
|
140
|
+
let domain condition_domain(replacement.subject)
|
|
141
|
+
when !replacement.truthiness || replacement.unknown || domain.nullish
|
|
142
|
+
when !(ignoreTernaryTests && replacement.kind == "ternary" || ignoreIfStatements && replacement.kind == "if")
|
|
143
|
+
when !replacement.truthiness || !(ignoreConditionalTests && replacement.conditional || ignoreBooleanCoercion && replacement.boolean || ignoreMixedLogicalExpressions && replacement.mixed)
|
|
144
|
+
when !replacement.truthiness || !(replacement.unknown && ignoreUnknown || ts_value_has_trait(replacement.subject, "bigint") == true && ignoreBigint || ts_value_has_trait(replacement.subject, "boolean") == true && ignoreBoolean || ts_value_has_trait(replacement.subject, "number") == true && ignoreNumber || ts_value_has_trait(replacement.subject, "string") == true && ignoreString)
|
|
145
|
+
report at replacement.report
|
|
146
|
+
warn if replacement.kind == "ternary" then "Prefer using nullish coalescing operator (`??`) instead of a ternary expression, as it is simpler to read." else if replacement.kind == "if" then "Prefer using nullish coalescing operator (`??=`) instead of an assignment expression, as it is simpler to read." else if replacement.kind == "assignment" then "Prefer using nullish coalescing operator (`??=`) instead of a logical assignment (`||=`), as it is a safer operator." else "Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator."
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
}
|
|
@@ -0,0 +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
|
+
}
|