Class: RuboCop::Cop::Yoshiki::Style::NestedHtmlOptionKeys::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/yoshiki/style/nested_html_option_keys/rewriter.rb

Overview

Mutate an entry list: nest, merge, or keep the original pair.

Instance Method Summary collapse

Constructor Details

#initialize(forest) ⇒ Tree

Returns a new instance of Tree.



134
135
136
# File 'lib/rubocop/cop/yoshiki/style/nested_html_option_keys/rewriter.rb', line 134

def initialize forest
  @forest = forest
end

Instance Method Details

#append(entries, kind, **fields) ⇒ Object



161
162
163
164
165
# File 'lib/rubocop/cop/yoshiki/style/nested_html_option_keys/rewriter.rb', line 161

def append entries, kind, **fields
  entry = { kind: kind, **fields }
  entries << entry
  entry
end

#insert_path(entries, path, pair, direct) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rubocop/cop/yoshiki/style/nested_html_option_keys/rewriter.rb', line 138

def insert_path entries, path, pair, direct
  @forest.note_offense(pair, direct)
  slot = ensure_branch(entries, path.first)
  if slot && merge_leaf?(slot[:children], path.drop(1), pair)
    @forest.mutated!
    return
  end

  entries.delete(slot) if slot && slot[:children].empty?
  return if join_leaf?(entries, path, pair)

  append(entries, :raw, source: pair.source)
end

#merge_hash(entries, name, pair, mode) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/rubocop/cop/yoshiki/style/nested_html_option_keys/rewriter.rb', line 152

def merge_hash entries, name, pair, mode
  nested = @forest.build(pair.value, (@forest.prefix?(name) ? :inside_prefix : mode), false)
  slot   = find_named(entries, name)
  return append(entries, :branch, name: name, children: nested, node: pair.value) if slot.nil?
  return absorb(slot, nested, pair.value) if slot[:kind] == :branch

  append(entries, :raw, source: pair.source)
end