Class: RuboCop::Slim::WhenDecomposer

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/slim/when_decomposer.rb

Constant Summary collapse

REGEXP =
/
  \A
  \s*
  when[ \t]
/x.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby_clip) ⇒ WhenDecomposer

Returns a new instance of WhenDecomposer.

Parameters:



24
25
26
# File 'lib/rubocop/slim/when_decomposer.rb', line 24

def initialize(ruby_clip)
  @ruby_clip = ruby_clip
end

Class Method Details

.call(ruby_clip) ⇒ Array<RuboCop::Slim::RubyClip>

Parameters:

Returns:



18
19
20
# File 'lib/rubocop/slim/when_decomposer.rb', line 18

def call(ruby_clip)
  new(ruby_clip).call
end

Instance Method Details

#callArray<RuboCop::Slim::RubyClip>

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/slim/when_decomposer.rb', line 29

def call
  match_data = @ruby_clip.code.match(REGEXP)
  if match_data
    offset = match_data[0].length
    condition = @ruby_clip.code[offset..].sub(/[ \t]then(?:[ \t].*)?/, '')
    parse("[#{condition}]").children.map do |child|
      RubyClip.new(
        code: child.location.expression.source,
        offset: @ruby_clip.offset + offset + child.location.expression.begin_pos - 1
      )
    end
  else
    [@ruby_clip]
  end
end