Skip to content

Instantly share code, notes, and snippets.

@sebastian-palma
Created September 20, 2023 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastian-palma/157e337527c8aa9ab81dd135eb010406 to your computer and use it in GitHub Desktop.
Save sebastian-palma/157e337527c8aa9ab81dd135eb010406 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
module RuboCop
module Cop
module Custom
# Looks for the require of the rails_helper or spec_helper and raises
# a warning as those files are set to be included through the .rspec
# file.
#
# On auto correction it just deletes those lines.
#
# # bad
# require 'rails_helper'
#
# # bad
# require 'spec_helper'
class NoTestHelperRequire < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
def_node_matcher :rails_helper, <<~PATTERN
$(send nil? :require (str $/rails_helper|spec_helper/))
PATTERN
def on_send(node)
match = rails_helper(node)
return unless match
line, file = match
add_offense(node, message: format(MESSAGE, file:)) do |corrector|
corrector.replace(line, '')
end
end
MESSAGE = "No need to require %<file>s as it's added in the .rspec file."
private_constant :MESSAGE
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment