Class: RuboCop::Cop::PhlexForms::RawForm
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::PhlexForms::RawForm
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/phlex_forms/cop/raw_form.rb
Overview
Enforces Form() (the phlex-forms kit helper) over form_with or a raw
form() element.
Constant Summary collapse
- MSG_FORM_WITH =
"Use `Form(model: @model)` instead of `form_with`."- MSG_RAW_FORM =
"Use `Form()` instead of raw `form()`."
Instance Method Summary collapse
Instance Method Details
#form_with_call?(node) ⇒ Object
23 24 25 |
# File 'lib/rubocop/phlex_forms/cop/raw_form.rb', line 23 def_node_matcher :form_with_call?, <<~PATTERN (send nil? :form_with ...) PATTERN |
#on_send(node) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rubocop/phlex_forms/cop/raw_form.rb', line 32 def on_send(node) if form_with_call?(node) add_offense(node.loc.selector, message: MSG_FORM_WITH) do |corrector| corrector.replace(node.loc.selector, "Form") end elsif raw_form_call?(node) # Skip bare `form` with no args and no block — it's a variable/method # reference, not a Phlex form element (e.g. `form.label(...)`, # `IconField(form:)`). return if node.arguments.empty? && !node.block_node add_offense(node.loc.selector, message: MSG_RAW_FORM) do |corrector| corrector.replace(node.loc.selector, "Form") end end end |
#raw_form_call?(node) ⇒ Object
28 29 30 |
# File 'lib/rubocop/phlex_forms/cop/raw_form.rb', line 28 def_node_matcher :raw_form_call?, <<~PATTERN (send nil? :form ...) PATTERN |