Class: ERBLint::Linters::MigrateDeprecatedFlashArguments

Inherits:
Linter
  • Object
show all
Includes:
ERBLint::LinterRegistry, Helpers::RubocopHelpers
Defined in:
lib/yattho/view_components/linters/migrate_deprecated_flash_arguments.rb

Overview

Migrates deprecated arguments in Yattho::Beta::Flash components to their equivalents.

Constant Summary collapse

BLOCK_EXPR =
/\s*((\s+|\))do|\{)(\s*\|[^|]*\|)?\s*\Z/.freeze

Instance Method Summary collapse

Methods included from Helpers::RubocopHelpers

#erb_ast

Instance Method Details

#autocorrect(_, offense) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/yattho/view_components/linters/migrate_deprecated_flash_arguments.rb', line 62

def autocorrect(_, offense)
  return unless offense.context

  lambda do |corrector|
    corrector.replace(offense.source_range, offense.context)
  end
end

#run(processed_source) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/yattho/view_components/linters/migrate_deprecated_flash_arguments.rb', line 15

def run(processed_source)
  processed_source.ast.descendants(:erb).each do |erb_node|
    indicator_node, _, code_node = *erb_node
    code = code_node.children.first
    ast = erb_ast(maybe_close_block(code))
    next unless ast

    constructor_arg_hashes = find_new_flash_instances(ast)
    next if constructor_arg_hashes.empty?

    indicator, = *indicator_node
    indicator ||= ""

    # +2 to account for the leading "<%" characters
    code_start_pos = erb_node.location.begin_pos + indicator.size + 2

    constructor_arg_hashes.each do |constructor_arg_hash|
      spacious_arg = constructor_arg_hash.include?(:spacious)
      next unless spacious_arg

      orig_loc = code_node.location
      key_node, value_node = constructor_arg_hash[:spacious]

      new_loc = orig_loc.with(
        begin_pos: key_node.location.expression.begin_pos + code_start_pos,
        end_pos: value_node.location.expression.end_pos + code_start_pos
      )

      if value_node.source == "true"
        add_offense(
          new_loc,
          "The :spacious argument is deprecated. Use `mb: 4` instead.",
          "mb: 4"
        )
      else
        new_loc = adjust_to_preceding_comma(new_loc)

        add_offense(
          new_loc,
          "The :spacious argument is deprecated; `spacious: false` can be removed.",
          ""
        )
      end
    end
  end
end