Class: RuboCop::RBS::CopBase

Inherits:
Cop::Base
  • Object
show all
Includes:
Cop::RangeHelp, OnTypeHelper
Defined in:
lib/rubocop/rbs/cop_base.rb

Overview

Base class for cops that operate on RBS signatures.

Constant Summary collapse

@@cache =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OnTypeHelper

#on_not_type, #on_type

Instance Attribute Details

#processed_rbs_sourceObject (readonly)

Returns the value of attribute processed_rbs_source.



12
13
14
# File 'lib/rubocop/rbs/cop_base.rb', line 12

def processed_rbs_source
  @processed_rbs_source
end

Instance Method Details

#investigation_rbsObject



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
# File 'lib/rubocop/rbs/cop_base.rb', line 31

def investigation_rbs
  return unless processed_source.buffer.name.then { |n| n.end_with?(".rbs") || n == "(string)" }

  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
    on_rbs_new_investigation()

    processed_rbs_source.decls.each do |decl|
      walk(decl)
    end
  end
end

#location_to_range(location) ⇒ Object



115
116
117
# File 'lib/rubocop/rbs/cop_base.rb', line 115

def location_to_range(location)
  range_between(location.start_pos, location.end_pos)
end

#on_new_investigationObject



16
17
18
19
# File 'lib/rubocop/rbs/cop_base.rb', line 16

def on_new_investigation
  # Called here when valid as Ruby
  investigation_rbs()
end

#on_other_fileObject



21
22
23
# File 'lib/rubocop/rbs/cop_base.rb', line 21

def on_other_file
  investigation_rbs()
end

#on_rbs_attribute(member) ⇒ Object



73
# File 'lib/rubocop/rbs/cop_base.rb', line 73

def on_rbs_attribute(member); end

#on_rbs_class(member) ⇒ Object

other on_* methods should sync with ‘#walk` method



66
# File 'lib/rubocop/rbs/cop_base.rb', line 66

def on_rbs_class(member); end

#on_rbs_constant(const) ⇒ Object



69
# File 'lib/rubocop/rbs/cop_base.rb', line 69

def on_rbs_constant(const); end

#on_rbs_def(member) ⇒ Object



72
# File 'lib/rubocop/rbs/cop_base.rb', line 72

def on_rbs_def(member); end

#on_rbs_global(global) ⇒ Object



70
# File 'lib/rubocop/rbs/cop_base.rb', line 70

def on_rbs_global(global); end

#on_rbs_interface(member) ⇒ Object



68
# File 'lib/rubocop/rbs/cop_base.rb', line 68

def on_rbs_interface(member); end

#on_rbs_module(member) ⇒ Object



67
# File 'lib/rubocop/rbs/cop_base.rb', line 67

def on_rbs_module(member); end

#on_rbs_new_investigationObject



62
# File 'lib/rubocop/rbs/cop_base.rb', line 62

def on_rbs_new_investigation; end

#on_rbs_parsing_errorObject



63
# File 'lib/rubocop/rbs/cop_base.rb', line 63

def on_rbs_parsing_error; end

#on_rbs_private(member) ⇒ Object



75
# File 'lib/rubocop/rbs/cop_base.rb', line 75

def on_rbs_private(member); end

#on_rbs_public(member) ⇒ Object



74
# File 'lib/rubocop/rbs/cop_base.rb', line 74

def on_rbs_public(member); end

#on_rbs_type_alias(decl) ⇒ Object



71
# File 'lib/rubocop/rbs/cop_base.rb', line 71

def on_rbs_type_alias(decl); end

#on_rbs_var(member) ⇒ Object



76
# File 'lib/rubocop/rbs/cop_base.rb', line 76

def on_rbs_var(member); end

#parse_rbsObject



26
27
28
29
# File 'lib/rubocop/rbs/cop_base.rb', line 26

def parse_rbs
  buffer = rbs_buffer()
  @processed_rbs_source = RuboCop::RBS::ProcessedRBSSource.new(buffer)
end

#rbs_bufferObject



108
109
110
111
112
113
# File 'lib/rubocop/rbs/cop_base.rb', line 108

def rbs_buffer
  ::RBS::Buffer.new(
    name: processed_source.buffer.name,
    content: processed_source.raw_source
  )
end

#tokenize(source) ⇒ Object



119
120
121
# File 'lib/rubocop/rbs/cop_base.rb', line 119

def tokenize(source)
  ::RBS::Parser.lex(source).value.reject { |t| t.type == :tTRIVIA }
end

#walk(decl) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rubocop/rbs/cop_base.rb', line 78

def walk(decl)
  case decl
  when ::RBS::AST::Declarations::Module
    on_rbs_module(decl)
    decl.members.each { |member| walk(member) }
  when ::RBS::AST::Declarations::Class
    on_rbs_class(decl)
    decl.members.each { |member| walk(member) }
  when ::RBS::AST::Declarations::Interface
    on_rbs_interface(decl)
    decl.members.each { |member| walk(member) }
  when ::RBS::AST::Declarations::Constant
    on_rbs_constant(decl)
  when ::RBS::AST::Declarations::Global
    on_rbs_global(decl)
  when ::RBS::AST::Declarations::TypeAlias
    on_rbs_type_alias(decl)
  when ::RBS::AST::Members::MethodDefinition
    on_rbs_def(decl)
  when ::RBS::AST::Members::Attribute
    on_rbs_attribute(decl)
  when ::RBS::AST::Members::Public
    on_rbs_public(decl)
  when ::RBS::AST::Members::Private
    on_rbs_private(decl)
  when ::RBS::AST::Members::Var
    on_rbs_var(decl)
  end
end