class RuboCop::Cop::RSpec::LetBeforeExamples
end
expect(some).to be
it ‘checks what some does’ do
end
expect(foo).to be
it ‘checks what foo does’ do
let(:some) { other }
let(:foo) { bar }
# Good
end
expect(some).to be
it ‘checks what some does’ do
let(:some) { other }
end
expect(foo).to be
it ‘checks what foo does’ do
let(:foo) { bar }
# Bad
@example
Checks for ‘let` definitions that come after an example.
def autocorrect(node)
def autocorrect(node) lambda do |corrector| first_example = find_first_example(node.parent) first_example_pos = first_example.loc.expression indent = "\n" + ' ' * first_example.loc.column corrector.insert_before(first_example_pos, source(node) + indent) corrector.remove(node_range_with_surrounding_space(node)) end end
def check_let_declarations(node)
def check_let_declarations(node) first_example = find_first_example(node) return unless first_example node.each_child_node do |child| next if child.sibling_index < first_example.sibling_index add_offense(child, location: :expression) if let?(child) end end
def find_first_example(node)
def find_first_example(node) node.children.find { |sibling| example_or_group?(sibling) } end
def multiline_block?(block)
def multiline_block?(block) block.begin_type? end
def node_range(node)
def node_range(node) range_between( node.loc.expression.begin_pos, final_end_location(node).end_pos ) end
def node_range_with_surrounding_space(node)
def node_range_with_surrounding_space(node) range = node_range(node) range_by_whole_lines(range, include_final_newline: true) end
def on_block(node)
def on_block(node) return unless example_group_with_body?(node) check_let_declarations(node.body) if multiline_block?(node.body) end
def source(node)
def source(node) node_range(node).source end