class RuboCop::AST::RegexpNode

A node extension for `regexp` nodes. This will be used in place of a plain node when the builder constructs the AST, making its methods available to all `regexp` nodes within RuboCop.

Constants

OPTIONS

Public Instance Methods

content() click to toggle source

@return [String] a string of regexp content

# File lib/rubocop/ast/node/regexp_node.rb, line 36
def content
  children.select(&:str_type?).map(&:str_content).join
end
delimiter?(char) click to toggle source

@return [Bool] if char is one of the delimiters

# File lib/rubocop/ast/node/regexp_node.rb, line 56
def delimiter?(char)
  delimiters.include?(char)
end
delimiters() click to toggle source

@return [String] the regexp delimiters (without %r)

# File lib/rubocop/ast/node/regexp_node.rb, line 51
def delimiters
  [loc.begin.source[-1], loc.end.source[0]]
end
extended?() click to toggle source

@return [Bool] if regexp uses the extended regopt

# File lib/rubocop/ast/node/regexp_node.rb, line 71
def extended?
  regopt_include?(:x)
end
ignore_case?() click to toggle source

@return [Bool] if regexp uses the ignore-case regopt

# File lib/rubocop/ast/node/regexp_node.rb, line 76
def ignore_case?
  regopt_include?(:i)
end
interpolation?() click to toggle source

@return [Bool] if regexp contains interpolation

# File lib/rubocop/ast/node/regexp_node.rb, line 61
def interpolation?
  children.any?(&:begin_type?)
end
multiline_mode?() click to toggle source

@return [Bool] if regexp uses the multiline regopt

# File lib/rubocop/ast/node/regexp_node.rb, line 66
def multiline_mode?
  regopt_include?(:m)
end
no_encoding?() click to toggle source

@return [Bool] if regexp uses the no-encoding regopt

# File lib/rubocop/ast/node/regexp_node.rb, line 86
def no_encoding?
  regopt_include?(:n)
end
options() click to toggle source

NOTE: The 'o' option is ignored.

@return [Integer] the Regexp option bits as returned by Regexp#options

# File lib/rubocop/ast/node/regexp_node.rb, line 31
def options
  regopt.children.map { |opt| OPTIONS.fetch(opt) }.inject(0, :|)
end
percent_r_literal?() click to toggle source

@return [Bool] if the regexp is a %r{…} literal (using any delimiters)

# File lib/rubocop/ast/node/regexp_node.rb, line 46
def percent_r_literal?
  !slash_literal?
end
regopt() click to toggle source

@return [RuboCop::AST::Node] a regopt node

# File lib/rubocop/ast/node/regexp_node.rb, line 24
def regopt
  children.last
end
single_interpolation?() click to toggle source

@return [Bool] if regexp uses the single-interpolation regopt

# File lib/rubocop/ast/node/regexp_node.rb, line 81
def single_interpolation?
  regopt_include?(:o)
end
slash_literal?() click to toggle source

@return [Bool] if the regexp is a /…/ literal

# File lib/rubocop/ast/node/regexp_node.rb, line 41
def slash_literal?
  loc.begin.source == '/'
end
to_regexp() click to toggle source

@return [Regexp] a regexp of this node

# File lib/rubocop/ast/node/regexp_node.rb, line 19
def to_regexp
  Regexp.new(content, options)
end

Private Instance Methods

regopt_include?(option) click to toggle source
# File lib/rubocop/ast/node/regexp_node.rb, line 92
def regopt_include?(option)
  regopt.children.include?(option)
end