Class: RuboCop::Cop::SketchupSuggestions::MonkeyPatchedApi

Inherits:
SketchUp::Cop
  • Object
show all
Includes:
SketchUp::DynamicComponentMethods
Defined in:
lib/rubocop/sketchup/cop/suggestions/monkey_patched_api.rb

Overview

Some of the shipped extensions in SketchUp monkey-patch the API namespace. This is an unfortunate no-no that was done a long time ago before the extension best-practices were established. These functions might change or be removed at any time. They will also not work when the extensions are disabled. Avoid using these methods.

Constant Summary

Constants included from SketchUp::DynamicComponentMethods

SketchUp::DynamicComponentMethods::DC_METHODS

Constants included from SketchUp::Config

SketchUp::Config::DEFAULT_CONFIGURATION

Instance Method Summary collapse

Methods inherited from SketchUp::Cop

inherited, #relevant_file?

Instance Method Details

#on_send(node) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/sketchup/cop/suggestions/monkey_patched_api.rb', line 15

def on_send(node)
  # Only check instance methods.
  return if node.receiver && node.receiver.const_type?

  name = node.method_name

  dc_method = DC_METHODS.find { |m| m[:name] == name }
  return unless dc_method

  if dc_method.key?(:variables)
    return unless node.receiver && node.receiver.variable?
    receiver_name = node.receiver.children.first    # Account for instance and class variables.

    receiver_name = receiver_name.to_s.tr('@', '').to_sym
    return unless dc_method[:variables].include?(receiver_name)
  end

  path = dc_method[:path]
  message = "#{path}##{name} is not part of the official API. "\
            "It's a monkey-patched addition by Dynamic Components."
  add_offense(node, severity: :error, message: message)
end