Created
September 20, 2023 10:21
-
-
Save sebastian-palma/157e337527c8aa9ab81dd135eb010406 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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