lib/rubocop/cop/style/command_literal.rb in rubocop-0.89.0 vs lib/rubocop/cop/style/command_literal.rb in rubocop-0.89.1
- old
+ new
@@ -73,56 +73,55 @@
# %x(echo `ls`)
#
# @example AllowInnerBackticks: true
# # good
# `echo \`ls\``
- class CommandLiteral < Cop
+ class CommandLiteral < Base
include ConfigurableEnforcedStyle
+ extend AutoCorrector
MSG_USE_BACKTICKS = 'Use backticks around command string.'
MSG_USE_PERCENT_X = 'Use `%x` around command string.'
def on_xstr(node)
return if node.heredoc?
if backtick_literal?(node)
- check_backtick_literal(node)
+ check_backtick_literal(node, MSG_USE_PERCENT_X)
else
- check_percent_x_literal(node)
+ check_percent_x_literal(node, MSG_USE_BACKTICKS)
end
end
- def autocorrect(node)
- return if contains_backtick?(node)
-
- replacement = if backtick_literal?(node)
- ['%x', ''].zip(preferred_delimiter).map(&:join)
- else
- %w[` `]
- end
-
- lambda do |corrector|
- corrector.replace(node.loc.begin, replacement.first)
- corrector.replace(node.loc.end, replacement.last)
- end
- end
-
private
- def check_backtick_literal(node)
+ def check_backtick_literal(node, message)
return if allowed_backtick_literal?(node)
- add_offense(node)
+ add_offense(node, message: message) do |corrector|
+ autocorrect(corrector, node)
+ end
end
- def check_percent_x_literal(node)
+ def check_percent_x_literal(node, message)
return if allowed_percent_x_literal?(node)
- add_offense(node)
+ add_offense(node, message: message) do |corrector|
+ autocorrect(corrector, node)
+ end
end
- def message(node)
- backtick_literal?(node) ? MSG_USE_PERCENT_X : MSG_USE_BACKTICKS
+ def autocorrect(corrector, node)
+ return if contains_backtick?(node)
+
+ replacement = if backtick_literal?(node)
+ ['%x', ''].zip(preferred_delimiter).map(&:join)
+ else
+ %w[` `]
+ end
+
+ corrector.replace(node.loc.begin, replacement.first)
+ corrector.replace(node.loc.end, replacement.last)
end
def allowed_backtick_literal?(node)
case style
when :backticks