Class: RuboCop::Cop::RBS::Lint::TopLevelTypeAlias

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

Overview

Top-level namespaces are likely to conflict and should be avoided.

Examples:

# bad
type foo = String

# good
class Foo
  type bar = Integer
end

Constant Summary collapse

MSG =
'Top level type alias detected.'

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_constant, #on_rbs_def, #on_rbs_global, #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

#on_rbs_class(decl) ⇒ Object Also known as: on_rbs_module, on_rbs_interface



25
26
27
28
29
# File 'lib/rubocop/cop/rbs/lint/top_level_type_alias.rb', line 25

def on_rbs_class(decl)
  return unless @last_end_pos.nil? || (@last_end_pos < decl.location.end_pos)

  @last_end_pos = decl.location.end_pos
end

#on_rbs_new_investigationObject



21
22
23
# File 'lib/rubocop/cop/rbs/lint/top_level_type_alias.rb', line 21

def on_rbs_new_investigation
  @last_end = nil
end

#on_rbs_type_alias(decl) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rubocop/cop/rbs/lint/top_level_type_alias.rb', line 33

def on_rbs_type_alias(decl)
  return unless @last_end_pos.nil? || (@last_end_pos < decl.location.start_pos)
  return unless decl.name.namespace.path.empty?

  range = location_to_range(decl.location)
  add_offense(range)
end