UnneededCapitalW cop now does auto-correction

When autocorrection is called for, UnneededCapitalW does a naive
search-and-replace of %w for %W.
Unfortunately, I'm not at the machine my enlistment is on to empirically
re-verify this, so if I've erred, I apologize.  The goal was to extract the
entire %W expression, and then replace just the (first) %W with %w, then
replace the original phrase, beginning with %W, with the new one beginning
with %w.  I called it a naive replacement because I processed the string
representation instead of the AST.

The tests are descriptive of the need, though--the first one verifies that
"%W(one two three)" becomes "%w(one two three)".



On Thu, Jun 5, 2014 at 5:23 PM, Yuji Nakayama <notifications@github.com>
wrote:

> In lib/rubocop/cop/style/unneeded_capital_w.rb:
>
> > @@ -23,6 +23,15 @@ def on_percent_literal(node, types)
> >
> >            add_offense(node, :expression)
> >          end
> > +
> > +        def autocorrect(node)
> > +          sb = node.loc.expression.source_buffer
> > +          uninterpolated_percent_w =  sb.source.sub(/%W/, '%w')
> > +
> > +          @corrections << lambda do |corrector|
> > +            corrector.replace(node.loc.expression, uninterpolated_percent_w)
>
> Doesn't this replace the regexp portion with the entire source?
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/bbatsov/rubocop/pull/1139/files#r13472126>.
>