@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,110 @@
1
+ # Built-in globals, prototypes and their aliases.
2
+
3
+ define {
4
+
5
+ # The global `eval`, called or referenced (`(0, eval)`, `globalThis.eval`).
6
+ @macro suggestion_eval_policy {
7
+ option allowIndirect boolean = false "allow indirect eval references and calls"
8
+ when if is ts_identifier then text == "eval" else static_name == "eval"
9
+ let eval_info eval_usage
10
+ when eval_info != null && (!allowIndirect || eval_info.direct && !eval_info.optional)
11
+ report at { start: eval_info.offset, end: eval_info.end }
12
+ warn "`eval` can be harmful."
13
+ }
14
+ in ts_identifier rule es-no-eval { @suggestion_eval_policy }
15
+ in ts_member, ts_index rule es-no-eval:member { @suggestion_eval_policy }
16
+
17
+ in ts_declarator rule es-no-undef-init {
18
+ when init == "undefined" && init.resolved == "global" && re_test(ts_variable_declaration.kind, /^(var|let)$/)
19
+ let undef_initializer { offset: type.end_offset ?? name.end_offset, end_offset: init.end_offset }
20
+ fix at { start: undef_initializer.offset, end: undef_initializer.end_offset } if ts_variable_declaration.kind == "let" && name is ts_identifier && expression_fix_clean(undef_initializer) as ""
21
+ warn "It's not necessary to initialize '{name}' to undefined."
22
+ }
23
+
24
+ fn parse_int_target(n) = if enabled_global_identifier(n, "parseInt") || n is (ts_member, ts_index) && n.static_name == "parseInt" && { value n.object.node; unparenthesised_node; enabled_global_identifier "Number" } then n.text else null
25
+ in ts_call let parse_int_name { value callee.node; unparenthesised_node; parse_int_target }
26
+ fn valid_radix(value) = value.type == "number" && value.value >= 2 && value.value <= 36 && value.value % 1 == 0
27
+ fn invalid_radix(n) = n is ts_identifier && n.text == "undefined" && n.resolved == "global" || (n is (ts_number, ts_string, ts_boolean, ts_null, ts_regex, ts_bigint) || n is ts_unary && contains(["+", "-"], n.operator) && n.argument is ts_number) && !valid_radix(n.literal_value)
28
+
29
+ in ts_call rule es-radix {
30
+ option mode ("always" | "as-needed") = "always" "legacy radix policy"
31
+ when parse_int_name != null
32
+ let argument_count count(argument)
33
+ let first_argument { value argument.nodes; first; unparenthesised_node }
34
+ let second_argument {
35
+ value argument.nodes
36
+ slice 1
37
+ first
38
+ unparenthesised_node
39
+ }
40
+ when first_argument not ts_spread && second_argument not ts_spread
41
+ when argument_count < 2 || invalid_radix(second_argument)
42
+ warn when {
43
+ argument_count == 0 then "Missing parameters."
44
+ argument_count == 1 then "Missing radix parameter."
45
+ else then "Invalid radix parameter, must be an integer between 2 and 36."
46
+ }
47
+ }
48
+
49
+ in ts_identifier rule es-no-shadow-restricted-names {
50
+ option reportGlobalThis boolean = true "include globalThis among restricted names"
51
+ when contains(["NaN", "Infinity", "undefined", "eval", "arguments", "globalThis"], identifier_name)
52
+ && restricted_binding && (text != "globalThis" || reportGlobalThis)
53
+ warn "Shadowing of global property '{text}'."
54
+ }
55
+
56
+ in ts_member, ts_index rule es-no-proto {
57
+ when static_name == "__proto__"
58
+ warn "The '__proto__' property is deprecated."
59
+ }
60
+
61
+ in ts_member, ts_index rule es-no-iterator {
62
+ when static_name == "__iterator__"
63
+ warn "Reserved name '__iterator__'."
64
+ }
65
+
66
+ in ts_call rule es-symbol-description {
67
+ when callee == "Symbol" && callee.resolved == "global" && count(argument) == 0
68
+ warn "Expected Symbol to have a description."
69
+ }
70
+
71
+ in ts_call rule es-prefer-numeric-literals {
72
+ when count(argument) == 2 && parse_int_name != null
73
+ when first([] argument.literal_value).type == "string"
74
+ when { value argument; last_argument: last; value contains(["2", "8", "16"], last_argument) }
75
+ let radix_label when {
76
+ last(argument) == "2" then "binary"
77
+ last(argument) == "8" then "octal"
78
+ else then "hexadecimal"
79
+ }
80
+ warn "Use {radix_label} literals instead of {parse_int_name}()."
81
+ let literal first(argument.nodes)
82
+ let base last(argument)
83
+ let digits literal.literal_value.value
84
+ let valid_digits when {
85
+ base == "2" then /\A[01]+\z/
86
+ base == "8" then /\A[0-7]+\z/
87
+ else then /\A[0-9a-fA-F]+\z/
88
+ }
89
+ let replacement (when {
90
+ base == "2" then "0b"
91
+ base == "8" then "0o"
92
+ else then "0x"
93
+ }) digits
94
+ fix if (literal is ts_string || literal is ts_template && count(literal.expression) == 0) && re_test(digits, valid_digits) && expression_fix_clean(node) as expression_fix_edges(node, replacement)
95
+ }
96
+
97
+ in ts_member, ts_index rule es-no-console {
98
+ option allow string[] = [] "console methods to allow"
99
+ when object == "console" && object.resolved == "global" && !contains(allow, static_name)
100
+ warn if count(allow) == 0 then "Unexpected console statement." else "Unexpected console statement. Only these console methods are allowed: {join(allow, ', ')}."
101
+ }
102
+
103
+ in ts_call rule es-prefer-object-has-own {
104
+ when own_property_call
105
+ warn "Use 'Object.hasOwn()' instead of 'Object.prototype.hasOwnProperty.call()'."
106
+ let target unparenthesised_node(callee.node)
107
+ fix at target if expression_fix_clean(target) as expression_fix_edges(target, "Object.hasOwn")
108
+ }
109
+
110
+ }
@@ -0,0 +1,41 @@
1
+ # Literal-side comparison policy and source-preserving operand swaps.
2
+ import .ts/expression-fixes
3
+
4
+ define {
5
+
6
+ fn comparison_literal(n) = {
7
+ value bare: unparenthesised_node(n)
8
+ value bare is ts_literal || bare is ts_template && count(bare.expression) == 0
9
+ || bare is ts_unary && bare.operator == "-" && unparenthesised_node(bare.argument.node) is (ts_number, ts_bigint)
10
+ }
11
+ in ts_binary let left_literal comparison_literal(left.node)
12
+ in ts_binary let right_literal comparison_literal(right.node)
13
+ in ts_binary let range_terms if !contains(["<", "<="], operator) then null else { left_key: left.reference_key, right_key: right.reference_key, left_literal: left_literal, right_literal: right_literal, left_value: left.literal_value, right_value: right.literal_value }
14
+ in ts_parenthesized forward range_terms from expression
15
+ fn yoda_ordered(a_literal, a, b_literal, b) = if !a_literal && !b_literal then false else if !a_literal || !b_literal then true else a != null && b != null && js_compare(a, b) != null && js_compare(a, b) <= 0
16
+ fn yoda_range_pair(a, b, op) = a != null && b != null && (if op == "&&" then a.right_key != null && a.right_key == b.left_key && yoda_ordered(a.left_literal, a.left_value, b.right_literal, b.right_value) else op == "||" && a.left_key != null && a.left_key == b.right_key && yoda_ordered(a.right_literal, a.right_value, b.left_literal, b.left_value))
17
+ in ts_logical let logical_range (parent is ts_parenthesized || is (ts_if.test, ts_while.test, ts_do_while.test, ts_for.test)) && yoda_range_pair(left.range_terms, right.range_terms, operator)
18
+ in ts_parenthesized let logical_range parent is ts_logical && parent.logical_range == true
19
+
20
+ fn comparison_fix(source, n) = {
21
+ value reversed: get({ "<": ">", "<=": ">=", ">": "<", ">=": "<=" }, n.operator) ?? n.operator
22
+ value right: byte_slice(source, n.right.offset, n.end_offset)
23
+ value before: byte_slice(source, n.left.end_offset, n.operator.offset)
24
+ value after: byte_slice(source, n.operator.end_offset, n.right.offset)
25
+ value left: byte_slice(source, n.offset, n.left.end_offset)
26
+ value text: right before reversed after left
27
+ value expression_fix_edges(n, text)
28
+ }
29
+
30
+ in ts_binary rule es-yoda {
31
+ option mode ("never" | "always") = "never" "literal comparison side"
32
+ option exceptRange boolean = false "allow parenthesised ordered range checks"
33
+ option onlyEquality boolean = false "apply only to equality operators"
34
+ when contains(["==", "===", "!=", "!==", "<", ">", "<=", ">="], operator)
35
+ when (!onlyEquality || contains(["==", "==="], operator)) && !(exceptRange && parent.logical_range == true)
36
+ when if mode == "always" then right_literal && !left_literal else left_literal && !right_literal
37
+ fix at node as comparison_fix(source, node)
38
+ warn "Expected literal to be on the {if mode == 'always' then 'left' else 'right'} side of {operator}."
39
+ }
40
+
41
+ }
@@ -0,0 +1,20 @@
1
+ # Computed literal key policy and token-safe fixes.
2
+ import .ts/expression-fixes
3
+
4
+ define {
5
+
6
+ in ts_computed_key let computed_literal_text if (expression.unwrapped_node.nonempty ?? expression.node) is (ts_string, ts_number) then expression.unwrapped_text.nonempty ?? expression else null
7
+ in ts_method, ts_class_property let member_static contains([] modifier, "static")
8
+ in ts_property, ts_pattern_property let computed_exception parent is ts_object && parent.flow_destructuring_target != true && static_name == "__proto__"
9
+ in ts_method let computed_exception if parent is ts_class then static_name == (if member_static then "prototype" else "constructor") else parent is ts_object && parent.flow_destructuring_target != true && static_name == "__proto__"
10
+ in ts_class_property let computed_exception static_name == "constructor" || member_static && static_name == "prototype"
11
+
12
+ in ts_property, ts_pattern_property, ts_method, ts_class_property rule es-no-useless-computed-key {
13
+ option enforceForClassMembers boolean = true "check class member keys"
14
+ let computed_text key.computed_literal_text
15
+ when computed_text != null && !computed_exception && (parent not ts_class || enforceForClassMembers)
16
+ warn "Unnecessarily computed property [{computed_text}] found."
17
+ fix at key.node if expression_fix_clean(key.node) as expression_fix_edges(key.node, computed_text)
18
+ }
19
+
20
+ }
@@ -0,0 +1,54 @@
1
+ # Constructors and native prototype extension.
2
+
3
+ define {
4
+
5
+ in ts_new rule es-no-new-wrappers {
6
+ when re_test(global_callee, /^(String|Number|Boolean)$/)
7
+ warn "Do not use {global_callee} as a constructor."
8
+ }
9
+
10
+ fn array_constructor_fix(source, n) = {
11
+ value open: {
12
+ value overlapping(ts_open_paren, n.callee.end_offset, n.end_offset)
13
+ find (token) token.paired_token.end == n.end_offset
14
+ }
15
+ value arguments: if open != null then byte_slice(source, open.end_offset, open.paired_token.start) else ""
16
+ value replacement: "[" arguments "]"
17
+ value statement: expression_fix_statement(n)
18
+ value owners: if statement != null then expression_fix_before_nodes(n) else []
19
+ value semicolon: if statement != null then expression_fix_asi(statement, n.token_neighbours.before, owners, replacement) else false
20
+ value if open == null && count(n.argument) > 0 || semicolon == null then null
21
+ else (if semicolon then ";" else "") replacement
22
+ }
23
+ in ts_call, ts_new rule es-no-array-constructor {
24
+ when global_callee == "Array" && count(type_argument) == 0
25
+ when count(argument) != 1 || { value argument; first; re_test /^\.\.\./ }
26
+ warn "The array literal notation [] is preferable."
27
+ let replacement array_constructor_fix(source, node)
28
+ fix at node if replacement != null as replacement
29
+ }
30
+
31
+ in ts_call, ts_new rule es-no-object-constructor {
32
+ when global_callee == "Object" && count(argument) == 0
33
+ warn "The object literal notation {{}} is preferable."
34
+ }
35
+
36
+ in ts_expression_statement.expression rule es-no-new {
37
+ when is ts_new
38
+ warn "Do not use 'new' for side effects."
39
+ }
40
+
41
+ in ts_member, ts_index let global_function_method object == "Function" && object.resolved == "global" && contains(["apply", "bind", "call"], static_name)
42
+ in ts_parenthesized let global_function_method expression.global_function_method
43
+ in ts_call, ts_new rule es-no-new-func {
44
+ when callee == "Function" && callee.resolved == "global" || callee.global_function_method == true
45
+ warn "The Function constructor is eval."
46
+ }
47
+
48
+ in ts_assignment, ts_call rule es-no-extend-native {
49
+ option exceptions string[] = [] "builtins that may be extended"
50
+ when extended_native != null && !contains(exceptions, extended_native)
51
+ warn "{extended_native} prototype is read only, properties should not be added."
52
+ }
53
+
54
+ }
@@ -0,0 +1,204 @@
1
+ # Loops, branches, statements and clauses.
2
+
3
+ define {
4
+
5
+ # Statement properties stay lazy: rules request only the shape they need.
6
+ in ts_if, ts_for, ts_for_in, ts_for_of, ts_while, ts_with, ts_labeled let unmatched_tail curly_unmatched_tail(node)
7
+ in ts_if, ts_for, ts_for_in, ts_for_of, ts_while, ts_with, ts_labeled, ts_block let enclosing_else_boundary curly_enclosing_else(node)
8
+ in ts_block let else_block is ts_if.alternate
9
+
10
+ in ts_return let return_end true
11
+ in ts_block let return_end last(body.nodes) is ts_return
12
+ in ts_return let branch_exit true
13
+ in ts_if let branch_exit consequent.return_end == true && alternate.return_end == true
14
+ in ts_return, ts_if let returning_branch branch_exit == true
15
+ in ts_block let returning_branch any([] body.branch_exit, r => r == true)
16
+ in ts_if let chain_else_node when {
17
+ consequent.returning_branch != true || alternate == null then null
18
+ alternate is ts_if then alternate.chain_else_node
19
+ else then alternate.node
20
+ }
21
+ in ts_if let chain_else_offset chain_else_node.offset
22
+
23
+ in ts_unary let negative_test operator == "!"
24
+ in ts_binary let negative_test operator == "!=" || operator == "!=="
25
+ in ts_parenthesized let negative_test expression.negative_test == true
26
+ in ts_continue let continue_guard true
27
+ in ts_block let continue_guard count([] body) == 1 && first(body.nodes) is ts_continue
28
+ in ts_if let conditional_continue consequent.continue_guard == true
29
+ in ts_block let guarded_iteration count([] body) == 0 || first(body.nodes) is ts_if && (count([] body) == 1 || first([] body.conditional_continue) == true)
30
+
31
+ in ts_variable_declaration let async_resource_declaration kind == "await using"
32
+ in ts_for_of let asynchronous_iteration contains([] modifier, "await")
33
+ in ts_labeled let declared_label label
34
+ in ts_node let repeated_evaluation when {
35
+ parent is ts_function_like || parent is ts_for_of && parent.asynchronous_iteration == true then false
36
+ is (ts_for.body, ts_for.test, ts_for.update, ts_for_in.body, ts_for_of.body) || is (ts_for_in.left, ts_for_of.left) && async_resource_declaration == true || is (ts_while.body, ts_while.test, ts_do_while.body, ts_do_while.test) then true
37
+ else then parent.repeated_evaluation == true
38
+ }
39
+ in ts_node let nearest_break_target when {
40
+ is (ts_loop, ts_switch) then { label: if parent is ts_labeled then parent.declared_label else null }
41
+ is ts_function_like || is ts_labeled && declared_label == get(parent.nearest_break_target, "label") then null
42
+ else then parent.nearest_break_target
43
+ }
44
+
45
+ in ts_with rule es-no-with {
46
+ report at { start: first_token.start, end: first_token.end }
47
+ warn "Unexpected use of 'with' statement."
48
+ }
49
+
50
+ in ts_case rule es-default-case-last {
51
+ when test == null && index < count(parent.case) - 1
52
+ warn "Default clause should be the last clause."
53
+ }
54
+
55
+ in ts_switch let empty_switch count(case) == 0
56
+ in ts_block, ts_switch rule es-no-empty {
57
+ option allowEmptyCatch boolean = false "allow empty catch blocks"
58
+ when parent not (ts_function_like, ts_static_block)
59
+ when !allowEmptyCatch || not ts_try.handler
60
+ let empty_start if is ts_switch then opener.ts_open_brace.offset else offset
61
+ when empty_start != null
62
+ when if is ts_switch then empty_switch && !{ value descendants(ts_comment); any (comment) comment.offset > empty_start && comment.end_offset < end_offset } else uncommented_empty_block
63
+ report at { start: empty_start, end: end_offset }
64
+ warn "Empty {if is ts_switch then 'switch' else 'block'} statement."
65
+ }
66
+
67
+ in ts_block rule es-no-lone-blocks {
68
+ let nested parent is ts_block
69
+ when nested || parent is ts_program || parent is ts_case && count(parent.body) != 1
70
+ when nested && count(parent.body) == 1 || !any(body.nodes, statement => if statement is ts_variable_declaration then statement.kind != "var" else statement is ts_class || statement is ts_function && statement.scoping_strict_code)
71
+ warn if nested then "Nested block is redundant." else "Block is redundant."
72
+ }
73
+
74
+ fn suggestion_lonely_if_fix(source, n) = {
75
+ value body: n.parent.node
76
+ value before_gap: { value byte_slice(source, body.first_token.end, n.offset); trim }
77
+ value after_gap: { value byte_slice(source, n.end_offset, body.last_token.start); trim }
78
+ value last_token: n.consequent.last_token
79
+ value after: body.token_neighbours.after
80
+ value hazard: n.consequent not ts_block && last_token.text != ";" && after != null
81
+ && (position(n.consequent.end_offset).js_line == position(after.start).js_line
82
+ || expression_fix_continuation(after.text) || contains(["++", "--"], last_token.text))
83
+ value if before_gap != "" || after_gap != "" || hazard then null
84
+ else (if body.token_neighbours.before.end == body.offset then " " else "") n.text
85
+ }
86
+ in ts_if rule es-no-lonely-if {
87
+ when parent.else_block == true && parent.statement_count == 1
88
+ when !(unmatched_tail && parent.enclosing_else_boundary == true)
89
+ let lonely_if_fix suggestion_lonely_if_fix(source, node)
90
+ fix at parent.node if lonely_if_fix != null as lonely_if_fix
91
+ warn "Unexpected if as the only statement in an else block."
92
+ }
93
+
94
+ in ts_if, ts_conditional rule es-no-negated-condition {
95
+ when is ts_conditional || alternate != null && alternate not ts_if
96
+ when test.negative_test == true
97
+ warn "Unexpected negated condition."
98
+ }
99
+
100
+ in ts_switch rule es-default-case {
101
+ option commentPattern regex | null = null "comments that allow a missing default"
102
+ when count(case) > 0 && !any(case.nodes, branch => branch.test.node == null)
103
+ let final_comment switch_default_comment
104
+ when final_comment == null || !{
105
+ value final_comment.structure_comment_body
106
+ trim: trim
107
+ value default: commentPattern.nonempty ?? "(?i)^no default$"
108
+ value re_test(trim, default)
109
+ }
110
+ warn "Expected a default case."
111
+ }
112
+
113
+ in ts_break, ts_continue rule es-no-extra-label {
114
+ when label != null && label == get(parent.nearest_break_target, "label")
115
+ report at label.node
116
+ fix at { start: first_token.end, end: label.end_offset } if !label.node.space_before.comments as ""
117
+ warn "This label '{label}' is unnecessary."
118
+ }
119
+
120
+ in ts_await, ts_for_of, ts_variable_declaration rule es-no-await-in-loop {
121
+ when is ts_await || asynchronous_iteration == true || async_resource_declaration == true
122
+ when repeated_evaluation
123
+ warn "Unexpected `await` inside a loop."
124
+ }
125
+
126
+ in ts_for_in rule es-guard-for-in {
127
+ when body != null
128
+ when body not (ts_empty, ts_if) && body.guarded_iteration != true
129
+ warn "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype."
130
+ }
131
+
132
+ fn suggestion_else_within(node, owner) = { value node; walk (current) current.parent.node; any (current) current == owner }
133
+ fn suggestion_else_scope_safe(body, destination, declarations) = body not ts_function && (body not ts_block || {
134
+ value moved: { value declarations; filter (item) item.scope.node == body }
135
+ value names: { value moved; map (item) item.identifier_name }
136
+ value collision: {
137
+ value declarations
138
+ any (item) contains(names, item.identifier_name) && (item.scope.node == destination
139
+ || item.declaration_kind == "var" && destination.offset <= item.offset && item.end_offset <= destination.end_offset)
140
+ }
141
+ value identifiers: overlapping(ts_identifier, destination.offset, destination.end_offset)
142
+ value captured: {
143
+ value identifiers
144
+ any (item) contains(names, item.identifier_name) && item.scope != null
145
+ && !(body.offset <= item.offset && item.end_offset <= body.end_offset)
146
+ && item.declaration.node != item
147
+ && (item.declaration == null || !suggestion_else_within(item.declaration.scope.node, destination))
148
+ }
149
+ value uncertain: any(moved, item => item.declaration_kind == "function")
150
+ value !collision && !captured && !uncertain
151
+ })
152
+ fn suggestion_else_fix(body, declarations) = {
153
+ value conditional: body.parent.node
154
+ value consequent: conditional.consequent.node
155
+ value destination: when {
156
+ conditional.parent is ts_case then conditional.parent.parent.node
157
+ conditional.parent is ts_block && conditional.parent.parent is ts_function_like then conditional.parent.parent.node
158
+ else then conditional.parent.node
159
+ }
160
+ value owner: {
161
+ value conditional
162
+ walk (current) if current is (ts_function_like, ts_program) then null else current.parent.node
163
+ last
164
+ }
165
+ value first_statement: first(body.body.nodes)
166
+ value last_statement: last(body.body.nodes)
167
+ value before: consequent.last_token
168
+ value first_content: if body is ts_block then first_statement.first_token ?? body.last_token else body.first_token
169
+ value last_content: if body is ts_block then last_statement.last_token ?? body.first_token else body.last_token
170
+ value after: body.token_neighbours.after
171
+ value before_hazard: consequent not ts_block && before.text != ";" && expression_fix_continuation(first_content.text)
172
+ value after_hazard: last_content.text != ";" && after != null && (expression_fix_continuation(after.text)
173
+ || position(after.start).js_line == position(last_content.start).js_line && after.text != "}}")
174
+ value content: if body is ts_block then byte_slice(body.text, 1, body.end_offset - body.offset - 1) else body.text
175
+ value replacement: byte_slice(owner.text, 0, body.token_neighbours.before.start - owner.offset) content byte_slice(owner.text, body.end_offset - owner.offset)
176
+ value if before_hazard || after_hazard || !suggestion_else_scope_safe(body, destination, declarations) || !expression_fix_retains_comments(owner, replacement) then null
177
+ else {start: owner.offset, end: owner.end_offset, text: replacement}
178
+ }
179
+ in ts_if rule es-no-else-return {
180
+ option allowElseIf boolean = true "allow returning else-if branches"
181
+ when parent is (ts_program, ts_block, ts_case) && consequent.returning_branch == true && alternate != null
182
+ let else_report if allowElseIf then chain_else_offset else alternate.offset
183
+ when else_report != null
184
+ report at else_report
185
+ warn "Unnecessary 'else' after 'return'."
186
+ let body if allowElseIf then chain_else_node else alternate.node
187
+ let replacement suggestion_else_fix(body, ts_program.binding_declarations)
188
+ fix at replacement ?? node if replacement != null as replacement.text
189
+ }
190
+
191
+ in ts_labeled rule es-no-label-var {
192
+ when label_binding
193
+ warn "Found identifier with same name as label."
194
+ }
195
+
196
+ group unreachable_loop ts_while, ts_do_while, ts_for, ts_for_in, ts_for_of
197
+ in unreachable_loop rule es-no-unreachable-loop {
198
+ let continued flow_exits(body.node, "continued")
199
+ when reachable(body.node) == true && flow_normal(body.node) == false
200
+ when continued != null && !any(continued, effect => effect.target == node)
201
+ warn "Invalid loop. Its body allows only one iteration."
202
+ }
203
+
204
+ }
@@ -0,0 +1,124 @@
1
+ # Control-body brace policy and statement-safe source rewrites.
2
+ import .ts/expression-fixes
3
+ import .ts/tokens
4
+
5
+ define {
6
+
7
+ fn curly_unmatched_tail(n) = {
8
+ value tail: walk(if n == null then [] else [n], current =>
9
+ if current is ts_if then current.alternate.node
10
+ else if current is (ts_for, ts_for_in, ts_for_of, ts_while, ts_with, ts_labeled) then current.body.node
11
+ else null)
12
+ value terminal: last(tail)
13
+ value terminal is ts_if && terminal.alternate.node == null
14
+ }
15
+ fn curly_enclosing_else(n) = {
16
+ value ancestors: walk([n], current =>
17
+ if current.parent is (ts_if, ts_for, ts_for_in, ts_for_of, ts_while, ts_with, ts_labeled)
18
+ then current.parent.node else null)
19
+ value any(ancestors, current => current is ts_if.consequent && current.parent.alternate.node != null)
20
+ }
21
+ fn curly_braces_necessary(n) = {
22
+ value statement: first(n.body.nodes)
23
+ value statement is (ts_function, ts_class) || statement is ts_variable_declaration && statement.kind != "var"
24
+ || curly_unmatched_tail(statement) && curly_enclosing_else(n)
25
+ }
26
+ fn curly_one_line(source, n) = {
27
+ value ending: token(n, "last")
28
+ value semicolon: if ending.text == ";" then first(overlapping(ts_semicolon, ending.start, ending.end)) else null
29
+ value last: if semicolon != null then token(semicolon, "before") else ending
30
+ value n is ts_empty || !re_test(byte_slice(source, n.offset, last.end ?? n.end_offset), /[\r\n\u{2028}\u{2029}]/)
31
+ }
32
+ fn curly_expectations(source, owner, body) = {
33
+ value block: body is ts_block
34
+ value statements: body.body.nodes
35
+ value required: block && (count(statements) != 1 || curly_braces_necessary(body))
36
+ value before: body.token_neighbours.before
37
+ value start: if before.start >= owner.offset then before.start else owner.offset
38
+ value collapsed: if block then !re_test(byte_slice(source, start, body.end_offset), /[\r\n\u{2028}\u{2029}]/)
39
+ else {
40
+ value text: trim_end(re_replace(trim_end(body.text), /;$/, ""))
41
+ value !re_test(byte_slice(source, start, body.offset + byte_len(text)), /[\r\n\u{2028}\u{2029}]/)
42
+ }
43
+ value statement: first(statements)
44
+ value nested: if block then !curly_one_line(source, statement)
45
+ || any(overlapping(ts_comment, body.offset + 1, statement.offset), comment => comment.end_offset <= statement.offset)
46
+ else !curly_one_line(source, body)
47
+ value {block: block, multi: if required then 1 else 0,
48
+ multiline: if required then 1 else if collapsed then -1 else 1,
49
+ nested: if required || nested then 1 else 0}
50
+ }
51
+ in ts_node let curly_body_expectations curly_expectations(source, parent.node, node)
52
+ in ts_if let curly_chain_expectations {
53
+ value branches: walk([node], current => if current.alternate is ts_if then current.alternate.node else null)
54
+ value bodies: flat_map(branches, branch => branch.consequent.nodes
55
+ (if branch.alternate not ts_if then branch.alternate.nodes else []))
56
+ value map(bodies, body => body.curly_body_expectations)
57
+ }
58
+ fn curly_body_info(n, mode, consistent) = {
59
+ value owner: n.parent.node
60
+ value alternate: n is ts_if.alternate
61
+ value name: when {
62
+ owner is ts_if then if alternate then "else" else "if"
63
+ owner is ts_while then "while"
64
+ owner is ts_for then "for"
65
+ owner is ts_for_in then "for-in"
66
+ owner is ts_for_of then "for-of"
67
+ owner is ts_do_while then "do"
68
+ else then null
69
+ }
70
+ value if name == null || alternate && n is ts_if then null
71
+ else if mode == "all" then {
72
+ block: n is ts_block, name: name, condition: !alternate && owner is (ts_if, ts_while, ts_for)
73
+ } else {
74
+ value expected: n.curly_body_expectations
75
+ value all: if !consistent then [] else if owner is ts_if then {
76
+ value chain: walk([owner], current => if current is ts_if.alternate then current.parent.node else null)
77
+ value last(chain).curly_chain_expectations
78
+ } else [expected]
79
+ value {
80
+ block: expected.block, name: name, condition: !alternate && owner is (ts_if, ts_while, ts_for),
81
+ multi: expected.multi, multiline: expected.multiline, nested: expected.nested,
82
+ multi_consistent: any(all, item => item.multi == 1 || item.multi == -1 && item.block),
83
+ multiline_consistent: any(all, item => item.multiline == 1 || item.multiline == -1 && item.block),
84
+ nested_consistent: any(all, item => item.nested == 1 || item.nested == -1 && item.block)
85
+ }
86
+ }
87
+ }
88
+
89
+ fn curly_fix_allowed(n, block) = if !block then true else {
90
+ value statement: last(n.body.nodes)
91
+ value after: n.token_neighbours.after
92
+ value tail: statement.last_token
93
+ value after == null || tail.text == ";" || contains([";", "}}"], after.text)
94
+ || (if tail.text == "++" || tail.text == "--" then false
95
+ else if tail.text == "}}" && statement is (ts_block, ts_if, ts_for, ts_for_in, ts_for_of, ts_while, ts_switch, ts_try, ts_with, ts_labeled) then true
96
+ else if after.text == "else" then false
97
+ else after.line_break && !contains(["(", "[", "+", "-", "++", "--"], after.text)
98
+ && !starts_with(after.text, "/") && !starts_with(after.text, "`"))
99
+ }
100
+
101
+ fn curly_replacement(source, n, block) = if !block then "{{" n.text "}}" else {
102
+ value source
103
+ interior: byte_slice n.first_token.end n.last_token.start
104
+ value expression_fix_edges(n, interior)
105
+ }
106
+
107
+ @macro suggestion_curly_policy {
108
+ option mode ("all" | "multi" | "multi-line" | "multi-or-nest") = "all" "control body brace policy"
109
+ option style ("consistent" | null) = null "consistent braces throughout if chains"
110
+ let curly_info curly_body_info(node, mode, style == "consistent")
111
+ when curly_info != null
112
+ let body_expected if mode == "all" then 1 else if mode == "multi" then curly_info.multi else if mode == "multi-line" then curly_info.multiline else curly_info.nested
113
+ let chain_required if mode == "all" || style != "consistent" then false else if mode == "multi" then curly_info.multi_consistent else if mode == "multi-line" then curly_info.multiline_consistent else curly_info.nested_consistent
114
+ let curly_expected if style == "consistent" && mode != "all" then if chain_required then 1 else 0 else body_expected
115
+ when curly_expected != -1 && (curly_expected == 1 && !curly_info.block || curly_expected == 0 && curly_info.block)
116
+ fix at node if curly_fix_allowed(node, curly_info.block) as curly_replacement(source, node, curly_info.block)
117
+ warn "{if curly_expected == 1 then 'Expected' else 'Unnecessary'} {{ after '{curly_info.name}'{if curly_info.condition then ' condition' else ''}."
118
+ }
119
+ in ts_if.consequent rule es-curly:if { @suggestion_curly_policy }
120
+ in ts_if.alternate rule es-curly:else { @suggestion_curly_policy }
121
+ in ts_for.body, ts_while.body rule es-curly:loop { @suggestion_curly_policy }
122
+ in ts_for_in.body, ts_for_of.body, ts_do_while.body rule es-curly:loop-body { @suggestion_curly_policy }
123
+
124
+ }
@@ -0,0 +1,79 @@
1
+ # `use strict` directive policy.
2
+
3
+ define {
4
+
5
+ fn strict_enclosing_function_directive(n) =
6
+ {
7
+ value n.parent.node
8
+ walk (current) current.parent.node
9
+ any (current) current is (ts_function, ts_arrow, ts_method) && count(current.directive_scope.directives) > 0
10
+ }
11
+ fn strict_directive_kind(n, scope, mode, directive) =
12
+ when {
13
+ n not ts_program && !scope.simple_parameters then
14
+ if directive == first(scope.directives) then "non-simple" else "multiple"
15
+ mode == "never" then "never"
16
+ mode == "module" then "module"
17
+ mode == "implied" then "implied"
18
+ n is ts_program then when {
19
+ mode == "function" then "function"
20
+ directive == first(scope.directives) then null
21
+ else then "multiple"
22
+ }
23
+ mode == "global" then "global"
24
+ directive != first(scope.directives) then "multiple"
25
+ strict_enclosing_function_directive(n) then "unnecessary"
26
+ scope.class_body then "class"
27
+ else then null
28
+ }
29
+ fn strict_directive_message(kind) = get({
30
+ "non-simple": "'use strict' directive inside a function with non-simple parameter list throws a syntax error since ES2016.",
31
+ multiple: "Multiple 'use strict' directives.",
32
+ never: "Strict mode is not permitted.",
33
+ module: "'use strict' is unnecessary inside of modules.",
34
+ implied: "'use strict' is unnecessary when implied strict mode is enabled.",
35
+ function: "Use the function form of 'use strict'.",
36
+ global: "Use the global form of 'use strict'.",
37
+ unnecessary: "Unnecessary 'use strict' directive.",
38
+ class: "'use strict' is unnecessary inside of classes."
39
+ }, kind)
40
+ fn strict_missing(n, scope, mode) =
41
+ when {
42
+ count(scope.directives) != 0 then []
43
+ n is ts_program && mode == "global" && count(n.body.node) > 0 then [{ report: { start: first(n.body.node).offset, end: last(n.body.node).end_offset }, message: "Use the global form of 'use strict'." }]
44
+ n not ts_program && mode == "function" && !scope.parent_function && !scope.class_body then [{
45
+ report: n.offset,
46
+ message: if scope.simple_parameters then "Use the function form of 'use strict'." else "Wrap " (n.function_name.nonempty ?? "function") " in a function with 'use strict' directive."
47
+ }]
48
+ else then []
49
+ }
50
+ in ts_program, ts_function, ts_arrow, ts_method rule es-strict {
51
+ option mode ("safe" | "never" | "global" | "function") = "safe" "strict directive policy"
52
+ let strict_scope directive_scope
53
+ let strict_mode when {
54
+ ts_source_type(ts_module_program(node)) == "module" then "module"
55
+ source_environment.implied_strict then "implied"
56
+ mode == "safe" then if source_environment.root_function then "global" else "function"
57
+ else then mode
58
+ }
59
+ let strict_findings {
60
+ value initial_findings: strict_missing(node, strict_scope, strict_mode)
61
+ value strict_scope.directives
62
+ fold initial_findings (findings, directive) {
63
+ value kind: strict_directive_kind(node, strict_scope, strict_mode, directive)
64
+ value findings [{
65
+ report: directive.offset,
66
+ directive: directive,
67
+ kind: kind,
68
+ message: strict_directive_message(kind)
69
+ }]
70
+ }
71
+ }
72
+ each strict_finding in filter(strict_findings, finding => finding.message != null) {
73
+ report at strict_finding.report
74
+ fix at strict_finding.directive if contains(["multiple", "unnecessary", "module", "implied", "class"], strict_finding.kind) && expression_fix_clean(strict_finding.directive) as ""
75
+ warn strict_finding.message
76
+ }
77
+ }
78
+
79
+ }