Class: RuboCop::Cop::Gusto::RedundantSpecHelperRequire
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Gusto::RedundantSpecHelperRequire
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/gusto/redundant_spec_helper_require.rb
Overview
Flags an inline require 'spec_helper' / require 'rails_helper' (or the
require_relative equivalent) that the governing .rspec already loads via
--require, making the inline require a redundant no-op.
Correctness is per file: the cop resolves the .rspec that governs the file by walking
up to the nearest ancestor .rspec, but stops at a project boundary (a directory holding
a *.gemspec or Gemfile). A project with no .rspec of its own is therefore never
attributed a parent project's .rspec -- e.g. a standalone gem/engine that boots its own
test environment keeps its inline require. Packs (no gemspec/Gemfile) still resolve to the
repo-root .rspec.
spec_helper.rb / rails_helper.rb themselves are never edited (they are the definitions
and the rails_helper -> spec_helper shim).
rails_helper is treated as redundant only when the governing .rspec auto-requires it
directly, or auto-requires spec_helper AND the project's spec/rails_helper.rb is a pure
shim (nothing but require 'spec_helper'). Otherwise it is kept, since a rails_helper
that does real setup (e.g. boots Rails) is not covered by spec_helper.
Constant Summary collapse
- MSG =
"Redundant `require '%{name}'` - the governing .rspec already `--require`s it."- HELPERS =
%w(spec_helper rails_helper).freeze
- RESTRICT_ON_SEND =
%i(require require_relative).freeze
Instance Method Summary collapse
- #on_send(node) ⇒ Object (also: #on_csend)
- #require_path(node) ⇒ Object
Instance Method Details
#on_send(node) ⇒ Object Also known as: on_csend
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rubocop/cop/gusto/redundant_spec_helper_require.rb', line 48 def on_send(node) return unless (path = require_path(node)) name = helper_name(path) return unless name return if helper_definition_file? return unless redundant?(name) add_offense(node, message: format(MSG, name:)) do |corrector| corrector.remove(removal_range(node)) end end |
#require_path(node) ⇒ Object
44 45 46 |
# File 'lib/rubocop/cop/gusto/redundant_spec_helper_require.rb', line 44 def_node_matcher :require_path, <<~PATTERN (send nil? {:require :require_relative} (str $_)) PATTERN |