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.

Direct Known Subclasses

Cop::RBS::Layout::CommentIndentation, Cop::RBS::Layout::EmptyLineBetweenDeclarations, Cop::RBS::Layout::EmptyLines, Cop::RBS::Layout::EmptyLinesAroundAccessModifier, Cop::RBS::Layout::EmptyLinesAroundClassBody, Cop::RBS::Layout::EmptyLinesAroundInterfaceBody, Cop::RBS::Layout::EmptyLinesAroundModuleBody, Cop::RBS::Layout::EmptyLinesAroundOverloads, Cop::RBS::Layout::EndAlignment, Cop::RBS::Layout::ExtraSpacing, Cop::RBS::Layout::IndentationWidth, Cop::RBS::Layout::OverloadIndentation, Cop::RBS::Layout::SpaceAfterComma, Cop::RBS::Layout::SpaceAroundArrow, Cop::RBS::Layout::SpaceAroundBraces, Cop::RBS::Layout::SpaceAroundOperators, Cop::RBS::Layout::SpaceBeforeColon, Cop::RBS::Layout::SpaceBeforeOverload, Cop::RBS::Layout::TrailingWhitespace, Cop::RBS::Lint::AmbiguousKeywordArgumentKey, Cop::RBS::Lint::AmbiguousOperatorPrecedence, Cop::RBS::Lint::DuplicateAnnotation, Cop::RBS::Lint::DuplicateOverload, Cop::RBS::Lint::ImplicitlyReturnsNil, Cop::RBS::Lint::LiteralIntersection, Cop::RBS::Lint::NewReturnsVoid, Cop::RBS::Lint::RestKeywordHash, Cop::RBS::Lint::Syntax, Cop::RBS::Lint::TopLevelInterface, Cop::RBS::Lint::TopLevelTypeAlias, Cop::RBS::Lint::UnusedOverloadTypeParams, Cop::RBS::Lint::UnusedTypeAliasTypeParams, Cop::RBS::Lint::UselessAccessModifier, Cop::RBS::Lint::WillSyntaxError, Cop::RBS::Style::BlockReturnBoolish, Cop::RBS::Style::ClassWithSingleton, Cop::RBS::Style::ClassicType, Cop::RBS::Style::DuplicatedType, Cop::RBS::Style::EmptyArgument, Cop::RBS::Style::InitializeReturnType, Cop::RBS::Style::InstanceWithInstance, Cop::RBS::Style::OptionalNil, Cop::RBS::Style::RedundantParentheses, Cop::RBS::Style::TrueFalse

Constant Summary collapse

@@cache =
{}

Instance Attribute Summary collapse

Class Method 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

Class Method Details

.documentation_url(_config = nil) ⇒ Object



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

def self.documentation_url(_config = nil)
  base = "cops_#{department.to_s.downcase.tr('/', '_')}"
  fragment = cop_name.downcase.gsub(/[^a-z]/, '')
  "https://github.com/ksss/rubocop-on-rbs/blob/v#{VERSION}/docs/modules/ROOT/pages/#{base}.adoc##{fragment}"
end

Instance Method Details

#investigation_rbsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubocop/rbs/cop_base.rb', line 37

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



121
122
123
# File 'lib/rubocop/rbs/cop_base.rb', line 121

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

#on_new_investigationObject



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

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

#on_other_fileObject



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

def on_other_file
  investigation_rbs()
end

#on_rbs_attribute(member) ⇒ Object



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

def on_rbs_attribute(member); end

#on_rbs_class(member) ⇒ Object

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



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

def on_rbs_class(member); end

#on_rbs_constant(const) ⇒ Object



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

def on_rbs_constant(const); end

#on_rbs_def(member) ⇒ Object



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

def on_rbs_def(member); end

#on_rbs_global(global) ⇒ Object



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

def on_rbs_global(global); end

#on_rbs_interface(member) ⇒ Object



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

def on_rbs_interface(member); end

#on_rbs_module(member) ⇒ Object



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

def on_rbs_module(member); end

#on_rbs_new_investigationObject



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

def on_rbs_new_investigation; end

#on_rbs_parsing_errorObject



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

def on_rbs_parsing_error; end

#on_rbs_private(member) ⇒ Object



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

def on_rbs_private(member); end

#on_rbs_public(member) ⇒ Object



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

def on_rbs_public(member); end

#on_rbs_type_alias(decl) ⇒ Object



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

def on_rbs_type_alias(decl); end

#on_rbs_var(member) ⇒ Object



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

def on_rbs_var(member); end

#parse_rbsObject



32
33
34
35
# File 'lib/rubocop/rbs/cop_base.rb', line 32

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

#rbs_bufferObject



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

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

#tokenize(source) ⇒ Object



125
126
127
# File 'lib/rubocop/rbs/cop_base.rb', line 125

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

#walk(decl) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rubocop/rbs/cop_base.rb', line 84

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