Class: RuboCop::Cop::Gemspec::DevelopmentDependencies

Inherits:
Base
  • Object
show all
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/gemspec/development_dependencies.rb

Overview

Enforce that development dependencies for a gem are specified in ‘Gemfile`, rather than in the `gemspec` using `add_development_dependency`. Alternatively, using `EnforcedStyle: gemspec`, enforce that all dependencies are specified in `gemspec`, rather than in `Gemfile`.

Examples:

EnforcedStyle: Gemfile (default)

# Specify runtime dependencies in your gemspec,
# but all other dependencies in your Gemfile.

# bad
# example.gemspec
s.add_development_dependency "foo"

# good
# Gemfile
gem "foo"

# good
# gems.rb
gem "foo"

# good (with AllowedGems: ["bar"])
# example.gemspec
s.add_development_dependency "bar"

EnforcedStyle: gems.rb

# Specify runtime dependencies in your gemspec,
# but all other dependencies in your Gemfile.
#
# Identical to `EnforcedStyle: Gemfile`, but with a different error message.
# Rely on Bundler/GemFilename to enforce the use of `Gemfile` vs `gems.rb`.

# bad
# example.gemspec
s.add_development_dependency "foo"

# good
# Gemfile
gem "foo"

# good
# gems.rb
gem "foo"

# good (with AllowedGems: ["bar"])
# example.gemspec
s.add_development_dependency "bar"

EnforcedStyle: gemspec

# Specify all dependencies in your gemspec.

# bad
# Gemfile
gem "foo"

# good
# example.gemspec
s.add_development_dependency "foo"

# good (with AllowedGems: ["bar"])
# Gemfile
gem "bar"

Constant Summary collapse

MSG =
'Specify development dependencies in %<preferred>s.'
RESTRICT_ON_SEND =
%i[add_development_dependency gem].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from ConfigurableEnforcedStyle

#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_configured?, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#add_development_dependency?(node) ⇒ Object



77
78
79
# File 'lib/rubocop/cop/gemspec/development_dependencies.rb', line 77

def_node_matcher :add_development_dependency?, <<~PATTERN
  (send _ :add_development_dependency (str #forbidden_gem? ...) _? _?)
PATTERN

#gem?(node) ⇒ Object



82
83
84
# File 'lib/rubocop/cop/gemspec/development_dependencies.rb', line 82

def_node_matcher :gem?, <<~PATTERN
  (send _ :gem (str #forbidden_gem? ...))
PATTERN

#on_send(node) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/rubocop/cop/gemspec/development_dependencies.rb', line 86

def on_send(node)
  case style
  when :Gemfile, :'gems.rb'
    add_offense(node) if add_development_dependency?(node)
  when :gemspec
    add_offense(node) if gem?(node)
  end
end