@inthedark/dxlint 0.0.0-stage → 0.1.0

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.
Files changed (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +84 -2
  3. package/bin/dxlint +15 -0
  4. package/bin/dxlint-linux-x64 +0 -0
  5. package/bin/dxlint-win32-x64.exe +0 -0
  6. package/package.json +29 -4
  7. package/rules/conventions.dx +34 -0
  8. package/rules/correctness.dx +33 -0
  9. package/rules/es-no-semi.dx +11 -0
  10. package/rules/format.dx +49 -0
  11. package/rules/recommended.dx +51 -0
  12. package/rules/suspicious.dx +31 -0
  13. package/rules/types.dx +21 -0
  14. package/std/c.dx +23 -0
  15. package/std/core/lines.dx +51 -0
  16. package/std/core/lists.dx +76 -0
  17. package/std/core.dx +30 -0
  18. package/std/css.dx +17 -0
  19. package/std/dx.dx +31 -0
  20. package/std/json.dx +104 -0
  21. package/std/markdown.dx +11 -0
  22. package/std/python.dx +21 -0
  23. package/std/rust/format.dx +316 -0
  24. package/std/rust.dx +34 -0
  25. package/std/toml.dx +19 -0
  26. package/std/ts/arithmetic.dx +87 -0
  27. package/std/ts/assertions.dx +477 -0
  28. package/std/ts/calls.dx +246 -0
  29. package/std/ts/comments.dx +10 -0
  30. package/std/ts/conditions.dx +531 -0
  31. package/std/ts/constants.dx +335 -0
  32. package/std/ts/deprecation.dx +185 -0
  33. package/std/ts/directives.dx +60 -0
  34. package/std/ts/effects.dx +219 -0
  35. package/std/ts/enums.dx +233 -0
  36. package/std/ts/expression-fixes.dx +67 -0
  37. package/std/ts/expressions.dx +270 -0
  38. package/std/ts/flow.dx +2 -0
  39. package/std/ts/format.dx +755 -0
  40. package/std/ts/globals.dx +106 -0
  41. package/std/ts/grammar.dx +488 -0
  42. package/std/ts/helpers.dx +102 -0
  43. package/std/ts/import-attributes.dx +88 -0
  44. package/std/ts/import-no-deprecated.dx +73 -0
  45. package/std/ts/indentation.dx +442 -0
  46. package/std/ts/jsx.dx +646 -0
  47. package/std/ts/layout/blocks.dx +281 -0
  48. package/std/ts/layout/calls.dx +79 -0
  49. package/std/ts/layout/expressions.dx +213 -0
  50. package/std/ts/layout/literals.dx +114 -0
  51. package/std/ts/layout/separators.dx +184 -0
  52. package/std/ts/layout/shared.dx +203 -0
  53. package/std/ts/layout/text.dx +142 -0
  54. package/std/ts/layout/types.dx +182 -0
  55. package/std/ts/layout.dx +9 -0
  56. package/std/ts/license.dx +54 -0
  57. package/std/ts/literal-text.dx +3 -0
  58. package/std/ts/literals.dx +95 -0
  59. package/std/ts/methods.dx +69 -0
  60. package/std/ts/modules.dx +268 -0
  61. package/std/ts/names.dx +348 -0
  62. package/std/ts/naming.dx +169 -0
  63. package/std/ts/narrowing.dx +96 -0
  64. package/std/ts/parentheses.dx +262 -0
  65. package/std/ts/precedence.dx +25 -0
  66. package/std/ts/problems/calls.dx +34 -0
  67. package/std/ts/problems/classes.dx +39 -0
  68. package/std/ts/problems/control-flow.dx +257 -0
  69. package/std/ts/problems/declarations.dx +67 -0
  70. package/std/ts/problems/duplicates.dx +70 -0
  71. package/std/ts/problems/expressions.dx +154 -0
  72. package/std/ts/problems/helpers.dx +22 -0
  73. package/std/ts/problems/imports.dx +43 -0
  74. package/std/ts/problems/non-null.dx +78 -0
  75. package/std/ts/problems/numeric-precision.dx +66 -0
  76. package/std/ts/problems/regexp.dx +66 -0
  77. package/std/ts/problems/statements.dx +36 -0
  78. package/std/ts/problems/typescript.dx +55 -0
  79. package/std/ts/problems/whitespace.dx +40 -0
  80. package/std/ts/problems.dx +20 -0
  81. package/std/ts/regex/checks.dx +249 -0
  82. package/std/ts/regex/findings.dx +209 -0
  83. package/std/ts/regex/grammar.dx +80 -0
  84. package/std/ts/regex/suggestions.dx +94 -0
  85. package/std/ts/regex/values.dx +378 -0
  86. package/std/ts/regex.dx +5 -0
  87. package/std/ts/restrictions/comments.dx +76 -0
  88. package/std/ts/restrictions.dx +811 -0
  89. package/std/ts/scopes.dx +68 -0
  90. package/std/ts/scoping/atomic.dx +40 -0
  91. package/std/ts/scoping/bindings.dx +126 -0
  92. package/std/ts/scoping/constness.dx +72 -0
  93. package/std/ts/scoping/globals.dx +52 -0
  94. package/std/ts/scoping/loops.dx +86 -0
  95. package/std/ts/scoping/member-uses.dx +111 -0
  96. package/std/ts/scoping/receivers.dx +107 -0
  97. package/std/ts/scoping/shadowing.dx +202 -0
  98. package/std/ts/scoping/usage.dx +393 -0
  99. package/std/ts/scoping.dx +842 -0
  100. package/std/ts/selectors.dx +10 -0
  101. package/std/ts/spreads.dx +97 -0
  102. package/std/ts/structure/declarations.dx +52 -0
  103. package/std/ts/structure/lines.dx +79 -0
  104. package/std/ts/structure/lists.dx +144 -0
  105. package/std/ts/structure/padding.dx +258 -0
  106. package/std/ts/structure.dx +963 -0
  107. package/std/ts/style/classes.dx +157 -0
  108. package/std/ts/style/comments.dx +75 -0
  109. package/std/ts/style/declarations.dx +82 -0
  110. package/std/ts/style/expressions.dx +125 -0
  111. package/std/ts/style/imports.dx +44 -0
  112. package/std/ts/style/ordering.dx +192 -0
  113. package/std/ts/style/returns.dx +152 -0
  114. package/std/ts/style/signatures.dx +308 -0
  115. package/std/ts/style/type-exports.dx +62 -0
  116. package/std/ts/style/type-imports.dx +166 -0
  117. package/std/ts/style/types.dx +474 -0
  118. package/std/ts/style.dx +29 -0
  119. package/std/ts/suggestions/accessors.dx +138 -0
  120. package/std/ts/suggestions/builtins.dx +110 -0
  121. package/std/ts/suggestions/comparisons.dx +41 -0
  122. package/std/ts/suggestions/computed-keys.dx +20 -0
  123. package/std/ts/suggestions/constructors.dx +54 -0
  124. package/std/ts/suggestions/control-flow.dx +204 -0
  125. package/std/ts/suggestions/curly.dx +124 -0
  126. package/std/ts/suggestions/directives.dx +79 -0
  127. package/std/ts/suggestions/errors.dx +106 -0
  128. package/std/ts/suggestions/expressions.dx +230 -0
  129. package/std/ts/suggestions/functions.dx +260 -0
  130. package/std/ts/suggestions/globals.dx +187 -0
  131. package/std/ts/suggestions/helpers.dx +17 -0
  132. package/std/ts/suggestions/object-shorthand.dx +148 -0
  133. package/std/ts/suggestions/object-spread.dx +96 -0
  134. package/std/ts/suggestions/regexp.dx +59 -0
  135. package/std/ts/suggestions/renames.dx +50 -0
  136. package/std/ts/suggestions/strings.dx +47 -0
  137. package/std/ts/suggestions/templates.dx +104 -0
  138. package/std/ts/suggestions.dx +24 -0
  139. package/std/ts/template-fixes.dx +22 -0
  140. package/std/ts/tokens.dx +124 -0
  141. package/std/ts/type-specifiers.dx +93 -0
  142. package/std/ts/typed/assignments.dx +154 -0
  143. package/std/ts/typed/awaitability.dx +157 -0
  144. package/std/ts/typed/boolean-comparisons.dx +82 -0
  145. package/std/ts/typed/defaults.dx +97 -0
  146. package/std/ts/typed/enum-consistency.dx +68 -0
  147. package/std/ts/typed/enums.dx +52 -0
  148. package/std/ts/typed/errors.dx +32 -0
  149. package/std/ts/typed/floating-promises.dx +147 -0
  150. package/std/ts/typed/for-in-arrays.dx +21 -0
  151. package/std/ts/typed/includes.dx +102 -0
  152. package/std/ts/typed/nullish-coalescing.dx +149 -0
  153. package/std/ts/typed/optional-chains.dx +317 -0
  154. package/std/ts/typed/promises.dx +205 -0
  155. package/std/ts/typed/qualifiers.dx +108 -0
  156. package/std/ts/typed/readonly-members.dx +159 -0
  157. package/std/ts/typed/readonly-parameters.dx +121 -0
  158. package/std/ts/typed/redundant-constituents.dx +171 -0
  159. package/std/ts/typed/regexp-exec.dx +79 -0
  160. package/std/ts/typed/rejection-handlers.dx +91 -0
  161. package/std/ts/typed/return-await.dx +116 -0
  162. package/std/ts/typed/return-this.dx +67 -0
  163. package/std/ts/typed/strict-void-return.dx +121 -0
  164. package/std/ts/typed/string-boundaries.dx +153 -0
  165. package/std/ts/typed/switches.dx +147 -0
  166. package/std/ts/typed/template-expressions.dx +127 -0
  167. package/std/ts/typed/template-values.dx +88 -0
  168. package/std/ts/typed/transfers.dx +106 -0
  169. package/std/ts/typed/type-arguments.dx +337 -0
  170. package/std/ts/typed/type-constituents.dx +143 -0
  171. package/std/ts/typed/type-conversions.dx +82 -0
  172. package/std/ts/typed/type-parameters.dx +280 -0
  173. package/std/ts/typed/unbound-method.dx +167 -0
  174. package/std/ts/typed/unsafe-calls.dx +73 -0
  175. package/std/ts/typed/unsafe-members.dx +36 -0
  176. package/std/ts/typed/void-expressions.dx +101 -0
  177. package/std/ts/typed.dx +585 -0
  178. package/std/ts/values/arguments.dx +13 -0
  179. package/std/ts/values/enums.dx +104 -0
  180. package/std/ts/values/inspection.dx +274 -0
  181. package/std/ts/values.dx +1217 -0
  182. package/std/ts.dx +77 -0
  183. package/std/whitespace.dx +37 -0
@@ -0,0 +1,219 @@
1
+ # .ts/effects: evaluation order and separate execution entries.
2
+
3
+ define {
4
+ fn ts_effects_function_owner(n) = find(walk(n, current => current.parent.node),
5
+ current => current is (ts_function, ts_arrow, ts_method))
6
+ }
7
+
8
+ in ts_program, ts_block, ts_case, ts_namespace { runs body }
9
+ in ts_expression_statement, ts_export_assignment, ts_decorator, ts_computed_key,
10
+ ts_parenthesized, ts_as, ts_satisfies, ts_non_null, ts_type_assertion,
11
+ ts_instantiation, ts_jsx_expression { runs expression }
12
+ in ts_variable_declaration { runs declaration }
13
+ in ts_declarator {
14
+ runs init then name
15
+ when init != null && name is ts_identifier { assigned name with init }
16
+ }
17
+ in ts_parameter { runs decorator then maybe init then name }
18
+ in ts_identifier {
19
+ when contains(["read", "read_write"], access) { unwraps read it }
20
+ }
21
+ in ts_return {
22
+ runs argument
23
+ let calls_return_owner if argument != null then ts_effects_function_owner(node) else null
24
+ when argument == null { unwraps returned nearest function }
25
+ when argument != null {
26
+ unwraps returned it nearest function
27
+ when calls_return_owner.calls_inferred_widen {
28
+ unwraps argument.widened
29
+ }
30
+ when !calls_return_owner.calls_inferred_widen {
31
+ unwraps argument
32
+ }
33
+ }
34
+ }
35
+ in ts_throw { runs argument; unwraps thrown argument }
36
+ in ts_break { runs nothing; unwraps broken label nearest breakable }
37
+ in ts_continue { runs nothing; unwraps continued label nearest loop }
38
+ in ts_await { runs argument; unwraps awaited argument }
39
+ in ts_yield { runs argument; unwraps yielded argument; unwraps maybe exited nearest function }
40
+ in ts_call, ts_new, ts_member, ts_index, ts_yield, ts_await, ts_import_call,
41
+ ts_tagged_template, ts_spread, ts_jsx_spread_attribute { unwraps maybe thrown }
42
+ in ts_block { when is ts_try.block { unwraps maybe thrown } }
43
+ in ts_if, ts_conditional { runs test then (consequent or alternate) }
44
+ in ts_logical { runs left then maybe right }
45
+
46
+ in ts_function, ts_arrow {
47
+ runs nothing
48
+ in body runs separately
49
+ }
50
+ in ts_method {
51
+ runs decorator then key
52
+ in body runs separately
53
+ }
54
+ in ts_class { runs decorator then extends then member }
55
+ in ts_static_block {
56
+ runs body
57
+ in body runs separately
58
+ }
59
+ in ts_class_property {
60
+ when contains([] modifier, "static") { runs decorator then key then init }
61
+ when !contains([] modifier, "static") { runs decorator then key }
62
+ in init runs separately
63
+ }
64
+
65
+ in ts_while {
66
+ scope loop, breakable
67
+ runs repeat body while test.flow_truth
68
+ }
69
+ in ts_do_while {
70
+ scope loop, breakable
71
+ runs repeat body while test.flow_truth first
72
+ }
73
+ in ts_for {
74
+ scope loop, breakable
75
+ when test != null { runs init then (repeat body while test.flow_truth next update) }
76
+ when test == null { runs init then (repeat body while flow_truth next update) }
77
+ }
78
+ in ts_for_in, ts_for_of {
79
+ scope loop, breakable
80
+ runs right then repeat (left then body)
81
+ }
82
+ in ts_switch {
83
+ scope breakable
84
+ runs discriminant then (enter one of case testing test then body)
85
+ }
86
+ in ts_labeled { labels label; runs body }
87
+ in ts_try {
88
+ runs block
89
+ when handler != null { in handler catches thrown from block }
90
+ when finalizer != null { in finalizer runs after block, handler }
91
+ }
92
+ in ts_with { runs object then body }
93
+
94
+ in ts_node let flow_assignment_scalar false
95
+ in ts_identifier, ts_member, ts_index let flow_assignment_scalar true
96
+ in ts_parenthesized, ts_as, ts_satisfies, ts_non_null, ts_type_assertion, ts_instantiation let flow_assignment_scalar expression.flow_assignment_scalar
97
+ in ts_property, ts_pattern_property let flow_shorthand_default value is ts_assignment_pattern && value.left == null
98
+ in ts_node let flow_destructuring_target
99
+ if is (ts_for_in.left, ts_for_of.left) then true
100
+ else if is ts_declarator.name then
101
+ (node.parent.init != null && flow_assignment_scalar == false)
102
+ || node.parent.parent is (ts_for_in.left, ts_for_of.left)
103
+ else if is ts_assignment.left then node.parent.flow_destructuring_target
104
+ || node.parent.operator == "=" && flow_assignment_scalar == false
105
+ else if is (ts_array.element, ts_array_pattern.element, ts_object.property, ts_object_pattern.property,
106
+ ts_object_pattern.rest, ts_rest.argument, ts_spread.argument, ts_assignment_pattern.left,
107
+ ts_parenthesized.expression, ts_as.expression, ts_satisfies.expression, ts_non_null.expression,
108
+ ts_type_assertion.expression, ts_instantiation.expression) then parent.flow_destructuring_target
109
+ else if is (ts_property.value, ts_pattern_property.value) then parent.flow_destructuring_target
110
+ else if is (ts_property.key, ts_pattern_property.key) && (node.parent.value == null || node.parent.flow_shorthand_default) then parent.flow_destructuring_target
111
+ else false
112
+ in ts_identifier, ts_member, ts_index {
113
+ when is (ts_for_in.left, ts_for_of.left, ts_declarator.name, ts_assignment.left,
114
+ ts_array.element, ts_array_pattern.element, ts_object.property, ts_object_pattern.property,
115
+ ts_object_pattern.rest, ts_rest.argument, ts_spread.argument, ts_assignment_pattern.left,
116
+ ts_parenthesized.expression, ts_as.expression, ts_satisfies.expression, ts_non_null.expression,
117
+ ts_type_assertion.expression, ts_instantiation.expression, ts_property.value, ts_pattern_property.value,
118
+ ts_property.key, ts_pattern_property.key) && flow_destructuring_target { assigned it }
119
+ }
120
+ in ts_assignment {
121
+ when flow_destructuring_target { runs maybe right then left }
122
+ when !flow_destructuring_target && contains(["&&=", "||=", "??="], operator) {
123
+ runs left then maybe right
124
+ unwraps maybe mutated left
125
+ }
126
+ when !flow_destructuring_target && !contains(["&&=", "||=", "??="], operator) && left.flow_assignment_scalar != true { runs right then left }
127
+ when !flow_destructuring_target && !contains(["&&=", "||=", "??="], operator) && left.flow_assignment_scalar == true { runs left then right }
128
+ when !flow_destructuring_target && operator == "=" && left.flow_assignment_scalar == true { assigned left with right }
129
+ when !flow_destructuring_target && operator != "=" && !contains(["&&=", "||=", "??="], operator) { unwraps mutated left }
130
+ }
131
+ in ts_assignment_pattern {
132
+ runs maybe right then left
133
+ when !flow_destructuring_target { unwraps maybe assigned left }
134
+ }
135
+ in ts_object_pattern { runs property then rest }
136
+ in ts_array_pattern, ts_array { runs element }
137
+ in ts_pattern_property {
138
+ when flow_shorthand_default { runs value then key }
139
+ when !flow_shorthand_default { runs key then value }
140
+ }
141
+ in ts_rest, ts_spread, ts_jsx_spread_attribute { runs argument }
142
+ in ts_object { runs property }
143
+ in ts_property {
144
+ when flow_shorthand_default { runs value then key }
145
+ when !flow_shorthand_default { runs key then value }
146
+ }
147
+ in ts_binary { runs left then right }
148
+ in ts_unary, ts_update, ts_import_call { runs argument }
149
+ in ts_update { unwraps mutated argument }
150
+ in ts_unary { when operator == "delete" { unwraps deleted argument } }
151
+ in ts_member { runs object }
152
+ in ts_index {
153
+ when optional == null { runs object then index }
154
+ when optional != null { runs object then maybe index }
155
+ }
156
+ in ts_call {
157
+ when optional == null { runs callee then argument }
158
+ when optional != null { runs callee then maybe argument }
159
+ }
160
+ in ts_new { runs callee then argument }
161
+ in ts_sequence, ts_template { runs expression }
162
+ in ts_tagged_template { runs tag then quasi }
163
+ in ts_export { runs declaration then expression }
164
+ in ts_enum { runs member }
165
+ in ts_enum_member { runs init }
166
+ in ts_jsx_element, ts_jsx_fragment { runs opening then body }
167
+ in ts_jsx_opening { runs name then property }
168
+ in ts_jsx_attribute { runs value }
169
+
170
+ in ts_import, ts_import_specifier, ts_import_equals, ts_export_specifier,
171
+ ts_interface, ts_type_alias, ts_index_signature, ts_type_reference,
172
+ ts_type_parameter, ts_union, ts_intersection, ts_array_type, ts_tuple_type,
173
+ ts_tuple_member, ts_function_type, ts_type_literal, ts_property_signature,
174
+ ts_method_signature, ts_call_signature, ts_type_query, ts_type_operator,
175
+ ts_indexed_access, ts_conditional_type, ts_infer, ts_mapped_type,
176
+ ts_parenthesized_type, ts_template_type, ts_type_predicate, ts_import_type,
177
+ ts_literal_type, ts_type_keyword, ts_qualified_name { runs nothing }
178
+
179
+ in ts_identifier, ts_private_name, ts_this, ts_super, ts_null, ts_boolean,
180
+ ts_number, ts_bigint, ts_string, ts_regex, ts_meta_property, ts_hole,
181
+ ts_empty, ts_debugger, ts_jsx_closing, ts_jsx_text, ts_jsx_name,
182
+ ts_comma, ts_semicolon, ts_colon, ts_dot, ts_question, ts_ellipsis, ts_equals,
183
+ ts_open_paren, ts_close_paren, ts_open_bracket, ts_close_bracket,
184
+ ts_open_brace, ts_close_brace, ts_open_angle, ts_close_angle,
185
+ ts_keyword, ts_punctuator { runs nothing }
186
+
187
+ in ts_boolean let flow_truth node == "true"
188
+ in ts_null let flow_truth false
189
+ in ts_number let flow_truth js_number({ type: "number", value: node }) != 0
190
+ in ts_string let flow_truth len(node) > 2
191
+ in ts_template let flow_truth if expression == null then len(node) > 2 else null
192
+ in ts_regex let flow_truth true
193
+ in ts_bigint let flow_truth if js_bigint(node) == null then null else js_bigint(node) != "0"
194
+ in ts_for let flow_truth if test == null then true else null
195
+
196
+ in ts_node let flow_constructor
197
+ if is ts_method then if node.key == "constructor" && parent is ts_class && parent.extends != null then node else null
198
+ else if is (ts_program, ts_function, ts_arrow, ts_class, ts_static_block) then null
199
+ else parent.flow_constructor
200
+ in ts_call {
201
+ when callee is ts_super && flow_constructor != null { unwraps constructed it }
202
+ }
203
+ in ts_return {
204
+ when argument != null && flow_constructor != null { unwraps constructed it }
205
+ }
206
+ in ts_node let flow_constructor_heritage
207
+ if is (ts_class, ts_function, ts_this, ts_member, ts_index, ts_call, ts_new, ts_yield, ts_tagged_template, ts_meta_property) then true
208
+ else if is ts_identifier then node != "undefined"
209
+ else if is (ts_parenthesized, ts_non_null, ts_as, ts_satisfies) then node.expression.flow_constructor_heritage
210
+ else if is ts_assignment then
211
+ if contains(["=", "&&="], node.operator) then right.flow_constructor_heritage
212
+ else if contains(["||=", "??="], node.operator) then left.flow_constructor_heritage == true || right.flow_constructor_heritage == true
213
+ else false
214
+ else if is ts_logical then
215
+ if node.operator == "&&" then right.flow_constructor_heritage
216
+ else left.flow_constructor_heritage == true || right.flow_constructor_heritage == true
217
+ else if is ts_conditional then consequent.flow_constructor_heritage == true || alternate.flow_constructor_heritage == true
218
+ else if is ts_sequence then { value [] node.expression.nodes; tail: last; value tail.flow_constructor_heritage }
219
+ else false
@@ -0,0 +1,233 @@
1
+ import .ts/typed/transfers
2
+
3
+ define {
4
+ fn enum_origins(arm) = filter(arm, part => contains(part.traits, "enum"))
5
+ fn enum_receiver_parts(parts) = if parts == null then null else filter(parts, arm =>
6
+ !all(arm, part => contains(part.traits, "null") || contains(part.traits, "undefined")))
7
+ fn enum_root_bad(sender, receiver) = sender != null && receiver != null && len(receiver) > 0
8
+ && all(receiver, arm => len(enum_origins(arm)) > 0
9
+ && all(enum_origins(arm), part => part.carrier_known && part.carrier != null))
10
+ && any(sender, source => {
11
+ value origins: enum_origins(source)
12
+ value domains: filter(["number", "string"], trait => any(source, part => contains(part.traits, trait)))
13
+ value len(domains) > 0 && all(origins, part => part.carrier_known && part.carrier != null)
14
+ && any(receiver, target => any(domains, domain => any(target, part => contains(part.traits, domain))))
15
+ && !any(receiver, target => any(enum_origins(target), b => any(origins, a =>
16
+ a.carrier.file == b.carrier.file && a.carrier.node == b.carrier.node)))
17
+ && all(source, part => !contains(part.traits, "any") && !contains(part.traits, "unknown")
18
+ && !contains(part.uncertain_traits, "enum"))
19
+ })
20
+ fn enum_relation_children(pair, source) = {
21
+ value identity: {sender: pair.sender, receiver: pair.receiver}
22
+ value senders: if pair.sender != null then value_parts(pair.sender) else null
23
+ value receivers: if pair.receiver != null then value_parts(pair.receiver) else null
24
+ value if contains(pair.seen, identity) || senders == null || receivers == null
25
+ || len(senders) != 1 || len(receivers) != 1 then [] else {
26
+ value sender: first(senders)
27
+ value receiver: first(receivers)
28
+ value arguments: {
29
+ value sender.parts
30
+ flat_map (a) {
31
+ value receiver.parts
32
+ flat_map (b) {
33
+ value assignment_carrier_arguments(a, b, source)
34
+ map (args) {sender: first(args), receiver: last(args)}
35
+ }
36
+ }
37
+ }
38
+ value members: {
39
+ value value_children(receiver.handle, true, []) ?? []
40
+ map (edge) {sender: value_lookup(sender.handle, edge.member.key).member, receiver: edge.handle}
41
+ filter (child) child.sender != null && child.receiver != null
42
+ }
43
+ value indexes: {
44
+ value ["text", "ordinal", "symbol"]
45
+ map (domain) {key: {domain: domain}, target: value_lookup(receiver.handle, {domain: domain})}
46
+ filter (entry) entry.target.member != null && any(entry.target.alternatives, arm =>
47
+ any(arm.sources, origin => origin.kind == "indexer"))
48
+ map (entry) {sender: value_lookup(sender.handle, entry.key).member, receiver: entry.target.member}
49
+ filter (child) child.sender != null
50
+ }
51
+ value unique(arguments members indexes)
52
+ map (child) {sender: child.sender, receiver: child.receiver, seen: pair.seen [identity]}
53
+ }
54
+ }
55
+ fn enum_relation_pairs(sender, receiver, source) = {
56
+ value {sender: sender, receiver: receiver, seen: []}
57
+ walk (pair) enum_relation_children(pair, source)
58
+ map (pair) {
59
+ sender: if pair.sender != null then value_constituents(pair.sender) else null,
60
+ receiver: if pair.receiver != null then enum_receiver_parts(value_constituents(pair.receiver)) else null,
61
+ }
62
+ }
63
+ fn enum_bitwise_shape(n) = n is ts_binary && contains(["&", "|", "^"], n.operator)
64
+ || n is ts_unary && n.operator == "~"
65
+ fn enum_bitwise_leaves(source) = {
66
+ value unparenthesised_node(source)
67
+ walk (n) if n is ts_binary && enum_bitwise_shape(n) then map([n.left.node, n.right.node], child => unparenthesised_node(child))
68
+ else if n is ts_unary && enum_bitwise_shape(n) then [unparenthesised_node(n.argument.node)] else []
69
+ filter (n) !enum_bitwise_shape(n)
70
+ }
71
+ fn enum_safe_bitwise(source, receiver) = enum_bitwise_shape(source) && {
72
+ value enum_bitwise_leaves(source)
73
+ all (n) {
74
+ value parts: value_constituents(n)
75
+ value parts != null && !enum_root_bad(parts, receiver)
76
+ }
77
+ }
78
+ fn enum_annotation_step(entry) = {
79
+ value parent: entry.site.parent.node
80
+ value key: if entry.site is (ts_property, ts_pattern_property) && parent is (ts_object, ts_object_pattern)
81
+ then ts_value_key(entry.site.key.node)
82
+ else if parent is (ts_array, ts_array_pattern) && entry.site not (ts_spread, ts_rest)
83
+ then index_of(parent.element.nodes, entry.site)
84
+ else null
85
+ value if key != null then [{site: parent, path: [key] entry.path}]
86
+ else if parent is ts_assignment_pattern && entry.site == parent.right.node
87
+ || entry.site is ts_assignment_pattern && parent is ts_pattern_property
88
+ || parent is (ts_parenthesized, ts_non_null, ts_satisfies)
89
+ || parent is (ts_as, ts_type_assertion) && parent.type.node.text == "const"
90
+ then [{site: parent, path: entry.path}]
91
+ else []
92
+ }
93
+ fn enum_annotation_target(context) = {
94
+ value frames: walk({site: context, path: []}, entry => enum_annotation_step(entry))
95
+ value candidates: map(frames, entry => {
96
+ value parent: entry.site.parent.node
97
+ value returned: parent is ts_return && entry.site == parent.argument.node
98
+ || parent is ts_arrow && entry.site == parent.body.node
99
+ value owner: if returned then return_value_owner(entry.site) else null
100
+ value annotation: if parent is (ts_parameter, ts_declarator, ts_class_property)
101
+ && (entry.site == parent.init.node || entry.site == parent.name.node) then parent.type.node
102
+ else if parent is (ts_as, ts_type_assertion) && parent.type.node.text != "const" then parent.type.node
103
+ else if returned then owner.return_type.node else null
104
+ value target: if returned && annotation != null && contains([] owner.modifier, "async")
105
+ then ts_awaited_value(annotation, annotation) else annotation
106
+ value {target: target, path: entry.path}
107
+ })
108
+ value frame: find(candidates, entry => entry.target != null)
109
+ value if frame == null then null else
110
+ fold(frame.path, frame.target, (target, key) => value_lookup(target, key).member)
111
+ }
112
+ fn enum_policy(site, source) = {
113
+ value context: if site is ts_property then site
114
+ else if source is ts_spread && source.parent is ts_object
115
+ then if source == last(source.parent.property.nodes) then source.parent.node else null
116
+ else source
117
+ value expected: if context != null then value_expected(context) else null
118
+ value target: if site is ts_update || site is ts_assignment && site.enum_mutation then source
119
+ else if site is (ts_as, ts_type_assertion) then site.type.node
120
+ else if site.type != null then site.type.node
121
+ else if context != null && expected.present == true then value_expected_handle(context)
122
+ else if context != null then enum_annotation_target(context) else null
123
+ value owner: if source is ts_return.argument || source is ts_arrow.body then return_value_owner(source) else null
124
+ value asynchronous: owner != null && contains([] owner.modifier, "async")
125
+ value receiver_handle: if asynchronous && target != null then ts_awaited_value(target, target) else target
126
+ value sender_handle: if asynchronous && source != null then ts_awaited_value(source, source) else source
127
+ value receiver: if receiver_handle != null then value_constituents(receiver_handle) else null
128
+ value sender: if sender_handle != null then value_constituents(sender_handle) else null
129
+ value pairs: enum_relation_pairs(sender_handle, receiver_handle, source)
130
+ value names: {
131
+ value pairs
132
+ flat_map (pair) pair.receiver ?? []
133
+ flat_map (arm) enum_origins(arm)
134
+ map (part) part.carrier.description.name
135
+ filter (name) name != null
136
+ unique
137
+ }
138
+ value if receiver == null || sender == null || len(names) == 0
139
+ || enum_bitwise_shape(unparenthesised_node(source))
140
+ && any(enum_bitwise_leaves(unparenthesised_node(source)), leaf => value_constituents(leaf) == null) then []
141
+ else [{names: names, bad: any(pairs, pair => enum_root_bad(pair.sender, pair.receiver))
142
+ && !enum_safe_bitwise(unparenthesised_node(source), receiver)}]
143
+ }
144
+ fn enum_context_bad(context) = context.bad
145
+ fn enum_names_text(contexts) = {
146
+ value contexts
147
+ fold ([]) (names, context) names context.names
148
+ unique
149
+ sort
150
+ map (name) "'" name "'"
151
+ join ", "
152
+ }
153
+
154
+ fn enum_declaration_owner(n) = {
155
+ value parent: n.parent.node
156
+ value if parent == null then null
157
+ else if parent is (ts_declarator, ts_class_property) then parent
158
+ else if parent is (ts_conditional, ts_logical) then enum_declaration_owner(parent)
159
+ else null
160
+ }
161
+ fn enum_report_site(n) = if n not ts_property then n else {
162
+ value object: n.parent.node
163
+ value declared: enum_declaration_owner(object)
164
+ value if declared == null then n
165
+ else if declared is ts_class_property then declared
166
+ else if declared.init.node != object then declared
167
+ else n
168
+ }
169
+
170
+ group enum_transfer ts_declarator, ts_parameter, ts_class_property, ts_assignment, ts_assignment_pattern, ts_property, ts_as, ts_type_assertion, ts_update
171
+ in enum_transfer let enum_mutation is ts_update || is ts_assignment && !contains(["=", "&&=", "&=", "??=", "^=", "|=", "||="], node.operator)
172
+ in enum_transfer let enum_value
173
+ if is (ts_declarator, ts_parameter, ts_class_property) then init.node
174
+ else if is (ts_assignment, ts_assignment_pattern) then (if enum_mutation then left.node else right.node)
175
+ else if is (ts_as, ts_type_assertion) then expression.node
176
+ else if is ts_update then argument.node
177
+ else value.node ?? key.node
178
+ in enum_transfer let enum_contexts enum_policy(node, enum_value)
179
+ in enum_transfer let enum_selected_contexts enum_contexts
180
+ in enum_transfer let enum_reports enum_value != null && len(enum_contexts) > 0 && (enum_mutation || unparenthesised_node(enum_value) not (ts_object, ts_array)) && (if enum_mutation then ts_value_has_enum(enum_value) else any(enum_selected_contexts, context => enum_context_bad(context)))
181
+ in ts_node let enum_checked
182
+ if is ts_property then parent.enum_checked == true
183
+ else if is (ts_object, ts_array, ts_parenthesized, ts_as, ts_type_assertion, ts_non_null, ts_satisfies, ts_spread, ts_conditional, ts_logical)
184
+ then parent.enum_checked == true || parent is enum_transfer && node == parent.enum_value && parent.enum_reports
185
+ else false
186
+ in enum_transfer rule es-no-unsafe-enum-assignment {
187
+ when enum_reports && !(is ts_property && enum_checked)
188
+ let contexts enum_selected_contexts
189
+ report at if node is ts_parameter then { start: node.name.node.offset, end: end_offset }
190
+ else if node is ts_assignment_pattern && node.left.node == null then { start: parent.key.node.offset, end: end_offset }
191
+ else enum_report_site(node)
192
+ warn if enum_mutation then "This mutation can produce a value outside of the expected enum {enum_names_text(contexts)}."
193
+ else if is (ts_as, ts_type_assertion) then "The value asserted here does not have a shared enum type with the expected enum {enum_names_text(contexts)}."
194
+ else "The value assigned here does not have a shared enum type with the expected enum {enum_names_text(contexts)}."
195
+ }
196
+ in ts_array.element rule es-no-unsafe-enum-assignment:element {
197
+ when !parent.enum_checked && unparenthesised_node(node) not (ts_object, ts_array)
198
+ let contexts enum_policy(node, node)
199
+ when any(contexts, context => enum_context_bad(context))
200
+ warn "The value assigned here does not have a shared enum type with the expected enum {enum_names_text(contexts)}."
201
+ }
202
+ in ts_object.property rule es-no-unsafe-enum-assignment:spread {
203
+ when node is ts_spread && !parent.enum_checked
204
+ let contexts enum_policy(node, node)
205
+ when any(contexts, context => enum_context_bad(context))
206
+ warn "The value assigned here does not have a shared enum type with the expected enum {enum_names_text(contexts)}."
207
+ }
208
+ in ts_jsx_expression.expression rule es-no-unsafe-enum-assignment:jsx {
209
+ when unparenthesised_node(node) not (ts_object, ts_array)
210
+ let contexts enum_policy(node, node)
211
+ when any(contexts, context => enum_context_bad(context))
212
+ warn "The value assigned here does not have a shared enum type with the expected enum {enum_names_text(contexts)}."
213
+ }
214
+ in ts_call.argument, ts_new.argument, ts_template.expression rule es-no-unsafe-enum-assignment:arguments {
215
+ when !(is ts_template.expression) || parent is ts_tagged_template.quasi
216
+ when unparenthesised_node(node) not (ts_object, ts_array)
217
+ let contexts enum_policy(node, node)
218
+ when any(contexts, context => enum_context_bad(context))
219
+ warn "The argument passed here does not have a shared enum type with the expected enum {enum_names_text(contexts)}."
220
+ }
221
+ in ts_return.argument, ts_arrow.body rule es-no-unsafe-enum-assignment:returns {
222
+ when unparenthesised_node(node) not (ts_object, ts_array, ts_block)
223
+ let contexts enum_policy(node, node)
224
+ when any(contexts, context => enum_context_bad(context))
225
+ report at if is ts_return.argument then parent.node else node
226
+ warn "The value returned here does not have a shared enum type with the expected enum {enum_names_text(contexts)}."
227
+ }
228
+ in ts_index.index rule es-no-unsafe-enum-assignment:access {
229
+ let contexts enum_policy(parent.node, node)
230
+ when any(contexts, context => enum_context_bad(context))
231
+ warn "The computed key used here does not have a shared enum type with the expected enum {enum_names_text(contexts)}."
232
+ }
233
+ }
@@ -0,0 +1,67 @@
1
+ # Shared expression autofix boundaries and precedence.
2
+ import .ts/parentheses
3
+ import .ts/helpers
4
+
5
+ define {
6
+
7
+ fn expression_fix_clean(n) = !{
8
+ value overlapping(ts_comment, n.offset, n.end_offset)
9
+ any (comment) n.offset <= comment.offset && comment.end_offset <= n.end_offset
10
+ }
11
+ fn expression_fix_continuation(text) = any(["(", "[", "/", "+", "`", "-"], token => starts_with(text, token))
12
+ fn expression_fix_edits(source, start, edits) = {
13
+ value edits
14
+ sort_by (edit) edit.start
15
+ result: fold ({ end: start, text: "" }) (state, edit) {
16
+ end: edit.end,
17
+ text: state.text byte_slice(source, state.end - start, edit.start - start) edit.text
18
+ }
19
+ value result.text byte_slice(source, result.end - start)
20
+ }
21
+ fn expression_fix_retains_comments(n, replacement) = {
22
+ value overlapping(ts_comment, n.offset, n.end_offset)
23
+ filter (comment) n.offset <= comment.offset && comment.end_offset <= n.end_offset
24
+ # Count all source occurrences: another comment or string cannot cover a loss.
25
+ all (comment) {
26
+ value required: split(n.text, comment.text)
27
+ value pieces: split(replacement, comment.text)
28
+ value count(pieces) >= count(required)
29
+ }
30
+ }
31
+ fn expression_fix_wrap(text, needed) = if needed then "(" text ")" else text
32
+ fn expression_fix_separator(left, right) = if re_test(left, /[\w$\u{200c}\u{200d}]$/) && re_test(right, /^[\w$\u{200c}\u{200d}]/)
33
+ || ends_with(left, "+") && starts_with(right, "+")
34
+ || ends_with(left, "-") && starts_with(right, "-")
35
+ || ends_with(left, "/") && (starts_with(right, "/") || starts_with(right, "*")) then " " else ""
36
+ fn expression_fix_edges(n, text) =
37
+ ((if n.token_neighbours.before.end == n.offset then expression_fix_separator(n.token_neighbours.before.text, text) else "")
38
+ text
39
+ (if n.token_neighbours.after.start == n.end_offset then expression_fix_separator(text, n.token_neighbours.after.text) else ""))
40
+ fn expression_fix_parens(source, n, operand) = if n.parent is ts_parenthesized then 0
41
+ else structure_parens_required(source, n.parent.node, operand, n)
42
+ fn expression_fix_statement(n) = {
43
+ value n
44
+ walk (current) if current.parent.offset == n.offset then current.parent.node else null
45
+ find_last (current) current is ts_expression_statement
46
+ }
47
+ fn expression_fix_before_nodes(n) = if n.token_neighbours.before == null then []
48
+ else overlapping(ts_node, n.token_neighbours.before.start, n.token_neighbours.before.end)
49
+ fn expression_fix_declaration(n) = n.parent is (ts_program, ts_block, ts_export, ts_case, ts_if, ts_labeled, ts_while, ts_do_while, ts_for, ts_for_in, ts_for_of)
50
+ fn expression_fix_asi(n, before, owners, replacement) =
51
+ if before == null || { value [":", ";", "{{", "=>", "++", "--"]; contains before.text } then false
52
+ else if starts_with(replacement, "[") && any(owners, owner => owner is ts_as && owner.end_offset == before.end) then false
53
+ else if any(owners, owner => owner is ts_interface && owner.end_offset == before.end) then false
54
+ else if any(owners, owner => owner is ts_type_alias && owner.end_offset == before.end) then null
55
+ else if before.text == ")" then !any(owners, owner =>
56
+ owner is ts_do_while && owner.end_offset == before.end
57
+ || owner is ts_if && owner.consequent.offset >= before.end
58
+ || owner is (ts_for, ts_for_in, ts_for_of, ts_while, ts_with) && owner.body.offset >= before.end)
59
+ else if before.text == "}}" then any(owners, owner => owner.end_offset == before.end
60
+ && (owner is (ts_object, ts_arrow) || owner is (ts_function, ts_class) && !expression_fix_declaration(owner)))
61
+ else if any(owners, owner => owner.end_offset == before.end && owner is (ts_break, ts_continue, ts_debugger, ts_return, ts_yield)
62
+ || owner is ts_declarator && owner.init == null && owner.name.end_offset == before.end
63
+ || owner is (ts_import, ts_export) && owner.from.end_offset == before.end) then false
64
+ else if before.text == "else" && n.parent is ts_if || before.text == "do" && n.parent is ts_do_while then false
65
+ else true
66
+
67
+ }