@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,101 @@
1
+ # Void-expression positions and source-preserving statement fixes.
2
+ import .ts/expression-fixes
3
+
4
+ define {
5
+
6
+ fn void_expression_like(n) =
7
+ if n is (ts_call, ts_tagged_template) then n.result could undefined && n.result always undefined
8
+ else n could undefined && n always undefined
9
+ fn void_expression_fixable(n) = {
10
+ value n
11
+ walk (current) {
12
+ value expression: unparenthesised_node(current)
13
+ value if expression is ts_conditional then [expression.consequent.node, expression.alternate.node]
14
+ else if expression is ts_logical then [expression.left.node, expression.right.node] else null
15
+ }
16
+ all (current) {
17
+ value expression: unparenthesised_node(current)
18
+ value if expression is ts_conditional then expression.consequent != null && expression.alternate != null
19
+ else if expression is ts_logical then expression.left != null && expression.right != null
20
+ else void_expression_like(expression)
21
+ }
22
+ }
23
+ fn void_expression_annotation_void(n) = {
24
+ value arms: value_trait_parts(n, ["void", "parameter"])
25
+ value arms != null && any(arms, arm => any(arm.parts, part => contains(part.traits, "void")
26
+ && !contains(part.uncertain_traits, "void") && !contains(part.traits, "parameter")))
27
+ }
28
+ fn void_expression_function(n) = {
29
+ value n
30
+ walk (current) current.parent.node
31
+ find (current) current is (ts_function, ts_arrow, ts_method)
32
+ }
33
+ fn void_expression_expected_void(n) = {
34
+ value direct: value_has_expected(n) && n.expected could callable && n.expected.result could void
35
+ value signatures: expected_call_signatures(n) ?? []
36
+ value headers: flat_map(signatures, arm => arm)
37
+ value signed: any(headers, header => header.result_known && any(header.result ?? [], arm => any(arm, part => contains(part.traits, "void"))))
38
+ value direct || signed
39
+ }
40
+ fn void_expression_void_returning(n) = n != null && (
41
+ if n.return_type != null then void_expression_annotation_void(n.return_type.node)
42
+ else (n is ts_arrow || n is ts_function && !expression_fix_declaration(n))
43
+ && void_expression_expected_void(n))
44
+ fn void_expression_ancestor(n, arrows, operators) = {
45
+ value terminal: {
46
+ value n
47
+ walk (current) {
48
+ value p: current.parent.node
49
+ value if p is ts_parenthesized || p is ts_logical && p.right.offset == current.offset
50
+ || p is ts_conditional && (p.consequent.offset == current.offset || p.alternate.offset == current.offset)
51
+ then p else null
52
+ }
53
+ last
54
+ }
55
+ value p: terminal.parent.node
56
+ value if p is ts_expression_statement
57
+ || p is ts_sequence && terminal.index < count(p.expression) - 1
58
+ || p is ts_arrow && arrows || p is ts_unary && p.operator == "void" && operators then null
59
+ else p
60
+ }
61
+ fn void_expression_final_return(n) = n.parent is ts_block
62
+ && n.parent.parent is (ts_function, ts_arrow, ts_method)
63
+ && last(n.parent.body.nodes).offset == n.offset
64
+ fn void_expression_return_text(n, final) = {
65
+ value argument: n.argument.node
66
+ value statement: (if any(["(", "[", "`"], token => starts_with(argument.text, token)) then ";" else "") argument.text ";" (if final then "" else " return;")
67
+ value if !final && n.parent not ts_block then "{{ " statement " }}" else statement
68
+ }
69
+
70
+ in ts_await, ts_call, ts_tagged_template rule es-no-confusing-void-expression {
71
+ option ignoreArrowShorthand boolean = false "Allow concise arrow bodies"
72
+ option ignoreVoidOperator boolean = false "Allow explicit void expressions"
73
+ option ignoreVoidReturningFunctions boolean = false "Allow explicitly or contextually void-returning functions"
74
+ let ancestor void_expression_ancestor(node, ignoreArrowShorthand, ignoreVoidOperator)
75
+ when ancestor != null
76
+ when void_expression_like(node)
77
+ let enclosing if ignoreVoidReturningFunctions then void_expression_function(ancestor) else null
78
+ when !ignoreVoidReturningFunctions || ancestor not (ts_arrow, ts_return)
79
+ || !void_expression_void_returning(enclosing)
80
+ let final ancestor is ts_return && void_expression_final_return(ancestor)
81
+ let mode if ancestor is ts_arrow then if ignoreVoidOperator then "arrow-void" else "arrow"
82
+ else if ancestor is ts_return then if ignoreVoidOperator then "return-void" else if final then "last" else "return"
83
+ else if ignoreVoidOperator then "expression-void" else "expression"
84
+ let target if mode == "arrow" then ancestor.body.node
85
+ else if contains(["last", "return"], mode) then ancestor else node
86
+ let replacement if mode == "arrow" then if void_expression_fixable(target) then "{{ " target.structure_unwrapped.text "; }}" else null
87
+ else if mode == "last" then if void_expression_fixable(ancestor.argument.node) then void_expression_return_text(ancestor, true) else null
88
+ else if mode == "return" then if void_expression_fixable(ancestor.argument.node) then void_expression_return_text(ancestor, false) else null
89
+ else if contains(["arrow-void", "return-void"], mode) then "void " text
90
+ else null
91
+ warn if mode == "expression" then "Placing a void expression inside another expression is forbidden. Move it to its own statement instead."
92
+ else if mode == "arrow" then "Returning a void expression from an arrow function shorthand is forbidden. Please add braces to the arrow function."
93
+ else if mode == "last" then "Returning a void expression from a function is forbidden. Please remove the `return` statement."
94
+ else if mode == "return" then "Returning a void expression from a function is forbidden. Please move it before the `return` statement."
95
+ else if mode == "arrow-void" then "Void expressions returned from an arrow function shorthand must be marked explicitly with the `void` operator."
96
+ else if mode == "return-void" then "Void expressions returned from a function must be marked explicitly with the `void` operator."
97
+ else "Void expressions used inside another expression must be moved to its own statement or marked explicitly with the `void` operator."
98
+ fix at target if replacement != null && expression_fix_retains_comments(target, replacement) as replacement
99
+ }
100
+
101
+ }