Move comments in tandem with the lines they belong to
This commit is contained in:
parent
dae9b0cd53
commit
779f1bba7d
@ -26,39 +26,42 @@ module RuboCop
|
|||||||
|
|
||||||
next if array.children.length <= 1
|
next if array.children.length <= 1
|
||||||
|
|
||||||
comments = find_inline_comments(array.source)
|
sorted_array = sort_array(array.source.split("\n")).join("\n")
|
||||||
array_with_comments = array.children.dup
|
|
||||||
array.children.map(&:source).each_with_index do |child, index|
|
|
||||||
comment = comments.find { |c| c.include?(child) }
|
|
||||||
next unless comment
|
|
||||||
|
|
||||||
p comment.strip
|
next if array.source == sorted_array
|
||||||
# Add the comment to the main array.
|
|
||||||
array_with_comments[index] = comment.strip
|
|
||||||
end
|
|
||||||
|
|
||||||
sorted_array = array_with_comments.sort_by { |child| child.to_s.downcase }
|
|
||||||
next if sorted_array == array_with_comments
|
|
||||||
|
|
||||||
add_offense(array, message: "The array elements should be ordered alphabetically") do |corrector|
|
add_offense(array, message: "The array elements should be ordered alphabetically") do |corrector|
|
||||||
array.children.each_with_index do |child, index|
|
corrector.replace(array.source_range, sorted_array)
|
||||||
p sorted_array[index]
|
|
||||||
corrector.replace(child.source_range, sorted_array[index])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_inline_comments(source)
|
def sort_array(source)
|
||||||
comments = []
|
# Combine each comment with the line below it so that they remain connected to the line they comment
|
||||||
source.each_line do |line|
|
combined_source = source.each_with_index.map do |line, index|
|
||||||
# Comments are naively detected by looking for lines that include a `#` surrounded by spaces.
|
if line.strip.start_with?('#') && index < source.length - 1
|
||||||
comments << line if line.include?(" # ")
|
"#{line}\n#{source[index + 1]}"
|
||||||
|
elsif source[index - 1]&.strip&.start_with?('#')
|
||||||
|
nil
|
||||||
|
else
|
||||||
|
line
|
||||||
|
end
|
||||||
|
end.compact
|
||||||
|
|
||||||
|
# Separate the lines into those that should be sorted and those that should not
|
||||||
|
# ie. skip the opening and closing brackets of the array
|
||||||
|
to_sort, to_keep = combined_source.partition { |line| !line.include?('[') && !line.include?(']') }
|
||||||
|
|
||||||
|
# Sort the lines that should be sorted
|
||||||
|
to_sort.sort! do |a, b|
|
||||||
|
a_non_comment = a.split("\n").reject { |line| line.strip.start_with?('#') }.first
|
||||||
|
b_non_comment = b.split("\n").reject { |line| line.strip.start_with?('#') }.first
|
||||||
|
a_non_comment.downcase <=> b_non_comment.downcase
|
||||||
end
|
end
|
||||||
|
|
||||||
# Remove lines that are only comments, we don't want to move those.
|
# Merge the sorted lines and the unsorted lines, preserving the original positions of the unsorted lines
|
||||||
comments.reject { |comment| comment.strip.start_with?("# ") }
|
combined_source.map { |line| to_keep.include?(line) ? line : to_sort.shift }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -137,14 +137,14 @@ describe RuboCop::Cop::Cask::ArrayAlphabetization, :config do
|
|||||||
CASK
|
CASK
|
||||||
end
|
end
|
||||||
|
|
||||||
focus it "moves comments when autocorrecting" do
|
it "moves comments when autocorrecting" do
|
||||||
expect_offense(<<~CASK)
|
expect_offense(<<~CASK)
|
||||||
cask "foo" do
|
cask "foo" do
|
||||||
url "https://example.com/foo.zip"
|
url "https://example.com/foo.zip"
|
||||||
|
|
||||||
zap trash: [
|
zap trash: [
|
||||||
^ The array elements should be ordered alphabetically
|
^ The array elements should be ordered alphabetically
|
||||||
# overall comment, shouldn't move
|
# comment related to foo
|
||||||
"~/Library/Application Support/Foo",
|
"~/Library/Application Support/Foo",
|
||||||
"~/Library/Application Support/Bar",
|
"~/Library/Application Support/Bar",
|
||||||
"~/Library/Application Support/Baz", # in-line comment
|
"~/Library/Application Support/Baz", # in-line comment
|
||||||
@ -157,9 +157,9 @@ describe RuboCop::Cop::Cask::ArrayAlphabetization, :config do
|
|||||||
url "https://example.com/foo.zip"
|
url "https://example.com/foo.zip"
|
||||||
|
|
||||||
zap trash: [
|
zap trash: [
|
||||||
# overall comment, shouldn't move
|
|
||||||
"~/Library/Application Support/Bar",
|
"~/Library/Application Support/Bar",
|
||||||
"~/Library/Application Support/Baz", # in-line comment
|
"~/Library/Application Support/Baz", # in-line comment
|
||||||
|
# comment related to foo
|
||||||
"~/Library/Application Support/Foo",
|
"~/Library/Application Support/Foo",
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user