lib/rubocop/rbs/cop_base.rb in rubocop-on-rbs-0.4.0 vs lib/rubocop/rbs/cop_base.rb in rubocop-on-rbs-0.5.0

- old
+ new

@@ -1,12 +1,15 @@ # frozen_string_literal: true +require 'zlib' + module RuboCop module RBS # Base class for cops that operate on RBS signatures. class CopBase < RuboCop::Cop::Base include RuboCop::Cop::RangeHelp + include RuboCop::RBS::OnTypeHelper attr_reader :processed_rbs_source exclude_from_registry @@ -17,15 +20,35 @@ def on_other_file investigation_rbs() end + @@cache = {} + def parse_rbs + buffer = rbs_buffer() + @processed_rbs_source = RuboCop::RBS::ProcessedRBSSource.new(buffer) + end + def investigation_rbs return unless processed_source.buffer.name.then { |n| n.end_with?(".rbs") || n == "(string)" } - buffer = rbs_buffer() - @processed_rbs_source = RuboCop::RBS::ProcessedRBSSource.new(buffer) + if processed_source.buffer.name == "(string)" + parse_rbs + else + crc32 = Zlib.crc32(processed_source.raw_source) + hit_path = @@cache[processed_source.buffer.name] + if hit_path + if hit_crc32 = hit_path[crc32] + @processed_rbs_source = hit_crc32 + else + hit_path.clear # Other key expect clear by GC + hit_path[crc32] = parse_rbs + end + else + (@@cache[processed_source.buffer.name] ||= {})[crc32] = parse_rbs + end + end if processed_rbs_source.error on_rbs_parsing_error() else # HACK: Autocorrector needs to clear diagnostics @@ -87,31 +110,9 @@ range_between(location.start_pos, location.end_pos) end def tokenize(source) ::RBS::Parser.lex(source).value.reject { |t| t.type == :tTRIVIA } - end - - def on_type(types, type, &block) - case type - when *types - yield type - end - type.each_type do |t| - on_type(types, t, &block) - end - end - - def on_not_type(types, type, &block) - case type - when *types - # not - else - yield type - end - type.each_type do |t| - on_not_type(types, t, &block) - end end end end end