@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,477 @@
1
+ # .ts/assertions: assertion syntax, type-aware and non-null policy.
2
+ import .ts/expression-fixes
3
+ import .ts/modules
4
+ import .ts/values/inspection
5
+
6
+ define {
7
+
8
+ in ts_number, ts_string, ts_boolean, ts_bigint, ts_null, ts_regex let raw_literal_expression text
9
+ in ts_parenthesized let raw_literal_expression expression.raw_literal_expression
10
+ in ts_literal_type let literal_type_text value
11
+ in ts_as, ts_type_assertion let use_const_assertion type.literal_type_text != null && type.literal_type_text == expression.raw_literal_expression
12
+ in ts_declarator, ts_class_property let use_const_annotation type.literal_type_text != null && type.literal_type_text == init.raw_literal_expression
13
+ in ts_as.type, ts_type_assertion.type rule es-prefer-as-const {
14
+ when parent.use_const_assertion
15
+ warn "Expected a `const` instead of a literal type assertion."
16
+ fix at node as "const"
17
+ }
18
+ in ts_declarator.type, ts_class_property.type rule es-prefer-as-const:annotation {
19
+ when parent.use_const_annotation
20
+ warn "Expected a `const` assertion instead of a literal type annotation."
21
+ }
22
+
23
+ in ts_type_assertion let assertion_style_operand expression.node
24
+ in ts_type_assertion let assertion_style_type type.text
25
+ fn assertion_style_as(source, n) = {
26
+ value operand: n.assertion_style_operand
27
+ value operand_parens: operand.precedence < 10 || operand is ts_instantiation
28
+ value body: expression_fix_wrap(operand.text, operand_parens) " as " n.assertion_style_type
29
+ # Describe the replacement, not the unary angle assertion being removed.
30
+ value shape: {
31
+ kind: "ts_as", precedence: 10, offset: n.offset,
32
+ structure_parens_hazard: !operand_parens && operand.structure_parens_hazard,
33
+ structure_parens_has_in: !operand_parens && operand.structure_parens_has_in,
34
+ structure_parens_has_call: operand.structure_parens_has_call,
35
+ structure_parens_optional: false
36
+ }
37
+ value required: if n.parent is ts_arrow then 1 else expression_fix_parens(source, n, shape)
38
+ value replacement: if required == null then null else expression_fix_wrap(body, required > 0)
39
+ value if replacement != null && expression_fix_retains_comments(n, replacement) then assertion_fix_output(n, replacement) else null
40
+ }
41
+
42
+ in ts_object let assertion_literal_kind "object"
43
+ in ts_array let assertion_literal_kind "array"
44
+ in ts_parenthesized let assertion_literal_kind expression.assertion_literal_kind
45
+ in ts_as, ts_type_assertion rule es-consistent-type-assertions {
46
+ option assertionStyle ("as" | "angle-bracket" | "never") = "as" "Preferred assertion syntax."
47
+ when is ts_as && assertionStyle != "as" || is ts_type_assertion && assertionStyle != "angle-bracket"
48
+ when !(assertionStyle == "never" && type == "const")
49
+ warn if assertionStyle == "never" then "Do not use any type assertions." else if assertionStyle == "as" then "Use 'as {type}' instead of '<{type}>'." else "Use '<{type}>' instead of 'as {type}'."
50
+ let edit if assertionStyle == "as" then assertion_style_as(source, node) else null
51
+ fix at edit.target if edit != null as edit.text
52
+ }
53
+ in ts_template let tagged_substitution parent is ts_tagged_template
54
+ in ts_as, ts_type_assertion, ts_parenthesized let assertion_argument_context if parent is ts_parenthesized then parent.assertion_argument_context else parent is (ts_new, ts_call, ts_throw, ts_assignment_pattern, ts_jsx_expression) || is ts_parameter.init || parent is ts_template && parent.tagged_substitution
55
+ in ts_as, ts_type_assertion {
56
+ let preferred_assertion_style if is ts_as then "as" else "angle-bracket"
57
+ let literal_assertion_eligible type != "any" && type != "unknown" && type != "const"
58
+ }
59
+ in ts_as, ts_type_assertion rule es-consistent-type-assertions:object {
60
+ option assertionStyle ("as" | "angle-bracket" | "never") = "as" "Preferred assertion syntax."
61
+ default option mode ("allow" | "allow-as-parameter" | "never") = "allow" "When literal assertions are allowed."
62
+ when literal_assertion_eligible && expression.assertion_literal_kind == "object"
63
+ when assertionStyle == preferred_assertion_style
64
+ when mode == "never" || mode == "allow-as-parameter" && !assertion_argument_context
65
+ warn "Always prefer const x: T = {{ ... }}."
66
+ }
67
+ in ts_as, ts_type_assertion rule es-consistent-type-assertions:array {
68
+ option assertionStyle ("as" | "angle-bracket" | "never") = "as" "Preferred assertion syntax."
69
+ default option mode ("allow" | "allow-as-parameter" | "never") = "allow" "When literal assertions are allowed."
70
+ when literal_assertion_eligible && expression.assertion_literal_kind == "array"
71
+ when assertionStyle == preferred_assertion_style
72
+ when mode == "never" || mode == "allow-as-parameter" && !assertion_argument_context
73
+ warn "Always prefer const x: T[] = [ ... ]."
74
+ }
75
+
76
+ fn assertion_outer(n) = { value n; walk (current) if current.parent is ts_parenthesized then current.parent else null; last }
77
+ fn assertion_literal_preserved(n) = assertion_outer(n).parent is ts_declarator && assertion_outer(n).parent.parent.kind == "const" || assertion_outer(n).parent is ts_class_property && { value [] assertion_outer(n).parent.modifier; contains "readonly" }
78
+ fn assertion_const_scalar(n) = {
79
+ value operand: unparenthesised_node(n.expression.node)
80
+ value literal: operand is (ts_number, ts_string, ts_boolean, ts_bigint)
81
+ || operand is ts_template && count(operand.expression.nodes) == 0
82
+ || operand is ts_unary && contains(["+", "-"], operand.operator)
83
+ && unparenthesised_node(operand.argument.node) is (ts_number, ts_bigint)
84
+ value parts: value_parts(operand)
85
+ value assertion_literal_preserved(n) && (literal || ts_value_has_enum(operand) == true
86
+ && parts != null && count(parts) > 0 && all(parts, arm => any(arm.parts, part =>
87
+ part.identity_known && part.identity.kind == "value" && count(part.uncertain_traits) == 0)))
88
+ }
89
+ fn assertion_call(n) = {
90
+ value n
91
+ unparenthesised_node
92
+ walk (current) if current is ts_await then unparenthesised_node(current.argument.node) else if current is ts_non_null then unparenthesised_node(current.expression.node) else if current is ts_sequence then { value current.expression.nodes; last; unparenthesised_node } else null
93
+ last
94
+ }
95
+ fn assertion_generic_call(n) = {
96
+ value call: assertion_call(n)
97
+ value callee: unparenthesised_node(call.callee.node)
98
+ value binding: if callee is ts_identifier then name_binding(callee) else null
99
+ value declaration: binding.node.parent.node
100
+ value target: if callee is (ts_function, ts_arrow) then callee
101
+ else if count(binding.declarations) == 1 && declaration is ts_function then declaration
102
+ else if count(binding.declarations) == 1 && declaration is ts_declarator && declaration.type == null
103
+ then unparenthesised_node(declaration.init.node) else null
104
+ value if call not ts_call || call.type_argument != null then false
105
+ else if target is (ts_function, ts_arrow) then count(target.type_parameter) > 0 else {
106
+ value alternatives: value_signatures(callee)
107
+ value headers: flat_map(alternatives ?? [], arm => arm)
108
+ value parameters: map(headers, header => site_field(call, header.site, "type_parameter"))
109
+ value if alternatives != null && count(headers) > 0
110
+ && all(parameters, formals => formals != null && count(formals) == 0) then false else null
111
+ }
112
+ }
113
+ fn assertion_same_handles(source, target) = if source == null || target == null then null else
114
+ if all(source, arm => any(target, other => arm.handle == other.handle))
115
+ && all(target, arm => any(source, other => arm.handle == other.handle)) then true else null
116
+ fn assertion_part_known(part) = count(part.uncertain_traits) == 0
117
+ && !any(["any", "unknown", "error", "parameter"], trait => contains(part.traits, trait))
118
+ fn assertion_parts_known(arms) = arms != null && all(arms, arm => count(arm.parts) > 0
119
+ && all(arm.parts, part => assertion_part_known(part)))
120
+ fn assertion_receiver_known(arms) = arms != null && count(arms) > 0
121
+ && all(arms, arm => count(arm.parts) > 0
122
+ && all(arm.parts, part => part.traits == ["unknown"]
123
+ || assertion_part_known(part)))
124
+ fn assertion_parts_non_null(arms) = arms != null && count(arms) > 0
125
+ && all(arms, arm => any(arm.parts, part => assertion_part_known(part)
126
+ && !any(["null", "undefined", "void"], trait => contains(part.traits, trait))))
127
+ fn assertion_scalar_identity_known(value) = value != null && all(value, arm => all(arm, part =>
128
+ part.identity_known && part.carrier_known && part.product_known && count(part.uncertain_traits) == 0
129
+ && part.carrier == null && part.product == null && (part.identity != null || any(
130
+ ["number", "string", "boolean", "bigint", "symbol", "null", "undefined", "void"],
131
+ trait => contains(part.traits, trait)))))
132
+ fn assertion_same_value(source, target) = {
133
+ value a: value_constituents(source)
134
+ value b: value_constituents(target)
135
+ value assertion_same_handles(value_parts(source), value_parts(target)) == true
136
+ || assertion_scalar_identity_known(a) && assertion_scalar_identity_known(b) && a == b
137
+ }
138
+ fn assertion_scalar_types(n) = {
139
+ value arms: value_constituents(n)
140
+ value if !assertion_scalar_identity_known(arms) then null else map(arms, arm => map(arm, part => {
141
+ traits: filter(part.traits, trait => !contains(["truthy", "falsy"], trait)),
142
+ identity: part.identity
143
+ }))
144
+ }
145
+ fn assertion_scalar_types_equal(left, right) = left != null && right != null && count(left) > 0 && count(right) > 0
146
+ && all(left, arm => count(arm) > 0 && contains(right, arm))
147
+ && all(right, arm => count(arm) > 0 && contains(left, arm))
148
+ fn assertion_same_scalar_type(source, target) = assertion_scalar_types_equal(
149
+ assertion_scalar_types(source), assertion_scalar_types(target))
150
+ fn assertion_undefined_arm(arm) = count(arm) == 1 && contains(first(arm).traits, "undefined")
151
+ fn assertion_same_optional_type(source, target) = {
152
+ value left: assertion_scalar_types(source)
153
+ value right: assertion_scalar_types(target)
154
+ value ts_compiler_option(source, "exactOptionalPropertyTypes") == true
155
+ && left != null && right != null && any(left, arm => assertion_undefined_arm(arm))
156
+ && any(right, arm => assertion_undefined_arm(arm))
157
+ && assertion_scalar_types_equal(filter(left, arm => !assertion_undefined_arm(arm)),
158
+ filter(right, arm => !assertion_undefined_arm(arm)))
159
+ }
160
+ fn assertion_type_parameter(n) = {
161
+ value bare: ts_value_bare_type(n)
162
+ value declarations: if bare is ts_type_reference && count(bare.type_argument.nodes) == 0
163
+ then name_binding(bare.name.node).declarations else []
164
+ value if count(declarations) == 1 && first(declarations).parent is ts_type_parameter
165
+ then first(declarations).parent.node else null
166
+ }
167
+ fn assertion_parameter_constraint(parameter) = {
168
+ value base: { value parameter; walk (current) assertion_type_parameter(current.constraint.node); last }
169
+ value constraint: base.constraint.node
170
+ value parts: value_parts(constraint)
171
+ value top: parts != null && count(parts) > 0 && all(parts, arm => count(arm.parts) == 1
172
+ && (first(arm.parts).traits == ["unknown"] || first(arm.parts).traits == ["any"]))
173
+ value {type: constraint, unconstrained: base != null && (constraint == null || top)}
174
+ }
175
+ fn assertion_relation_parts_known(arms) = arms != null
176
+ && all(arms, arm => count(arm.parts) > 0 && all(arm.parts, part => assertion_part_known(part)
177
+ || part.traits == ["parameter"] && part.carrier_known && part.carrier.description.kind == "parameter"))
178
+ fn assertion_parameter_present(arms, parameter) = {
179
+ value site: declaration_identity(parameter)
180
+ value site != null && all(arms, arm => any(arm.parts, part => part.carrier_known
181
+ && part.carrier.file == site.file && part.carrier.node == site.node))
182
+ }
183
+ fn assertion_unsafe_narrowing(n) = {
184
+ value source: value_parts(n.expression.node)
185
+ value target: value_parts(n.type.node)
186
+ value parameter: assertion_type_parameter(n.type.node)
187
+ value accepted: value_accepts(n.type.node, n.expression.node, "value", "value")
188
+ value constraint: assertion_parameter_constraint(parameter)
189
+ value declaration: first(name_binding(unparenthesised_node(n.expression.node)).declarations).parent.node
190
+ value source_parameter: assertion_type_parameter(declaration.type.node)
191
+ value source_constraint: assertion_parameter_constraint(source_parameter)
192
+ value label: if parameter != null then parameter.name.text else ts_value_print_handle(n.type.node)
193
+ value if !assertion_relation_parts_known(source) || !assertion_relation_parts_known(target)
194
+ || accepted == true || parameter != null && assertion_parameter_present(source, parameter) then null
195
+ else if parameter != null && constraint.unconstrained then {kind: "unconstrained", label: label}
196
+ else if parameter != null && value_accepts(constraint.type, n.expression.node, "value", "value") == true
197
+ then {kind: "constraint", label: label}
198
+ else if accepted == false || source_parameter != null && source_constraint.unconstrained
199
+ && assertion_parts_known(target) then {kind: "narrow", label: label}
200
+ else null
201
+ }
202
+ fn assertion_subject(n, ignored) = {
203
+ value n.expression.node
204
+ walk (current) {
205
+ value cast: unparenthesised_node(current)
206
+ value if cast is (ts_as, ts_type_assertion) && cast.type.text != "const"
207
+ && !contains(ignored, cast.type.text) then cast.expression.node else null
208
+ }
209
+ last
210
+ }
211
+ fn assertion_parts_contain_any(parts) = any(parts ?? [], arm => any(arm.parts, part => contains(part.traits, "any")))
212
+ fn assertion_handles_contain_any(handles, depth) = handles != null && depth > 0
213
+ && any(handles, handle => assertion_parts_deep_contain_any(value_parts(handle), depth))
214
+ fn assertion_parts_deep_contain_any(parts, depth) = parts != null && depth > 0 && any(parts, arm => any(arm.parts, part =>
215
+ contains(part.traits, "any")
216
+ || part.carrier_known && part.carrier != null && assertion_handles_contain_any(part.carrier.arguments, depth - 1)
217
+ || part.product_known && part.product != null && assertion_handles_contain_any(part.product.elements, depth - 1)))
218
+ fn assertion_contains_any(value) = assertion_parts_deep_contain_any(value_parts(value), 4)
219
+ # Literal operands only keep a literal type through a const/readonly declaration.
220
+ fn assertion_literal_expression(n) = n.expression.node is (ts_number, ts_string, ts_boolean, ts_bigint, ts_null, ts_regex, ts_array, ts_template, ts_arrow, ts_function, ts_class)
221
+ fn assertion_empty_object_expression(n) = n.expression.node is ts_object && count(n.expression.node.property.nodes) == 0
222
+ fn assertion_conceptual_literal(n) = assertion_literal_expression(n) || assertion_empty_object_expression(n)
223
+ fn assertion_parameter_value(value) = any(value_trait_parts(value, ["parameter"]) ?? [], arm => any(arm.parts, part => contains(part.traits, "parameter")))
224
+ fn assertion_written_annotation(subject) = {
225
+ value declaration: if subject is ts_identifier then subject.declaration else null
226
+ value owner: if declaration != null then declaration.parent else null
227
+ value if owner != null && owner.type != null then owner.type.text else null
228
+ }
229
+ fn assertion_named_unchanged(n, subject) = {
230
+ value written: assertion_written_annotation(subject)
231
+ value printed: ts_value_print_handle(subject)
232
+ value if assertion_parameter_value(subject) then true
233
+ else if written != null then written == n.type.text
234
+ else !contains(["object", "unknown"], printed) && printed == ts_value_print_handle(n.type.node)
235
+ }
236
+ # The receiver's own typing (union, satisfies, another assertion) gives no contextual type.
237
+ fn assertion_property_receiver(n) = {
238
+ value property: n.parent.node
239
+ value object: if property is ts_property then property.parent.node else null
240
+ value parent: if object is ts_object then object.parent.node else null
241
+ value expected: if object is ts_object then value_expected_handle(object) else null
242
+ value if parent is (ts_as, ts_type_assertion, ts_satisfies) then true
243
+ else if parent is ts_call && parent.parent.node is ts_satisfies then true
244
+ else if object is ts_object && expected == null then true
245
+ else if property is ts_property && assertion_union_narrowing(n) then true
246
+ else false
247
+ }
248
+ # Selecting one arm of a union receiver keeps meaning; widening a whole union does not.
249
+ fn assertion_union_narrowing(n) = {
250
+ value subject: unparenthesised_node(assertion_subject(n, []))
251
+ value arms: value_alternatives(subject)
252
+ value expected: value_expected_handle(n)
253
+ value receiver_any: expected != null && ts_value_trait(value_trait_parts(expected, ["any"]) ?? [], "any")
254
+ value narrows: value_accepts(subject, n.type.node, "value", "value") == true
255
+ && value_accepts(n.type.node, subject, "value", "value") == false
256
+ value arms != null && count(arms) > 1 && receiver_any != true && narrows
257
+ }
258
+ # Overloads with differing parameter lists give the argument no single receiver type.
259
+ fn assertion_overloaded_argument(n) = {
260
+ value parent: n.parent.node
261
+ value arguments: if parent is (ts_call, ts_new) then parent.argument.nodes else null
262
+ value if arguments != null && any(arguments, argument => argument.node == n) then {
263
+ value signatures: call_parameters(parent)
264
+ value headers: flat_map(signatures ?? [], arm => arm)
265
+ value counts: map(headers, header => count(header.parameters))
266
+ value count(headers) > 1 && !all(counts, size => size == first(counts))
267
+ } else false
268
+ }
269
+ # Inside a generic call only a literal operand (or a property's declared type) is a receiver context.
270
+ fn assertion_generic_skip(n) = assertion_generic_context(n)
271
+ && !assertion_literal_expression(n) && n.parent.node not ts_property
272
+ # A literal asserted to an enum keeps a nominal member type instead of the literal's own.
273
+ fn assertion_enum_literal(n) = n.expression.node is (ts_number, ts_string, ts_boolean) && ts_value_has_enum(n) == true
274
+ fn assertion_context_identity(n) = if n.expression.node is ts_object
275
+ && count(n.expression.node.property.nodes) > 0
276
+ && !assertion_parts_contain_any(value_trait_parts(n.type.node, ["any"]))
277
+ && !assertion_parts_contain_any(value_trait_parts(n.expression.node, ["any"]))
278
+ && ts_value_trait(value_constituent_traits(n.type.node, ["unknown"]), "unknown") != true
279
+ && value_same(n.type.node, value_expected_handle(n.expression.node)) == true
280
+ && value_accepts(n.type.node, n.expression.node, "value", "value") == true then true else false
281
+ fn assertion_error_identity(n) = {
282
+ value annotation: n.type.node
283
+ value if annotation is ts_type_reference && name_binding(annotation.name.node) == null
284
+ then assertion_parts_contain_any(value_trait_parts(n.type.node, ["any"]))
285
+ && assertion_parts_contain_any(value_trait_parts(n.expression.node, ["any"]))
286
+ else false
287
+ }
288
+ # `NonNullable<X>` is `X` itself once `X` has no nullish arm.
289
+ fn assertion_non_nullable_identity(n, subject) = {
290
+ value annotation: n.type.node
291
+ value arguments: if annotation is ts_type_reference && annotation.name.text == "NonNullable"
292
+ then annotation.type_argument.nodes else null
293
+ value arguments != null && count(arguments) == 1 && !assertion_nullish_parts(value_trait_parts(subject, ["null", "undefined", "void"]))
294
+ && assertion_same_value(subject, first(arguments)) == true
295
+ }
296
+ fn assertion_same_shape(source, target) = if !assertion_parts_contain_any(value_trait_parts(source, ["any"]))
297
+ && !assertion_parts_contain_any(value_trait_parts(target, ["any"]))
298
+ && !assertion_nullish_parts(value_trait_parts(source, ["null", "undefined", "void"])) && !assertion_nullish_parts(value_trait_parts(target, ["null", "undefined", "void"]))
299
+ && value_accepts(target, source, "value", "value") == true
300
+ && value_accepts(source, target, "value", "value") == true then true else false
301
+ # `undefined`, `null` and `void` are mutually assignable without being the same type.
302
+ fn assertion_nullish_parts(parts) = any(parts ?? [], arm => any(arm.parts, part =>
303
+ contains(part.traits, "null") || contains(part.traits, "undefined") || contains(part.traits, "void")))
304
+ fn assertion_generic_callee(call) = {
305
+ value callee: unparenthesised_node(call.callee.node)
306
+ value binding: if callee is ts_identifier then name_binding(callee) else null
307
+ value declaration: if binding != null && count(binding.declarations) == 1 then first(binding.declarations).parent.node else null
308
+ value if declaration is ts_function then count(declaration.type_parameter) > 0
309
+ else any(flat_map(value_signatures(callee) ?? [], arm => arm),
310
+ header => site_field(call, header.site, "type_parameter") != null
311
+ && count(site_field(call, header.site, "type_parameter")) > 0)
312
+ }
313
+ fn assertion_generic_context(n) = {
314
+ value n
315
+ walk (current) if current is (ts_function, ts_arrow) && current.body is ts_block then null else current.parent
316
+ any (current) current is (ts_call, ts_new) && (current not ts_call || current.type_argument == null)
317
+ && assertion_generic_callee(current)
318
+ }
319
+ fn assertion_declared_same(n) = {
320
+ value declared: n.declaration.parent.type.node
321
+ value declared != null
322
+ && value_accepts(declared, n, "value", "value") == true
323
+ && value_accepts(n, declared, "value", "value") == true
324
+ }
325
+ fn assertion_context_skipped(n) = {
326
+ value parent: n.parent.node
327
+ value if assertion_parts_contain_any(value_trait_parts(n.type.node, ["any"])) then parent is ts_logical || assertion_generic_context(n)
328
+ else if n.expression.node is ts_template && count(n.expression.node.expression.nodes) > 0 then true
329
+ else if n.expression.node is ts_array then true
330
+ else if n.type.node is ts_union && (n.expression.node is ts_null || n.expression.node is ts_identifier && n.expression.node.text == "undefined") then true
331
+ else if assertion_enum_literal(n) then true
332
+ else if assertion_generic_skip(n) then true
333
+ else if assertion_property_receiver(n) then true
334
+ else if assertion_overloaded_argument(n) then true
335
+ else if parent is (ts_as, ts_type_assertion, ts_spread, ts_satisfies) then true
336
+ else if parent is ts_assignment && parent.operator != "=" && parent.right.offset == n.offset then true
337
+ else if parent is ts_assignment && parent.right.offset == n.offset && parent.parent not ts_expression_statement then true
338
+ else false
339
+ }
340
+ fn assertion_check(n, ignored, check_const) = {
341
+ value literal_const: n.type.text == "const"
342
+ value raw_subject: if literal_const then n.expression.node else assertion_subject(n, ignored)
343
+ value subject: if literal_const then raw_subject else unparenthesised_node(raw_subject)
344
+ value operand: value_parts(subject)
345
+ value annotation: if literal_const then value_parts(n) else value_parts(n.type.node)
346
+ value contextual: !literal_const && assertion_outer(n).parent not (ts_as, ts_type_assertion)
347
+ && !assertion_contains_any(n.expression.node)
348
+ && (value_accepts(n, subject, "expected", "value") == true
349
+ || value_accepts(n, n.expression.node, "expected", "widened") == true
350
+ || n.parent.node is ts_property && value_accepts(n.expression.node, n.expression.node, "expected", "value") == true)
351
+ && !assertion_context_skipped(n)
352
+ value chain_contextual: !literal_const && n.expression.node is (ts_as, ts_type_assertion)
353
+ && (assertion_parts_contain_any(value_trait_parts(n.expression.node, ["any"]))
354
+ || ts_value_trait(value_constituent_traits(n.expression.node, ["unknown"]), "unknown") == true)
355
+ && value_accepts(n, subject, "expected", "value") == true
356
+ value receiver: contextual || chain_contextual
357
+ value identity: !literal_const && assertion_context_identity(n)
358
+ value same: if literal_const then assertion_same_handles(operand, annotation) == true
359
+ else if assertion_context_identity(n) == true then true
360
+ else if assertion_error_identity(n) == true then true
361
+ else if assertion_non_nullable_identity(n, subject) == true then true
362
+ else if assertion_parts_known(operand) && assertion_parts_known(annotation)
363
+ && (assertion_same_scalar_type(subject, n.type.node) || assertion_same_optional_type(subject, n.type.node)) then n.expression.node is ts_template || !assertion_conceptual_literal(n) || assertion_literal_preserved(n)
364
+ else assertion_parts_known(operand) && assertion_parts_known(annotation)
365
+ && !assertion_conceptual_literal(n)
366
+ && !assertion_parts_deep_contain_any(operand, 4) && !assertion_parts_deep_contain_any(annotation, 4)
367
+ && (assertion_same_value(subject, n.type.node)
368
+ || (subject not (ts_object, ts_array) || assertion_context_identity(n) == true)
369
+ && assertion_named_unchanged(n, subject)
370
+ && assertion_same_shape(subject, n.type.node))
371
+ value generic: assertion_generic_call(subject)
372
+ value literal_preserved: !any(annotation ?? [], arm => any(arm.parts, part =>
373
+ part.identity_known && part.identity.kind == "value")) || assertion_literal_preserved(n)
374
+ || subject is ts_call && generic == false
375
+ value enum_preserved: ts_value_has_enum(n) != null && ts_value_has_enum(n) == ts_value_has_enum(subject)
376
+ value if contains(ignored, n.type.text) || literal_const && (!check_const || !assertion_const_scalar(n))
377
+ || !receiver && (!same || !identity && subject is (ts_object, ts_array)
378
+ || !literal_const && subject is ts_template && count(subject.expression.nodes) == 0
379
+ || !literal_preserved || !enum_preserved) || generic == null then null
380
+ else {subject: subject, raw_subject: raw_subject, contextual: receiver && !same, generic: generic}
381
+ }
382
+ fn assertion_non_null_style(n) = {
383
+ value source: value_parts(n.expression.node)
384
+ value target: value_parts(n.type.node)
385
+ value retained: filter(source ?? [], arm => !any(arm.parts, part =>
386
+ contains(part.traits, "null") || contains(part.traits, "undefined")))
387
+ value non_null: value_union(first(retained).handle, map(slice(retained, 1), arm => arm.handle))
388
+ value if !assertion_parts_known(source) || !assertion_parts_non_null(target) then null
389
+ else count(retained) < count(source)
390
+ && (assertion_same_handles(retained, target) == true || assertion_same_value(non_null, n.type.node)
391
+ || assertion_same_scalar_type(non_null, n.type.node))
392
+ }
393
+ fn assertion_unassigned(n) = n is ts_identifier && (n.declaration == null && n.resolved != "global" || n.declaration.parent is ts_declarator && n.declaration.ambient != true && (n.declaration.parent.init == null && { value n.declaration.parent.token_texts; slice 1 2; first } != "!"
394
+ && assertion_declared_same(n) == true || n.declaration.parent.parent.kind == "var" && {
395
+ value n.declaration.parent.parent.parent
396
+ walk (current) current.parent
397
+ any (current) current is ts_block && (n.offset < current.offset || n.end_offset > current.end_offset)
398
+ }))
399
+ fn assertion_fix_body(source, n) = {
400
+ value operand: n.expression.node
401
+ value text: if n is ts_type_assertion then
402
+ byte_slice(source, operand.token_neighbours.before.end, n.expression.offset) operand.text
403
+ else if n is ts_as then operand.text { value source; byte_slice n.expression.end_offset operand.token_neighbours.after.start; trim_end }
404
+ else operand.text byte_slice(source, n.expression.end_offset, n.last_token.start)
405
+ value {node: operand, text: text}
406
+ }
407
+ fn assertion_fix_enclosed(n) = {
408
+ value n
409
+ walk (current) if current == n.structure_parens_context then null else current.parent.node
410
+ any (current) current is ts_parenthesized
411
+ }
412
+ fn assertion_fix_parens(source, n, body, needed) = if needed == null then null else if n.parent is ts_parenthesized then 0 else {
413
+ value result: if n.structure_parens_context != null && n.structure_parens_context not ts_return && !assertion_fix_enclosed(n) && body.structure_parens_hazard == true
414
+ || n.structure_parens_for_init && body.structure_parens_has_in == true
415
+ || n.structure_parens_context is ts_return && { value source; byte_slice n.structure_parens_context.offset body.offset; re_test /[\n\r\u{2028}\u{2029}]/ } then 1 else 0
416
+ value max(needed, result)
417
+ }
418
+ fn assertion_fix_result(n, text, asi) = if asi == null then null else {
419
+ target: n, text: (if asi then ";" else "") expression_fix_edges(n, text)
420
+ }
421
+ fn assertion_fix_output(n, text) = {
422
+ value statement: expression_fix_statement(n)
423
+ value asi: if statement != null && {
424
+ value text
425
+ trim_start
426
+ slice 0 1
427
+ then (head) contains(["(", "[", "/", "`"], head)
428
+ }
429
+ then {
430
+ value owners: expression_fix_before_nodes(n)
431
+ value expression_fix_asi(statement, n.token_neighbours.before, owners, text)
432
+ } else false
433
+ value assertion_fix_result(n, text, asi)
434
+ }
435
+ fn assertion_fix_wrap_output(n, body, needed) = if needed == null then null
436
+ else { value body.text; text: expression_fix_wrap (needed > 0); value assertion_fix_output(n, text) }
437
+ fn assertion_fix_with_body(source, n, body) = {
438
+ value precedence_parens: expression_fix_parens(source, n, body.node)
439
+ value required_parens: assertion_fix_parens(source, n, body.node, precedence_parens)
440
+ value assertion_fix_wrap_output(n, body, required_parens)
441
+ }
442
+ fn assertion_fix(source, n) = {
443
+ value body: assertion_fix_body(source, n)
444
+ value assertion_fix_with_body(source, n, body)
445
+ }
446
+
447
+ in ts_as, ts_type_assertion rule es-no-unnecessary-type-assertion {
448
+ option typesToIgnore string[] = [] "Ignore these written assertion types"
449
+ option checkLiteralConstAssertions boolean = false "Check const assertions on literal values"
450
+ let check assertion_check(node, typesToIgnore, checkLiteralConstAssertions)
451
+ when check != null
452
+ let owner assertion_outer(node).parent.node
453
+ when owner not (ts_as, ts_type_assertion) || assertion_check(owner, typesToIgnore, checkLiteralConstAssertions) == null
454
+ warn if check.generic then "The type arguments for this generic call may be inferred from the assertion. Specify them explicitly instead." else if !check.contextual then "This assertion is unnecessary since it does not change the type of the expression." else "This assertion is unnecessary since the receiver accepts the original type of the expression."
455
+ let chain check.subject != unparenthesised_node(expression.node)
456
+ let base if expression_fix_retains_comments(check.raw_subject, check.subject.text) then check.subject else check.raw_subject
457
+ let body if !chain then assertion_fix_body(source, node) else {node: base, text: base.text}
458
+ let edit if check.generic || chain && !expression_fix_retains_comments(node, body.text) then null else assertion_fix_with_body(source, node, body)
459
+ fix at edit.target if edit != null as edit.text
460
+ }
461
+
462
+ in ts_non_null rule es-no-unnecessary-type-assertion:non-null {
463
+ let outer assertion_outer(node)
464
+ let assignment outer.parent is ts_assignment && outer.parent.operator == "="
465
+ let target assignment && outer is ts_assignment.left
466
+ let unassigned { value expression.node; unparenthesised_node; assertion_unassigned }
467
+ let unchanged assertion_parts_non_null(value_parts(expression.node)) && !unassigned
468
+ let contextual !unassigned && assertion_receiver_known(value_parts(value_expected_handle(node)))
469
+ && value_accepts(node, expression.node, "expected", "value") == true
470
+ let unchecked ts_compiler_option(node, "noUncheckedIndexedAccess") == true && unparenthesised_node(expression.node) is ts_index && condition_access(unparenthesised_node(expression.node)).from_index
471
+ when target || !assignment && !unchecked && (unchanged || contextual)
472
+ warn if target || !unchanged then "This assertion is unnecessary since the receiver accepts the original type of the expression." else "This assertion is unnecessary since it does not change the type of the expression."
473
+ let edit assertion_fix(source, node)
474
+ fix at edit.target if edit != null as edit.text
475
+ }
476
+
477
+ }