Class: RuboCop::Cop::SketchupRequirements::MinimalRegistration

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

Overview

Don’t load extension files in the root file registering the extension. Extensions should not load additional files when it’s disabled.

Examples:

Bad - Extension will always load.

module Example
  require 'example/main' # BAD! This will load even when extension
                         #      is disabled.
  unless file_loaded?(__FILE__)
    extension = SketchupExtension.new('Hello World', 'example/main')
    Sketchup.register_extension(extension, true)
    file_loaded(__FILE__)
  end
end

Good - Extension doesn’t load anything unless its enabled.

module Example
  unless file_loaded?(__FILE__)
    extension = SketchupExtension.new('Hello World', 'example/main')
    Sketchup.register_extension(extension, true)
    file_loaded(__FILE__)
  end
end

Constant Summary collapse

MSG =
"Don't load extension files in the root file registering the "\
'extension.'

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

inherited, #relevant_file?

Instance Method Details

#extension_file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/rubocop/sketchup/cop/requirements/minimal_registration.rb', line 53

def extension_file?(filename)
  return false unless filename.include?('/')

  first_directory = filename.split('/').first
  @extension_basename.casecmp(first_directory) == 0
end

#investigate(processed_source) ⇒ Object



46
47
48
49
50
51
# File 'lib/rubocop/sketchup/cop/requirements/minimal_registration.rb', line 46

def investigate(processed_source)
  if root_file?(processed_source)
    filename = processed_source.buffer.name
    @extension_basename = File.basename(filename, '.*')
  end
end

#on_send(node) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/rubocop/sketchup/cop/requirements/minimal_registration.rb', line 60

def on_send(node)
  return unless @extension_basename

  filename = require_filename(node)
  return if filename.nil?
  return unless extension_file?(filename)

  add_offense(node)
end