Skip to content

Commit

Permalink
New cop UnneededPercentX
Browse files Browse the repository at this point in the history
Part of rubocop#835.
  • Loading branch information
jonas054 committed Apr 30, 2014
1 parent 8e67b3e commit 62e86ef
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Add new cop `EachWithObject` to prefer `each_with_object` over `inject` or `reduce`. ([@geniou][])
* [#1010](https://github.com/bbatsov/rubocop/issues/1010): New Cop `Next` check for conditions at the end of an interation and propose to use `next` instead. ([@geniou][])
* The `GuardClause` cop now also looks for unless and it is configurable how many lines the body of an if / unless needs to have to not be ignored. ([@geniou][])
* [#835](https://github.com/bbatsov/rubocop/issues/835): New cop `UnneededPercentX` checks for `%x` when backquotes would do. ([@jonas054][])

### Changes

Expand Down
4 changes: 4 additions & 0 deletions config/enabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,10 @@ UnneededCapitalW:
Description: 'Checks for %W when interpolation is not needed.'
Enabled: true

UnneededPercentX:
Description: 'Checks for %x when `` would do.'
Enabled: true

VariableInterpolation:
Description: >-
Don't interpolate global, instance and class variables
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
require 'rubocop/cop/style/trivial_accessors'
require 'rubocop/cop/style/unless_else'
require 'rubocop/cop/style/unneeded_capital_w'
require 'rubocop/cop/style/unneeded_percent_x'
require 'rubocop/cop/style/variable_interpolation'
require 'rubocop/cop/style/variable_name'
require 'rubocop/cop/style/when_then'
Expand Down
26 changes: 26 additions & 0 deletions lib/rubocop/cop/style/unneeded_percent_x.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# encoding: utf-8

module Rubocop
module Cop
module Style
# This cop checks for usage of the %x() syntax when `` would do.
class UnneededPercentX < Cop
MSG = 'Do not use `%x` unless the command string contains backquotes.'

def on_xstr(node)
add_offense(node, :expression) if node.loc.expression.source !~ /`/
end

private

def autocorrect(node)
string, = *node
@corrections << lambda do |corrector|
corrector.replace(node.loc.expression,
"`#{string.loc.expression.source}`")
end
end
end
end
end
end
34 changes: 34 additions & 0 deletions spec/rubocop/cop/style/unneeded_percent_x_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# encoding: utf-8

require 'spec_helper'

describe Rubocop::Cop::Style::UnneededPercentX do
subject(:cop) { described_class.new }

it 'registers an offense for a %x string without backquotes' do
inspect_source(cop, '%x(ls)')
expect(cop.messages)
.to eq(['Do not use `%x` unless the command string contains ' \
'backquotes.'])
end

it 'accepts a %x string with backquotes' do
inspect_source(cop, '%x(echo `ls`)')
expect(cop.offenses).to be_empty
end

it 'accepts a `` string without inner backquotes' do
inspect_source(cop, '`ls`')
expect(cop.offenses).to be_empty
end

it 'accepts a `` string with inner backquotes' do
inspect_source(cop, '`echo \`ls\``')
expect(cop.offenses).to be_empty
end

it 'auto-corrects' do
new_source = autocorrect_source(cop, '%x(ls)')
expect(new_source).to eq('`ls`')
end
end

0 comments on commit 62e86ef

Please sign in to comment.