@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,270 @@
|
|
|
1
|
+
# Expression simplification and atomic, precedence-safe fixes.
|
|
2
|
+
import .ts/expression-fixes
|
|
3
|
+
|
|
4
|
+
define {
|
|
5
|
+
|
|
6
|
+
fn boolean_cast_call(n) = {
|
|
7
|
+
value callee: unparenthesised_node(n.callee.node)
|
|
8
|
+
value n is (ts_call, ts_new) && callee is ts_identifier && callee.text == "Boolean"
|
|
9
|
+
&& contains(["readonly", "writable"], callee.global)
|
|
10
|
+
}
|
|
11
|
+
fn boolean_cast_direct(n) = n is (ts_if.test, ts_while.test, ts_do_while.test, ts_for.test, ts_conditional.test)
|
|
12
|
+
|| n.parent is ts_unary && n.parent.operator == "!"
|
|
13
|
+
|| boolean_cast_call(n.parent.node) && first(n.parent.argument.nodes) == n
|
|
14
|
+
fn boolean_cast_next(n) = if boolean_cast_direct(n) then null
|
|
15
|
+
else if n.parent is ts_parenthesized
|
|
16
|
+
|| n.parent is ts_logical && contains(["&&", "||"], n.parent.operator)
|
|
17
|
+
|| n.parent is ts_logical && n.parent.operator == "??" && n is ts_logical.right
|
|
18
|
+
|| n is (ts_conditional.consequent, ts_conditional.alternate)
|
|
19
|
+
|| n.parent is ts_sequence && last(n.parent.expression.nodes) == n then n.parent.node else null
|
|
20
|
+
fn boolean_cast_mode(n) = if boolean_cast_direct(n) || n.parent is ts_parenthesized then 0
|
|
21
|
+
else if n.parent is ts_logical && contains(["&&", "||"], n.parent.operator) then 1 else 2
|
|
22
|
+
fn boolean_cast_info(n) = {
|
|
23
|
+
value argument: if n is ts_unary && n.operator == "!" then unparenthesised_node(n.argument.node) else null
|
|
24
|
+
value negation: n is ts_unary && n.operator == "!" && argument is ts_unary && argument.operator == "!"
|
|
25
|
+
value if !negation && !(n is ts_call && boolean_cast_call(n)) then null else {
|
|
26
|
+
value chain: walk([n], current => boolean_cast_next(current))
|
|
27
|
+
value direct: any(chain, current => boolean_cast_direct(current))
|
|
28
|
+
value if !direct then null else {
|
|
29
|
+
value modes: map(chain, current => boolean_cast_mode(current))
|
|
30
|
+
value {negation: negation, mode: max(modes)}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fn boolean_cast_operand(n, negation) = {
|
|
36
|
+
value if negation
|
|
37
|
+
then unparenthesised_node(n.argument.node).argument.node else first(n.argument.nodes)
|
|
38
|
+
unparenthesised_node
|
|
39
|
+
}
|
|
40
|
+
fn boolean_cast_empty_target(n) = { value n; walk (current) if current.parent is ts_parenthesized then current.parent.node else null; last }
|
|
41
|
+
fn boolean_cast_fix(source, n, info) =
|
|
42
|
+
if !info.negation && count(n.argument.node) == 0 then
|
|
43
|
+
if boolean_cast_empty_target(n).parent is ts_unary && boolean_cast_empty_target(n).parent.operator == "!" then
|
|
44
|
+
if { value boolean_cast_empty_target(n).parent.node; expression_fix_clean } then {
|
|
45
|
+
target: boolean_cast_empty_target(n).parent.node,
|
|
46
|
+
text: { value boolean_cast_empty_target(n).parent.node; expression_fix_edges "true" }
|
|
47
|
+
} else null
|
|
48
|
+
else if expression_fix_clean(n) then { target: n, text: expression_fix_edges(n, "false") } else null
|
|
49
|
+
else if !expression_fix_clean(n) || !info.negation && (count(n.argument.node) != 1 || first(n.argument.nodes) is ts_spread) then null
|
|
50
|
+
else {
|
|
51
|
+
value operand: boolean_cast_operand(n, info.negation)
|
|
52
|
+
value required: expression_fix_parens(source, n, operand)
|
|
53
|
+
value if required == null then null else { target: n, text: {
|
|
54
|
+
value operand.text
|
|
55
|
+
wrapped: expression_fix_wrap (required > 0 || n.parent not ts_parenthesized && operand is structure_assertion)
|
|
56
|
+
value expression_fix_edges(n, wrapped)
|
|
57
|
+
} }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
in ts_unary, ts_call rule es-no-extra-boolean-cast {
|
|
61
|
+
option enforceForInnerExpressions boolean = false "check casts nested inside boolean expressions"
|
|
62
|
+
option enforceForLogicalOperands boolean = false "check casts nested inside logical expressions"
|
|
63
|
+
when if is ts_call then node.callee is ts_parenthesized || node.callee is ts_identifier && node.callee.text == "Boolean" else node.operator == "!"
|
|
64
|
+
let cast_info boolean_cast_info(node)
|
|
65
|
+
when cast_info != null && (cast_info.mode == 0 || cast_info.mode == 1 && enforceForLogicalOperands || enforceForInnerExpressions)
|
|
66
|
+
let cast_fix boolean_cast_fix(source, node, cast_info)
|
|
67
|
+
warn if cast_info.negation then "Redundant double negation." else "Redundant Boolean call."
|
|
68
|
+
fix at cast_fix.target if cast_fix != null as cast_fix.text
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
group expression_fix_precedence_owner ts_binary, ts_logical, ts_assignment, ts_conditional, ts_sequence, ts_unary, ts_await, ts_update, ts_call, ts_new, ts_member, ts_index, ts_tagged_template, ts_non_null, ts_as, ts_satisfies, ts_type_assertion, ts_arrow, ts_yield
|
|
72
|
+
fn exponentiation_base_parens(base) = base.precedence <= 14 || base is (ts_unary, ts_await, structure_assertion)
|
|
73
|
+
fn exponentiation_outer_parens(n, base) = n.parent not ts_parenthesized
|
|
74
|
+
&& (n is ts_class.extends
|
|
75
|
+
|| n.parent is structure_assertion
|
|
76
|
+
|| n is ts_arrow.body && !exponentiation_base_parens(base) && (base is ts_object || base.first_token.text == "{{")
|
|
77
|
+
|| n.parent is expression_fix_precedence_owner && n.parent.precedence >= 14
|
|
78
|
+
&& !(n is ts_binary.right && n.parent.operator == "**")
|
|
79
|
+
&& n not (ts_call.argument, ts_new.argument, ts_import_call.argument, ts_index.index))
|
|
80
|
+
fn exponentiation_statement_head(base) = base is (ts_function, ts_class, ts_object)
|
|
81
|
+
|| { value ["function", "class", "{{"]; contains base.first_token.text }
|
|
82
|
+
|| base.first_token.text == "async" && re_test(base.text, /^async\s+function\b/)
|
|
83
|
+
fn exponentiation_text(n, base, exponent) = {
|
|
84
|
+
value text: (
|
|
85
|
+
{ value base; needed: exponentiation_base_parens; value expression_fix_wrap(base.text, needed) }
|
|
86
|
+
"**" expression_fix_wrap(exponent.text, exponent.precedence < 14 || exponent is structure_assertion)
|
|
87
|
+
)
|
|
88
|
+
value needed_2: exponentiation_outer_parens(n, base) || expression_fix_statement(n) != null && exponentiation_statement_head(base) && !exponentiation_base_parens(base)
|
|
89
|
+
value expression_fix_wrap(text, needed_2)
|
|
90
|
+
}
|
|
91
|
+
fn exponentiation_asi(n, text) = {
|
|
92
|
+
value statement: expression_fix_statement(n)
|
|
93
|
+
value if statement != null && {
|
|
94
|
+
value text
|
|
95
|
+
slice 0 1
|
|
96
|
+
then (head) contains(["(", "[", "/", "`"], head)
|
|
97
|
+
} then {
|
|
98
|
+
value owners: expression_fix_before_nodes(n)
|
|
99
|
+
value expression_fix_asi(statement, n.token_neighbours.before, owners, text)
|
|
100
|
+
} else false
|
|
101
|
+
}
|
|
102
|
+
fn exponentiation_fix_result(n, text, semicolon) = if semicolon == null then null
|
|
103
|
+
else (if semicolon then ";" else "") expression_fix_edges(n, text)
|
|
104
|
+
fn exponentiation_fix_output(n, text) = { value n; semicolon: exponentiation_asi text; value exponentiation_fix_result(n, text, semicolon) }
|
|
105
|
+
fn exponentiation_fix(n) =
|
|
106
|
+
if count(n.argument.node) != 2 || any(n.argument.nodes, argument => argument is ts_spread) || !expression_fix_clean(n) then null
|
|
107
|
+
else {
|
|
108
|
+
value n.argument.nodes
|
|
109
|
+
first
|
|
110
|
+
base: unparenthesised_node
|
|
111
|
+
value n.argument.nodes
|
|
112
|
+
last
|
|
113
|
+
exponent: unparenthesised_node
|
|
114
|
+
value text: exponentiation_text(n, base, exponent)
|
|
115
|
+
value exponentiation_fix_output(n, text)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
in ts_call rule es-prefer-exponentiation-operator {
|
|
119
|
+
when ts_global_method_candidate(node, "pow")
|
|
120
|
+
when global_method_callee == "Math.pow"
|
|
121
|
+
let power_text exponentiation_fix(node)
|
|
122
|
+
warn "Use the '**' operator instead of 'Math.pow'."
|
|
123
|
+
fix if power_text != null as power_text
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
fn logical_chain_top(n) = n is (ts_member, ts_index, ts_call) && n.in_optional_chain == true && n not (ts_member.object, ts_index.object, ts_call.callee, ts_non_null.expression)
|
|
127
|
+
fn logical_reference(n) = n != null && !logical_chain_top(n) && (n is (ts_member, ts_index) || n is ts_identifier && n.text != "undefined")
|
|
128
|
+
fn logical_same_reference(a, b) = a.reference_key != null && a.reference_key == b.reference_key
|
|
129
|
+
fn logical_nullish(n) = if n is ts_null then false else if n is ts_identifier && n.text == "undefined" && n.resolved == "global" || n is ts_unary && n.operator == "void" && unparenthesised_node(n.argument.node) is ts_number && unparenthesised_node(n.argument.node).literal_value.value == 0 then true else null
|
|
130
|
+
fn logical_nullish_reference(reference, value) = if logical_reference(reference) && logical_nullish(value) != null then { reference: reference, undefined: logical_nullish(value), operator: "??" } else null
|
|
131
|
+
fn logical_nullish_comparison(n, op) = if n is ts_binary && n.operator == op then {
|
|
132
|
+
value n.left.node
|
|
133
|
+
reference: unparenthesised_node
|
|
134
|
+
value n.right.node
|
|
135
|
+
value_2: unparenthesised_node
|
|
136
|
+
value logical_nullish_reference(reference, value_2)
|
|
137
|
+
nonempty
|
|
138
|
+
} ?? {
|
|
139
|
+
value n.right.node
|
|
140
|
+
reference_2: unparenthesised_node
|
|
141
|
+
value n.left.node
|
|
142
|
+
value_3: unparenthesised_node
|
|
143
|
+
value logical_nullish_reference(reference_2, value_3)
|
|
144
|
+
} else null
|
|
145
|
+
fn logical_nullish_pair(a, b) = if a != null && b != null && a.undefined != b.undefined && logical_same_reference(a.reference, b.reference) then a else null
|
|
146
|
+
fn logical_boolean_existence(n, negated) = if logical_reference(n) then { reference: n, operator: if negated then "||" else "&&" } else if n is ts_unary && n.operator == "!" && { value n.argument.node; unparenthesised_node; logical_reference } then { reference: unparenthesised_node(n.argument.node), operator: "&&" } else if n is ts_call && n.global_callee == "Boolean" && count(n.argument.node) == 1 && {
|
|
147
|
+
value n.argument.node
|
|
148
|
+
first
|
|
149
|
+
unparenthesised_node
|
|
150
|
+
logical_reference
|
|
151
|
+
} then { reference: { value n.argument.node; first; unparenthesised_node }, operator: if negated then "||" else "&&" } else null
|
|
152
|
+
fn logical_existence(n) = {
|
|
153
|
+
value if n is ts_unary && n.operator == "!" then n.argument.node else n
|
|
154
|
+
unparenthesised_node
|
|
155
|
+
logical_boolean_existence (n is ts_unary && n.operator == "!")
|
|
156
|
+
nonempty
|
|
157
|
+
} ?? { value n; logical_nullish_comparison "=="; nonempty } ?? if n is ts_logical && n.operator == "||" then {
|
|
158
|
+
value n.left.node
|
|
159
|
+
unparenthesised_node
|
|
160
|
+
logical_nullish_comparison: logical_nullish_comparison "==="
|
|
161
|
+
value n.right.node
|
|
162
|
+
unparenthesised_node
|
|
163
|
+
logical_nullish_comparison_2: logical_nullish_comparison "==="
|
|
164
|
+
value logical_nullish_pair(logical_nullish_comparison, logical_nullish_comparison_2)
|
|
165
|
+
} else null
|
|
166
|
+
fn logical_leftmost(value) = {
|
|
167
|
+
value value.left.node
|
|
168
|
+
walk (current) if current is ts_logical && current.operator == value.operator then current.left.node else null
|
|
169
|
+
last
|
|
170
|
+
}
|
|
171
|
+
fn logical_assignment_value(assigned, value) = if value is ts_logical && { value value; logical_leftmost: logical_leftmost; value logical_same_reference(assigned, logical_leftmost) } then { kind: "assignment", operator: value.operator "=" } else null
|
|
172
|
+
fn logical_expression_value(reference, assignment, op, kind) = if logical_reference(reference) && assignment is ts_assignment && assignment.operator == "=" && logical_same_reference(reference, assignment.left.node) then { kind: kind, operator: op "=" } else null
|
|
173
|
+
fn logical_if_body(n) = if n.consequent is ts_block then (if count(n.consequent.body.node) == 1 then first(n.consequent.body.node) else null) else n.consequent.node
|
|
174
|
+
fn logical_if_value(existence, body) = if existence != null && body is ts_expression_statement then {
|
|
175
|
+
value body.expression.node
|
|
176
|
+
assignment: unparenthesised_node
|
|
177
|
+
value logical_expression_value(existence.reference, assignment, existence.operator, "if")
|
|
178
|
+
} else null
|
|
179
|
+
fn logical_assignment_info(n) = if n is ts_assignment && contains(["&&=", "||=", "??="], n.operator) then { kind: "shorthand", operator: n.operator } else if n is ts_assignment && n.operator == "=" then { value n.right.node; value_2: unparenthesised_node; value logical_assignment_value(n.left.node, value_2) } else if n is ts_logical then {
|
|
180
|
+
value n.left.node
|
|
181
|
+
reference: unparenthesised_node
|
|
182
|
+
value n.right.node
|
|
183
|
+
assignment: unparenthesised_node
|
|
184
|
+
value logical_expression_value(reference, assignment, n.operator, "logical")
|
|
185
|
+
} else if n is ts_if && n.alternate == null then {
|
|
186
|
+
value n.test.node
|
|
187
|
+
unparenthesised_node
|
|
188
|
+
existence: logical_existence
|
|
189
|
+
value n
|
|
190
|
+
body: logical_if_body
|
|
191
|
+
value logical_if_value(existence, body)
|
|
192
|
+
} else null
|
|
193
|
+
fn logical_fix_root(n) = {
|
|
194
|
+
value n
|
|
195
|
+
walk (current) current.parent.node
|
|
196
|
+
filter (current) current is ts_program
|
|
197
|
+
first
|
|
198
|
+
}
|
|
199
|
+
fn logical_fix_with(n) = !logical_fix_root(n).scoping_strict_code
|
|
200
|
+
&& { value n; walk (current) current.parent.node; any (current) current is ts_with.body }
|
|
201
|
+
fn logical_fix_identifier(n) = n is ts_identifier && !logical_fix_with(n)
|
|
202
|
+
# A getter on a mutable base or key can run between the read and the write, so
|
|
203
|
+
# only a single access on a plain base is safe. Inside a sloppy `with` block
|
|
204
|
+
# even an identifier can name a getter, but `if`/`??` fixes still accept it.
|
|
205
|
+
fn logical_fix_single_property(n) = if logical_fix_with(n) then n is ts_identifier
|
|
206
|
+
else n is (ts_member, ts_index)
|
|
207
|
+
&& unparenthesised_node(n.object.node) is (ts_identifier, ts_super, ts_this)
|
|
208
|
+
&& (n is ts_member || unparenthesised_node(n.index.node) not (ts_member, ts_index))
|
|
209
|
+
fn logical_fix_operator(source, n, operator) = byte_slice(source, n.offset, n.operator.offset) operator byte_slice(source, n.operator.end_offset, n.end_offset)
|
|
210
|
+
fn logical_fix_expand(source, n) = if !{ value n.left.node; unparenthesised_node; logical_fix_identifier } then null
|
|
211
|
+
else (byte_slice(source, n.offset, n.operator.offset) "= " unparenthesised_node(n.left.node).text " " slice(n.operator, 0, -1)
|
|
212
|
+
byte_slice(source, n.operator.end_offset, n.right.offset)
|
|
213
|
+
{
|
|
214
|
+
value needed: n.right not ts_parenthesized
|
|
215
|
+
&& (n.right is structure_assertion || n.right.precedence <= (if n.operator == "&&=" then 5 else 4) || n.operator == "??=" && n.right is ts_logical)
|
|
216
|
+
value expression_fix_wrap(n.right.text, needed)
|
|
217
|
+
})
|
|
218
|
+
fn logical_fix_assignment(source, n, value, first) = if !{ value n.left.node; unparenthesised_node; logical_fix_identifier } then null
|
|
219
|
+
else (byte_slice(source, n.offset, n.operator.offset) value.operator "="
|
|
220
|
+
byte_slice(source, n.operator.end_offset, first.parent.offset) byte_slice(source, first.parent.right.offset, n.end_offset))
|
|
221
|
+
fn logical_fix_expression(source, n, assignment) = if !{ value n.left.node; unparenthesised_node; logical_fix_identifier } && !{ value n.left.node; unparenthesised_node; logical_fix_single_property } then null
|
|
222
|
+
else if expression_fix_parens(source, n, assignment) == null then null
|
|
223
|
+
else {
|
|
224
|
+
value source
|
|
225
|
+
text: logical_fix_operator assignment (n.operator "=")
|
|
226
|
+
value needed: expression_fix_parens(source, n, assignment) > 0
|
|
227
|
+
value expression_fix_wrap(text, needed)
|
|
228
|
+
}
|
|
229
|
+
fn logical_fix_if(source, n, body, assignment, existence) =
|
|
230
|
+
if !logical_fix_identifier(existence.reference) && !(unparenthesised_node(n.test.node) not ts_logical && logical_fix_single_property(existence.reference)) then null
|
|
231
|
+
else if n.token_neighbours.before != null && !{ value [";", "{{"]; contains n.token_neighbours.before.text }
|
|
232
|
+
&& !re_test(body.first_token.text, /^[\w$\\]/) then null
|
|
233
|
+
else (byte_slice(source, body.offset, assignment.operator.offset) existence.operator "="
|
|
234
|
+
byte_slice(source, assignment.operator.end_offset, body.end_offset)
|
|
235
|
+
(if n.consequent is ts_block && !{ value body.text; trim_end; ends_with ";" } then ";" else ""))
|
|
236
|
+
fn logical_assignment_fix(source, n, info) = if !expression_fix_clean(n) then null
|
|
237
|
+
else if info.kind == "shorthand" then logical_fix_expand(source, n)
|
|
238
|
+
else if info.kind == "assignment" then {
|
|
239
|
+
value n.right.node
|
|
240
|
+
value_2: unparenthesised_node
|
|
241
|
+
value n.right.node
|
|
242
|
+
unparenthesised_node
|
|
243
|
+
first: logical_leftmost
|
|
244
|
+
value logical_fix_assignment(source, n, value_2, first)
|
|
245
|
+
}
|
|
246
|
+
else if info.kind == "logical" then { value n.right.node; assignment: unparenthesised_node; value logical_fix_expression(source, n, assignment) }
|
|
247
|
+
else {
|
|
248
|
+
value n
|
|
249
|
+
body: logical_if_body
|
|
250
|
+
value logical_if_body(n).expression.node
|
|
251
|
+
assignment_2: unparenthesised_node
|
|
252
|
+
value n.test.node
|
|
253
|
+
unparenthesised_node
|
|
254
|
+
existence: logical_existence
|
|
255
|
+
value logical_fix_if(source, n, body, assignment_2, existence)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
in ts_assignment, ts_logical, ts_if rule es-logical-assignment-operators {
|
|
259
|
+
option mode ("always" | "never") = "always" "logical assignment policy"
|
|
260
|
+
option enforceForIfStatements boolean = false "include conditional assignments"
|
|
261
|
+
when not ts_if || enforceForIfStatements
|
|
262
|
+
let logical_info logical_assignment_info(node)
|
|
263
|
+
when logical_info != null
|
|
264
|
+
when if mode == "never" then logical_info.kind == "shorthand" else logical_info.kind != "shorthand"
|
|
265
|
+
let logical_text logical_assignment_fix(source, node, logical_info)
|
|
266
|
+
warn if mode == "never" then "Unexpected logical operator assignment ({logical_info.operator}) shorthand." else if logical_info.kind == "assignment" then "Assignment (=) can be replaced with operator assignment ({logical_info.operator})." else if logical_info.kind == "logical" then "Logical expression can be replaced with an assignment ({logical_info.operator})." else "'if' statement can be replaced with a logical operator assignment with operator {logical_info.operator}."
|
|
267
|
+
fix if logical_text != null as logical_text
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
}
|
package/std/ts/flow.dx
ADDED