Skip to content

Commit 0889fef

Browse files
committed
fix ruby/prefer-endless-method for heredoc and def nodes
1 parent 7d23dc5 commit 0889fef

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/ruby/prefer-endless-method.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def one_plus_one = 1 + 1
2323

2424
within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do
2525
find_node '.def_node[body!=nil][body.body.length=1]' do
26-
new_body = dedent(node.body.to_source)
26+
body_column = mutation_adapter.get_start_loc(node.body.body.first).column
27+
new_body = node.body.body.first.to_source.split("\n").map { |line| line.sub(/^ {#{body_column}}/, '') }.join("\n")
2728
replace_with "def {{name}}{{lparen}}{{parameters}}{{rparen}} = #{new_body}"
2829
end
2930
end

spec/ruby/prefer-endless-method_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,52 @@ def existing_key(device) = transaction do
3737

3838
include_examples 'convertable'
3939
end
40+
41+
context 'heredoc' do
42+
let(:test_content) { <<~EOS }
43+
def sample_gemfile_lock_content
44+
<<~GEMFILE_LOCK
45+
GEM
46+
remote: https://rubygems.org/
47+
GEMFILE_LOCK
48+
end
49+
EOS
50+
let(:test_rewritten_content) { <<~EOS }
51+
def sample_gemfile_lock_content = <<~GEMFILE_LOCK
52+
GEM
53+
remote: https://rubygems.org/
54+
GEMFILE_LOCK
55+
EOS
56+
57+
include_examples 'convertable'
58+
end
59+
60+
context 'def inside class' do
61+
let(:test_content) { <<~EOS }
62+
class User
63+
def generate_invitation_token
64+
loop do
65+
token = SecureRandom.hex(10)
66+
unless Membership.exists?(invitation_token: token)
67+
self.invitation_token = token
68+
break
69+
end
70+
end
71+
end
72+
end
73+
EOS
74+
let(:test_rewritten_content) { <<~EOS }
75+
class User
76+
def generate_invitation_token = loop do
77+
token = SecureRandom.hex(10)
78+
unless Membership.exists?(invitation_token: token)
79+
self.invitation_token = token
80+
break
81+
end
82+
end
83+
end
84+
EOS
85+
86+
include_examples 'convertable'
87+
end
4088
end

0 commit comments

Comments
 (0)