@ttsc/lint 0.13.0 → 0.14.0-dev.20260528

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 (467) hide show
  1. package/README.md +948 -69
  2. package/lib/defaultFormat.d.ts +9 -6
  3. package/lib/defaultFormat.js +9 -6
  4. package/lib/defaultFormat.js.map +1 -1
  5. package/lib/index.d.ts +46 -0
  6. package/lib/index.js +60 -37
  7. package/lib/index.js.map +1 -1
  8. package/lib/structures/ITtscLintConfig.d.ts +12 -6
  9. package/lib/structures/{ITtscLintFormatConfig.d.ts → ITtscLintFormat.d.ts} +2 -2
  10. package/lib/structures/{TtscLintRuleMap.js → ITtscLintFormat.js} +1 -1
  11. package/lib/structures/ITtscLintFormat.js.map +1 -0
  12. package/lib/structures/TtscLintRuleSetting.d.ts +49 -0
  13. package/lib/structures/{TtscLintRuleOptions.js → TtscLintRuleSetting.js} +1 -1
  14. package/lib/structures/TtscLintRuleSetting.js.map +1 -0
  15. package/lib/structures/index.d.ts +3 -4
  16. package/lib/structures/index.js +3 -4
  17. package/lib/structures/index.js.map +1 -1
  18. package/lib/structures/rules/ITtscLintBoundariesRuleOptions.d.ts +104 -0
  19. package/lib/structures/rules/ITtscLintBoundariesRuleOptions.js +14 -0
  20. package/lib/structures/rules/ITtscLintBoundariesRuleOptions.js.map +1 -0
  21. package/lib/structures/rules/ITtscLintBoundariesRules.d.ts +79 -0
  22. package/lib/structures/rules/ITtscLintBoundariesRules.js +3 -0
  23. package/lib/structures/rules/ITtscLintBoundariesRules.js.map +1 -0
  24. package/lib/structures/rules/ITtscLintContributorRules.d.ts +17 -0
  25. package/lib/structures/rules/ITtscLintContributorRules.js +3 -0
  26. package/lib/structures/rules/ITtscLintContributorRules.js.map +1 -0
  27. package/lib/structures/rules/ITtscLintCoreRules.d.ts +1257 -0
  28. package/lib/structures/{ITtscLintFormatConfig.js → rules/ITtscLintCoreRules.js} +1 -1
  29. package/lib/structures/rules/ITtscLintCoreRules.js.map +1 -0
  30. package/lib/structures/rules/ITtscLintCypressRuleOptions.d.ts +17 -0
  31. package/lib/structures/rules/ITtscLintCypressRuleOptions.js +10 -0
  32. package/lib/structures/rules/ITtscLintCypressRuleOptions.js.map +1 -0
  33. package/lib/structures/rules/ITtscLintCypressRules.d.ts +129 -0
  34. package/lib/structures/rules/ITtscLintCypressRules.js +3 -0
  35. package/lib/structures/rules/ITtscLintCypressRules.js.map +1 -0
  36. package/lib/structures/rules/ITtscLintFunctionalRuleOptions.d.ts +157 -0
  37. package/lib/structures/rules/ITtscLintFunctionalRuleOptions.js +13 -0
  38. package/lib/structures/rules/ITtscLintFunctionalRuleOptions.js.map +1 -0
  39. package/lib/structures/rules/ITtscLintFunctionalRules.d.ts +167 -0
  40. package/lib/structures/rules/ITtscLintFunctionalRules.js +3 -0
  41. package/lib/structures/rules/ITtscLintFunctionalRules.js.map +1 -0
  42. package/lib/structures/rules/ITtscLintJestRules.d.ts +201 -0
  43. package/lib/structures/rules/ITtscLintJestRules.js +3 -0
  44. package/lib/structures/rules/ITtscLintJestRules.js.map +1 -0
  45. package/lib/structures/rules/ITtscLintJsdocRules.d.ts +140 -0
  46. package/lib/structures/rules/ITtscLintJsdocRules.js +3 -0
  47. package/lib/structures/rules/ITtscLintJsdocRules.js.map +1 -0
  48. package/lib/structures/rules/ITtscLintJsxA11yRules.d.ts +302 -0
  49. package/lib/structures/rules/ITtscLintJsxA11yRules.js +3 -0
  50. package/lib/structures/rules/ITtscLintJsxA11yRules.js.map +1 -0
  51. package/lib/structures/rules/ITtscLintNextjsRules.d.ts +185 -0
  52. package/lib/structures/rules/ITtscLintNextjsRules.js +3 -0
  53. package/lib/structures/rules/ITtscLintNextjsRules.js.map +1 -0
  54. package/lib/structures/rules/ITtscLintPlaywrightRules.d.ts +330 -0
  55. package/lib/structures/rules/ITtscLintPlaywrightRules.js +3 -0
  56. package/lib/structures/rules/ITtscLintPlaywrightRules.js.map +1 -0
  57. package/lib/structures/rules/ITtscLintPromiseRules.d.ts +188 -0
  58. package/lib/structures/rules/ITtscLintPromiseRules.js +3 -0
  59. package/lib/structures/rules/ITtscLintPromiseRules.js.map +1 -0
  60. package/lib/structures/rules/ITtscLintReactPerfRuleOptions.d.ts +18 -0
  61. package/lib/structures/rules/ITtscLintReactPerfRuleOptions.js +8 -0
  62. package/lib/structures/rules/ITtscLintReactPerfRuleOptions.js.map +1 -0
  63. package/lib/structures/rules/ITtscLintReactPerfRules.d.ts +51 -0
  64. package/lib/structures/rules/ITtscLintReactPerfRules.js +3 -0
  65. package/lib/structures/rules/ITtscLintReactPerfRules.js.map +1 -0
  66. package/lib/structures/rules/ITtscLintReactRuleOptions.d.ts +39 -0
  67. package/lib/structures/rules/ITtscLintReactRuleOptions.js +12 -0
  68. package/lib/structures/rules/ITtscLintReactRuleOptions.js.map +1 -0
  69. package/lib/structures/rules/ITtscLintReactRules.d.ts +289 -0
  70. package/lib/structures/rules/ITtscLintReactRules.js +3 -0
  71. package/lib/structures/rules/ITtscLintReactRules.js.map +1 -0
  72. package/lib/structures/rules/ITtscLintRegexpRules.d.ts +190 -0
  73. package/lib/structures/rules/ITtscLintRegexpRules.js +3 -0
  74. package/lib/structures/rules/ITtscLintRegexpRules.js.map +1 -0
  75. package/lib/structures/rules/ITtscLintRuleOptionsMap.d.ts +66 -0
  76. package/lib/structures/rules/ITtscLintRuleOptionsMap.js +3 -0
  77. package/lib/structures/rules/ITtscLintRuleOptionsMap.js.map +1 -0
  78. package/lib/structures/rules/ITtscLintRules.d.ts +55 -0
  79. package/lib/structures/{TtscLintRule.js → rules/ITtscLintRules.js} +1 -1
  80. package/lib/structures/rules/ITtscLintRules.js.map +1 -0
  81. package/lib/structures/rules/ITtscLintSecurityRules.d.ts +148 -0
  82. package/lib/structures/rules/ITtscLintSecurityRules.js +3 -0
  83. package/lib/structures/rules/ITtscLintSecurityRules.js.map +1 -0
  84. package/lib/structures/rules/ITtscLintSolidRules.d.ts +181 -0
  85. package/lib/structures/rules/ITtscLintSolidRules.js +3 -0
  86. package/lib/structures/rules/ITtscLintSolidRules.js.map +1 -0
  87. package/lib/structures/rules/ITtscLintStorybookRuleOptions.d.ts +21 -0
  88. package/lib/structures/rules/ITtscLintStorybookRuleOptions.js +9 -0
  89. package/lib/structures/rules/ITtscLintStorybookRuleOptions.js.map +1 -0
  90. package/lib/structures/rules/ITtscLintStorybookRules.d.ts +174 -0
  91. package/lib/structures/rules/ITtscLintStorybookRules.js +3 -0
  92. package/lib/structures/rules/ITtscLintStorybookRules.js.map +1 -0
  93. package/lib/structures/rules/ITtscLintTanstackQueryRules.d.ts +94 -0
  94. package/lib/structures/rules/ITtscLintTanstackQueryRules.js +3 -0
  95. package/lib/structures/rules/ITtscLintTanstackQueryRules.js.map +1 -0
  96. package/lib/structures/rules/ITtscLintTestingLibraryRuleOptions.d.ts +22 -0
  97. package/lib/structures/rules/ITtscLintTestingLibraryRuleOptions.js +10 -0
  98. package/lib/structures/rules/ITtscLintTestingLibraryRuleOptions.js.map +1 -0
  99. package/lib/structures/rules/ITtscLintTestingLibraryRules.d.ts +310 -0
  100. package/lib/structures/rules/ITtscLintTestingLibraryRules.js +3 -0
  101. package/lib/structures/rules/ITtscLintTestingLibraryRules.js.map +1 -0
  102. package/lib/structures/rules/ITtscLintTypeScriptRules.d.ts +1132 -0
  103. package/lib/structures/rules/ITtscLintTypeScriptRules.js +3 -0
  104. package/lib/structures/rules/ITtscLintTypeScriptRules.js.map +1 -0
  105. package/lib/structures/rules/ITtscLintUnicornRules.d.ts +997 -0
  106. package/lib/structures/rules/ITtscLintUnicornRules.js +3 -0
  107. package/lib/structures/rules/ITtscLintUnicornRules.js.map +1 -0
  108. package/lib/structures/rules/ITtscLintVitestRules.d.ts +141 -0
  109. package/lib/structures/rules/ITtscLintVitestRules.js +3 -0
  110. package/lib/structures/rules/ITtscLintVitestRules.js.map +1 -0
  111. package/lib/structures/rules/index.d.ts +31 -0
  112. package/lib/structures/rules/index.js +48 -0
  113. package/lib/structures/rules/index.js.map +1 -0
  114. package/linthost/ast_helpers.go +30 -5
  115. package/linthost/compile.go +33 -2
  116. package/linthost/config.go +48 -17
  117. package/linthost/config_format.go +9 -9
  118. package/linthost/directives.go +67 -25
  119. package/linthost/dispatch.go +14 -3
  120. package/linthost/engine.go +160 -40
  121. package/linthost/fix.go +2 -2
  122. package/linthost/flags_gen.go +17 -15
  123. package/linthost/host.go +40 -3
  124. package/linthost/lsp.go +747 -0
  125. package/linthost/print_dispatch.go +2 -2
  126. package/linthost/print_nodes_function.go +2 -2
  127. package/linthost/print_nodes_object.go +1 -1
  128. package/linthost/rule_names.go +14 -0
  129. package/linthost/rules_arrays.go +2 -2
  130. package/linthost/rules_boundaries.go +532 -0
  131. package/linthost/rules_camelcase.go +133 -0
  132. package/linthost/rules_complexity.go +94 -0
  133. package/linthost/rules_consistent_return.go +130 -0
  134. package/linthost/rules_console.go +1 -1
  135. package/linthost/rules_core_extra.go +739 -0
  136. package/linthost/rules_curly.go +70 -0
  137. package/linthost/rules_cypress.go +608 -0
  138. package/linthost/rules_debugger.go +2 -2
  139. package/linthost/rules_default_case.go +39 -0
  140. package/linthost/rules_default_case_last.go +46 -0
  141. package/linthost/rules_dupes.go +3 -3
  142. package/linthost/rules_empty.go +3 -3
  143. package/linthost/rules_escape.go +44 -24
  144. package/linthost/rules_eval.go +2 -2
  145. package/linthost/rules_finally.go +2 -2
  146. package/linthost/rules_format_jsdoc.go +1 -1
  147. package/linthost/rules_format_print_width.go +9 -9
  148. package/linthost/rules_format_quotes.go +1 -1
  149. package/linthost/rules_format_semi.go +1 -1
  150. package/linthost/rules_format_sort_imports.go +1 -1
  151. package/linthost/rules_format_trailing_comma.go +1 -1
  152. package/linthost/rules_functional.go +967 -0
  153. package/linthost/rules_gap.go +24 -45
  154. package/linthost/rules_grouped_accessor_pairs.go +87 -0
  155. package/linthost/rules_guard_for_in.go +198 -0
  156. package/linthost/rules_id_length.go +77 -0
  157. package/linthost/rules_imports.go +70 -2
  158. package/linthost/rules_init_declarations.go +53 -0
  159. package/linthost/rules_jest.go +618 -0
  160. package/linthost/rules_jsdoc.go +502 -0
  161. package/linthost/rules_jsx_a11y.go +1264 -0
  162. package/linthost/rules_logic.go +12 -12
  163. package/linthost/rules_loops.go +1 -1
  164. package/linthost/rules_max_classes_per_file.go +58 -0
  165. package/linthost/rules_max_depth.go +83 -0
  166. package/linthost/rules_max_lines.go +101 -0
  167. package/linthost/rules_max_lines_per_function.go +57 -0
  168. package/linthost/rules_max_nested_callbacks.go +61 -0
  169. package/linthost/rules_max_params.go +49 -0
  170. package/linthost/rules_max_statements.go +59 -0
  171. package/linthost/rules_misc.go +1 -1
  172. package/linthost/rules_nextjs.go +697 -0
  173. package/linthost/rules_no_else_return.go +75 -0
  174. package/linthost/rules_no_empty_named_blocks.go +91 -0
  175. package/linthost/rules_no_extend_native.go +51 -0
  176. package/linthost/rules_no_import_assign.go +140 -0
  177. package/linthost/rules_no_invalid_this.go +52 -0
  178. package/linthost/rules_no_loop_func.go +85 -0
  179. package/linthost/rules_no_magic_numbers.go +123 -0
  180. package/linthost/rules_no_mixed_operators.go +80 -0
  181. package/linthost/rules_no_param_reassign.go +98 -0
  182. package/linthost/rules_no_redeclare.go +270 -0
  183. package/linthost/rules_no_restricted_imports.go +105 -0
  184. package/linthost/rules_no_restricted_syntax.go +49 -0
  185. package/linthost/rules_no_shadow.go +147 -0
  186. package/linthost/rules_no_unreachable.go +77 -0
  187. package/linthost/rules_no_useless_assignment.go +120 -0
  188. package/linthost/rules_no_useless_constructor.go +245 -0
  189. package/linthost/rules_no_useless_return.go +51 -0
  190. package/linthost/rules_params.go +1 -1
  191. package/linthost/rules_playwright.go +788 -0
  192. package/linthost/rules_prefer_arrow_callback.go +124 -0
  193. package/linthost/rules_prefer_destructuring.go +98 -0
  194. package/linthost/rules_prefer_named_capture_group.go +129 -0
  195. package/linthost/rules_prefer_rest_params.go +94 -0
  196. package/linthost/rules_problems.go +41 -30
  197. package/linthost/rules_promise.go +1128 -73
  198. package/linthost/rules_protos.go +2 -2
  199. package/linthost/rules_react.go +773 -0
  200. package/linthost/rules_react_extras.go +227 -0
  201. package/linthost/rules_react_hooks.go +848 -0
  202. package/linthost/rules_react_perf.go +293 -0
  203. package/linthost/rules_react_refresh.go +583 -0
  204. package/linthost/rules_regexp.go +410 -0
  205. package/linthost/rules_security.go +830 -0
  206. package/linthost/rules_self.go +2 -2
  207. package/linthost/rules_solid.go +1073 -0
  208. package/linthost/rules_sort_imports.go +69 -0
  209. package/linthost/rules_sort_keys.go +134 -0
  210. package/linthost/rules_storybook.go +1176 -0
  211. package/linthost/rules_strings.go +4 -4
  212. package/linthost/rules_suggestions.go +38 -38
  213. package/linthost/rules_tanstack_query.go +844 -0
  214. package/linthost/rules_testing_library.go +1204 -0
  215. package/linthost/rules_throw.go +1 -1
  216. package/linthost/rules_ts.go +19 -19
  217. package/linthost/rules_ts_async.go +399 -0
  218. package/linthost/rules_ts_class_literal_property_style.go +115 -0
  219. package/linthost/rules_ts_consistent_type_exports.go +256 -0
  220. package/linthost/rules_ts_explicit_function_return_type.go +67 -0
  221. package/linthost/rules_ts_extra.go +155 -36
  222. package/linthost/rules_ts_no_base_to_string.go +218 -0
  223. package/linthost/rules_ts_no_confusing_void.go +72 -0
  224. package/linthost/rules_ts_no_deprecated.go +195 -0
  225. package/linthost/rules_ts_no_for_in_array.go +84 -0
  226. package/linthost/rules_ts_no_magic_numbers.go +124 -0
  227. package/linthost/rules_ts_no_meaningless_void_operator.go +84 -0
  228. package/linthost/rules_ts_no_misused_spread.go +104 -0
  229. package/linthost/rules_ts_no_redundant_type_constituents.go +113 -0
  230. package/linthost/rules_ts_no_restricted_types.go +62 -0
  231. package/linthost/rules_ts_no_unnecessary_boolean_literal_compare.go +129 -0
  232. package/linthost/rules_ts_no_unnecessary_condition.go +250 -0
  233. package/linthost/rules_ts_no_unnecessary_qualifier.go +118 -0
  234. package/linthost/rules_ts_no_unnecessary_template_expression.go +178 -0
  235. package/linthost/rules_ts_no_unnecessary_type_arguments.go +234 -0
  236. package/linthost/rules_ts_no_unnecessary_type_assertion.go +157 -0
  237. package/linthost/rules_ts_no_unsafe_argument.go +82 -0
  238. package/linthost/rules_ts_no_unsafe_assignment.go +102 -0
  239. package/linthost/rules_ts_no_unsafe_call.go +82 -0
  240. package/linthost/rules_ts_no_unsafe_enum_comparison.go +235 -0
  241. package/linthost/rules_ts_no_unsafe_member_access.go +58 -0
  242. package/linthost/rules_ts_no_unsafe_return.go +126 -0
  243. package/linthost/rules_ts_no_unsafe_unary_minus.go +81 -0
  244. package/linthost/rules_ts_no_useless_constructor.go +52 -0
  245. package/linthost/rules_ts_non_nullable_type_assertion_style.go +108 -0
  246. package/linthost/rules_ts_parameter_properties.go +38 -0
  247. package/linthost/rules_ts_prefer_find.go +178 -0
  248. package/linthost/rules_ts_prefer_includes.go +255 -0
  249. package/linthost/rules_ts_prefer_nullish_coalescing.go +104 -0
  250. package/linthost/rules_ts_prefer_optional_chain.go +228 -0
  251. package/linthost/rules_ts_prefer_promise_reject_errors.go +176 -0
  252. package/linthost/rules_ts_prefer_readonly.go +73 -0
  253. package/linthost/rules_ts_prefer_reduce_type_parameter.go +116 -0
  254. package/linthost/rules_ts_prefer_regexp_exec.go +122 -0
  255. package/linthost/rules_ts_prefer_return_this_type.go +135 -0
  256. package/linthost/rules_ts_prefer_string_starts_ends_with.go +360 -0
  257. package/linthost/rules_ts_promise_function_async.go +77 -0
  258. package/linthost/rules_ts_related_getter_setter_pairs.go +113 -0
  259. package/linthost/rules_ts_require_array_sort_compare.go +96 -0
  260. package/linthost/rules_ts_restrict_plus_operands.go +127 -0
  261. package/linthost/rules_ts_restrict_template_expressions.go +95 -0
  262. package/linthost/rules_ts_simple.go +387 -0
  263. package/linthost/rules_ts_sort_type_constituents.go +144 -0
  264. package/linthost/rules_ts_strict_boolean_expressions.go +157 -0
  265. package/linthost/rules_ts_switch_exhaustiveness_check.go +147 -0
  266. package/linthost/rules_ts_unbound_method.go +126 -0
  267. package/linthost/rules_tsdoc.go +136 -0
  268. package/linthost/rules_unicorn_better_regex.go +27 -0
  269. package/linthost/rules_unicorn_catch_error_name.go +46 -0
  270. package/linthost/rules_unicorn_consistent_assert.go +47 -0
  271. package/linthost/rules_unicorn_consistent_date_clone.go +54 -0
  272. package/linthost/rules_unicorn_consistent_destructuring.go +25 -0
  273. package/linthost/rules_unicorn_consistent_empty_array_spread.go +62 -0
  274. package/linthost/rules_unicorn_consistent_existence_index_check.go +80 -0
  275. package/linthost/rules_unicorn_consistent_function_scoping.go +25 -0
  276. package/linthost/rules_unicorn_consistent_template_literal_escape.go +25 -0
  277. package/linthost/rules_unicorn_custom_error_definition.go +138 -0
  278. package/linthost/rules_unicorn_empty_brace_spaces.go +73 -0
  279. package/linthost/rules_unicorn_error_message.go +64 -0
  280. package/linthost/rules_unicorn_escape_case.go +51 -0
  281. package/linthost/rules_unicorn_expiring_todo_comments.go +78 -0
  282. package/linthost/rules_unicorn_explicit_length_check.go +88 -0
  283. package/linthost/rules_unicorn_filename_case.go +29 -0
  284. package/linthost/rules_unicorn_import_style.go +28 -0
  285. package/linthost/rules_unicorn_isolated_functions.go +29 -0
  286. package/linthost/rules_unicorn_new_for_builtins.go +87 -0
  287. package/linthost/rules_unicorn_no_abusive_eslint_disable.go +63 -0
  288. package/linthost/rules_unicorn_no_accessor_recursion.go +115 -0
  289. package/linthost/rules_unicorn_no_anonymous_default_export.go +74 -0
  290. package/linthost/rules_unicorn_no_array_callback_reference.go +65 -0
  291. package/linthost/rules_unicorn_no_array_for_each.go +40 -0
  292. package/linthost/rules_unicorn_no_array_method_this_argument.go +61 -0
  293. package/linthost/rules_unicorn_no_array_reduce.go +41 -0
  294. package/linthost/rules_unicorn_no_array_reverse.go +44 -0
  295. package/linthost/rules_unicorn_no_array_sort.go +43 -0
  296. package/linthost/rules_unicorn_no_await_expression_member.go +40 -0
  297. package/linthost/rules_unicorn_no_await_in_promise_methods.go +69 -0
  298. package/linthost/rules_unicorn_no_console_spaces.go +61 -0
  299. package/linthost/rules_unicorn_no_document_cookie.go +41 -0
  300. package/linthost/rules_unicorn_no_empty_file.go +44 -0
  301. package/linthost/rules_unicorn_no_for_loop.go +102 -0
  302. package/linthost/rules_unicorn_no_hex_escape.go +42 -0
  303. package/linthost/rules_unicorn_no_immediate_mutation.go +90 -0
  304. package/linthost/rules_unicorn_no_instanceof_builtins.go +66 -0
  305. package/linthost/rules_unicorn_no_invalid_fetch_options.go +79 -0
  306. package/linthost/rules_unicorn_no_invalid_remove_event_listener.go +51 -0
  307. package/linthost/rules_unicorn_no_keyword_prefix.go +101 -0
  308. package/linthost/rules_unicorn_no_lonely_if.go +44 -0
  309. package/linthost/rules_unicorn_no_magic_array_flat_depth.go +51 -0
  310. package/linthost/rules_unicorn_no_named_default.go +54 -0
  311. package/linthost/rules_unicorn_no_negated_condition.go +85 -0
  312. package/linthost/rules_unicorn_no_negation_in_equality_check.go +50 -0
  313. package/linthost/rules_unicorn_no_nested_ternary.go +43 -0
  314. package/linthost/rules_unicorn_no_new_array.go +30 -0
  315. package/linthost/rules_unicorn_no_new_buffer.go +31 -0
  316. package/linthost/rules_unicorn_no_null.go +24 -0
  317. package/linthost/rules_unicorn_no_object_as_default_parameter.go +48 -0
  318. package/linthost/rules_unicorn_no_process_exit.go +43 -0
  319. package/linthost/rules_unicorn_no_single_promise_in_promise_methods.go +59 -0
  320. package/linthost/rules_unicorn_no_static_only_class.go +56 -0
  321. package/linthost/rules_unicorn_no_thenable.go +74 -0
  322. package/linthost/rules_unicorn_no_this_assignment.go +35 -0
  323. package/linthost/rules_unicorn_no_typeof_undefined.go +58 -0
  324. package/linthost/rules_unicorn_no_unnecessary_array_flat_depth.go +49 -0
  325. package/linthost/rules_unicorn_no_unnecessary_array_splice_count.go +70 -0
  326. package/linthost/rules_unicorn_no_unnecessary_await.go +51 -0
  327. package/linthost/rules_unicorn_no_unnecessary_polyfills.go +32 -0
  328. package/linthost/rules_unicorn_no_unnecessary_slice_end.go +48 -0
  329. package/linthost/rules_unicorn_no_unreadable_array_destructuring.go +115 -0
  330. package/linthost/rules_unicorn_no_unreadable_iife.go +50 -0
  331. package/linthost/rules_unicorn_no_unused_properties.go +30 -0
  332. package/linthost/rules_unicorn_no_useless_collection_argument.go +63 -0
  333. package/linthost/rules_unicorn_no_useless_error_capture_stack_trace.go +46 -0
  334. package/linthost/rules_unicorn_no_useless_fallback_in_spread.go +81 -0
  335. package/linthost/rules_unicorn_no_useless_iterator_to_array.go +72 -0
  336. package/linthost/rules_unicorn_no_useless_length_check.go +144 -0
  337. package/linthost/rules_unicorn_no_useless_promise_resolve_reject.go +81 -0
  338. package/linthost/rules_unicorn_no_useless_spread.go +67 -0
  339. package/linthost/rules_unicorn_no_useless_switch_case.go +55 -0
  340. package/linthost/rules_unicorn_no_useless_undefined.go +55 -0
  341. package/linthost/rules_unicorn_no_zero_fractions.go +52 -0
  342. package/linthost/rules_unicorn_number_literal_case.go +69 -0
  343. package/linthost/rules_unicorn_numeric_separators_style.go +75 -0
  344. package/linthost/rules_unicorn_prefer_add_event_listener.go +66 -0
  345. package/linthost/rules_unicorn_prefer_array_find.go +51 -0
  346. package/linthost/rules_unicorn_prefer_array_flat.go +52 -0
  347. package/linthost/rules_unicorn_prefer_array_flat_map.go +63 -0
  348. package/linthost/rules_unicorn_prefer_array_index_of.go +147 -0
  349. package/linthost/rules_unicorn_prefer_array_some.go +67 -0
  350. package/linthost/rules_unicorn_prefer_at.go +77 -0
  351. package/linthost/rules_unicorn_prefer_bigint_literals.go +77 -0
  352. package/linthost/rules_unicorn_prefer_blob_reading_methods.go +44 -0
  353. package/linthost/rules_unicorn_prefer_class_fields.go +93 -0
  354. package/linthost/rules_unicorn_prefer_classlist_toggle.go +120 -0
  355. package/linthost/rules_unicorn_prefer_code_point.go +49 -0
  356. package/linthost/rules_unicorn_prefer_date_now.go +78 -0
  357. package/linthost/rules_unicorn_prefer_default_parameters.go +144 -0
  358. package/linthost/rules_unicorn_prefer_dom_node_append.go +40 -0
  359. package/linthost/rules_unicorn_prefer_dom_node_dataset.go +55 -0
  360. package/linthost/rules_unicorn_prefer_dom_node_remove.go +41 -0
  361. package/linthost/rules_unicorn_prefer_dom_node_text_content.go +37 -0
  362. package/linthost/rules_unicorn_prefer_event_target.go +36 -0
  363. package/linthost/rules_unicorn_prefer_export_from.go +129 -0
  364. package/linthost/rules_unicorn_prefer_global_this.go +127 -0
  365. package/linthost/rules_unicorn_prefer_import_meta_properties.go +66 -0
  366. package/linthost/rules_unicorn_prefer_includes.go +149 -0
  367. package/linthost/rules_unicorn_prefer_json_parse_buffer.go +65 -0
  368. package/linthost/rules_unicorn_prefer_keyboard_event_key.go +38 -0
  369. package/linthost/rules_unicorn_prefer_logical_operator_over_ternary.go +48 -0
  370. package/linthost/rules_unicorn_prefer_math_min_max.go +60 -0
  371. package/linthost/rules_unicorn_prefer_math_trunc.go +62 -0
  372. package/linthost/rules_unicorn_prefer_modern_dom_apis.go +47 -0
  373. package/linthost/rules_unicorn_prefer_modern_math_apis.go +70 -0
  374. package/linthost/rules_unicorn_prefer_module.go +50 -0
  375. package/linthost/rules_unicorn_prefer_native_coercion_functions.go +114 -0
  376. package/linthost/rules_unicorn_prefer_negative_index.go +74 -0
  377. package/linthost/rules_unicorn_prefer_node_protocol.go +122 -0
  378. package/linthost/rules_unicorn_prefer_number_properties.go +88 -0
  379. package/linthost/rules_unicorn_prefer_object_from_entries.go +55 -0
  380. package/linthost/rules_unicorn_prefer_optional_catch_binding.go +62 -0
  381. package/linthost/rules_unicorn_prefer_prototype_methods.go +54 -0
  382. package/linthost/rules_unicorn_prefer_query_selector.go +56 -0
  383. package/linthost/rules_unicorn_prefer_reflect_apply.go +36 -0
  384. package/linthost/rules_unicorn_prefer_regexp_test_rule.go +80 -0
  385. package/linthost/rules_unicorn_prefer_response_static_json.go +53 -0
  386. package/linthost/rules_unicorn_prefer_set_has.go +42 -0
  387. package/linthost/rules_unicorn_prefer_set_size.go +48 -0
  388. package/linthost/rules_unicorn_prefer_simple_condition_first.go +34 -0
  389. package/linthost/rules_unicorn_prefer_single_call.go +96 -0
  390. package/linthost/rules_unicorn_prefer_spread.go +51 -0
  391. package/linthost/rules_unicorn_prefer_string_raw.go +43 -0
  392. package/linthost/rules_unicorn_prefer_string_replace_all.go +95 -0
  393. package/linthost/rules_unicorn_prefer_string_slice.go +43 -0
  394. package/linthost/rules_unicorn_prefer_string_starts_ends_with.go +148 -0
  395. package/linthost/rules_unicorn_prefer_string_trim_start_end.go +48 -0
  396. package/linthost/rules_unicorn_prefer_structured_clone.go +71 -0
  397. package/linthost/rules_unicorn_prefer_switch.go +103 -0
  398. package/linthost/rules_unicorn_prefer_ternary.go +71 -0
  399. package/linthost/rules_unicorn_prefer_top_level_await.go +72 -0
  400. package/linthost/rules_unicorn_prefer_type_error.go +127 -0
  401. package/linthost/rules_unicorn_prevent_abbreviations.go +127 -0
  402. package/linthost/rules_unicorn_relative_url_style.go +49 -0
  403. package/linthost/rules_unicorn_require_array_join_separator.go +45 -0
  404. package/linthost/rules_unicorn_require_module_attributes.go +54 -0
  405. package/linthost/rules_unicorn_require_module_specifiers.go +55 -0
  406. package/linthost/rules_unicorn_require_number_to_fixed_digits_argument.go +43 -0
  407. package/linthost/rules_unicorn_require_post_message_target_origin.go +44 -0
  408. package/linthost/rules_unicorn_string_content.go +27 -0
  409. package/linthost/rules_unicorn_switch_case_braces.go +42 -0
  410. package/linthost/rules_unicorn_switch_case_break_position.go +31 -0
  411. package/linthost/rules_unicorn_template_indent.go +28 -0
  412. package/linthost/rules_unicorn_text_encoding_identifier_case.go +72 -0
  413. package/linthost/rules_unicorn_throw_new_error.go +61 -0
  414. package/linthost/rules_var.go +10 -4
  415. package/linthost/rules_vitest.go +568 -0
  416. package/package.json +3 -3
  417. package/plugin/main.go +49 -3
  418. package/rule/astutil/astutil.go +10 -3
  419. package/rule/rule.go +12 -10
  420. package/src/defaultFormat.ts +11 -8
  421. package/src/index.ts +65 -43
  422. package/src/structures/ITtscLintConfig.ts +12 -6
  423. package/src/structures/{ITtscLintFormatConfig.ts → ITtscLintFormat.ts} +2 -2
  424. package/src/structures/TtscLintRuleSetting.ts +55 -0
  425. package/src/structures/index.ts +3 -4
  426. package/src/structures/rules/ITtscLintBoundariesRuleOptions.ts +128 -0
  427. package/src/structures/rules/ITtscLintBoundariesRules.ts +95 -0
  428. package/src/structures/rules/ITtscLintContributorRules.ts +21 -0
  429. package/src/structures/rules/ITtscLintCoreRules.ts +1406 -0
  430. package/src/structures/rules/ITtscLintCypressRuleOptions.ts +18 -0
  431. package/src/structures/rules/ITtscLintCypressRules.ts +145 -0
  432. package/src/structures/rules/ITtscLintFunctionalRuleOptions.ts +213 -0
  433. package/src/structures/rules/ITtscLintFunctionalRules.ts +202 -0
  434. package/src/structures/rules/ITtscLintJestRules.ts +220 -0
  435. package/src/structures/rules/ITtscLintJsdocRules.ts +153 -0
  436. package/src/structures/rules/ITtscLintJsxA11yRules.ts +339 -0
  437. package/src/structures/rules/ITtscLintNextjsRules.ts +206 -0
  438. package/src/structures/rules/ITtscLintPlaywrightRules.ts +361 -0
  439. package/src/structures/rules/ITtscLintPromiseRules.ts +205 -0
  440. package/src/structures/rules/ITtscLintReactPerfRuleOptions.ts +19 -0
  441. package/src/structures/rules/ITtscLintReactPerfRules.ts +55 -0
  442. package/src/structures/rules/ITtscLintReactRuleOptions.ts +43 -0
  443. package/src/structures/rules/ITtscLintReactRules.ts +321 -0
  444. package/src/structures/rules/ITtscLintRegexpRules.ts +212 -0
  445. package/src/structures/rules/ITtscLintRuleOptionsMap.ts +89 -0
  446. package/src/structures/rules/ITtscLintRules.ts +77 -0
  447. package/src/structures/rules/ITtscLintSecurityRules.ts +162 -0
  448. package/src/structures/rules/ITtscLintSolidRules.ts +202 -0
  449. package/src/structures/rules/ITtscLintStorybookRuleOptions.ts +23 -0
  450. package/src/structures/rules/ITtscLintStorybookRules.ts +193 -0
  451. package/src/structures/rules/ITtscLintTanstackQueryRules.ts +102 -0
  452. package/src/structures/rules/ITtscLintTestingLibraryRuleOptions.ts +24 -0
  453. package/src/structures/rules/ITtscLintTestingLibraryRules.ts +342 -0
  454. package/src/structures/rules/ITtscLintTypeScriptRules.ts +1230 -0
  455. package/src/structures/rules/ITtscLintUnicornRules.ts +1143 -0
  456. package/src/structures/rules/ITtscLintVitestRules.ts +154 -0
  457. package/src/structures/rules/index.ts +31 -0
  458. package/lib/structures/ITtscLintFormatConfig.js.map +0 -1
  459. package/lib/structures/TtscLintRule.d.ts +0 -17
  460. package/lib/structures/TtscLintRule.js.map +0 -1
  461. package/lib/structures/TtscLintRuleMap.d.ts +0 -46
  462. package/lib/structures/TtscLintRuleMap.js.map +0 -1
  463. package/lib/structures/TtscLintRuleOptions.d.ts +0 -137
  464. package/lib/structures/TtscLintRuleOptions.js.map +0 -1
  465. package/src/structures/TtscLintRule.ts +0 -160
  466. package/src/structures/TtscLintRuleMap.ts +0 -55
  467. package/src/structures/TtscLintRuleOptions.ts +0 -151
