Sha256: 992c1116ddb23fc176784ddf3697f5bca0b442dc0ca134547182e9f8d2f86cca
Contents?: true
Size: 1.56 KB
Versions: 3
Compression:
Stored size: 1.56 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module RSpec # Checks for let scattered across the example group. # # Group lets together # # @example # # bad # describe Foo do # let(:foo) { 1 } # subject { Foo } # let(:bar) { 2 } # before { prepare } # let!(:baz) { 3 } # end # # # good # describe Foo do # subject { Foo } # before { prepare } # let(:foo) { 1 } # let(:bar) { 2 } # let!(:baz) { 3 } # end # class ScatteredLet < Cop MSG = 'Group all let/let! blocks in the example group together.' def on_block(node) return unless example_group_with_body?(node) check_let_declarations(node.body) end def autocorrect(node) lambda do |corrector| first_let = find_first_let(node.parent) RuboCop::RSpec::Corrector::MoveNode.new( node, corrector, processed_source ).move_after(first_let) end end private def check_let_declarations(body) lets = body.each_child_node.select { |node| let?(node) } first_let = lets.first lets.each_with_index do |node, idx| next if node.sibling_index == first_let.sibling_index + idx add_offense(node) end end def find_first_let(node) node.children.find { |child| let?(child) } end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
rubocop-rspec-1.41.0 | lib/rubocop/cop/rspec/scattered_let.rb |
rubocop-rspec-1.40.0 | lib/rubocop/cop/rspec/scattered_let.rb |
rubocop-rspec-1.39.0 | lib/rubocop/cop/rspec/scattered_let.rb |