Class: RuboCop::Cop::SketchupRequirements::ExtensionNamespace

Inherits:
RuboCop::Cop
  • Object
show all
Includes:
NoCommentDisable, RuboCop::Cop::SketchUp
Defined in:
lib/rubocop/sketchup/requirements/extension_namespace.rb

Constant Summary collapse

@@namespace =

Class variables are normally frowned upon since they leak through all instances. However, in this case this is exactly what we want. The Cop picks up the first top level namespace it encounters and then keep track of whether it detects more top level namespaces.

nil

Instance Method Summary collapse

Instance Method Details

#check_class_or_module(node) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rubocop/sketchup/requirements/extension_namespace.rb', line 23

def check_class_or_module(node)
  name = node.defined_module_name
  parent = Namespace.new(node.parent_module_name)
  # Don't want to process anything that aren't top level namespaces.
  return unless parent.top_level?
  check_namespace(node, parent.join(name))
end

#check_namespace(node, namespace) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubocop/sketchup/requirements/extension_namespace.rb', line 36

def check_namespace(node, namespace)
  # Make sure the namespace isn't part of reserved namespaces that other
  # cops are checking.
  return if reserved?(namespace)
  # Remember the first namespace encountered and log an offence if
  # more top level namespaces are registered.
  top = namespace.first
  @@namespace ||= top
  return if @@namespace == top
  add_offense(node, :name, nil, :error)
end

#message(node) ⇒ Object



56
57
58
59
# File 'lib/rubocop/sketchup/requirements/extension_namespace.rb', line 56

def message(node)
  namespace = Namespace.new(node.defined_module_name).from_root
  format('Use a single root namespace. (Found %s; Previously found %s)', namespace, @@namespace)
end

#on_class(node) ⇒ Object



15
16
17
# File 'lib/rubocop/sketchup/requirements/extension_namespace.rb', line 15

def on_class(node)
  check_class_or_module(node)
end

#on_module(node) ⇒ Object



19
20
21
# File 'lib/rubocop/sketchup/requirements/extension_namespace.rb', line 19

def on_module(node)
  check_class_or_module(node)
end

#reserved?(namespace) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/rubocop/sketchup/requirements/extension_namespace.rb', line 48

def reserved?(namespace)
  top = namespace.first
  return true if RubyCoreNamespace::NAMESPACES.include?(top)
  return true if RubyStdLibNamespace::NAMESPACES.include?(top)
  return true if ApiNamespace::NAMESPACES.include?(top)
  false
end