Module: RuboCop::Cop::PercentLiteral

Overview

Common functionality for handling percent literals.

Instance Method Summary collapse

Instance Method Details

#begin_source(node) ⇒ Object



18
19
20
# File 'lib/rubocop/cop/mixin/percent_literal.rb', line 18

def begin_source(node)
  node.loc.begin.source if node.loc.respond_to?(:begin) && node.loc.begin
end

#contents_range(node) ⇒ Object

A range containing only the contents of the percent literal (e.g. in %i2 3 this will be the range covering ‘1 2 3’ only)



28
29
30
31
32
33
34
# File 'lib/rubocop/cop/mixin/percent_literal.rb', line 28

def contents_range(node)
  Parser::Source::Range.new(
    node.loc.expression.source_buffer,
    node.loc.begin.end_pos,
    node.loc.end.begin_pos
  )
end

#percent_literal?(node) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/rubocop/cop/mixin/percent_literal.rb', line 8

def percent_literal?(node)
  return unless (begin_source = begin_source(node))
  begin_source.start_with?('%')
end

#process(node, *types) ⇒ Object



13
14
15
16
# File 'lib/rubocop/cop/mixin/percent_literal.rb', line 13

def process(node, *types)
  return unless percent_literal?(node) && types.include?(type(node))
  on_percent_literal(node)
end

#type(node) ⇒ Object



22
23
24
# File 'lib/rubocop/cop/mixin/percent_literal.rb', line 22

def type(node)
  node.loc.begin.source[0..-2]
end