Class: RuboCop::AST::RegexpNode

Inherits:
Node
  • Object
show all
Defined in:
lib/rubocop/ast/node/regexp_node.rb

Overview

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.

Constant Summary collapse

OPTIONS =
{
  x: Regexp::EXTENDED,
  i: Regexp::IGNORECASE,
  m: Regexp::MULTILINE,
  n: Regexp::NOENCODING,
  o: 0
}.freeze

Constants inherited from Node

Node::ARGUMENT_TYPES, Node::ASSIGNMENTS, Node::BASIC_CONDITIONALS, Node::BASIC_LITERALS, Node::COMPARISON_OPERATORS, Node::COMPOSITE_LITERALS, Node::CONDITIONALS, Node::EQUALS_ASSIGNMENTS, Node::FALSEY_LITERALS, Node::IMMUTABLE_LITERALS, Node::KEYWORDS, Node::LITERALS, Node::LOOP_TYPES, Node::MUTABLE_LITERALS, Node::OPERATOR_KEYWORDS, Node::POST_CONDITION_LOOP_TYPES, Node::REFERENCES, Node::SHORTHAND_ASSIGNMENTS, Node::SPECIAL_KEYWORDS, Node::TRUTHY_LITERALS, Node::VARIABLES

Instance Method Summary collapse

Methods inherited from Node

#ancestors, #argument?, #argument_type?, #assignment?, #basic_conditional?, #basic_literal?, #boolean_type?, #call_type?, #chained?, #child_nodes, #complete!, #complete?, #conditional?, #const_name, #defined_module, #defined_module_name, #descendants, #each_ancestor, #each_child_node, #each_descendant, #each_node, #empty_source?, #equals_asgn?, #falsey_literal?, #first_line, #guard_clause?, #immutable_literal?, #initialize, #keyword?, #last_line, #left_sibling, #left_siblings, #line_count, #literal?, #loop_keyword?, #multiline?, #mutable_literal?, #node_parts, #nonempty_line_count, #numeric_type?, #operator_keyword?, #parent, #parent_module_name, #parenthesized_call?, #post_condition_loop?, #pure?, #range_type?, #receiver, #reference?, #right_sibling, #right_siblings, #shorthand_asgn?, #sibling_index, #single_line?, #source, #source_length, #source_range, #special_keyword?, #truthy_literal?, #updated, #value_used?, #variable?

Methods included from NodePattern::Macros

#def_node_matcher, #def_node_search

Methods included from Sexp

#s

Constructor Details

This class inherits a constructor from RuboCop::AST::Node

Instance Method Details

#contentString

Returns a string of regexp content.

Returns:

  • (String)

    a string of regexp content


31
32
33
# File 'lib/rubocop/ast/node/regexp_node.rb', line 31

def content
  children.select(&:str_type?).map(&:str_content).join
end

#delimiter?(char) ⇒ Bool

Returns if char is one of the delimiters.

Returns:

  • (Bool)

    if char is one of the delimiters


51
52
53
# File 'lib/rubocop/ast/node/regexp_node.rb', line 51

def delimiter?(char)
  delimiters.include?(char)
end

#delimitersString

Returns the regexp delimiters (without %r).

Returns:

  • (String)

    the regexp delimiters (without %r)


46
47
48
# File 'lib/rubocop/ast/node/regexp_node.rb', line 46

def delimiters
  [loc.begin.source[-1], loc.end.source[0]]
end

#extended?Bool

Returns if regexp uses the extended regopt.

Returns:

  • (Bool)

    if regexp uses the extended regopt


66
67
68
# File 'lib/rubocop/ast/node/regexp_node.rb', line 66

def extended?
  regopt_include?(:x)
end

#ignore_case?Bool

Returns if regexp uses the ignore-case regopt.

Returns:

  • (Bool)

    if regexp uses the ignore-case regopt


71
72
73
# File 'lib/rubocop/ast/node/regexp_node.rb', line 71

def ignore_case?
  regopt_include?(:i)
end

#interpolation?Bool

Returns if regexp contains interpolation.

Returns:

  • (Bool)

    if regexp contains interpolation


56
57
58
# File 'lib/rubocop/ast/node/regexp_node.rb', line 56

def interpolation?
  children.any?(&:begin_type?)
end

#multiline_mode?Bool

Returns if regexp uses the multiline regopt.

Returns:

  • (Bool)

    if regexp uses the multiline regopt


61
62
63
# File 'lib/rubocop/ast/node/regexp_node.rb', line 61

def multiline_mode?
  regopt_include?(:m)
end

#no_encoding?Bool

Returns if regexp uses the no-encoding regopt.

Returns:

  • (Bool)

    if regexp uses the no-encoding regopt


81
82
83
# File 'lib/rubocop/ast/node/regexp_node.rb', line 81

def no_encoding?
  regopt_include?(:n)
end

#percent_r_literal?Bool

Returns if the regexp is a %r… literal (using any delimiters).

Returns:

  • (Bool)

    if the regexp is a %r… literal (using any delimiters)


41
42
43
# File 'lib/rubocop/ast/node/regexp_node.rb', line 41

def percent_r_literal?
  !slash_literal?
end

#regoptRuboCop::AST::Node

Returns a regopt node.

Returns:


26
27
28
# File 'lib/rubocop/ast/node/regexp_node.rb', line 26

def regopt
  children.last
end

#single_interpolation?Bool

Returns if regexp uses the single-interpolation regopt.

Returns:

  • (Bool)

    if regexp uses the single-interpolation regopt


76
77
78
# File 'lib/rubocop/ast/node/regexp_node.rb', line 76

def single_interpolation?
  regopt_include?(:o)
end

#slash_literal?Bool

Returns if the regexp is a /…/ literal.

Returns:

  • (Bool)

    if the regexp is a /…/ literal


36
37
38
# File 'lib/rubocop/ast/node/regexp_node.rb', line 36

def slash_literal?
  loc.begin.source == '/'
end

#to_regexpRegexp

Note: The 'o' option is ignored.

Returns:

  • (Regexp)

    a regexp of this node


20
21
22
23
# File 'lib/rubocop/ast/node/regexp_node.rb', line 20

def to_regexp
  option = regopt.children.map { |opt| OPTIONS.fetch(opt) }.inject(:|)
  Regexp.new(content, option)
end