forked from rubocop/rubocop-ast
/
regexp_node.rb
41 lines (36 loc) · 1.06 KB
/
regexp_node.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true
module RuboCop
module AST
# 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.
class RegexpNode < Node
OPTIONS = {
x: Regexp::EXTENDED,
i: Regexp::IGNORECASE,
m: Regexp::MULTILINE,
n: Regexp::NOENCODING,
o: 0
}.freeze
# Note: The 'o' option is ignored.
#
# @return [Regexp] a regexp of this node
def to_regexp
option = regopt.children.map { |opt| OPTIONS.fetch(opt) }.inject(:|)
Regexp.new(content, option)
end
# @return [RuboCop::AST::Node] a regopt node
def regopt
children.last
end
# @return [String] a string of regexp content
def content
children.select(&:str_type?).map(&:str_content).join
end
# @return [Bool] if regexp contains interpolation
def interpolation?
children.any?(&:begin_type?)
end
end
end
end