forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of rubocop#835.
- Loading branch information
Showing
5 changed files
with
66 additions
and
0 deletions.
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
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
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 |
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,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 |