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

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?, #assignment_or_similar?, #basic_conditional?, #basic_literal?, #boolean_type?, #call_type?, #chained?, #class_constructor?, #class_definition?, #complete!, #complete?, #conditional?, #const_name, #defined_module, #defined_module_name, #each_ancestor, #empty_source?, #equals_asgn?, #falsey_literal?, #first_line, #global_const?, #guard_clause?, #immutable_literal?, #initialize, #keyword?, #lambda?, #lambda_or_proc?, #last_line, #left_sibling, #left_siblings, #line_count, #literal?, #loop_keyword?, #match_guard_clause?, #module_definition?, #multiline?, #mutable_literal?, #node_parts, #nonempty_line_count, #numeric_type?, #operator_keyword?, #parent, #parent?, #parent_module_name, #parenthesized_call?, #post_condition_loop?, #proc?, #pure?, #range_type?, #receiver, #reference?, #right_sibling, #right_siblings, #root?, #shorthand_asgn?, #sibling_index, #single_line?, #source, #source_length, #source_range, #special_keyword?, #str_content, #struct_constructor?, #truthy_literal?, #updated, #value_used?, #variable?

Methods included from NodePattern::Macros

#def_node_matcher, #def_node_search

Methods included from Descendence

#child_nodes, #descendants, #each_child_node, #each_descendant, #each_node

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



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

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



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

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

#delimitersString

Returns the regexp delimiters (without %r).

Returns:

  • (String)

    the regexp delimiters (without %r)



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

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



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

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



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

def ignore_case?
  regopt_include?(:i)
end

#interpolation?Bool

Returns if regexp contains interpolation.

Returns:

  • (Bool)

    if regexp contains interpolation



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

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



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

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



86
87
88
# File 'lib/rubocop/ast/node/regexp_node.rb', line 86

def no_encoding?
  regopt_include?(:n)
end

#optionsInteger

NOTE: The ‘o’ option is ignored.

Returns:

  • (Integer)

    the Regexp option bits as returned by Regexp#options



31
32
33
# 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?Bool

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

Returns:

  • (Bool)

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



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

def percent_r_literal?
  !slash_literal?
end

#regoptRuboCop::AST::Node

Returns a regopt node.

Returns:



24
25
26
# File 'lib/rubocop/ast/node/regexp_node.rb', line 24

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



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

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



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

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

#to_regexpRegexp

Returns a regexp of this node.

Returns:

  • (Regexp)

    a regexp of this node



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

def to_regexp
  Regexp.new(content, options)
end