@inthedark/dxlint 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +40 -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,171 @@
1
+ define {
2
+
3
+ fn redundant_bare(n) = {
4
+ value n
5
+ walk (current) if current is ts_parenthesized_type then current.type.node else null
6
+ last
7
+ }
8
+ fn redundant_part(category, primitive, name) = {
9
+ category: category, primitive: primitive, name: name
10
+ }
11
+ fn redundant_value_part(part) = {
12
+ value primitive: find(["string", "number", "boolean", "bigint"], name => contains(part.traits, name))
13
+ value literal: part.identity_known && part.identity.kind == "value"
14
+ value when {
15
+ contains(part.traits, "error") then redundant_part("error", null, null)
16
+ contains(part.traits, "any") then redundant_part("any", null, "any")
17
+ contains(part.traits, "unknown") then redundant_part("unknown", null, "unknown")
18
+ part.carrier_known && part.carrier.description.kind == "template" then
19
+ redundant_part("literal", "string", "template literal type")
20
+ primitive == null || !part.identity_known || !part.carrier_known || part.carrier != null
21
+ || contains(part.traits, "enum") || contains(part.traits, "parameter") then null
22
+ literal && primitive == "bigint" && part.identity.value.value == null then null
23
+ else then redundant_part(if literal then "literal" else "primitive", primitive, when {
24
+ !literal then primitive
25
+ primitive == "string" then json_string(part.identity.value)
26
+ primitive == "bigint" then part.identity.value.value "n"
27
+ else then "" part.identity.value
28
+ })
29
+ }
30
+ }
31
+ fn redundant_reflected(n) = {
32
+ value arms: value_parts(n)
33
+ value parts: if arms == null then null else map(arms, arm =>
34
+ if len(arm.parts) == 1 then redundant_value_part(first(arm.parts)) else null)
35
+ value known: filter(parts ?? [], part => part != null)
36
+ value distinct: { value known; unique }
37
+ value boolean: len(distinct) == 2 && all(distinct, part => part.category == "literal" && part.primitive == "boolean")
38
+ && any(distinct, part => part.name == "true") && any(distinct, part => part.name == "false")
39
+ value when {
40
+ parts == null then [null]
41
+ len(parts) == 0 then [redundant_part("never", null, "never")]
42
+ boolean && len(known) == len(parts) then [redundant_part("primitive", "boolean", "boolean")]
43
+ len(distinct) == 1 && len(known) == len(parts) then distinct
44
+ else then parts
45
+ }
46
+ }
47
+ fn redundant_parts(n) = {
48
+ value redundant_bare(n)
49
+ walk (current) if current is ts_union then map(current.type.nodes, part => redundant_bare(part)) else null
50
+ filter (current) current not ts_union
51
+ sort_by (current) current.offset
52
+ flat_map (bare) when {
53
+ bare is ts_type_keyword && contains(["any", "unknown", "never"], bare.text) then
54
+ [redundant_part(bare.text, null, bare.text)]
55
+ bare is ts_type_keyword && contains(["string", "number", "boolean", "bigint"], bare.text) then
56
+ [redundant_part("primitive", bare.text, bare.text)]
57
+ bare is ts_template_type then [redundant_part("literal", "string", "template literal type")]
58
+ else then redundant_reflected(bare)
59
+ }
60
+ }
61
+ fn redundant_finding(site, mode, name, primitive, container) = {
62
+ site: site, mode: mode, name: name, primitive: primitive, container: container
63
+ }
64
+ fn redundant_special(part, union, returned) = when {
65
+ part.category == "error" then "error"
66
+ part.category == "any" then "overrides"
67
+ part.category == "unknown" then if union then "overrides" else "overridden"
68
+ part.category == "never" then when {
69
+ !union then "overrides"
70
+ !returned then "overridden"
71
+ else then null
72
+ }
73
+ else then null
74
+ }
75
+ fn redundant_findings(n) = {
76
+ value union: n is ts_union
77
+ value container: if union then "union" else "intersection"
78
+ value outer: { value n; walk (current) if current.parent is ts_parenthesized_type then current.parent.node else null; last }
79
+ value returned: outer is (ts_function.return_type, ts_arrow.return_type, ts_method.return_type,
80
+ ts_function_type.return_type, ts_call_signature.return_type, ts_method_signature.return_type)
81
+ value entries: map(n.type.nodes, site => {site: redundant_bare(site), parts: redundant_parts(site)})
82
+ value primitives: {
83
+ value entries
84
+ flat_map (entry) filter(entry.parts, part => part.category == "primitive")
85
+ map (part) part.primitive
86
+ unique
87
+ }
88
+ value special: {
89
+ value entries
90
+ flat_map (entry) {
91
+ value entry.parts
92
+ filter (part) redundant_special(part, union, returned) != null
93
+ map (part) redundant_finding(entry.site, redundant_special(part, union, returned),
94
+ if part.category == "error" then entry.site.text else part.name, null, container)
95
+ }
96
+ }
97
+ value unions: filter(entries, entry => len(entry.parts) >= 2)
98
+ value dominance: when {
99
+ union then {
100
+ value entries
101
+ flat_map (entry) {
102
+ value ["string", "number", "boolean", "bigint"]
103
+ flat_map (primitive) {
104
+ value literals: filter(entry.parts, part => part.category == "literal" && part.primitive == primitive)
105
+ value if !contains(primitives, primitive) || len(literals) == 0 then [] else
106
+ [redundant_finding(entry.site, "literal", {value literals; map (part) part.name; join " | "}, primitive, container)]
107
+ }
108
+ }
109
+ }
110
+ len(unions) > 0 then {
111
+ value unions
112
+ filter (entry) all(entry.parts, part => part.category == "literal" && contains(primitives, part.primitive))
113
+ map (entry) redundant_finding(entry.site, "primitive",
114
+ {value entry.parts; map (part) part.name; join " | "}, last(entry.parts).primitive, container)
115
+ }
116
+ else then {
117
+ value entries
118
+ flat_map (entry) {
119
+ value entry.parts
120
+ filter (part) part.category == "primitive"
121
+ flat_map (part) {
122
+ value literals: {value entries; flat_map (other) filter(other.parts, literal => literal.category == "literal" && literal.primitive == part.primitive)}
123
+ value if len(literals) == 0 then [] else
124
+ [redundant_finding(entry.site, "primitive", {value literals; map (literal) literal.name; join " | "}, part.primitive, container)]
125
+ }
126
+ }
127
+ }
128
+ }
129
+ value special dominance
130
+ }
131
+
132
+ in ts_union, ts_intersection {
133
+ let redundant_findings redundant_findings(node)
134
+ rule es-no-redundant-type-constituents {
135
+ each part in redundant_findings {
136
+ when part.mode == "overrides"
137
+ report at part.site
138
+ warn "'{part.name}' overrides all other types in this {part.container} type."
139
+ }
140
+ }
141
+ rule es-no-redundant-type-constituents:overridden {
142
+ each part in redundant_findings {
143
+ when part.mode == "overridden"
144
+ report at part.site
145
+ warn "'{part.name}' is overridden by other types in this {part.container} type."
146
+ }
147
+ }
148
+ rule es-no-redundant-type-constituents:error {
149
+ each part in redundant_findings {
150
+ when part.mode == "error"
151
+ report at part.site
152
+ warn "'{part.name}' is an 'error' type that acts as 'any' and overrides all other types in this {part.container} type."
153
+ }
154
+ }
155
+ rule es-no-redundant-type-constituents:literal {
156
+ each part in redundant_findings {
157
+ when part.mode == "literal"
158
+ report at part.site
159
+ warn "{part.name} is overridden by {part.primitive} in this union type."
160
+ }
161
+ }
162
+ rule es-no-redundant-type-constituents:primitive {
163
+ each part in redundant_findings {
164
+ when part.mode == "primitive"
165
+ report at part.site
166
+ warn "{part.primitive} is overridden by the {part.name} in this intersection type."
167
+ }
168
+ }
169
+ }
170
+
171
+ }
@@ -0,0 +1,79 @@
1
+ import .ts/constants
2
+
3
+ define {
4
+
5
+ fn regexp_nonglobal(n) = {
6
+ value bare: collection_value_node(n)
7
+ value input: bare.regexp_input
8
+ value flags_argument: regexp_at(bare.argument.nodes, 1)
9
+ value if input != null then type_of(input.flags) == "str" && !contains(input.flags, "g")
10
+ else if type_of(ts_runtime_string(bare)) == "str" then true
11
+ else if bare is (ts_call, ts_new) && bare.callee is ts_identifier && bare.callee.text == "RegExp" then
12
+ flags_argument == null || type_of(ts_runtime_string(flags_argument)) == "str" && !contains(ts_runtime_string(flags_argument), "g")
13
+ || type_of(ts_runtime_number(flags_argument)) == "num" || flags_argument is ts_identifier && flags_argument.text == "undefined" && flags_argument.declaration == null
14
+ else false
15
+ }
16
+ fn regexp_exec_argument(n) = {
17
+ value arms: value_constituents(n)
18
+ value strings: any(arms, arm => {
19
+ value part: first(arm)
20
+ value len(arm) == 1 && contains(part.traits, "string")
21
+ })
22
+ value regexes: any(arms, arm => len(arm) > 0 && all(arm, part =>
23
+ part.carrier_known == true && part.carrier.description.kind == "named"
24
+ && part.carrier.description.name == "RegExp"))
25
+ value strings != regexes
26
+ }
27
+ fn regexp_match_exec_offset(n) = {
28
+ value method: unparenthesised_node(n.callee.node)
29
+ value arguments: n.argument.nodes
30
+ value argument: first(arguments)
31
+ value bare: unparenthesised_node(argument)
32
+ value if n not ts_call || len(arguments) != 1 || !collection_method(method, "match")
33
+ || !(method.object.node always string) || !regexp_nonglobal(argument) then null
34
+ else if bare is ts_string then {
35
+ value input: regexp_string_input(bare, "")
36
+ value if input != null && regexp_mode_error(n, input, "") == null then method.property.offset ?? method.index.offset else null
37
+ } else if regexp_exec_argument(argument) then method.property.offset ?? method.index.offset else null
38
+ }
39
+ in ts_call let regexp_match_exec_offset regexp_match_exec_offset(node)
40
+
41
+ fn regexp_exec_literal(text) = {
42
+ value characters: split(text, "")
43
+ value escaped: fold(characters, { text: "", escaped: false }, (state, character) => {
44
+ text: state.text (if character == "/" && !state.escaped then "\\/"
45
+ else if character == "\n" then (if state.escaped then "n" else "\\n")
46
+ else if character == "\r" then (if state.escaped then "r" else "\\r")
47
+ else if character == "\u{2028}" then (if state.escaped then "u2028" else "\\u2028")
48
+ else if character == "\u{2029}" then (if state.escaped then "u2029" else "\\u2029")
49
+ else character),
50
+ escaped: character == "\\" && !state.escaped
51
+ })
52
+ value "/" (if escaped.text == "" then "(?:)" else escaped.text) "/"
53
+ }
54
+ fn regexp_exec_fix(n) = {
55
+ value method: unparenthesised_node(n.callee.node)
56
+ value argument: first(n.argument.nodes)
57
+ value bare: unparenthesised_node(argument)
58
+ value strings: bare always string
59
+ value receiver: if bare is ts_string then regexp_exec_literal(regexp_string_input(bare, "").text)
60
+ else if strings then "RegExp(" argument.text ")"
61
+ else expression_fix_wrap(argument.text, bare not (ts_identifier, ts_member, ts_index, ts_call, ts_new, ts_regex, ts_parenthesized))
62
+ value replacement: receiver ".exec(" method.object.text ")"
63
+ value statement: expression_fix_statement(n)
64
+ value semicolon: statement != null && statement.offset == n.offset && expression_fix_continuation(replacement)
65
+ && expression_fix_asi(statement, statement.token_neighbours.before, expression_fix_before_nodes(statement), replacement)
66
+ value if strings && bare not ts_string && ts_global_visible(n, "RegExp") != false
67
+ || semicolon == null || !expression_fix_retains_comments(n, replacement) then null
68
+ else (if semicolon then ";" else "") replacement
69
+ }
70
+
71
+ in ts_call rule es-prefer-regexp-exec {
72
+ when regexp_match_exec_offset != null
73
+ let method unparenthesised_node(callee.node)
74
+ report at method.property.node ?? method.index.node
75
+ let replacement regexp_exec_fix(node)
76
+ fix at node if replacement != null as expression_fix_edges(node, replacement)
77
+ warn "Use the `RegExp#exec()` method instead."
78
+ }
79
+ }
@@ -0,0 +1,91 @@
1
+ import .ts/values/inspection
2
+
3
+ define {
4
+
5
+ fn rejection_handler(call) = {
6
+ value callee: call.callee.node
7
+ value method: if callee is (ts_member, ts_index) then callee.static_name.nonempty ?? callee.index.static_string else null
8
+ value index: when {
9
+ method == "catch" then 0
10
+ method == "then" then 1
11
+ else then null
12
+ }
13
+ value arguments: call.argument.nodes
14
+ value preceding: { value arguments; slice 0 (index + 1) }
15
+ value if index == null || count(preceding) != index + 1
16
+ || any(preceding, argument => argument is ts_spread) || !(callee.object could thenable) then null
17
+ else { method: method, handler: last(preceding) }
18
+ }
19
+ fn rejection_inline(handler) = {
20
+ value walk(handler, current => when {
21
+ current is ts_parenthesized then current.expression.node
22
+ current is ts_logical then [current.left.node, current.right.node]
23
+ current is ts_conditional then [current.consequent.node, current.alternate.node]
24
+ current is ts_sequence then last(current.expression.nodes)
25
+ else then null
26
+ })
27
+ filter (current) current is (ts_arrow, ts_function)
28
+ }
29
+ fn rejection_rest_type(type, source) = if type == null then null else {
30
+ value shape: ts_value_array_parts(type, source)
31
+ value carrier: find(first(type), part => contains(["tuple", "array"], part.carrier.description.kind)
32
+ || part.carrier.description.kind == "named" && contains(["Array", "ReadonlyArray"], part.carrier.description.name)).carrier
33
+ value when {
34
+ ts_value_trait(type, "any") || ts_value_trait(type, "unknown") then []
35
+ count(type) == 1 && shape.known && (shape.array || shape.tuple) then first(carrier.arguments) ?? []
36
+ shape.known then []
37
+ else then null
38
+ }
39
+ }
40
+ fn rejection_parameter_type(function, parameter) = {
41
+ value declared: parameter.type.node
42
+ value signatures: if declared == null then expected_call_signatures(function) else null
43
+ value header: first(first(signatures))
44
+ value context: if header.parameters_known == true then first(header.parameters) else null
45
+ value when {
46
+ declared != null then
47
+ if parameter.name is ts_rest then value_at(declared, [0]) ?? rejection_rest_type(value_constituents(declared), function)
48
+ else value_constituent_traits(declared, ["unknown"])
49
+ parameter.name is ts_rest then rejection_rest_type(context, function)
50
+ else then context
51
+ }
52
+ }
53
+ fn rejection_findings(call) = {
54
+ value selection: rejection_handler(call)
55
+ value if selection == null then [] else {
56
+ value rejection_inline(selection.handler)
57
+ flat_map (function) {
58
+ value parameter: first(function.parameter.nodes)
59
+ value declared: if parameter == null then null else parameter.type.node
60
+ value type: if parameter == null then null else rejection_parameter_type(function, parameter)
61
+ value safe: if type != null then ts_value_trait(type, "unknown")
62
+ else if declared == null then true
63
+ else if parameter.name is ts_rest && declared is ts_tuple_type then {
64
+ value first_element: first(declared.element.nodes)
65
+ value first_element != null && first_element.type is ts_type_keyword && first_element.type.text == "unknown"
66
+ }
67
+ else declared is ts_type_keyword && declared.text == "unknown"
68
+ value if parameter == null || safe then [] else [{
69
+ offset: parameter.offset, end: parameter.end_offset, method: selection.method,
70
+ pattern: when {
71
+ parameter.name is ts_array_pattern then "array"
72
+ parameter.name is ts_object_pattern then "object"
73
+ else then ""
74
+ },
75
+ }]
76
+ }
77
+ }
78
+ }
79
+
80
+ in ts_call rule es-use-unknown-in-catch-callback-variable {
81
+ each finding in rejection_findings(node) {
82
+ report at { start: finding.offset, end: finding.end }
83
+ warn "Prefer the safe `: unknown` for a `" finding.method "`" (if finding.method == "then" then " rejection" else "") " callback variable." (when {
84
+ finding.pattern == "array" then " The thrown error may not be iterable."
85
+ finding.pattern == "object" then " The thrown error may be nullable, or may not have the expected shape."
86
+ else then ""
87
+ })
88
+ }
89
+ }
90
+
91
+ }
@@ -0,0 +1,116 @@
1
+ # Returned-await policy and source-preserving fixes.
2
+ import .ts/expression-fixes
3
+
4
+ define {
5
+
6
+ fn return_await_leaves(n) = {
7
+ value unparenthesised_node(n)
8
+ walk (current) {
9
+ value branch: unparenthesised_node(current)
10
+ value if branch is ts_conditional then [branch.consequent.node, branch.alternate.node] else null
11
+ }
12
+ map (current) unparenthesised_node(current)
13
+ filter (current) current not ts_conditional
14
+ }
15
+ fn return_await_scope(n) = {
16
+ value n
17
+ walk (current) current.parent.node
18
+ find (current) current is (ts_block, ts_program, ts_function, ts_arrow, ts_method, ts_for, ts_for_in, ts_for_of, ts_switch)
19
+ }
20
+ fn return_await_handling(n, owner, resources) = {
21
+ value ancestors: walk(n, current => if current == owner then null else current.parent.node)
22
+ value any(ancestors, child => child.parent is ts_try
23
+ && (child is ts_try.block || child is ts_try.handler && child.parent.finalizer != null))
24
+ || any(resources, resource => {
25
+ value scope: return_await_scope(resource)
26
+ value resource.end_offset < n.offset && contains(ancestors, scope)
27
+ })
28
+ }
29
+ fn return_await_certainty(n) = {
30
+ value parts: value_parts(n)
31
+ value complete: parts != null && len(parts) > 0
32
+ && all(parts, arm => len(arm.parts) > 0 && all(arm.parts, part => !contains(part.uncertain_traits, "thenable")
33
+ && !contains(part.traits, "any") && !contains(part.traits, "unknown")))
34
+ value when {
35
+ complete && n could thenable then "always"
36
+ ts_value_nonthenable(n) == true then "never"
37
+ else then "may"
38
+ }
39
+ }
40
+ fn return_await_result(n, owner, resources) = {
41
+ value awaited: n is ts_await
42
+ value argument: if awaited then n.argument.node else n
43
+ value {
44
+ expression: n,
45
+ awaited: awaited,
46
+ certainty: return_await_certainty(argument),
47
+ handling: return_await_handling(n, owner, resources),
48
+ }
49
+ }
50
+ in ts_declarator let return_await_resource parent is ts_variable_declaration && re_test(parent.kind, /(?:^|\s)using$/)
51
+
52
+ fn return_await_remove(source, n) = {
53
+ value argument: n.argument.node
54
+ value operand: unparenthesised_node(argument)
55
+ value prefix: { value byte_slice(source, n.offset, argument.offset); re_replace /^await[\s\u{FEFF}]*/ "" }
56
+ value parens: if argument is ts_parenthesized || n.parent is ts_return then 0 else expression_fix_parens(source, n, operand)
57
+ value if parens == null then null else {
58
+ # Return must see an opening paren before a retained multiline comment.
59
+ value replacement: if n.parent is ts_return && re_test(prefix, /[\r\n\u{2028}\u{2029}]/) then "(" prefix argument.text ")"
60
+ else prefix expression_fix_wrap(argument.text, parens > 0)
61
+ value expression_fix_edges(n, replacement)
62
+ }
63
+ }
64
+ fn return_await_add(n) = {
65
+ value parens: n.precedence < 15 || n is structure_assertion
66
+ value replacement: "await " expression_fix_wrap(n.text, parens)
67
+ value expression_fix_edges(n, replacement)
68
+ }
69
+ fn return_await_selected(result, mode) = result.certainty == "never" && result.awaited || result.certainty == "always"
70
+ && (when {
71
+ mode == "always" || mode != "never" && result.handling then !result.awaited
72
+ mode == "error-handling-correctness-only" then false
73
+ else then result.awaited
74
+ })
75
+
76
+ in ts_function, ts_arrow, ts_method rule es-return-await {
77
+ option mode ("always" | "error-handling-correctness-only" | "in-try-catch" | "never") = "in-try-catch" "Await policy for returned promises"
78
+ when contains([] modifier, "async") && body != null
79
+ let owner node
80
+ let resources {
81
+ value body.node
82
+ value descendants(ts_declarator)
83
+ filter (declaration) declaration.return_await_resource == true
84
+ }
85
+ let expressions if body is ts_block then {
86
+ value flow_declared_effects(body.node, "returned")
87
+ filter (effect) effect.site is ts_return && effect.value != null
88
+ map (effect) effect.site.argument.node
89
+ } else [body.node]
90
+ let results {
91
+ value expressions
92
+ flat_map (expression) return_await_leaves(expression)
93
+ map (expression) return_await_result(expression, owner, resources)
94
+ filter (result) return_await_selected(result, mode)
95
+ map (result) {
96
+ data: result,
97
+ # Promise awaits change rejection handling; keep those reports manual.
98
+ replacement: when {
99
+ result.certainty == "always" && result.handling then null
100
+ result.awaited then return_await_remove(source, result.expression)
101
+ else then return_await_add(result.expression)
102
+ }
103
+ }
104
+ }
105
+ each result in results {
106
+ report at result.data.expression
107
+ warn when {
108
+ result.data.certainty == "never" then "Returning an awaited value that is not a promise is not allowed."
109
+ result.data.awaited then "Returning an awaited promise is not allowed in this context."
110
+ else then "Returning an awaited promise is required in this context."
111
+ }
112
+ fix at result.data.expression if result.replacement != null as result.replacement
113
+ }
114
+ }
115
+
116
+ }
@@ -0,0 +1,67 @@
1
+ import .ts/calls
2
+ import .ts/constants
3
+
4
+ define {
5
+
6
+ fn ts_return_this_target(n, name) = {
7
+ value walk(n, part => if part is ts_parenthesized_type then part.type.node
8
+ else if part is ts_union then part.type.nodes else null)
9
+ filter (part) part is ts_type_reference && part.name.text == name
10
+ sort_by (part) part.offset
11
+ first
12
+ }
13
+
14
+ fn ts_return_this_value(n) = {
15
+ value chain: walk(unparenthesised_node(n), bare =>
16
+ if bare not ts_identifier || name_binding(bare).node.parent.type != null then null
17
+ else unparenthesised_node(ts_runtime_binding_init(bare)))
18
+ value any(chain, bare => bare is ts_this)
19
+ }
20
+
21
+ fn ts_return_this_class(n, target) = {
22
+ value arms: value_parts(n)
23
+ value if arms == null || any(arms, arm => any(arm.parts, part => !part.carrier_known)) then null
24
+ else any(arms, arm => count(arm.parts) == 1 && {
25
+ value part: first(arm.parts)
26
+ value carrier: part.carrier
27
+ value carrier.description.kind == "named" && !contains(part.traits, "constructible")
28
+ && carrier.file == target.file && carrier.node == target.node
29
+ })
30
+ }
31
+
32
+ fn ts_return_this_annotation(n) = {
33
+ value owner: if n is ts_method then n.parent.node
34
+ else if n.parent is ts_class_property && n.parent.init.node == n then n.parent.parent.node
35
+ else null
36
+ value parameter: first(n.parameter.nodes).name.node
37
+ value if owner not ts_class || owner.name == null || n.return_type == null || n.body == null
38
+ || parameter is ts_this || parameter.text == "this" then null
39
+ else {
40
+ value annotation: ts_return_this_target(n.return_type.node, owner.name.text)
41
+ value if annotation == null then null
42
+ else if n.body not ts_block then
43
+ if ts_return_this_value(n.body.node) then annotation else null
44
+ else {
45
+ value arguments: filter(ts_calls_return_arguments(n), argument => argument != null)
46
+ value returned_this: filter(arguments, argument => ts_return_this_value(argument))
47
+ value target: declaration_identity(annotation.name.node)
48
+ value other: filter(arguments, argument => !contains(returned_this, argument))
49
+ value if count(returned_this) > 0 && target != null
50
+ && all(other, argument => {
51
+ value returned_class: ts_return_this_class(argument, target)
52
+ value type_of(returned_class) == "bool" && !returned_class
53
+ })
54
+ then annotation else null
55
+ }
56
+ }
57
+ }
58
+
59
+ in ts_method, ts_function, ts_arrow rule es-prefer-return-this-type {
60
+ let annotation ts_return_this_annotation(node)
61
+ when annotation != null
62
+ report at annotation
63
+ warn "Use `this` type instead."
64
+ fix at annotation if expression_fix_clean(annotation) as "this"
65
+ }
66
+
67
+ }
@@ -0,0 +1,121 @@
1
+ import .ts/expression-fixes
2
+
3
+ define {
4
+
5
+ fn strict_void_unsafe_parts(parts, allow_any) = parts != null && !all(parts, arm =>
6
+ count(arm) == 0 || any(arm, part => contains(part.traits, "void")
7
+ || contains(part.traits, "undefined") || contains(part.traits, "never")
8
+ || allow_any && contains(part.traits, "any")))
9
+ fn strict_void_signature_allowed(signature, allow_any) = all(signature.result, arm =>
10
+ any(arm, part => contains(part.traits, "undefined") || allow_any && contains(part.traits, "any")))
11
+ fn strict_void_signatures_known(signatures) = count(signatures) > 0
12
+ && all(signatures, signature => signature.result_known)
13
+ fn strict_void_source_allowed(signatures, allow_any) = strict_void_signatures_known(signatures)
14
+ && all(signatures, signature => strict_void_signature_allowed(signature, allow_any))
15
+ fn strict_void_handle_result(handle) = {
16
+ value parts: value_trait_parts(handle, ["void", "undefined", "any", "null", "parameter"])
17
+ value if parts == null then null else map(parts, arm => arm.parts)
18
+ }
19
+ fn strict_void_signature(header) = {
20
+ value result: strict_void_handle_result(header.result)
21
+ value {result: result, result_known: result != null}
22
+ }
23
+ fn strict_void_expected_transfer(signatures, argument) =
24
+ if argument then any(signatures, signature => any(signature.result, arm =>
25
+ any(arm, part => contains(part.traits, "void"))))
26
+ && all(signatures, signature => all(signature.result, arm => any(arm, part =>
27
+ any(["void", "undefined", "null", "any"], trait => contains(part.traits, trait)))))
28
+ else count(signatures) > 0 && all(signatures, signature => count(signature.result) > 0
29
+ && all(signature.result, arm => any(arm, part => contains(part.traits, "void"))))
30
+ fn strict_void_argument_transfer(n) = {
31
+ value outer: outer_parenthesised_node(n)
32
+ value call: outer.parent.node
33
+ value if call not (ts_call, ts_new) then null else {
34
+ value alternatives: if call is ts_new then value_constructors(call.callee.node) else value_signatures(call.callee.node)
35
+ value headers: flat_map(alternatives ?? [], arm => arm)
36
+ value index: index_of(call.argument.nodes, outer)
37
+ value groups: map(headers, header => {
38
+ value parameter: first(slice(header.parameters, index, index + 1))
39
+ value callbacks: if parameter == null then [] else value_signatures(parameter)
40
+ value if callbacks == null then null else map(callbacks, arm => map(arm, callback => {
41
+ value result: strict_void_handle_result(callback.result)
42
+ value {result: result, result_known: result != null}
43
+ }))
44
+ })
45
+ value signatures: { value groups; flat_map (group) group ?? []; flat_map (arm) arm }
46
+ value known: alternatives != null && count(headers) > 0 && count(signatures) > 0 && index != null
47
+ && all(groups, group => group != null) && all(signatures, signature => signature.result_known)
48
+ value contextual: { value expected_call_signatures(n) ?? []; flat_map (arm) arm }
49
+ value use_context: count(headers) == 1 || all(signatures, signature => all(signature.result, arm =>
50
+ any(arm, part => any(["void", "undefined", "null", "any", "parameter"], trait => contains(part.traits, trait)))))
51
+ value if !known then null else use_context && strict_void_expected_transfer(contextual, true)
52
+ || strict_void_expected_transfer(signatures, true)
53
+ }
54
+ }
55
+ fn strict_void_source(n) = if n is ts_property then n.value.node ?? n.key.node else n
56
+ fn strict_void_reports(n, allow_any) = when {
57
+ n not (ts_function, ts_arrow, ts_method) then [{site: n, mode: "function"}]
58
+ contains(n.modifier, "*") then [{site: function_head_site(n), mode: "function"}]
59
+ contains(n.modifier, "async") then [{site: function_head_site(n), mode: "async"}]
60
+ n.body not ts_block then {
61
+ value body: unparenthesised_node(n.body.node)
62
+ value eager: value_constituent_traits(body, ["void", "undefined", "any", "null", "parameter"])
63
+ value parts: if eager != null then eager else {
64
+ value arms: value_trait_parts(body, ["void", "undefined", "any", "null", "parameter"])
65
+ value if arms == null then null else map(arms, arm => arm.parts)
66
+ }
67
+ value if strict_void_unsafe_parts(parts, allow_any) then [{site: body, mode: "return"}] else []
68
+ }
69
+ n.return_type != null && n.return_type.text != "void" then [{site: n.return_type.node, mode: "function"}]
70
+ else then {
71
+ value invalid: filter(n.scoping_own_returns, returned => {
72
+ value eager: value_constituent_traits(returned.argument, ["void", "undefined", "any", "null", "parameter"])
73
+ value parts: if eager != null then eager else {
74
+ value arms: value_trait_parts(returned.argument, ["void", "undefined", "any", "null", "parameter"])
75
+ value if arms == null then null else map(arms, arm => arm.parts)
76
+ }
77
+ value returned.argument != null && strict_void_unsafe_parts(parts, allow_any)
78
+ })
79
+ value map(invalid, returned => {site: {start: returned.offset, end: returned.offset + 6}, mode: "return"})
80
+ }
81
+ }
82
+
83
+ in ts_call.argument, ts_new.argument, ts_declarator.init, ts_assignment.right, ts_property, ts_method, ts_class_property.init, ts_return.argument, ts_arrow.body, ts_array.element, ts_jsx_expression.expression rule es-strict-void-return {
84
+ option allowReturnAny boolean = false "Allow callbacks returning any"
85
+ when not (ts_spread, ts_block)
86
+ when not ts_property || node.value not ts_assignment_pattern
87
+ when parent not ts_assignment || contains(["=", "&&=", "||=", "??="], parent.operator)
88
+ when parent not ts_jsx_expression || parent.parent is ts_jsx_attribute
89
+ when not ts_method || node.body != null
90
+ when !(is ts_method && parent is ts_object && node.key.token_neighbours.before.text == "[")
91
+ let actual strict_void_source(node)
92
+ when actual could callable
93
+ let unwrapped unparenthesised_node(actual)
94
+ let signatures {
95
+ value value_signatures(actual) ?? []
96
+ flat_map (arm) arm
97
+ map (header) strict_void_signature(header)
98
+ }
99
+ let expected expected_call_signatures(actual)
100
+ let expected_signatures { value expected ?? []; flat_map (arm) arm }
101
+ let expected_known expected != null && all(expected_signatures, signature => signature.result_known)
102
+ let argument_transfer strict_void_argument_transfer(actual)
103
+ when strict_void_signatures_known(signatures) || unwrapped is (ts_function, ts_arrow, ts_method)
104
+ && (contains([] unwrapped.modifier, "async") || contains([] unwrapped.modifier, "*") || unwrapped.return_type == null)
105
+ when if argument_transfer != null
106
+ then argument_transfer
107
+ && !strict_void_source_allowed(signatures, allowReturnAny)
108
+ else expected_known
109
+ && strict_void_expected_transfer(expected_signatures, outer_parenthesised_node(actual).parent is (ts_call, ts_new))
110
+ && !strict_void_source_allowed(signatures, allowReturnAny)
111
+ each report in strict_void_reports(unwrapped, allowReturnAny) {
112
+ report at report.site
113
+ warn when {
114
+ report.mode == "function" then "Value-returning function used in a context where a void function is expected."
115
+ report.mode == "async" then "Async function used in a context where a void function is expected."
116
+ else then "Value returned in a context where a void return is expected."
117
+ }
118
+ }
119
+ }
120
+
121
+ }