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
[Fix #9144] Add aggressive
and conservative
enforced styles for Style/StringConcatenation
cop
#9153
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#9144](https://github.com/rubocop/rubocop/issues/9144): Add `aggressive` and `conservative` modes of operation for `Style/StringConcatenation` cop. ([@tejasbubane][]) |
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 |
---|---|---|
|
@@ -15,18 +15,37 @@ module Style | |
# lines, this cop does not register an offense; instead, | ||
# `Style/LineEndConcatenation` will pick up the offense if enabled. | ||
# | ||
# @example | ||
# Two modes are supported: | ||
# 1. `aggressive` style checks and corrects all occurrences of `+` where | ||
# either the left or right side of `+` is a string literal. | ||
# 2. `conservative` style on the other hand, checks and corrects only if | ||
# left side (receiver of `+` method call) is a string literal. | ||
# This is useful when the receiver is some expression that returns string like `Pathname` | ||
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.
I think this comment should read
|
||
# instead of a string literal. | ||
# | ||
# @example Mode: aggressive (default) | ||
# # bad | ||
# email_with_name = user.name + ' <' + user.email + '>' | ||
# Pathname.new('/') + 'test' | ||
# | ||
# # good | ||
# email_with_name = "#{user.name} <#{user.email}>" | ||
# email_with_name = format('%s <%s>', user.name, user.email) | ||
# "#{Pathname.new('/')}test" | ||
# | ||
# # accepted, line-end concatenation | ||
# name = 'First' + | ||
# 'Last' | ||
# | ||
# @example Mode: conservative | ||
# # bad | ||
# 'Hello' + user.name | ||
# | ||
# # good | ||
# "Hello #{user.name}" | ||
# user.name + '!!' | ||
# Pathname.new('/') + 'test' | ||
# | ||
class StringConcatenation < Base | ||
include Util | ||
include RangeHelp | ||
|
@@ -52,10 +71,15 @@ def on_send(node) | |
return if line_end_concatenation?(node) | ||
|
||
topmost_plus_node = find_topmost_plus_node(node) | ||
parts = collect_parts(topmost_plus_node) | ||
return unless parts[0..-2].any? { |receiver_node| offensive_for_mode?(receiver_node) } | ||
|
||
parts = [] | ||
collect_parts(topmost_plus_node, parts) | ||
register_offense(topmost_plus_node, parts) | ||
end | ||
|
||
private | ||
|
||
def register_offense(topmost_plus_node, parts) | ||
add_offense(topmost_plus_node) do |corrector| | ||
correctable_parts = parts.none? { |part| uncorrectable?(part) } | ||
if correctable_parts && !corrected_ancestor?(topmost_plus_node) | ||
|
@@ -67,7 +91,10 @@ def on_send(node) | |
end | ||
end | ||
|
||
private | ||
def offensive_for_mode?(receiver_node) | ||
mode = cop_config['Mode'].to_sym | ||
mode == :aggressive || mode == :conservative && receiver_node.str_type? | ||
end | ||
|
||
def line_end_concatenation?(node) | ||
# If the concatenation happens at the end of the line, | ||
|
@@ -87,7 +114,7 @@ def find_topmost_plus_node(node) | |
current | ||
end | ||
|
||
def collect_parts(node, parts) | ||
def collect_parts(node, parts = []) | ||
return unless node | ||
|
||
if plus_node?(node) | ||
|
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
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.
You should also mention how this ties to the cop safety - e.g. with the
Pathname
example.