Skip to content

Instantly share code, notes, and snippets.

@pirj
Last active August 8, 2019 22:55
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 pirj/3429c3bd28b53ede5829146c2c59363f to your computer and use it in GitHub Desktop.
Save pirj/3429c3bd28b53ede5829146c2c59363f to your computer and use it in GitHub Desktop.
RSpec/ExampleGroupArgument cop draft
RSpec.describe do
# good
describe MyClass do
end
describe 'sdfsd' do
end
describe "sfa #{dfsdf}" do
end
# questionable
describe VERSION do
end
# acceptable
describe variable do
end
# no real issue, just incorrect usage of the first argument
describe [1,2,3] do
end
describe a: 1 do
end
describe :sdfsf do
end
# bad
describe [1,2,3] do
described_class
end
describe a: 1 do
described_class
end
describe :sdfsf do
described_class
end
end
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
class ExampleGroupArgument < Cop
MSG = 'Bad first argument for example group'
def_node_matcher :example_group?,
RuboCop::RSpec::Language::ExampleGroups::ALL.block_pattern
def_node_search :dangerous_method_call?, <<-PATTERN
(send nil? { :it_behaves_like :inlcude_examples :described_class })
PATTERN
def on_block(node)
return unless example_group?(node)
return unless node.send_node.arguments? # though not having any arguments is bad, too
first_argument = node.send_node.arguments.first
return unless offensive_first_argument?(first_argument)
add_offense(node) if dangerous_method_call?(node)
end
def offensive_first_argument?(node)
# local variables are also bad, as they are an indication of
# https://rspec.rubystyle.guide/#it-in-iterators
!(node.str_type? || node.dstr_type? || node.const_type? || node.lvar_type? || node.send_type?)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment