@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 CHANGED
@@ -1,15 +1,15 @@
1
- #!/usr/bin/env node
2
- // Runs the dxlint binary for this platform. It finds its library (std/,
3
- // rules/) in the nearest directory above it that holds both: this package.
4
- const { spawnSync } = require("node:child_process");
5
- const path = require("node:path");
6
-
7
- const binary = path.join(__dirname, `dxlint-${process.platform}-${process.arch}${process.platform === "win32" ? ".exe" : ""}`);
8
- const result = spawnSync(binary, process.argv.slice(2), { stdio: "inherit", windowsHide: true });
9
- if (result.error) {
10
- process.stderr.write(result.error.code === "ENOENT"
11
- ? `dxlint: no binary for ${process.platform}-${process.arch} (expected ${binary})\n`
12
- : `dxlint: ${result.error.message}\n`);
13
- process.exit(2);
14
- }
15
- process.exit(result.status ?? 1);
1
+ #!/usr/bin/env node
2
+ // Runs the dxlint binary for this platform. It finds its library (std/,
3
+ // rules/) in the nearest directory above it that holds both: this package.
4
+ const { spawnSync } = require("node:child_process");
5
+ const path = require("node:path");
6
+
7
+ const binary = path.join(__dirname, `dxlint-${process.platform}-${process.arch}${process.platform === "win32" ? ".exe" : ""}`);
8
+ const result = spawnSync(binary, process.argv.slice(2), { stdio: "inherit", windowsHide: true });
9
+ if (result.error) {
10
+ process.stderr.write(result.error.code === "ENOENT"
11
+ ? `dxlint: no binary for ${process.platform}-${process.arch} (expected ${binary})\n`
12
+ : `dxlint: ${result.error.message}\n`);
13
+ process.exit(2);
14
+ }
15
+ process.exit(result.status ?? 1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inthedark/dxlint",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Fast linter and fixer for TypeScript, JavaScript, Rust and more, with ESLint-compatible rules written in .dx",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -9,15 +9,26 @@
9
9
  "directory": "package"
10
10
  },
11
11
  "homepage": "https://github.com/ErisInTheDark/dxlint#readme",
12
- "keywords": ["lint", "linter", "eslint", "typescript", "formatter"],
12
+ "keywords": [
13
+ "lint",
14
+ "linter",
15
+ "eslint",
16
+ "typescript",
17
+ "formatter"
18
+ ],
13
19
  "publishConfig": {
14
20
  "access": "public"
15
21
  },
16
22
  "engines": {
17
23
  "node": ">=18"
18
24
  },
19
- "os": ["win32", "linux"],
20
- "cpu": ["x64"],
25
+ "os": [
26
+ "win32",
27
+ "linux"
28
+ ],
29
+ "cpu": [
30
+ "x64"
31
+ ],
21
32
  "bin": {
22
33
  "dxlint": "bin/dxlint"
23
34
  },
@@ -1,270 +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
- }
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
+ }