-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Style/RedundantHeredocDelimiterQuotes
cop
Follow up #11522 (comment). This cop checks for redundant heredoc delimiter quotes. ```ruby # bad do_something(<<~'EOS') no string interpolation style text EOS # good do_something(<<~EOS) no string interpolation style text EOS ```
- Loading branch information
Showing
35 changed files
with
392 additions
and
173 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changelog/new_add_new_style_redundant_heredoc_delimiter_quotes_cop.md
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 @@ | ||
* [#11528](https://github.com/rubocop/rubocop/pull/11528): Add new `Style/RedundantHeredocDelimiterQuotes` cop. ([@koic][]) |
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
57 changes: 57 additions & 0 deletions
57
lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# Checks for redundant heredoc delimiter quotes. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# do_something(<<~'EOS') | ||
# no string interpolation style text | ||
# EOS | ||
# | ||
# # good | ||
# do_something(<<~EOS) | ||
# no string interpolation style text | ||
# EOS | ||
# | ||
# do_something(<<~'EOS') | ||
# #{string_interpolation_style_text_not_evaluated} | ||
# EOS | ||
# | ||
# do_something(<<~'EOS') | ||
# escaped character\. | ||
# EOS | ||
# | ||
class RedundantHeredocDelimiterQuotes < Base | ||
include Heredoc | ||
extend AutoCorrector | ||
|
||
MSG = 'Remove the redundant heredoc delimiter quotes, use `%<replacement>s` instead.' | ||
STRING_INTERPOLATION_OR_ESCAPED_CHARACTER_PATTERN = /#(\{|@|\$)|\\/.freeze | ||
|
||
def on_heredoc(node) | ||
return if need_heredoc_delimiter_quotes?(node) | ||
|
||
replacement = "#{heredoc_type(node)}#{delimiter_string(node)}" | ||
|
||
add_offense(node, message: format(MSG, replacement: replacement)) do |corrector| | ||
corrector.replace(node, replacement) | ||
end | ||
end | ||
|
||
private | ||
|
||
def need_heredoc_delimiter_quotes?(node) | ||
heredoc_delimiter = node.source.delete(heredoc_type(node)) | ||
return true unless heredoc_delimiter.start_with?("'", '"') | ||
|
||
node.loc.heredoc_end.source.strip.match?(/\W/) || | ||
node.loc.heredoc_body.source.match?(STRING_INTERPOLATION_OR_ESCAPED_CHARACTER_PATTERN) | ||
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
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
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
Oops, something went wrong.