@@ -0,0 +1,848 @@
1
+ package linthost
2
+
3
+ import (
4
+ "fmt"
5
+ "sort"
6
+ "strings"
7
+ "unicode"
8
+
9
+ shimast "github.com/microsoft/typescript-go/shim/ast"
10
+ )
11
+
12
+ type reactHooksRule struct {
13
+ name string
14
+ }
15
+
16
+ func (r reactHooksRule) Name() string { return r.name }
17
+ func (r reactHooksRule) Visits() []shimast.Kind {
18
+ switch r.name {
19
+ case "react/rules-of-hooks", "react/exhaustive-deps", "react/use-memo":
20
+ return []shimast.Kind{shimast.KindCallExpression}
21
+ }
22
+ return []shimast.Kind{shimast.KindSourceFile}
23
+ }
24
+ func (r reactHooksRule) Check(ctx *Context, node *shimast.Node) {
25
+ switch r.name {
26
+ case "react/rules-of-hooks":
27
+ checkReactRulesOfHooksCall(ctx, node)
28
+ return
29
+ case "react/exhaustive-deps":
30
+ checkReactExhaustiveDepsCall(ctx, node)
31
+ return
32
+ case "react/use-memo":
33
+ checkReactUseMemoCall(ctx, node)
34
+ return
35
+ }
36
+ analyzer := newReactHooksAnalyzer(ctx, node)
37
+ switch r.name {
38
+ case "react/component-hook-factories":
39
+ analyzer.checkComponentHookFactories(node)
40
+ case "react/set-state-in-render":
41
+ analyzer.checkSetStateInRender(node)
42
+ case "react/set-state-in-effect":
43
+ analyzer.checkSetStateInEffect(node)
44
+ case "react/immutability":
45
+ analyzer.checkImmutability(node)
46
+ case "react/refs":
47
+ analyzer.checkRefs(node)
48
+ }
49
+ }
50
+
51
+ func checkReactRulesOfHooksCall(ctx *Context, node *shimast.Node) {
52
+ call := node.AsCallExpression()
53
+ if call == nil || !isReactHookCallee(call.Expression) {
54
+ return
55
+ }
56
+ hookName := reactCalleeName(call.Expression)
57
+ fnNode := nearestFunctionNode(node.Parent)
58
+ if fnNode == nil {
59
+ ctx.Report(node, fmt.Sprintf("React Hook %q cannot be called at the top level.", hookName))
60
+ return
61
+ }
62
+ fnName := reactFunctionName(fnNode)
63
+ component := isReactComponentName(fnName) || reactFunctionBodyContainsJSX(reactFunctionBody(fnNode))
64
+ hook := isReactHookName(fnName)
65
+ if !component && !hook {
66
+ ctx.Report(node, fmt.Sprintf("React Hook %q is called in a function that is neither a React component nor a custom Hook.", hookName))
67
+ return
68
+ }
69
+ if conditional := firstConditionalHookAncestor(node, fnNode); conditional != nil {
70
+ ctx.Report(node, fmt.Sprintf("React Hook %q is called conditionally.", hookName))
71
+ return
72
+ }
73
+ if conditional := firstConditionalReturnBeforeHook(node, fnNode); conditional != nil {
74
+ ctx.Report(node, fmt.Sprintf("React Hook %q is called conditionally.", hookName))
75
+ }
76
+ }
77
+
78
+ func checkReactExhaustiveDepsCall(ctx *Context, node *shimast.Node) {
79
+ call := node.AsCallExpression()
80
+ if call == nil || !isReactDependencyHook(call.Expression) || call.Arguments == nil || len(call.Arguments.Nodes) < 2 {
81
+ return
82
+ }
83
+ callback := stripParens(call.Arguments.Nodes[0])
84
+ depsNode := stripParens(call.Arguments.Nodes[1])
85
+ if !isReactFunctionLike(callback) || depsNode == nil || depsNode.Kind != shimast.KindArrayLiteralExpression {
86
+ return
87
+ }
88
+ used := collectReactHookCallbackReads(callback)
89
+ deps := collectReactHookDependencyNames(depsNode)
90
+ missing := missingReactHookDependencies(used, deps)
91
+ if len(missing) == 0 {
92
+ return
93
+ }
94
+ ctx.Report(depsNode, "React Hook has missing dependencies: "+formatQuotedList(missing)+".")
95
+ }
96
+
97
+ func checkReactUseMemoCall(ctx *Context, node *shimast.Node) {
98
+ call := node.AsCallExpression()
99
+ if call == nil || !isNamedReactCallee(call.Expression, "useMemo") || call.Arguments == nil || len(call.Arguments.Nodes) == 0 {
100
+ return
101
+ }
102
+ callback := stripParens(call.Arguments.Nodes[0])
103
+ if !isReactFunctionLike(callback) {
104
+ return
105
+ }
106
+ body := reactFunctionBody(callback)
107
+ if body == nil || body.Kind != shimast.KindBlock || functionBlockHasOwnReturn(callback, body) {
108
+ return
109
+ }
110
+ ctx.Report(callback, "useMemo callback must return a value.")
111
+ }
112
+
113
+ type reactHooksAnalyzer struct {
114
+ ctx *Context
115
+ functions map[*shimast.Node]*reactHooksFunction
116
+ }
117
+
118
+ type reactHooksFunction struct {
119
+ node *shimast.Node
120
+ body *shimast.Node
121
+ name string
122
+ component bool
123
+ hook bool
124
+ props map[string]bool
125
+ stateValues map[string]bool
126
+ setters map[string]bool
127
+ refs map[string]bool
128
+ }
129
+
130
+ func newReactHooksAnalyzer(ctx *Context, root *shimast.Node) *reactHooksAnalyzer {
131
+ a := &reactHooksAnalyzer{
132
+ ctx: ctx,
133
+ functions: map[*shimast.Node]*reactHooksFunction{},
134
+ }
135
+ walkDescendants(root, func(node *shimast.Node) {
136
+ if !isReactFunctionLike(node) {
137
+ return
138
+ }
139
+ fn := &reactHooksFunction{
140
+ node: node,
141
+ body: reactFunctionBody(node),
142
+ name: reactFunctionName(node),
143
+ props: map[string]bool{},
144
+ stateValues: map[string]bool{},
145
+ setters: map[string]bool{},
146
+ refs: map[string]bool{},
147
+ }
148
+ fn.component = isReactComponentName(fn.name) || reactFunctionBodyContainsJSX(fn.body)
149
+ fn.hook = isReactHookName(fn.name)
150
+ a.functions[node] = fn
151
+ })
152
+ for _, fn := range a.functions {
153
+ a.collectFunctionBindings(fn)
154
+ }
155
+ return a
156
+ }
157
+
158
+ func (a *reactHooksAnalyzer) checkComponentHookFactories(root *shimast.Node) {
159
+ walkDescendants(root, func(node *shimast.Node) {
160
+ if !isReactFunctionLike(node) {
161
+ return
162
+ }
163
+ outer := a.nearestFunction(node.Parent)
164
+ nested := a.functions[node]
165
+ if outer == nil || nested == nil || (!outer.component && !outer.hook) || (!nested.component && !nested.hook) {
166
+ return
167
+ }
168
+ if !functionContainsReactHookCall(node) {
169
+ return
170
+ }
171
+ a.ctx.Report(node, "Do not define a component or Hook factory inside another component or Hook.")
172
+ })
173
+ }
174
+
175
+ func (a *reactHooksAnalyzer) checkSetStateInRender(root *shimast.Node) {
176
+ walkDescendants(root, func(node *shimast.Node) {
177
+ call := node.AsCallExpression()
178
+ if call == nil {
179
+ return
180
+ }
181
+ name := callCalleeName(call)
182
+ if name == "" {
183
+ return
184
+ }
185
+ fn := a.nearestFunction(node)
186
+ if fn == nil || (!fn.component && !fn.hook) || !fn.setters[name] {
187
+ return
188
+ }
189
+ a.ctx.Report(node, "Do not call a state setter during render.")
190
+ })
191
+ }
192
+
193
+ func (a *reactHooksAnalyzer) checkSetStateInEffect(root *shimast.Node) {
194
+ walkDescendants(root, func(node *shimast.Node) {
195
+ call := node.AsCallExpression()
196
+ if call == nil {
197
+ return
198
+ }
199
+ name := callCalleeName(call)
200
+ if name == "" {
201
+ return
202
+ }
203
+ callback := a.nearestFunction(node)
204
+ if callback == nil || !isEffectCallback(callback.node) {
205
+ return
206
+ }
207
+ owner := a.nearestReactFunction(callback.node.Parent)
208
+ if owner == nil || !owner.setters[name] {
209
+ return
210
+ }
211
+ a.ctx.Report(node, "Do not synchronously call a state setter inside an effect.")
212
+ })
213
+ }
214
+
215
+ func (a *reactHooksAnalyzer) checkImmutability(root *shimast.Node) {
216
+ walkDescendants(root, func(node *shimast.Node) {
217
+ switch node.Kind {
218
+ case shimast.KindBinaryExpression:
219
+ expr := node.AsBinaryExpression()
220
+ if expr == nil || expr.OperatorToken == nil || !isAssignmentOperator(expr.OperatorToken.Kind) {
221
+ return
222
+ }
223
+ a.reportImmutableWrite(expr.Left, node)
224
+ case shimast.KindPrefixUnaryExpression:
225
+ expr := node.AsPrefixUnaryExpression()
226
+ if expr == nil || (expr.Operator != shimast.KindPlusPlusToken && expr.Operator != shimast.KindMinusMinusToken) {
227
+ return
228
+ }
229
+ a.reportImmutableWrite(expr.Operand, node)
230
+ case shimast.KindPostfixUnaryExpression:
231
+ expr := node.AsPostfixUnaryExpression()
232
+ if expr == nil || (expr.Operator != shimast.KindPlusPlusToken && expr.Operator != shimast.KindMinusMinusToken) {
233
+ return
234
+ }
235
+ a.reportImmutableWrite(expr.Operand, node)
236
+ }
237
+ })
238
+ }
239
+
240
+ func (a *reactHooksAnalyzer) reportImmutableWrite(target *shimast.Node, reportNode *shimast.Node) {
241
+ target = stripParens(target)
242
+ if target == nil || (target.Kind != shimast.KindPropertyAccessExpression && target.Kind != shimast.KindElementAccessExpression) {
243
+ return
244
+ }
245
+ fn := a.nearestReactFunction(reportNode)
246
+ if fn == nil {
247
+ return
248
+ }
249
+ root := memberAccessRootName(target)
250
+ if root == "" || (!fn.props[root] && !fn.stateValues[root]) {
251
+ return
252
+ }
253
+ a.ctx.Report(reportNode, "Do not mutate props or state directly.")
254
+ }
255
+
256
+ func (a *reactHooksAnalyzer) checkRefs(root *shimast.Node) {
257
+ walkDescendants(root, func(node *shimast.Node) {
258
+ access := node.AsPropertyAccessExpression()
259
+ if access == nil || identifierText(access.Name()) != "current" {
260
+ return
261
+ }
262
+ fn := a.nearestFunction(node)
263
+ if fn == nil || (!fn.component && !fn.hook) || !fn.refs[memberAccessRootName(node)] {
264
+ return
265
+ }
266
+ a.ctx.Report(node, "Do not read or write ref.current during render.")
267
+ })
268
+ }
269
+
270
+ func (a *reactHooksAnalyzer) collectFunctionBindings(fn *reactHooksFunction) {
271
+ if fn == nil || fn.body == nil {
272
+ return
273
+ }
274
+ if params := reactFunctionParameters(fn.node); params != nil && len(params.Nodes) > 0 {
275
+ name := parameterName(params.Nodes[0])
276
+ if name != "" {
277
+ fn.props[name] = true
278
+ }
279
+ }
280
+ walkDescendants(fn.body, func(node *shimast.Node) {
281
+ if a.nearestFunction(node) != fn {
282
+ return
283
+ }
284
+ decl := node.AsVariableDeclaration()
285
+ if decl == nil || decl.Initializer == nil {
286
+ return
287
+ }
288
+ if isNamedReactCalleeFromCall(decl.Initializer, "useState") {
289
+ names := bindingPatternNames(decl.Name())
290
+ if len(names) == 0 {
291
+ names = parseSimpleArrayBindingNames(nodeText(a.ctx.File, decl.Name()))
292
+ }
293
+ if len(names) > 0 {
294
+ fn.stateValues[names[0]] = true
295
+ }
296
+ if len(names) > 1 {
297
+ fn.setters[names[1]] = true
298
+ }
299
+ return
300
+ }
301
+ if isNamedReactCalleeFromCall(decl.Initializer, "useReducer") {
302
+ names := bindingPatternNames(decl.Name())
303
+ if len(names) == 0 {
304
+ names = parseSimpleArrayBindingNames(nodeText(a.ctx.File, decl.Name()))
305
+ }
306
+ if len(names) > 0 {
307
+ fn.stateValues[names[0]] = true
308
+ }
309
+ if len(names) > 1 {
310
+ fn.setters[names[1]] = true
311
+ }
312
+ return
313
+ }
314
+ if isNamedReactCalleeFromCall(decl.Initializer, "useRef") {
315
+ if name := identifierText(decl.Name()); name != "" {
316
+ fn.refs[name] = true
317
+ }
318
+ }
319
+ })
320
+ }
321
+
322
+ func (a *reactHooksAnalyzer) nearestFunction(node *shimast.Node) *reactHooksFunction {
323
+ for cur := node; cur != nil; cur = cur.Parent {
324
+ if fn, ok := a.functions[cur]; ok {
325
+ return fn
326
+ }
327
+ }
328
+ return nil
329
+ }
330
+
331
+ func (a *reactHooksAnalyzer) nearestReactFunction(node *shimast.Node) *reactHooksFunction {
332
+ for cur := node; cur != nil; cur = cur.Parent {
333
+ if fn, ok := a.functions[cur]; ok && (fn.component || fn.hook) {
334
+ return fn
335
+ }
336
+ }
337
+ return nil
338
+ }
339
+
340
+ func isReactFunctionLike(node *shimast.Node) bool {
341
+ if node == nil {
342
+ return false
343
+ }
344
+ switch node.Kind {
345
+ case shimast.KindFunctionDeclaration, shimast.KindFunctionExpression, shimast.KindArrowFunction:
346
+ return true
347
+ }
348
+ return false
349
+ }
350
+
351
+ func reactFunctionBody(node *shimast.Node) *shimast.Node {
352
+ if node == nil {
353
+ return nil
354
+ }
355
+ switch node.Kind {
356
+ case shimast.KindFunctionDeclaration:
357
+ if fn := node.AsFunctionDeclaration(); fn != nil {
358
+ return fn.Body
359
+ }
360
+ case shimast.KindFunctionExpression:
361
+ if fn := node.AsFunctionExpression(); fn != nil {
362
+ return fn.Body
363
+ }
364
+ case shimast.KindArrowFunction:
365
+ if fn := node.AsArrowFunction(); fn != nil {
366
+ return fn.Body
367
+ }
368
+ }
369
+ return nil
370
+ }
371
+
372
+ func reactFunctionParameters(node *shimast.Node) *shimast.NodeList {
373
+ if node == nil {
374
+ return nil
375
+ }
376
+ switch node.Kind {
377
+ case shimast.KindFunctionDeclaration:
378
+ if fn := node.AsFunctionDeclaration(); fn != nil {
379
+ return fn.Parameters
380
+ }
381
+ case shimast.KindFunctionExpression:
382
+ if fn := node.AsFunctionExpression(); fn != nil {
383
+ return fn.Parameters
384
+ }
385
+ case shimast.KindArrowFunction:
386
+ if fn := node.AsArrowFunction(); fn != nil {
387
+ return fn.Parameters
388
+ }
389
+ }
390
+ return nil
391
+ }
392
+
393
+ func reactFunctionName(node *shimast.Node) string {
394
+ if node == nil {
395
+ return ""
396
+ }
397
+ switch node.Kind {
398
+ case shimast.KindFunctionDeclaration:
399
+ if fn := node.AsFunctionDeclaration(); fn != nil {
400
+ if name := identifierText(fn.Name()); name != "" {
401
+ return name
402
+ }
403
+ }
404
+ case shimast.KindFunctionExpression:
405
+ if fn := node.AsFunctionExpression(); fn != nil {
406
+ if name := identifierText(fn.Name()); name != "" {
407
+ return name
408
+ }
409
+ }
410
+ }
411
+ if parent := node.Parent; parent != nil {
412
+ switch parent.Kind {
413
+ case shimast.KindVariableDeclaration:
414
+ if decl := parent.AsVariableDeclaration(); decl != nil && decl.Initializer == node {
415
+ return identifierText(decl.Name())
416
+ }
417
+ case shimast.KindBinaryExpression:
418
+ if expr := parent.AsBinaryExpression(); expr != nil && expr.Right == node {
419
+ return identifierText(expr.Left)
420
+ }
421
+ case shimast.KindPropertyAssignment:
422
+ if prop := parent.AsPropertyAssignment(); prop != nil && prop.Initializer == node {
423
+ return identifierText(prop.Name())
424
+ }
425
+ }
426
+ }
427
+ return ""
428
+ }
429
+
430
+ func isReactComponentName(name string) bool {
431
+ if name == "" {
432
+ return false
433
+ }
434
+ first, _ := reactHooksFirstRune(name)
435
+ return unicode.IsUpper(first)
436
+ }
437
+
438
+ func isReactHookName(name string) bool {
439
+ if len(name) < 4 || !strings.HasPrefix(name, "use") {
440
+ return false
441
+ }
442
+ r, _ := reactHooksFirstRune(name[3:])
443
+ return unicode.IsUpper(r) || unicode.IsDigit(r)
444
+ }
445
+
446
+ func reactHooksFirstRune(text string) (rune, int) {
447
+ for i, r := range text {
448
+ return r, i
449
+ }
450
+ return 0, 0
451
+ }
452
+
453
+ func reactFunctionBodyContainsJSX(body *shimast.Node) bool {
454
+ found := false
455
+ walkDescendants(body, func(node *shimast.Node) {
456
+ if found || node == nil {
457
+ return
458
+ }
459
+ switch node.Kind {
460
+ case shimast.KindJsxElement, shimast.KindJsxSelfClosingElement, shimast.KindJsxFragment:
461
+ found = true
462
+ }
463
+ })
464
+ return found
465
+ }
466
+
467
+ func reactCalleeName(node *shimast.Node) string {
468
+ node = stripParens(node)
469
+ if name := identifierText(node); name != "" {
470
+ return name
471
+ }
472
+ if node != nil && node.Kind == shimast.KindPropertyAccessExpression {
473
+ access := node.AsPropertyAccessExpression()
474
+ if access != nil {
475
+ return identifierText(access.Name())
476
+ }
477
+ }
478
+ return ""
479
+ }
480
+
481
+ func isReactHookCallee(node *shimast.Node) bool {
482
+ return isReactHookName(reactCalleeName(node))
483
+ }
484
+
485
+ func isNamedReactCallee(node *shimast.Node, name string) bool {
486
+ return reactCalleeName(node) == name
487
+ }
488
+
489
+ func isNamedReactCalleeFromCall(node *shimast.Node, name string) bool {
490
+ call := stripParens(node).AsCallExpression()
491
+ return call != nil && isNamedReactCallee(call.Expression, name)
492
+ }
493
+
494
+ func isReactDependencyHook(node *shimast.Node) bool {
495
+ switch reactCalleeName(node) {
496
+ case "useEffect", "useLayoutEffect", "useInsertionEffect", "useMemo", "useCallback":
497
+ return true
498
+ }
499
+ return false
500
+ }
501
+
502
+ func firstConditionalHookAncestor(node *shimast.Node, stop *shimast.Node) *shimast.Node {
503
+ for cur := node.Parent; cur != nil && cur != stop; cur = cur.Parent {
504
+ switch cur.Kind {
505
+ case shimast.KindIfStatement,
506
+ shimast.KindConditionalExpression,
507
+ shimast.KindSwitchStatement,
508
+ shimast.KindCaseClause,
509
+ shimast.KindDefaultClause,
510
+ shimast.KindForStatement,
511
+ shimast.KindForInStatement,
512
+ shimast.KindForOfStatement,
513
+ shimast.KindWhileStatement,
514
+ shimast.KindDoStatement,
515
+ shimast.KindCatchClause:
516
+ return cur
517
+ }
518
+ }
519
+ return nil
520
+ }
521
+
522
+ func firstConditionalReturnBeforeHook(node *shimast.Node, fnNode *shimast.Node) *shimast.Node {
523
+ for cur := node; cur != nil && cur != fnNode; cur = cur.Parent {
524
+ parent := cur.Parent
525
+ if parent == nil || parent.Kind != shimast.KindBlock {
526
+ continue
527
+ }
528
+ for _, stmt := range parentStatements(parent) {
529
+ if stmt == cur {
530
+ break
531
+ }
532
+ if statementIsConditionalReturn(stmt, fnNode) {
533
+ return stmt
534
+ }
535
+ }
536
+ }
537
+ return nil
538
+ }
539
+
540
+ func statementIsConditionalReturn(stmt *shimast.Node, fnNode *shimast.Node) bool {
541
+ if stmt == nil || stmt.Kind != shimast.KindIfStatement {
542
+ return false
543
+ }
544
+ ifs := stmt.AsIfStatement()
545
+ if ifs == nil {
546
+ return false
547
+ }
548
+ return statementContainsOwnReturn(ifs.ThenStatement, fnNode) ||
549
+ statementContainsOwnReturn(ifs.ElseStatement, fnNode)
550
+ }
551
+
552
+ func statementContainsOwnReturn(stmt *shimast.Node, fnNode *shimast.Node) bool {
553
+ found := false
554
+ walkDescendants(stmt, func(node *shimast.Node) {
555
+ if found || node == nil || node.Kind != shimast.KindReturnStatement {
556
+ return
557
+ }
558
+ found = nearestFunctionNode(node) == fnNode
559
+ })
560
+ return found
561
+ }
562
+
563
+ func functionContainsReactHookCall(node *shimast.Node) bool {
564
+ found := false
565
+ walkDescendants(reactFunctionBody(node), func(child *shimast.Node) {
566
+ if found || child == node {
567
+ return
568
+ }
569
+ call := child.AsCallExpression()
570
+ if call != nil && isReactHookCallee(call.Expression) {
571
+ found = true
572
+ }
573
+ })
574
+ return found
575
+ }
576
+
577
+ func isEffectCallback(fnNode *shimast.Node) bool {
578
+ if fnNode == nil || fnNode.Parent == nil || fnNode.Parent.Kind != shimast.KindCallExpression {
579
+ return false
580
+ }
581
+ call := fnNode.Parent.AsCallExpression()
582
+ return call != nil && call.Arguments != nil && len(call.Arguments.Nodes) > 0 &&
583
+ stripParens(call.Arguments.Nodes[0]) == fnNode &&
584
+ isNamedReactCallee(call.Expression, "useEffect")
585
+ }
586
+
587
+ func functionBlockHasOwnReturn(fnNode *shimast.Node, body *shimast.Node) bool {
588
+ found := false
589
+ walkDescendants(body, func(node *shimast.Node) {
590
+ if found {
591
+ return
592
+ }
593
+ if node != body && isReactFunctionLike(node) {
594
+ return
595
+ }
596
+ if node.Kind == shimast.KindReturnStatement && nearestFunctionNode(node) == fnNode {
597
+ found = true
598
+ }
599
+ })
600
+ return found
601
+ }
602
+
603
+ func nearestFunctionNode(node *shimast.Node) *shimast.Node {
604
+ for cur := node; cur != nil; cur = cur.Parent {
605
+ if isReactFunctionLike(cur) {
606
+ return cur
607
+ }
608
+ }
609
+ return nil
610
+ }
611
+
612
+ func collectReactHookCallbackReads(callback *shimast.Node) map[string]bool {
613
+ declared := map[string]bool{}
614
+ used := map[string]bool{}
615
+ if params := reactFunctionParameters(callback); params != nil {
616
+ for _, param := range params.Nodes {
617
+ collectBindingNames(param.AsParameterDeclaration().Name(), declared)
618
+ }
619
+ }
620
+ walkDescendants(reactFunctionBody(callback), func(node *shimast.Node) {
621
+ if nearestFunctionNode(node) != callback {
622
+ return
623
+ }
624
+ switch node.Kind {
625
+ case shimast.KindVariableDeclaration:
626
+ if decl := node.AsVariableDeclaration(); decl != nil {
627
+ collectBindingNames(decl.Name(), declared)
628
+ }
629
+ case shimast.KindFunctionDeclaration:
630
+ if fn := node.AsFunctionDeclaration(); fn != nil {
631
+ collectBindingNames(fn.Name(), declared)
632
+ }
633
+ case shimast.KindIdentifier:
634
+ name := identifierText(node)
635
+ if name == "" || declared[name] || isReactHookStableName(name) || !isIdentifierRead(node) {
636
+ return
637
+ }
638
+ used[name] = true
639
+ }
640
+ })
641
+ return used
642
+ }
643
+
644
+ func collectReactHookDependencyNames(depsNode *shimast.Node) map[string]bool {
645
+ deps := map[string]bool{}
646
+ arr := depsNode.AsArrayLiteralExpression()
647
+ if arr == nil || arr.Elements == nil {
648
+ return deps
649
+ }
650
+ for _, element := range arr.Elements.Nodes {
651
+ name := identifierText(stripParens(element))
652
+ if name == "" {
653
+ name = memberAccessRootName(element)
654
+ }
655
+ if name != "" {
656
+ deps[name] = true
657
+ }
658
+ }
659
+ return deps
660
+ }
661
+
662
+ func missingReactHookDependencies(used map[string]bool, deps map[string]bool) []string {
663
+ var missing []string
664
+ for name := range used {
665
+ if !deps[name] {
666
+ missing = append(missing, name)
667
+ }
668
+ }
669
+ sort.Strings(missing)
670
+ return missing
671
+ }
672
+
673
+ func isIdentifierRead(node *shimast.Node) bool {
674
+ if node == nil || node.Kind != shimast.KindIdentifier || node.Parent == nil {
675
+ return false
676
+ }
677
+ parent := node.Parent
678
+ switch parent.Kind {
679
+ case shimast.KindVariableDeclaration:
680
+ decl := parent.AsVariableDeclaration()
681
+ return decl == nil || decl.Name() != node
682
+ case shimast.KindBindingElement:
683
+ elem := parent.AsBindingElement()
684
+ return elem == nil || elem.Name() != node
685
+ case shimast.KindParameter:
686
+ param := parent.AsParameterDeclaration()
687
+ return param == nil || param.Name() != node
688
+ case shimast.KindFunctionDeclaration:
689
+ fn := parent.AsFunctionDeclaration()
690
+ return fn == nil || fn.Name() != node
691
+ case shimast.KindFunctionExpression:
692
+ fn := parent.AsFunctionExpression()
693
+ return fn == nil || fn.Name() != node
694
+ case shimast.KindPropertyAccessExpression:
695
+ access := parent.AsPropertyAccessExpression()
696
+ return access == nil || access.Name() != node
697
+ case shimast.KindPropertyAssignment:
698
+ prop := parent.AsPropertyAssignment()
699
+ return prop == nil || prop.Name() != node
700
+ }
701
+ return true
702
+ }
703
+
704
+ func isReactHookStableName(name string) bool {
705
+ switch name {
706
+ case "console", "Math", "Date", "JSON", "Object", "Array", "String", "Number", "Boolean", "Promise",
707
+ "React", "window", "document", "undefined", "NaN", "Infinity", "setTimeout", "clearTimeout",
708
+ "setInterval", "clearInterval", "queueMicrotask", "requestAnimationFrame", "cancelAnimationFrame":
709
+ return true
710
+ }
711
+ return strings.HasPrefix(name, "set") && len(name) > 3 && unicode.IsUpper([]rune(name[3:])[0])
712
+ }
713
+
714
+ func bindingPatternNames(node *shimast.Node) []string {
715
+ names := []string{}
716
+ collectBindingNamesOrdered(node, &names, map[string]bool{})
717
+ return names
718
+ }
719
+
720
+ func parseSimpleArrayBindingNames(text string) []string {
721
+ text = strings.TrimSpace(text)
722
+ if !strings.HasPrefix(text, "[") || !strings.HasSuffix(text, "]") {
723
+ return nil
724
+ }
725
+ inner := strings.TrimSuffix(strings.TrimPrefix(text, "["), "]")
726
+ parts := strings.Split(inner, ",")
727
+ names := make([]string, 0, len(parts))
728
+ for _, part := range parts {
729
+ part = strings.TrimSpace(part)
730
+ if part == "" {
731
+ continue
732
+ }
733
+ if idx := strings.Index(part, "="); idx >= 0 {
734
+ part = strings.TrimSpace(part[:idx])
735
+ }
736
+ if strings.HasPrefix(part, "...") {
737
+ part = strings.TrimSpace(strings.TrimPrefix(part, "..."))
738
+ }
739
+ if isReactSimpleIdentifierName(part) {
740
+ names = append(names, part)
741
+ }
742
+ }
743
+ return names
744
+ }
745
+
746
+ func isReactSimpleIdentifierName(text string) bool {
747
+ if text == "" {
748
+ return false
749
+ }
750
+ for i, r := range text {
751
+ if i == 0 {
752
+ if r != '_' && r != '$' && !unicode.IsLetter(r) {
753
+ return false
754
+ }
755
+ continue
756
+ }
757
+ if r != '_' && r != '$' && !unicode.IsLetter(r) && !unicode.IsDigit(r) {
758
+ return false
759
+ }
760
+ }
761
+ return true
762
+ }
763
+
764
+ func collectBindingNames(node *shimast.Node, out map[string]bool) {
765
+ if node == nil {
766
+ return
767
+ }
768
+ if name := identifierText(node); name != "" {
769
+ out[name] = true
770
+ return
771
+ }
772
+ node.ForEachChild(func(child *shimast.Node) bool {
773
+ collectBindingNames(child, out)
774
+ return false
775
+ })
776
+ }
777
+
778
+ func collectBindingNamesOrdered(node *shimast.Node, names *[]string, seen map[string]bool) {
779
+ if node == nil {
780
+ return
781
+ }
782
+ if name := identifierText(node); name != "" {
783
+ if !seen[name] {
784
+ seen[name] = true
785
+ *names = append(*names, name)
786
+ }
787
+ return
788
+ }
789
+ node.ForEachChild(func(child *shimast.Node) bool {
790
+ collectBindingNamesOrdered(child, names, seen)
791
+ return false
792
+ })
793
+ }
794
+
795
+ func memberAccessRootName(node *shimast.Node) string {
796
+ node = stripParens(node)
797
+ for node != nil {
798
+ switch node.Kind {
799
+ case shimast.KindIdentifier:
800
+ return identifierText(node)
801
+ case shimast.KindPropertyAccessExpression:
802
+ access := node.AsPropertyAccessExpression()
803
+ if access == nil {
804
+ return ""
805
+ }
806
+ node = stripParens(access.Expression)
807
+ case shimast.KindElementAccessExpression:
808
+ access := node.AsElementAccessExpression()
809
+ if access == nil {
810
+ return ""
811
+ }
812
+ node = stripParens(access.Expression)
813
+ default:
814
+ return ""
815
+ }
816
+ }
817
+ return ""
818
+ }
819
+
820
+ func parameterName(node *shimast.Node) string {
821
+ if node == nil {
822
+ return ""
823
+ }
824
+ param := node.AsParameterDeclaration()
825
+ if param == nil {
826
+ return ""
827
+ }
828
+ return identifierText(param.Name())
829
+ }
830
+
831
+ func formatQuotedList(names []string) string {
832
+ quoted := make([]string, len(names))
833
+ for i, name := range names {
834
+ quoted[i] = fmt.Sprintf("%q", name)
835
+ }
836
+ return strings.Join(quoted, ", ")
837
+ }
838
+
839
+ func init() {
840
+ Register(reactHooksRule{name: "react/rules-of-hooks"})
841
+ Register(reactHooksRule{name: "react/exhaustive-deps"})
842
+ Register(reactHooksRule{name: "react/component-hook-factories"})
843
+ Register(reactHooksRule{name: "react/set-state-in-render"})
844
+ Register(reactHooksRule{name: "react/set-state-in-effect"})
845
+ Register(reactHooksRule{name: "react/immutability"})
846
+ Register(reactHooksRule{name: "react/refs"})
847
+ Register(reactHooksRule{name: "react/use-memo"})
848
+ }