File: regexp_node.rb

package info (click to toggle)
ruby-rubocop-ast 0.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 892 kB
  • sloc: ruby: 10,886; makefile: 4
file content (92 lines) | stat: -rw-r--r-- 2,354 bytes parent folder | download
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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 the regexp is a /.../ literal
      def slash_literal?
        loc.begin.source == '/'
      end

      # @return [Bool] if the regexp is a %r{...} literal (using any delimiters)
      def percent_r_literal?
        !slash_literal?
      end

      # @return [String] the regexp delimiters (without %r)
      def delimiters
        [loc.begin.source[-1], loc.end.source[0]]
      end

      # @return [Bool] if char is one of the delimiters
      def delimiter?(char)
        delimiters.include?(char)
      end

      # @return [Bool] if regexp contains interpolation
      def interpolation?
        children.any?(&:begin_type?)
      end

      # @return [Bool] if regexp uses the multiline regopt
      def multiline_mode?
        regopt_include?(:m)
      end

      # @return [Bool] if regexp uses the extended regopt
      def extended?
        regopt_include?(:x)
      end

      # @return [Bool] if regexp uses the ignore-case regopt
      def ignore_case?
        regopt_include?(:i)
      end

      # @return [Bool] if regexp uses the single-interpolation regopt
      def single_interpolation?
        regopt_include?(:o)
      end

      # @return [Bool] if regexp uses the no-encoding regopt
      def no_encoding?
        regopt_include?(:n)
      end

      private

      def regopt_include?(option)
        regopt.children.include?(option)
      end
    end
  end
end