lib/cc/analyzer/source_extractor.rb in codeclimate-0.63.5 vs lib/cc/analyzer/source_extractor.rb in codeclimate-0.63.6

- old
+ new

@@ -1,26 +1,35 @@ module CC module Analyzer class SourceExtractor - InvalidLocationPositions = Class.new(StandardError) + InvalidLocation = Class.new(StandardError) def initialize(source) @source = source end def extract(location) + validate_location(location) + if (lines = location["lines"]) extract_from_lines(lines) elsif (positions = location["positions"]) extract_from_positions(positions) end end private - attr_reader :location, :source + attr_reader :source + def validate_location(location) + validator = IssueValidations::LocationFormatValidation::Validator.new(location) + unless validator.valid? + raise InvalidLocation, validator.message + end + end + def extract_from_lines(lines) begin_index = lines.fetch("begin") - 1 end_index = lines.fetch("end") - 1 range = (begin_index..end_index) @@ -38,26 +47,17 @@ source[begin_offset, length + 1] end def convert_to_offsets(positions) positions.each_with_object({}) do |(key, value), memo| - if value.key?("offset") - memo[key] = value - else - validate_position_format!(value) - - memo[key] = { - "offset" => to_offset(value["line"] - 1, value["column"] - 1), - } - end - end - end - - def validate_position_format!(position) - unless position.key?("line") && position.key?("column") - message = "Location positions must have either line/column or offset form" - - raise InvalidLocationPositions, message + memo[key] = + if value.key?("offset") + value + else + { + "offset" => to_offset(value["line"] - 1, value["column"] - 1), + } + end end end def to_offset(line, column, offset = 0) source.each_line.with_index do |source_line, index|