class RuboCop::Cop::Style::CommandLiteral
Enforces using “ or %x around command literals.
@example EnforcedStyle: backticks (default)
# bad folders = %x(find . -type d).split # bad %x( ln -s foo.example.yml foo.example ln -s bar.example.yml bar.example ) # good folders = `find . -type d`.split # good ` ln -s foo.example.yml foo.example ln -s bar.example.yml bar.example `
@example EnforcedStyle: mixed
# bad folders = %x(find . -type d).split # bad ` ln -s foo.example.yml foo.example ln -s bar.example.yml bar.example ` # good folders = `find . -type d`.split # good %x( ln -s foo.example.yml foo.example ln -s bar.example.yml bar.example )
@example EnforcedStyle: percent_x
# bad folders = `find . -type d`.split # bad ` ln -s foo.example.yml foo.example ln -s bar.example.yml bar.example ` # good folders = %x(find . -type d).split # good %x( ln -s foo.example.yml foo.example ln -s bar.example.yml bar.example )
@example AllowInnerBackticks: false (default)
# If `false`, the cop will always recommend using `%x` if one or more # backticks are found in the command string. # bad `echo \`ls\`` # good %x(echo `ls`)
@example AllowInnerBackticks: true
# good `echo \`ls\``
Constants
- MSG_USE_BACKTICKS
- MSG_USE_PERCENT_X
Public Instance Methods
on_xstr(node)
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 85 def on_xstr(node) return if node.heredoc? if backtick_literal?(node) check_backtick_literal(node, MSG_USE_PERCENT_X) else check_percent_x_literal(node, MSG_USE_BACKTICKS) end end
Private Instance Methods
allow_inner_backticks?()
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 146 def allow_inner_backticks? cop_config['AllowInnerBackticks'] end
allowed_backtick_literal?(node)
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 122 def allowed_backtick_literal?(node) case style when :backticks !contains_disallowed_backtick?(node) when :mixed node.single_line? && !contains_disallowed_backtick?(node) end end
allowed_percent_x_literal?(node)
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 131 def allowed_percent_x_literal?(node) case style when :backticks contains_disallowed_backtick?(node) when :mixed node.multiline? || contains_disallowed_backtick?(node) when :percent_x true end end
autocorrect(corrector, node)
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 109 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
backtick_literal?(node)
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 159 def backtick_literal?(node) node.loc.begin.source == '`' end
check_backtick_literal(node, message)
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 97 def check_backtick_literal(node, message) return if allowed_backtick_literal?(node) add_offense(node, message: message) { |corrector| autocorrect(corrector, node) } end
check_percent_x_literal(node, message)
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 103 def check_percent_x_literal(node, message) return if allowed_percent_x_literal?(node) add_offense(node, message: message) { |corrector| autocorrect(corrector, node) } end
command_delimiter()
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 167 def command_delimiter preferred_delimiters_config['%x'] end
contains_backtick?(node)
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 150 def contains_backtick?(node) /`/.match?(node_body(node)) end
contains_disallowed_backtick?(node)
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 142 def contains_disallowed_backtick?(node) !allow_inner_backticks? && contains_backtick?(node) end
default_delimiter()
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 171 def default_delimiter preferred_delimiters_config['default'] end
node_body(node)
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 154 def node_body(node) loc = node.loc loc.expression.source[loc.begin.length...-loc.end.length] end
preferred_delimiter()
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 163 def preferred_delimiter (command_delimiter || default_delimiter).chars end
preferred_delimiters_config()
click to toggle source
# File lib/rubocop/cop/style/command_literal.rb, line 175 def preferred_delimiters_config config.for_cop('Style/PercentLiteralDelimiters') ['PreferredDelimiters'] end