class RubbyCop::Cop::Style::RegexpLiteral
This cop enforces using // or %r around regular expressions.
@example
# Good if EnforcedStyle is slashes or mixed, bad if percent_r. snake_case = /^[\dA-Z_]+$/ # Good if EnforcedStyle is percent_r, bad if slashes or mixed. snake_case = %r{^[\dA-Z_]+$} # Good if EnforcedStyle is slashes, bad if percent_r or mixed. regex = / foo (bar) (baz) /x # Good if EnforcedStyle is percent_r or mixed, bad if slashes. regex = %r{ foo (bar) (baz) }x # Bad unless AllowInnerSlashes is true. x =~ /home\//
Constants
- MSG_USE_PERCENT_R
- MSG_USE_SLASHES
Public Instance Methods
on_regexp(node)
click to toggle source
# File lib/rubbycop/cop/style/regexp_literal.rb, line 37 def on_regexp(node) if slash_literal?(node) check_slash_literal(node) else check_percent_r_literal(node) end end
Private Instance Methods
allow_inner_slashes?()
click to toggle source
# File lib/rubbycop/cop/style/regexp_literal.rb, line 80 def allow_inner_slashes? cop_config['AllowInnerSlashes'] end
allowed_percent_r_literal?(node)
click to toggle source
# File lib/rubbycop/cop/style/regexp_literal.rb, line 65 def allowed_percent_r_literal?(node) style == :slashes && contains_disallowed_slash?(node) || style == :percent_r || style == :mixed && node.multiline? || style == :mixed && contains_disallowed_slash?(node) end
allowed_slash_literal?(node)
click to toggle source
# File lib/rubbycop/cop/style/regexp_literal.rb, line 59 def allowed_slash_literal?(node) style == :slashes && !contains_disallowed_slash?(node) || style == :mixed && node.single_line? && !contains_disallowed_slash?(node) end
autocorrect(node)
click to toggle source
# File lib/rubbycop/cop/style/regexp_literal.rb, line 97 def autocorrect(node) return if contains_slash?(node) replacement = if slash_literal?(node) ['%r', ''].zip(preferred_delimiters).map(&:join) else %w[/ /] end lambda do |corrector| corrector.replace(node.loc.begin, replacement.first) corrector.replace(node.loc.end, replacement.last) end end
check_percent_r_literal(node)
click to toggle source
# File lib/rubbycop/cop/style/regexp_literal.rb, line 53 def check_percent_r_literal(node) return if allowed_percent_r_literal?(node) add_offense(node, :expression, MSG_USE_SLASHES) end
check_slash_literal(node)
click to toggle source
# File lib/rubbycop/cop/style/regexp_literal.rb, line 47 def check_slash_literal(node) return if allowed_slash_literal?(node) add_offense(node, :expression, MSG_USE_PERCENT_R) end
contains_disallowed_slash?(node)
click to toggle source
# File lib/rubbycop/cop/style/regexp_literal.rb, line 72 def contains_disallowed_slash?(node) !allow_inner_slashes? && contains_slash?(node) end
contains_slash?(node)
click to toggle source
# File lib/rubbycop/cop/style/regexp_literal.rb, line 76 def contains_slash?(node) node_body(node).include?('/') end
node_body(node)
click to toggle source
# File lib/rubbycop/cop/style/regexp_literal.rb, line 84 def node_body(node) node.each_child_node(:str).map(&:source).join end
preferred_delimiters()
click to toggle source
# File lib/rubbycop/cop/style/regexp_literal.rb, line 92 def preferred_delimiters config.for_cop('Style/PercentLiteralDelimiters') \ ['PreferredDelimiters']['%r'].split(//) end
slash_literal?(node)
click to toggle source
# File lib/rubbycop/cop/style/regexp_literal.rb, line 88 def slash_literal?(node) node.loc.begin.source == '/' end