2020-10-10 14:16:11 +02:00
|
|
|
# typed: false
|
2020-07-30 16:29:12 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-11-09 08:34:11 -08:00
|
|
|
require "rubocop"
|
|
|
|
|
2020-07-30 16:29:12 +01:00
|
|
|
module RuboCop
|
|
|
|
module Cop
|
2020-08-26 02:52:48 +02:00
|
|
|
# Helper functions for cops.
|
|
|
|
#
|
|
|
|
# @api private
|
2020-07-30 16:29:12 +01:00
|
|
|
module HelperFunctions
|
|
|
|
include RangeHelp
|
|
|
|
|
|
|
|
# Checks for regex match of pattern in the node and
|
2020-11-05 17:17:03 -05:00
|
|
|
# sets the appropriate instance variables to report the match.
|
2020-07-30 16:29:12 +01:00
|
|
|
def regex_match_group(node, pattern)
|
|
|
|
string_repr = string_content(node).encode("UTF-8", invalid: :replace)
|
|
|
|
match_object = string_repr.match(pattern)
|
|
|
|
return unless match_object
|
|
|
|
|
|
|
|
node_begin_pos = start_column(node)
|
|
|
|
line_begin_pos = line_start_column(node)
|
|
|
|
@column = if node_begin_pos == line_begin_pos
|
|
|
|
node_begin_pos + match_object.begin(0) - line_begin_pos
|
|
|
|
else
|
|
|
|
node_begin_pos + match_object.begin(0) - line_begin_pos + 1
|
|
|
|
end
|
|
|
|
@length = match_object.to_s.length
|
|
|
|
@line_no = line_number(node)
|
|
|
|
@source_buf = source_buffer(node)
|
|
|
|
@offensive_node = node
|
|
|
|
match_object
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the begin position of the node's line in source code.
|
2020-07-30 16:29:12 +01:00
|
|
|
def line_start_column(node)
|
|
|
|
node.source_range.source_buffer.line_range(node.loc.line).begin_pos
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the begin position of the node in source code.
|
2020-07-30 16:29:12 +01:00
|
|
|
def start_column(node)
|
|
|
|
node.source_range.begin_pos
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the line number of the node.
|
2020-07-30 16:29:12 +01:00
|
|
|
def line_number(node)
|
|
|
|
node.loc.line
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Source buffer is required as an argument to report style violations.
|
2020-07-30 16:29:12 +01:00
|
|
|
def source_buffer(node)
|
|
|
|
node.source_range.source_buffer
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the string representation if node is of type str(plain) or dstr(interpolated) or const.
|
2020-07-30 16:29:12 +01:00
|
|
|
def string_content(node)
|
|
|
|
case node.type
|
|
|
|
when :str
|
|
|
|
node.str_content
|
|
|
|
when :dstr
|
|
|
|
content = ""
|
|
|
|
node.each_child_node(:str, :begin) do |child|
|
|
|
|
content += if child.begin_type?
|
|
|
|
child.source
|
|
|
|
else
|
|
|
|
child.str_content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
content
|
|
|
|
when :const
|
|
|
|
node.const_name
|
|
|
|
when :sym
|
|
|
|
node.children.first.to_s
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-12 02:19:31 +11:00
|
|
|
def problem(msg, &block)
|
|
|
|
add_offense(@offensive_node, message: msg, &block)
|
2020-07-30 16:29:12 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|