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?, #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?, #send_type?, #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



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

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



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

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

#delimitersString

Returns the regexp delimiters (without %r).

Returns:

  • (String)

    the regexp delimiters (without %r)



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

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



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

def extended?
  regopt_include?(:x)
end

#fixed_encoding?Bool

Returns if regexp uses the fixed-encoding regopt.

Returns:

  • (Bool)

    if regexp uses the fixed-encoding regopt



92
93
94
# File 'lib/rubocop/ast/node/regexp_node.rb', line 92

def fixed_encoding?
  regopt_include?(:u)
end

#ignore_case?Bool

Returns if regexp uses the ignore-case regopt.

Returns:

  • (Bool)

    if regexp uses the ignore-case regopt



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

def ignore_case?
  regopt_include?(:i)
end

#interpolation?Bool

Returns if regexp contains interpolation.

Returns:

  • (Bool)

    if regexp contains interpolation



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

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



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

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



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

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



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

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)



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

def percent_r_literal?
  !slash_literal?
end

#regoptRuboCop::AST::Node

Returns a regopt node.

Returns:



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

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



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

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



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

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

#to_regexpRegexp

Returns a regexp of this node.

Returns:

  • (Regexp)

    a regexp of this node



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

def to_regexp
  Regexp.new(content, options)
end