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

Inherits:
SketchUp::Cop
  • Object
show all
Includes:
SketchUp, SketchUp::NoCommentDisable
Defined in:
lib/rubocop/sketchup/cop/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

Constants included from SketchUp

SketchUp::CONFIG, SketchUp::VERSION

Constants included from SketchUp::Config

SketchUp::Config::DEFAULT_CONFIGURATION

Instance Method Summary collapse

Methods inherited from SketchUp::Cop

inherited, #relevant_file?

Instance Method Details

#check_class_or_module(node) ⇒ Object



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

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

#check_namespace(node, namespace) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/sketchup/cop/requirements/extension_namespace.rb', line 39

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, location: :name, severity: :error)
end

#exempted?(namespace) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/rubocop/sketchup/cop/requirements/extension_namespace.rb', line 64

def exempted?(namespace)
  namespace_exceptions.include?(namespace.first)
end

#message(node) ⇒ Object



59
60
61
62
# File 'lib/rubocop/sketchup/cop/requirements/extension_namespace.rb', line 59

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

#namespace_exceptionsObject



68
69
70
71
72
73
# File 'lib/rubocop/sketchup/cop/requirements/extension_namespace.rb', line 68

def namespace_exceptions
  exceptions = cop_config['Exceptions'] || []
  return exceptions if exceptions.is_a?(Array)

  raise 'exceptions needs to be an array of strings!'
end

#on_class(node) ⇒ Object



15
16
17
# File 'lib/rubocop/sketchup/cop/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/cop/requirements/extension_namespace.rb', line 19

def on_module(node)
  check_class_or_module(node)
end

#reserved?(namespace) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/rubocop/sketchup/cop/requirements/extension_namespace.rb', line 51

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