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(processed_source, ruby_clip) ⇒ WhenDecomposer

Returns a new instance of WhenDecomposer.

Parameters:



29
30
31
32
33
34
35
# File 'lib/rubocop/slim/when_decomposer.rb', line 29

def initialize(
  processed_source,
  ruby_clip
)
  @processed_source = processed_source
  @ruby_clip = ruby_clip
end

Class Method Details

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

Parameters:

Returns:



16
17
18
19
20
21
22
23
24
# File 'lib/rubocop/slim/when_decomposer.rb', line 16

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

Instance Method Details

#callArray<RuboCop::Slim::RubyClip>

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/slim/when_decomposer.rb', line 38

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(
      <<~RUBY
        [
          #{condition}
        ]
      RUBY
    ).children.map do |child|
      RubyClip.new(
        code: child.location.expression.source,
        offset: @ruby_clip.offset + offset + child.location.expression.begin_pos - 4
      )
    end
  else
    [@ruby_clip]
  end
end