Class: RuboCop::Cop::SketchupRequirements::SketchupExtension
- Inherits:
-
SketchUp::Cop
- Object
- SketchUp::WorkaroundCop
- SketchUp::Cop
- RuboCop::Cop::SketchupRequirements::SketchupExtension
- Defined in:
- lib/rubocop/sketchup/cop/requirements/sketchup_extension.rb
Overview
Register a single instance of SketchupExtension per extension. This should be done by the root .rb file in the extension package.
Constant Summary collapse
- MSG =
rubocop:disable Layout/LineLength
'Create and register one SketchupExtension instance per extension.'
- MSG_CREATE_ONE =
'Create only SketchupExtension instance per extension.'
- MSG_CREATE_MISSING =
'SketchupExtension.new not found.'
- MSG_REGISTER_ONE =
'Only register one SketchupExtension instance per extension.'
- MSG_REGISTER_MISSING =
'Registration of SketchupExtension not found. Expected %s'
Constants included from SketchUp::Config
SketchUp::Config::DEFAULT_CONFIGURATION
Constants inherited from SketchUp::Cop
SketchUp::Cop::SKETCHUP_DEPARTMENT_SEVERITY
Instance Method Summary collapse
Methods included from SketchUp::ExtensionProject
#config_path, #extension_directory, #extension_root_file, #extension_root_files, #path_relative_to_source, #relative_source_path, #root_file?, #source_path
Methods inherited from SketchUp::Cop
Instance Method Details
#investigate(processed_source) ⇒ Object
44 45 46 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rubocop/sketchup/cop/requirements/sketchup_extension.rb', line 44 def investigate(processed_source) return unless root_file?(processed_source) source_node = processed_source.ast # Using range similar to RuboCop::Cop::Naming::Filename (file_name.rb) range = source_range(processed_source.buffer, 1, 0) # Look for SketchupExtension.new. extension_nodes = sketchup_extension_new(source_node).to_a # Threat instances not assigned to anything as non-existing. extension_nodes.select! { |node| node.parent&.assignment? } # There should not be multiple instances. if extension_nodes.size > 1 add_offense(nil, location: range, message: MSG_CREATE_ONE) return end # There should be exactly one. extension_node = extension_nodes.first if extension_node.nil? add_offense(nil, location: range, message: MSG_CREATE_MISSING) return end # Ensure it have two arguments. if extension_node.arguments.size < 2 = if extension_node.arguments.size == 1 'Missing second argument for the path' else 'Missing required name arguments' end add_offense(extension_node, message: ) return end # Find the name of the value SketchupExtension.new was assigned to. assignment_node = extension_node.parent if assignment_node.casgn_type? extension_var = assignment_node.to_a[1] else extension_var = assignment_node.to_a[0] end # Look for Sketchup.register and make sure it register the extension # object detected earlier. registered_vars = sketchup_register_extension(source_node).to_a # Make sure there is only one call to `register_extension`. if registered_vars.size > 1 add_offense(registered_vars[1], message: MSG_REGISTER_ONE) return end registered_var = sketchup_register_extension(source_node).first unless registered_var == extension_var msg = MSG_REGISTER_MISSING % extension_var.to_s add_offense(nil, location: range, message: msg) end end |