Class: RuboCop::Cop::RBS::Lint::WillSyntaxError

Inherits:
RBS::CopBase
  • Object
show all
Defined in:
lib/rubocop/cop/rbs/lint/will_syntax_error.rb

Overview

This cop checks the WillSyntaxError in RBS. RBS with this diagnostics will fail in ‘rbs validate` command.

Examples:

# bad
def foo: (void) -> void

# bad
CONST: self

Constant Summary collapse

AST =
::RBS::AST
Types =
::RBS::Types

Instance Attribute Summary

Attributes inherited from RBS::CopBase

#processed_rbs_source

Instance Method Summary collapse

Methods inherited from RBS::CopBase

documentation_url, #investigation_rbs, #location_to_range, #on_new_investigation, #on_other_file, #on_rbs_attribute, #on_rbs_def, #on_rbs_new_investigation, #on_rbs_parsing_error, #on_rbs_private, #on_rbs_public, #on_rbs_var, #parse_rbs, #rbs_buffer, #tokenize, #walk

Methods included from RBS::OnTypeHelper

#on_not_type, #on_type

Instance Method Details

#check_module_or_class(decl) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rubocop/cop/rbs/lint/will_syntax_error.rb', line 67

def check_module_or_class(decl)
  check_type_params(decl)

  decl.each_member do |member|
    case member
    when AST::Members::MethodDefinition
      member.overloads.each do |ov|
        void_type_context_validator(ov.method_type)
      end
    when AST::Members::Attribute
      void_type_context_validator(member.type)
    when AST::Members::Mixin
      member.args.each do |arg|
        no_self_type_validator(arg)
        unless arg.is_a?(Types::Bases::Void)
          void_type_context_validator(arg, true)
        end
      end
    when AST::Members::Var
      void_type_context_validator(member.type)
      if member.is_a?(AST::Members::ClassVariable)
        no_self_type_validator(member.type)
      end
    end
  end
end

#check_type_params(decl) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubocop/cop/rbs/lint/will_syntax_error.rb', line 49

def check_type_params(decl)
  decl.type_params.each do |param|
    if ub = param.upper_bound
      void_type_context_validator(ub)
      no_self_type_validator(ub)
      no_classish_type_validator(ub)
    end

    if param.respond_to?(:default_type)
      if dt = param.default_type
        void_type_context_validator(dt, true)
        no_self_type_validator(dt)
        no_classish_type_validator(dt)
      end
    end
  end
end

#on_rbs_class(decl) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/rbs/lint/will_syntax_error.rb', line 27

def on_rbs_class(decl)
  if super_class = decl.super_class
    super_class.args.each do |arg|
      void_type_context_validator(arg, true)
      no_self_type_validator(arg)
      no_classish_type_validator(arg)
    end
  end
  check_module_or_class(decl)
end

#on_rbs_constant(decl) ⇒ Object Also known as: on_rbs_global



108
109
110
111
112
# File 'lib/rubocop/cop/rbs/lint/will_syntax_error.rb', line 108

def on_rbs_constant(decl)
  no_self_type_validator(decl.type)
  no_classish_type_validator(decl.type)
  void_type_context_validator(decl.type)
end

#on_rbs_interface(decl) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rubocop/cop/rbs/lint/will_syntax_error.rb', line 94

def on_rbs_interface(decl)
  check_type_params(decl)

  decl.members.each do |member|
    case member
    when AST::Members::MethodDefinition
      member.overloads.each do |ov|
        void_type_context_validator(ov.method_type)
        no_classish_type_validator(ov.method_type)
      end
    end
  end
end

#on_rbs_module(decl) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/rbs/lint/will_syntax_error.rb', line 38

def on_rbs_module(decl)
  decl.self_types.each do |self_type|
    self_type.args.each do |arg|
      void_type_context_validator(arg, true)
      no_self_type_validator(arg)
      no_classish_type_validator(arg)
    end
  end
  check_module_or_class(decl)
end

#on_rbs_type_alias(decl) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/rubocop/cop/rbs/lint/will_syntax_error.rb', line 115

def on_rbs_type_alias(decl)
  no_self_type_validator(decl.type)
  no_classish_type_validator(decl.type)
  void_type_context_validator(decl.type)

  check_type_params(decl)
end