@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,280 @@
1
+ define {
2
+
3
+ # Returns of one function, for the inferred signature of a type parameter.
4
+ in ts_function, ts_arrow, ts_method let type_parameter_return_nodes descendants(ts_return)
5
+ # A `typeof` resolves to the queried value's type, which the value layer only
6
+ # reaches through the query itself.
7
+ in ts_function, ts_arrow, ts_method, ts_function_type, ts_method_signature, ts_call_signature let type_parameter_query_nodes descendants(ts_type_query)
8
+
9
+ fn type_parameter_usages(items) = {
10
+ value { known: all(items, item => item.known), uses: flat_map(items, item => item.uses) }
11
+ }
12
+ fn type_parameter_handle_signature(signature, target, from_class, seen) =
13
+ if signature.parameters == null || signature.has_receiver == null || signature.result == null
14
+ then { known: false, uses: [] }
15
+ else type_parameter_usages([
16
+ type_parameter_usages(map(signature.parameters,
17
+ parameter => type_parameter_handle(parameter, target, false, from_class, seen))),
18
+ if signature.has_receiver == false then { known: true, uses: [] }
19
+ else type_parameter_handle(signature.receiver, target, false, from_class, seen),
20
+ type_parameter_handle(signature.result, target, true, from_class, seen),
21
+ ])
22
+ fn type_parameter_object(part, target, from_class, seen) = {
23
+ value children: if part.handle == null || contains(seen, part.handle) then null
24
+ else value_children(part.handle, true, [])
25
+ value signatures: if contains(part.traits, "callable") then value_signatures(part.handle) else []
26
+ value if children == null then { known: false, uses: [] }
27
+ else if signatures == null then { known: false, uses: [] }
28
+ else type_parameter_usages([
29
+ type_parameter_usages(map(children,
30
+ edge => type_parameter_handle(edge.handle, target, false, from_class, seen [part.handle]))),
31
+ type_parameter_usages(flat_map(signatures, arm => map(arm,
32
+ signature => type_parameter_handle_signature(signature, target, from_class, seen [part.handle])))),
33
+ ])
34
+ }
35
+ fn type_parameter_part(part, target, returned, from_class, seen) =
36
+ if part.carrier.description.kind == "parameter" then {
37
+ known: true,
38
+ uses: if part.carrier.file == target.file && part.carrier.node == target.node then [true] else [],
39
+ } else if part.carrier_known != true || part.product_known != true
40
+ || count(part.uncertain_traits) > 0
41
+ || any(part.traits, trait => contains(["any", "error", "unknown"], trait)) then { known: false, uses: [] }
42
+ else if part.product != null && count(part.product.elements) == 0 then { known: true, uses: [] }
43
+ else if count(part.carrier.arguments) > 0 && part.handle != null && contains(seen, part.handle)
44
+ then { known: false, uses: [] }
45
+ else if count(part.carrier.arguments) > 0 then {
46
+ value multiple: from_class || (if part.carrier.description.kind == "template" then false
47
+ else if part.product != null then returned && !part.product.readonly
48
+ else if contains(["Array", "ReadonlyArray"], part.carrier.description.name)
49
+ then returned && part.carrier.description.name == "Array" else true)
50
+ value uses: type_parameter_usages(map(part.carrier.arguments,
51
+ argument => if argument.value_handle != null
52
+ then type_parameter_handle(argument, target, returned, from_class, seen [part.handle])
53
+ else type_parameter_parts(argument, target, returned, from_class, seen [part.handle])))
54
+ value { known: uses.known, uses: if multiple then uses.uses uses.uses else uses.uses }
55
+ } else if any(part.traits, trait => contains(["structural", "callable"], trait))
56
+ && !any(part.traits, trait => contains(["constructible", "intersection"], trait))
57
+ then type_parameter_object(part, target, from_class, seen)
58
+ else if any(part.traits, trait => contains(["structural", "callable", "constructible", "intersection"], trait))
59
+ then { known: false, uses: [] }
60
+ else { known: true, uses: [] }
61
+ fn type_parameter_arm(parts, target, returned, from_class, seen) =
62
+ if any(parts, part => part.carrier.description.kind == "parameter")
63
+ && any(parts, part => contains(part.traits, "structural")) then { known: false, uses: [] }
64
+ else type_parameter_usages({
65
+ value map(parts, part => {
66
+ value usage: type_parameter_part(part, target, returned, from_class, seen)
67
+ value { key: if part.carrier.description.kind == "named" then {
68
+ description: part.carrier.description, arguments: part.carrier.arguments, usage: usage,
69
+ } else part, usage: usage }
70
+ })
71
+ unique_by (part) part.key
72
+ map (part) part.usage
73
+ })
74
+ fn type_parameter_parts(parts, target, returned, from_class, seen) =
75
+ if parts == null || count(seen) > 8 then { known: false, uses: [] }
76
+ else {
77
+ value states: walk({ index: 0, known: true, uses: [] }, state =>
78
+ if !state.known || state.index >= count(parts) then [] else {
79
+ value arm: type_parameter_arm(get(parts, state.index), target, returned, from_class, seen)
80
+ value [{
81
+ index: state.index + 1,
82
+ known: arm.known,
83
+ uses: if count(arm.uses) > count(state.uses) then arm.uses else state.uses,
84
+ }]
85
+ })
86
+ value result: last(states)
87
+ value { known: result.known, uses: if result.known then result.uses else [] }
88
+ }
89
+ fn type_parameter_value(parts, target, returned, from_class) = type_parameter_parts(parts, target, returned, from_class, [])
90
+ fn type_parameter_handle(source, target, returned, from_class, seen) = {
91
+ value arms: value_parts(source)
92
+ value if arms == null then { known: false, uses: [] }
93
+ else type_parameter_parts(map(arms, arm => arm.parts), target, returned, from_class, seen)
94
+ }
95
+ fn type_parameter_written(source, parts, target, returned, from_class) = {
96
+ value bare: ts_value_bare_type(source)
97
+ value declaration: if bare is ts_type_reference && count(bare.type_argument.nodes) == 0
98
+ && bare.name is ts_identifier then name_binding(bare.name.node).node.parent.node else null
99
+ value if declaration is ts_type_parameter && declaration_identity(declaration) == target
100
+ then { known: true, uses: [true] }
101
+ else if source is (ts_type_literal, ts_function_type) then type_parameter_handle(source, target, returned, from_class, [])
102
+ else type_parameter_value(parts, target, returned, from_class)
103
+ }
104
+ fn type_parameter_independent_parameter(parameter) =
105
+ parameter.type is ts_type_keyword && contains(["any", "unknown"], parameter.type.text)
106
+ || parameter.type == null && parameter.init == null
107
+ fn type_parameter_parameters(signature, target, from_class, declaration) = {
108
+ value syntax: if declaration == null then null else declaration.calls_parameters
109
+ value if syntax != null && all(syntax, parameter => type_parameter_independent_parameter(parameter))
110
+ then { known: true, uses: [] }
111
+ else if signature.parameters_known != true then { known: false, uses: [] }
112
+ else type_parameter_usages(if syntax != null && count(signature.parameters) == count(syntax) then {
113
+ value zip(signature.parameters, syntax)
114
+ map (pair) if type_parameter_independent_parameter(last(pair)) then { known: true, uses: [] }
115
+ else type_parameter_written(last(pair).type.node, first(pair), target, false, from_class)
116
+ } else map(signature.parameters, parameter => type_parameter_value(parameter, target, false, from_class)))
117
+ }
118
+ fn type_parameter_signature(signature, target, from_class, result_type, declaration, inferred_result) = {
119
+ value parameters: type_parameter_parameters(signature, target, from_class, declaration)
120
+ value receiver: if signature.has_receiver == false then { known: true, uses: [] }
121
+ else type_parameter_value(signature.receiver, target, false, from_class)
122
+ value result: if result_type is ts_type_keyword && result_type.text == "unknown"
123
+ then { known: true, uses: [] }
124
+ else if declaration != null && result_type is ts_type_predicate && result_type.type != null
125
+ then type_parameter_value(value_constituents(result_type.type.node), target, true, from_class)
126
+ else if inferred_result != null then type_parameter_handle(inferred_result, target, true, from_class, [])
127
+ else type_parameter_written(result_type, signature.result, target, true, from_class)
128
+ value uses: type_parameter_usages([parameters, receiver, result])
129
+ value {
130
+ known: signature.receiver_known && signature.result_known && uses.known,
131
+ uses: uses.uses,
132
+ }
133
+ }
134
+ fn type_parameter_callable(n, target, from_class) = {
135
+ value signatures: call_signatures(n)
136
+ value declaration: if count(signatures) == 1 && count(first(signatures)) == 1 then n else null
137
+ value lazy_signatures: if n.return_type == null && count(signatures) == 1 && count(first(signatures)) == 1
138
+ then value_signatures(n) else null
139
+ value inferred_result: if count(lazy_signatures) == 1 && count(first(lazy_signatures)) == 1
140
+ then first(first(lazy_signatures)).result else null
141
+ value if signatures == null || count(signatures) == 0 then { known: false, uses: [] }
142
+ else type_parameter_usages({
143
+ value signatures
144
+ flat_map (arm) map(arm, signature => type_parameter_signature(signature, target, from_class,
145
+ n.return_type.node, declaration, inferred_result))
146
+ })
147
+ }
148
+ fn type_parameter_class(n, target) = type_parameter_usages(map(n.member.nodes,
149
+ member => if member is ts_method then type_parameter_callable(member, target, true)
150
+ else if member is ts_class_property then type_parameter_value(value_constituents(member), target, false, true)
151
+ else { known: false, uses: [] }))
152
+
153
+ # Written signatures need no types: the references themselves say how often a
154
+ # parameter is used. One entry for the parameter, plus two for each use that is
155
+ # already repeated by its syntax (a generic argument, or a mutable array that
156
+ # is returned), which is how ESLint counts before it consults types.
157
+ fn type_parameter_type_reference(reference) =
158
+ if reference.parent is ts_type_reference then reference.parent else null
159
+ fn type_parameter_argument_owner(reference) = {
160
+ value reference_node: type_parameter_type_reference(reference)
161
+ value if reference_node == null then null else find(
162
+ walk(reference_node, item => if item.parent == null then null else item.parent.node),
163
+ item => item.parent != null && item.parent is ts_type_reference
164
+ && item.parent.type_argument != null
165
+ && any(item.parent.type_argument.nodes, argument => argument.offset == item.offset))
166
+ }
167
+ fn type_parameter_array_owner(reference) = {
168
+ value reference_node: type_parameter_type_reference(reference)
169
+ value argument_owner: type_parameter_argument_owner(reference)
170
+ value outer: if reference_node == null then null else
171
+ last(walk(reference_node, item =>
172
+ if item.parent is (ts_union, ts_intersection, ts_parenthesized_type) then item.parent else null))
173
+ value if argument_owner != null then argument_owner
174
+ else if outer != null && outer.parent is ts_array_type then outer.parent
175
+ else null
176
+ }
177
+ fn type_parameter_repeated_reference(reference) = {
178
+ value owner: type_parameter_argument_owner(reference)
179
+ value owner != null && !(owner.name is ts_identifier && contains(["Array", "ReadonlyArray"], owner.name.text))
180
+ }
181
+ fn type_parameter_multiple_reference(reference, result) = {
182
+ value owner: type_parameter_array_owner(reference)
183
+ value array: if owner == null || result == null
184
+ || !(result.offset <= owner.offset && owner.end_offset <= result.end_offset) then null else owner
185
+ value array != null && !(array.parent is ts_type_operator && array.parent.operator == "readonly")
186
+ && (array is ts_array_type || array.name is ts_identifier && array.name.text == "Array")
187
+ }
188
+ fn type_parameter_reference_uses(reference, result) =
189
+ if type_parameter_repeated_reference(reference) || type_parameter_multiple_reference(reference, result)
190
+ then [true, true] else [true]
191
+ fn type_parameter_return_owner(n) = {
192
+ value child: last(walk(n, item =>
193
+ if item is (ts_function, ts_arrow, ts_method) then null
194
+ else if item.parent == null then null
195
+ else if item.parent is (ts_function, ts_arrow, ts_method) then null
196
+ else item.parent.node))
197
+ value if child == null || child.parent == null then null else child.parent.node
198
+ }
199
+ fn type_parameter_result_layer(result, target) = {
200
+ value bare: ts_value_bare_type(result)
201
+ value declaration: if bare is ts_type_reference && count(bare.type_argument.nodes) == 0 && bare.name is ts_identifier
202
+ && name_binding(bare.name.node) != null then name_binding(bare.name.node).node.parent.node else null
203
+ # Another parameter's constraint already counts wherever that parameter is
204
+ # written, so a bare `U` in the result adds nothing of its own.
205
+ value other_parameter: declaration is ts_type_parameter && declaration_identity(declaration).node != target.node
206
+ value if result is ts_type_keyword || other_parameter then { known: true, uses: [] }
207
+ else type_parameter_handle(result, target, true, false, [])
208
+ }
209
+ fn type_parameter_inferred_usage(n, target, returns) = {
210
+ value signatures: call_signatures(n)
211
+ value known: count(signatures) == 1 && count(first(signatures)) == 1
212
+ value result: if known then first(first(value_signatures(n))).result else null
213
+ value body_values: if n is ts_arrow && n.body.node != null && n.body.node not ts_block then [n.body.node] else []
214
+ value returned_values: map(returns, item => item.argument.node)
215
+ value values: if result != null then [] else filter(body_values returned_values, value => value != null)
216
+ value states: map(values, value => {
217
+ value usage: type_parameter_handle(value, target, true, false, [])
218
+ # A property read the layer cannot resolve (`x.length` on a constrained
219
+ # parameter) yields a concrete member type, never an indexed `T[K]`.
220
+ value if value is ts_member && !usage.known then { known: true, uses: [] } else usage
221
+ })
222
+ value if !known then { known: false, uses: [] }
223
+ else if result != null then type_parameter_result_layer(result, target)
224
+ else if any(states, state => !state.known) then { known: false, uses: [] }
225
+ else type_parameter_usages(states)
226
+ }
227
+ fn type_parameter_written_usage(n, references, result, target, returns, queries) = {
228
+ value result_references: if result == null then [] else filter(references, reference =>
229
+ result.offset <= reference.offset && reference.end_offset <= result.end_offset)
230
+ value other_references: if result == null then references else filter(references, reference =>
231
+ !(result.offset <= reference.offset && reference.end_offset <= result.end_offset))
232
+ value result_syntax: flat_map(result_references, reference => type_parameter_reference_uses(reference, result))
233
+ value result_layer: if result == null then null else type_parameter_result_layer(result, target)
234
+ value result_uses: if result_layer != null && result_layer.known && count(result_layer.uses) > count(result_syntax)
235
+ then result_layer.uses else result_syntax
236
+ value syntax_uses: ([true] flat_map(other_references, reference => type_parameter_reference_uses(reference, result))
237
+ result_uses)
238
+ value result_queries: if result == null then [] else
239
+ filter(queries, query => result.offset <= query.offset && query.end_offset <= result.end_offset)
240
+ value inferred: if result != null
241
+ then { known: !(count(result_syntax) == 0 && result_layer != null && !result_layer.known && count(result_queries) > 0),
242
+ uses: [] }
243
+ else type_parameter_inferred_usage(n, target, returns)
244
+ value if inferred.known then { known: true, uses: (syntax_uses inferred.uses) } else { known: false, uses: [] }
245
+ }
246
+
247
+ in ts_type_parameter rule es-no-unnecessary-type-parameters warn "Type parameter {name} is {type_parameter_uses} in the {type_parameter_descriptor} signature." {
248
+ let type_parameter_owner parent.node
249
+ when type_parameter_owner is (ts_function, ts_arrow, ts_method, ts_function_type,
250
+ ts_method_signature, ts_call_signature, ts_class)
251
+ when !(type_parameter_owner is ts_class)
252
+ || (type_parameter_owner.extends == null && count(type_parameter_owner.implements) == 0)
253
+ let type_parameter_descriptor if type_parameter_owner is ts_class then "class" else "function"
254
+ let type_parameter_binding name_binding(name.node)
255
+ when type_parameter_binding != null
256
+ let type_parameter_boundary if type_parameter_owner is ts_arrow && type_parameter_owner.body.node not ts_block
257
+ then type_parameter_owner.end_offset
258
+ else type_parameter_owner.body.offset ?? type_parameter_owner.return_type.end_offset ?? type_parameter_owner.end_offset
259
+ let type_parameter_references {
260
+ value type_parameter_binding.references
261
+ filter (reference) contains(name_role_classes(reference, "reference"), "type")
262
+ && reference.offset <= type_parameter_boundary
263
+ && (reference.offset >= node.end_offset || reference.end_offset <= node.offset)
264
+ }
265
+ let type_parameter_returns filter(type_parameter_owner.type_parameter_return_nodes ?? [],
266
+ item => type_parameter_return_owner(item).offset == type_parameter_owner.offset)
267
+ let type_parameter_result_queries if type_parameter_owner.return_type == null then []
268
+ else type_parameter_owner.type_parameter_query_nodes ?? []
269
+ let type_parameter_target declaration_identity(node)
270
+ let type_parameter_usage if type_parameter_owner is ts_class
271
+ then type_parameter_class(type_parameter_owner, type_parameter_target)
272
+ else type_parameter_written_usage(type_parameter_owner, type_parameter_references,
273
+ type_parameter_owner.return_type.node, type_parameter_target, type_parameter_returns, type_parameter_result_queries)
274
+ when type_parameter_usage.known
275
+ && count(type_parameter_usage.uses) < (if type_parameter_owner is ts_class then 2 else 3)
276
+ let type_parameter_uses if type_parameter_owner is ts_class
277
+ then if count(type_parameter_usage.uses) == 0 then "never used" else "used only once"
278
+ else if count(type_parameter_usage.uses) < 2 then "never used" else "used only once"
279
+ }
280
+ }
@@ -0,0 +1,167 @@
1
+ # Receiver-preserving method extraction.
2
+
3
+ define {
4
+ fn unbound_safe_use(n) = {
5
+ value n
6
+ walk (current) if current.parent is (ts_parenthesized, ts_non_null, ts_as, ts_type_assertion, ts_logical)
7
+ then current.parent.node else null
8
+ any (current) current.parent is (ts_if, ts_for, ts_member, ts_index, ts_switch, ts_update, ts_while)
9
+ || current is (ts_call.callee, ts_conditional.test, ts_tagged_template.tag)
10
+ || current.parent is ts_unary && contains(["!", "delete", "typeof", "void"], current.parent.operator)
11
+ || current.parent is ts_binary && contains(["!=", "!==", "==", "===", "instanceof"], current.parent.operator)
12
+ || current.parent is ts_assignment && current.parent.operator == "=" && (current is ts_assignment.left
13
+ || current.object is ts_super && current.parent.left is ts_member && current.parent.left.object is ts_this)
14
+ || current.parent is ts_logical && current.parent.operator == "&&" && current is ts_logical.left
15
+ }
16
+
17
+ fn unbound_type_only(n) = {
18
+ value n
19
+ walk (current) current.parent.node
20
+ any (current) current is (ts_interface, ts_type_alias, ts_function_type, ts_method_signature, ts_call_signature)
21
+ || current is (ts_class, ts_function, ts_variable_declaration) && contains([] current.modifier, "declare")
22
+ || current is ts_method && contains([] current.modifier, "abstract")
23
+ }
24
+
25
+ fn unbound_source(n, path, bound) = if n == null then [] else [{node: n, path: path, bound: bound}]
26
+ fn unbound_literal_keys(n) = {
27
+ value arms: value_constituents(n)
28
+ value keys: flat_map(arms ?? [], arm => map(filter(arm, part =>
29
+ part.identity_known && part.identity.kind == "value"
30
+ && contains(["str", "num"], type_of(part.identity.value))
31
+ && (contains(part.traits, "string") || contains(part.traits, "number"))),
32
+ part => part.identity.value))
33
+ value unique(keys)
34
+ }
35
+ fn unbound_pattern_sources(n) = {
36
+ value {node: n, path: []}
37
+ walk (current) if current.node is ts_assignment_pattern.left then {node: current.node.parent.node, path: current.path}
38
+ else if current.node is ts_pattern_property.value then
39
+ {node: current.node.parent.parent.node, path: [current.node.parent.static_name] current.path}
40
+ else null
41
+ flat_map (current) if current.node is (ts_declarator.name, ts_parameter.name) then {
42
+ value owner: current.node.parent
43
+ value annotated: owner.type.node ?? owner.node
44
+ value declared: unbound_source(owner.init.node, current.path, owner is ts_declarator)
45
+ value declared unbound_source(annotated, current.path, false)
46
+ } else if current.node is ts_assignment.left then unbound_source(current.node.parent.right.node, current.path, true)
47
+ else if current.node is ts_assignment_pattern.left then unbound_source(current.node.parent.right.node, current.path, false)
48
+ else []
49
+ }
50
+
51
+ fn unbound_profiles(n, path) = {
52
+ value subject: fold(path, n, (subject, key) => value_lookup(subject, key).member)
53
+ value arms: value_parts(subject) ?? []
54
+ value filter(arms, arm => any(arm.parts, part => !contains(part.traits, "parameter")
55
+ && !(contains(part.traits, "intersection") && count(part.traits) == 1
56
+ && part.carrier_known && part.carrier == null)))
57
+ }
58
+ fn unbound_member(arm, key) = {
59
+ value value_lookup(arm.handle, key).alternatives ?? []
60
+ flat_map (alternative) alternative.sources
61
+ first
62
+ }
63
+ fn unbound_library_name(part, n, names) = part.carrier_known && part.carrier != null
64
+ && contains(names, part.carrier.description.name)
65
+ && declaration_origin(n, {file: part.carrier.file, node: part.carrier.node}).kind == "library"
66
+ fn unbound_builtin_type(arm, n, key) = {
67
+ value member: unbound_member(arm, key)
68
+ value member_library: declaration_origin(n, member.site).kind == "library"
69
+ value key == "compare" && any(arm.parts, item => unbound_library_name(item, n, ["Collator"]))
70
+ || member_library && contains(member.flags, "array_constructor_member")
71
+ || member_library && any(arm.parts, item => unbound_library_name(item, n,
72
+ ["NumberConstructor", "ObjectConstructor", "StringConstructor", "SymbolConstructor", "ArrayConstructor",
73
+ "Array", "ProxyConstructor", "Console", "DateConstructor", "Atomics", "Math", "JSON"]))
74
+ }
75
+ fn unbound_global(n, key) = {
76
+ value spelling: join([n.text, key.text], ".")
77
+ value listed: n is ts_identifier && key is ts_identifier && n.declaration == null && contains([
78
+ "Number.isFinite", "Number.isInteger", "Number.isNaN", "Number.isSafeInteger", "Number.parseFloat", "Number.parseInt",
79
+ "Object.assign", "Object.getOwnPropertyDescriptor", "Object.getOwnPropertyDescriptors", "Object.getOwnPropertyNames",
80
+ "Object.getOwnPropertySymbols", "Object.hasOwn", "Object.is", "Object.preventExtensions", "Object.seal", "Object.create",
81
+ "Object.defineProperties", "Object.defineProperty", "Object.freeze", "Object.getPrototypeOf", "Object.setPrototypeOf",
82
+ "Object.isExtensible", "Object.isFrozen", "Object.isSealed", "Object.keys", "Object.entries", "Object.fromEntries",
83
+ "Object.values", "Object.groupBy", "String.fromCharCode", "String.fromCodePoint", "String.raw", "RegExp.escape",
84
+ "Symbol.for", "Symbol.keyFor", "Array.isArray", "Array.from", "Array.fromAsync", "Array.of", "Proxy.revocable",
85
+ "Date.now", "Date.parse", "Date.UTC", "Atomics.load", "Atomics.store", "Atomics.add", "Atomics.sub", "Atomics.and",
86
+ "Atomics.or", "Atomics.xor", "Atomics.exchange", "Atomics.compareExchange", "Atomics.isLockFree", "Atomics.wait",
87
+ "Atomics.waitAsync", "Atomics.notify", "Atomics.pause", "Reflect.defineProperty", "Reflect.deleteProperty", "Reflect.apply",
88
+ "Reflect.construct", "Reflect.get", "Reflect.getOwnPropertyDescriptor", "Reflect.getPrototypeOf", "Reflect.has",
89
+ "Reflect.isExtensible", "Reflect.ownKeys", "Reflect.preventExtensions", "Reflect.set", "Reflect.setPrototypeOf",
90
+ "console.log", "console.info", "console.debug", "console.warn", "console.error", "console.dir", "console.time",
91
+ "console.timeEnd", "console.timeLog", "console.trace", "console.assert", "console.clear", "console.count",
92
+ "console.countReset", "console.group", "console.groupEnd", "console.table", "console.dirxml", "console.groupCollapsed",
93
+ "console.Console", "console.profile", "console.profileEnd", "console.timeStamp", "console.context", "console.createTask",
94
+ "Math.abs", "Math.acos", "Math.acosh", "Math.asin", "Math.asinh", "Math.atan", "Math.atanh", "Math.atan2", "Math.ceil",
95
+ "Math.cbrt", "Math.expm1", "Math.clz32", "Math.cos", "Math.cosh", "Math.exp", "Math.floor", "Math.f16round", "Math.fround",
96
+ "Math.hypot", "Math.imul", "Math.log", "Math.log1p", "Math.log2", "Math.log10", "Math.max", "Math.min", "Math.pow",
97
+ "Math.random", "Math.round", "Math.sign", "Math.sin", "Math.sinh", "Math.sqrt", "Math.tan", "Math.tanh", "Math.trunc",
98
+ "JSON.parse", "JSON.stringify", "JSON.rawJSON", "JSON.isRawJSON", "Intl.getCanonicalLocales", "Intl.supportedValuesOf",
99
+ "Intl.DateTimeFormat", "Intl.NumberFormat", "Intl.Collator", "Intl.PluralRules", "Intl.RelativeTimeFormat", "Intl.ListFormat",
100
+ "Intl.Locale", "Intl.DisplayNames", "Intl.Segmenter", "Intl.DurationFormat",
101
+ ], spelling)
102
+ value globals: if listed then global_declarations(n, n.text, "value") else []
103
+ value identity: if listed then declaration_identity(n) else null
104
+ value origins: map(globals ?? [], site => declaration_origin(n, site))
105
+ value if !listed then false else if globals == null || identity == null then null
106
+ else if !contains(globals, identity) then false
107
+ else if any(origins, origin => contains(["library", "package"], origin.kind)) then true
108
+ else if any(origins, origin => origin == null) then null else false
109
+ }
110
+
111
+ fn unbound_danger(arm, key, ignore_static) = {
112
+ value declaration: unbound_member(arm, key)
113
+ value flags: declaration.flags ?? []
114
+ value if declaration == null || ignore_static && contains(flags, "static")
115
+ || !contains(flags, "method") && !contains(flags, "function_initializer")
116
+ || contains(flags, "void_receiver") then null
117
+ else {without_this: !contains(flags, "explicit_receiver")}
118
+ }
119
+ fn unbound_warning(danger) = {
120
+ value [
121
+ "A method that is not declared with `this: void` may cause unintentional scoping of `this` when separated from its object.\nConsider using an arrow function or explicitly `.bind()`ing the method to avoid calling the method with an unintended `this` value. ",
122
+ if danger.without_this then "\nIf a function does not access `this`, it can be annotated with `this: void`." else "",
123
+ ]
124
+ join ""
125
+ }
126
+
127
+ in ts_member, ts_index, ts_pattern_property.key, ts_property.key, ts_computed_key.expression rule es-unbound-method {
128
+ option ignoreStatic boolean = false "Allow extracting static methods"
129
+ let pattern_property if is ts_pattern_property.key then parent.node
130
+ else if is ts_property.key && parent.parent is ts_assignment.left then parent.node
131
+ else if is ts_computed_key.expression && parent.parent is ts_pattern_property then parent.parent.node else null
132
+ let pattern pattern_property != null
133
+ when pattern || is (ts_member, ts_index)
134
+ when if pattern then is ts_identifier && !unbound_type_only(node) else !unbound_safe_use(node)
135
+ when pattern || !(is ts_member) || property.node is ts_identifier
136
+ let keys if pattern then [if parent is ts_computed_key then text else pattern_property.static_name] else if is ts_member then [static_name]
137
+ else unbound_literal_keys(node.index.node)
138
+ when count(keys) > 0
139
+ let key_node if pattern then node else if is ts_member then property.node else node.index.node
140
+ let sources if pattern then unbound_pattern_sources(pattern_property.parent.node) else unbound_source(object.node, [], true)
141
+ let profiles {
142
+ value sources
143
+ map (source) {
144
+ value {
145
+ node: source.node,
146
+ bound: source.bound,
147
+ direct: count(source.path) == 0,
148
+ profiles: unbound_profiles(source.node, source.path),
149
+ }
150
+ }
151
+ }
152
+ let danger {
153
+ value keys
154
+ flat_map (key) if any(profiles, source => source.bound && (source.direct && unbound_global(source.node, key_node) != false
155
+ || count(source.profiles) > 0 && all(source.profiles, profile => unbound_builtin_type(profile, source.node, key)))) then []
156
+ else {
157
+ value profiles
158
+ flat_map (source) source.profiles
159
+ map (profile) unbound_danger(profile, key, ignoreStatic)
160
+ filter (danger) danger != null
161
+ }
162
+ first
163
+ }
164
+ when danger != null
165
+ warn unbound_warning(danger)
166
+ }
167
+ }
@@ -0,0 +1,73 @@
1
+ import .ts/values/inspection
2
+
3
+ define {
4
+
5
+ fn unsafe_call_kind(n) = {
6
+ value arms: value_parts(n)
7
+ value if arms == null then null else ts_value_category(map(arms, arm => arm.parts))
8
+ }
9
+ fn unsafe_call_void_result(handle) = {
10
+ value arms: value_parts(handle)
11
+ value if arms == null then null else count(arms) == 1 && count(first(arms).parts) == 1
12
+ && contains(first(first(arms).parts).traits, "void")
13
+ }
14
+ fn unsafe_call_signature_state(part, construct) = {
15
+ value ancestry: ts_value_ancestry(part)
16
+ value calls: map(ancestry.parts, item => value_signatures(item.handle))
17
+ value constructors: map(ancestry.parts, item => value_constructors(item.handle))
18
+ value headers: flat_map(calls, arms => flat_map(arms ?? [], arm => arm))
19
+ value constructor_headers: flat_map(constructors, arms => flat_map(arms ?? [], arm => arm))
20
+ value constructor_states: map(constructor_headers, header =>
21
+ if header.site == null || any(calls, arms => arms == null) then null
22
+ else !any(headers, call => call.site == header.site))
23
+ value results: if construct then map(headers, header => unsafe_call_void_result(header.result)) else []
24
+ value safe: any(constructor_states, state => state == true)
25
+ || (if construct then any(results, result => result == false) else count(headers) > 0)
26
+ value when {
27
+ safe then true
28
+ !ancestry.complete || any(calls, arms => arms == null)
29
+ || any(constructors, arms => arms == null) || any(results, result => result == null)
30
+ || any(constructor_states, state => state == null) then null
31
+ else then false
32
+ }
33
+ }
34
+ fn unsafe_call_value_kind(n, construct) = {
35
+ value category: unsafe_call_kind(n)
36
+ value if category != null then category else {
37
+ value arms: value_parts(n)
38
+ value arm_parts: if count(arms ?? []) == 1 then first(arms).parts else []
39
+ value parts: filter(arm_parts, part => !contains(part.traits, "intersection") && !contains(part.traits, "parameter"))
40
+ value if !any(parts, part => contains(part.traits, "builtin_function")) then null else {
41
+ value signatures: map(parts, part => unsafe_call_signature_state(part, construct))
42
+ value if any(signatures, state => state == true) || any(signatures, state => state == null) then null
43
+ else "function"
44
+ }
45
+ }
46
+ }
47
+ fn unsafe_call_this_kind(n) = {
48
+ value roots: walk([unparenthesised_node(n)], current => when {
49
+ current is (ts_member, ts_index) then unparenthesised_node(current.object.node)
50
+ current is (ts_as, ts_type_assertion) then unparenthesised_node(current.expression.node)
51
+ else then null
52
+ })
53
+ value subject: find(roots, current => current is ts_this)
54
+ value if subject == null then null else unsafe_call_kind(subject)
55
+ }
56
+
57
+ in ts_call.callee rule es-no-unsafe-call {
58
+ let unsafe_kind unsafe_call_value_kind(node, false)
59
+ when unsafe_kind != null
60
+ warn if unsafe_kind == "error" then if unsafe_call_this_kind(node) == "error" then "Unsafe call of a `this` type that could not be resolved." else "Unsafe call of a type that could not be resolved." else "Unsafe call of {if unsafe_kind == 'function' then 'a `Function`' else 'an `any`'} typed value."
61
+ }
62
+ in ts_new rule es-no-unsafe-call:new {
63
+ let unsafe_kind unsafe_call_value_kind(callee.node, true)
64
+ when unsafe_kind != null
65
+ warn if unsafe_kind == "error" then "Unsafe construction of a type that could not be resolved." else "Unsafe construction of {if unsafe_kind == 'function' then 'a `Function`' else 'an `any`'} typed value."
66
+ }
67
+ in ts_tagged_template.tag rule es-no-unsafe-call:tag {
68
+ let unsafe_kind unsafe_call_value_kind(node, false)
69
+ when unsafe_kind != null
70
+ warn if unsafe_kind == "error" then "Unsafe use of a template tag whose type could not be resolved." else "Unsafe use of {if unsafe_kind == 'function' then 'a `Function`' else 'an `any`'} typed template tag."
71
+ }
72
+
73
+ }
@@ -0,0 +1,36 @@
1
+ import .ts/values/inspection
2
+
3
+ define {
4
+
5
+ in ts_node let unsafe_member_kind {
6
+ value arms: value_parts(node)
7
+ value if arms == null then null else ts_value_category(map(arms, arm => arm.parts))
8
+ }
9
+ in ts_member, ts_index {
10
+ let any_access_root object.any_access_root == true || object.unsafe_member_kind != null
11
+ let unsafe_member_access object.unsafe_member_kind != null && object.any_access_root != true
12
+ let any_access_optional_root optional == null && (object.any_access_optional_root == true || object.unsafe_member_kind != null)
13
+ let unsafe_optional_member_access optional == null && object.unsafe_member_kind != null && object.any_access_optional_root != true
14
+ let object_unsafe_type object.unsafe_member_kind
15
+ }
16
+ in ts_index let unsafe_member_site unparenthesised_node(index.node)
17
+
18
+ in ts_member.property rule es-no-unsafe-member-access {
19
+ option allowOptionalChaining boolean = false "Allow optional chaining on any"
20
+ when if allowOptionalChaining then parent.unsafe_optional_member_access == true else parent.unsafe_member_access == true
21
+ warn if parent.object_unsafe_type == "error" then "Unsafe member access .{text} on a type that cannot be resolved." else "Unsafe member access .{text} on an `any` value."
22
+ }
23
+ in ts_index.index rule es-no-unsafe-member-access:computed {
24
+ option allowOptionalChaining boolean = false "Allow optional chaining on any"
25
+ when if allowOptionalChaining then parent.unsafe_optional_member_access == true else parent.unsafe_member_access == true
26
+ report at parent.unsafe_member_site
27
+ warn if parent.object_unsafe_type == "error" then "Unsafe member access [{parent.unsafe_member_site.text}] on a type that cannot be resolved." else "Unsafe member access [{parent.unsafe_member_site.text}] on an `any` value."
28
+ }
29
+ in ts_index.index rule es-no-unsafe-member-access:key {
30
+ option allowOptionalChaining boolean = false "Allow optional chaining on any"
31
+ when parent.unsafe_member_site.unsafe_member_kind != null && parent.unsafe_member_site not ts_update && (!allowOptionalChaining || parent.optional == null)
32
+ report at parent.unsafe_member_site
33
+ warn if parent.unsafe_member_site.unsafe_member_kind == "error" then "The type of computed name [{parent.unsafe_member_site.text}] cannot be resolved." else "Computed name [{parent.unsafe_member_site.text}] resolves to an `any` value."
34
+ }
35
+
36
+ }