From 62e86efb43a3c682a7ad2637a8cf031ff0e598c9 Mon Sep 17 00:00:00 2001 From: Jonas Arvidsson Date: Wed, 30 Apr 2014 21:32:02 +0200 Subject: [PATCH] New cop UnneededPercentX Part of #835. --- CHANGELOG.md | 1 + config/enabled.yml | 4 +++ lib/rubocop.rb | 1 + lib/rubocop/cop/style/unneeded_percent_x.rb | 26 ++++++++++++++ .../cop/style/unneeded_percent_x_spec.rb | 34 +++++++++++++++++++ 5 files changed, 66 insertions(+) create mode 100644 lib/rubocop/cop/style/unneeded_percent_x.rb create mode 100644 spec/rubocop/cop/style/unneeded_percent_x_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index a46c5870c584..7c9b8851be36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/enabled.yml b/config/enabled.yml index 8042f3c12791..00003764b173 100644 --- a/config/enabled.yml +++ b/config/enabled.yml @@ -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 diff --git a/lib/rubocop.rb b/lib/rubocop.rb index d5b4e40fc84a..11dfbbe5738f 100644 --- a/lib/rubocop.rb +++ b/lib/rubocop.rb @@ -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' diff --git a/lib/rubocop/cop/style/unneeded_percent_x.rb b/lib/rubocop/cop/style/unneeded_percent_x.rb new file mode 100644 index 000000000000..51209c9f2ebe --- /dev/null +++ b/lib/rubocop/cop/style/unneeded_percent_x.rb @@ -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 diff --git a/spec/rubocop/cop/style/unneeded_percent_x_spec.rb b/spec/rubocop/cop/style/unneeded_percent_x_spec.rb new file mode 100644 index 000000000000..7b2daeb589dc --- /dev/null +++ b/spec/rubocop/cop/style/unneeded_percent_x_spec.rb @@ -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