Class: RuboCop::Cop::SketchupRequirements::ExtensionNamespace
Overview
Extensions in SketchUp all share the same Ruby environment on the user’s machine. Because of this it’s important that each extension isolate itself to avoid clashing with other extensions.
Extensions submitted to Extension Warehouse is expected to use only one root module.
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
SketchUp::Cop::SKETCHUP_DEPARTMENT_SEVERITY
SketchUp::Config::DEFAULT_CONFIGURATION
Instance Method Summary
collapse
inherited, #relevant_file?
Instance Method Details
#check_class_or_module(node) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/rubocop/sketchup/cop/requirements/extension_namespace.rb', line 47
def check_class_or_module(node)
name = node.defined_module_name
if node.parent_module_name
parent = Namespace.new(node.parent_module_name)
else
parent = Namespace.new('Object')
end
namespace = parent.join(name)
return unless parent.top_level?
return if exempted?(namespace)
check_namespace(node, namespace)
end
|
#check_namespace(node, namespace) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/rubocop/sketchup/cop/requirements/extension_namespace.rb', line 81
def check_namespace(node, namespace)
return if reserved?(namespace)
top = namespace.first
@@namespace ||= top
return if @@namespace == top
add_offense(node, location: :name)
end
|
#exempted?(namespace) ⇒ Boolean
111
112
113
|
# File 'lib/rubocop/sketchup/cop/requirements/extension_namespace.rb', line 111
def exempted?(namespace)
namespace_exceptions.include?(namespace.first)
end
|
#message(node) ⇒ Object
104
105
106
107
108
109
|
# File 'lib/rubocop/sketchup/cop/requirements/extension_namespace.rb', line 104
def message(node)
namespace = Namespace.new(node.defined_module_name).from_root
format('Use a single root namespace. '\
'(Found `%<found>s`; Previously found `%<expected>s`)',
found: namespace, expected: @@namespace)
end
|
#namespace_exceptions ⇒ Object
115
116
117
118
119
120
|
# File 'lib/rubocop/sketchup/cop/requirements/extension_namespace.rb', line 115
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
39
40
41
|
# File 'lib/rubocop/sketchup/cop/requirements/extension_namespace.rb', line 39
def on_class(node)
check_class_or_module(node)
end
|
#on_module(node) ⇒ Object
43
44
45
|
# File 'lib/rubocop/sketchup/cop/requirements/extension_namespace.rb', line 43
def on_module(node)
check_class_or_module(node)
end
|
#reserved?(namespace) ⇒ Boolean