New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scattered let autocorrect #903
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module RSpec | ||
module Corrector | ||
# Helper methods to move a node | ||
class MoveNode | ||
include RuboCop::Cop::RangeHelp | ||
include RuboCop::RSpec::FinalEndLocation | ||
|
||
attr_reader :original, :corrector, :processed_source | ||
|
||
def initialize(node, corrector, processed_source) | ||
@original = node | ||
@corrector = corrector | ||
@processed_source = processed_source # used by RangeHelp | ||
end | ||
|
||
def move_before(other) # rubocop:disable Metrics/AbcSize | ||
position = other.loc.expression | ||
indent = "\n" + ' ' * other.loc.column | ||
|
||
corrector.insert_before(position, source(original) + indent) | ||
corrector.remove(node_range_with_surrounding_space(original)) | ||
end | ||
|
||
def move_after(other) | ||
position = final_end_location(other) | ||
indent = "\n" + ' ' * other.loc.column | ||
|
||
corrector.insert_after(position, indent + source(original)) | ||
corrector.remove(node_range_with_surrounding_space(original)) | ||
end | ||
|
||
private | ||
|
||
def source(node) | ||
node_range(node).source | ||
end | ||
|
||
def node_range(node) | ||
node.loc.expression.with(end_pos: final_end_location(node).end_pos) | ||
end | ||
|
||
def node_range_with_surrounding_space(node) | ||
range = node_range(node) | ||
range_by_whole_lines(range, include_final_newline: true) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,16 +7,86 @@ | |
expect_offense(<<-RUBY) | ||
RSpec.describe User do | ||
let(:a) { a } | ||
subject { User } | ||
it { expect(subject.foo).to eq(a) } | ||
let(:b) { b } | ||
^^^^^^^^^^^^^ Group all let/let! blocks in the example group together. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<-RUBY) | ||
RSpec.describe User do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to stress on |
||
let(:a) { a } | ||
let(:b) { b } | ||
it { expect(subject.foo).to eq(a) } | ||
end | ||
RUBY | ||
end | ||
|
||
it 'works with heredocs' do | ||
expect_offense(<<-RUBY) | ||
describe User do | ||
let(:a) { <<-BAR } | ||
hello | ||
world | ||
BAR | ||
it { expect(subject.foo).to eq(a) } | ||
let(:b) { <<-BAZ } | ||
^^^^^^^^^^^^^^^^^^ Group all let/let! blocks in the example group together. | ||
again | ||
BAZ | ||
end | ||
RUBY | ||
|
||
expect_correction(<<-RUBY) | ||
describe User do | ||
let(:a) { <<-BAR } | ||
hello | ||
world | ||
BAR | ||
let(:b) { <<-BAZ } | ||
again | ||
BAZ | ||
it { expect(subject.foo).to eq(a) } | ||
end | ||
RUBY | ||
end | ||
|
||
it 'flags `let` at different nesting levels' do | ||
expect_offense(<<-RUBY) | ||
describe User do | ||
let(:a) { a } | ||
it { expect(subject.foo).to eq(a) } | ||
|
||
describe '#property' do | ||
let(:c) { c } | ||
|
||
it { expect(subject.property).to eq c } | ||
|
||
let(:d) { d } | ||
^^^^^^^^^^^^^ Group all let/let! blocks in the example group together. | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<-RUBY) | ||
describe User do | ||
let(:a) { a } | ||
it { expect(subject.foo).to eq(a) } | ||
|
||
describe '#property' do | ||
let(:c) { c } | ||
let(:d) { d } | ||
|
||
it { expect(subject.property).to eq c } | ||
|
||
end | ||
end | ||
RUBY | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just in case, maybe add an example for |
||
|
||
it 'doesnt flag `let!` in the middle of multiple `let`s' do | ||
expect_no_offenses(<<-RUBY) | ||
RSpec.describe User do | ||
describe User do | ||
subject { User } | ||
|
||
let(:a) { a } | ||
|
@@ -26,14 +96,41 @@ | |
RUBY | ||
end | ||
|
||
it 'flags scattered `let!`s' do | ||
expect_offense(<<-RUBY) | ||
describe User do | ||
let!(:a) { a } | ||
it { expect(subject.foo).to eq(a) } | ||
let!(:c) { c } | ||
^^^^^^^^^^^^^^ Group all let/let! blocks in the example group together. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<-RUBY) | ||
describe User do | ||
let!(:a) { a } | ||
let!(:c) { c } | ||
it { expect(subject.foo).to eq(a) } | ||
end | ||
RUBY | ||
end | ||
|
||
it 'flags `let` with proc argument' do | ||
expect_offense(<<-RUBY) | ||
RSpec.describe User do | ||
describe User do | ||
let(:a) { a } | ||
subject { User } | ||
it { expect(subject.foo).to eq(a) } | ||
let(:user, &args[:build_user]) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Group all let/let! blocks in the example group together. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<-RUBY) | ||
describe User do | ||
let(:a) { a } | ||
let(:user, &args[:build_user]) | ||
it { expect(subject.foo).to eq(a) } | ||
end | ||
RUBY | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