Skip to content

Commit

Permalink
Fix false positive in UnneededPercentQ for /%Q(something)/
Browse files Browse the repository at this point in the history
Regular expressions starting with % or %Q were treated
as string literals starting with those delimiters.

Related to rubocop#835.
  • Loading branch information
jonas054 committed Jul 14, 2014
1 parent 834328c commit 40c724c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Change log

## master (unreleased)

### New features

* [#835](https://github.com/bbatsov/rubocop/issues/835): New cop `PercentQLiterals` checks if use of `%Q` and `%q` matches configuration. ([@jonas054][])

## master (unreleased)

### Bugs fixed

* [#1197](https://github.com/bbatsov/rubocop/issues/1197): Fix false positive for new lambda syntax in `SpaceInsideBlockBraces`. ([@jonas054][])
* [#1201](https://github.com/bbatsov/rubocop/issues/1201): Fix error at anonymous keyword splat arguments in some variable cops. ([@yujinakayama][])
* Fix false positive in `UnneededPercentQ` for `/%Q(something)/`. ([@jonas054][])

## 0.24.1 (03/07/2014)

Expand Down
8 changes: 8 additions & 0 deletions lib/rubocop/cop/style/unneeded_percent_q.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ def on_str(node)
check(node)
end

# We process regexp nodes because the inner str nodes can cause
# confusion in on_str if they start with %( or %Q(.
def on_regexp(node)
string_parts = node.children.select { |child| child.type == :str }
string_parts.each { |s| ignore_node(s) }
end

private

def check(node)
return if ignored_node?(node)
src = node.loc.expression.source
return unless src =~ /^%q/i
return if src =~ /'/ && src =~ /"/
Expand Down
5 changes: 5 additions & 0 deletions spec/rubocop/cop/style/unneeded_percent_q_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
expect(cop.offenses).to be_empty
end

it 'accepts regular expressions starting with %Q' do
inspect_source(cop, '/%Q?/')
expect(cop.offenses).to be_empty
end

it 'auto-corrects static %Q to double quotes' do
new_source = autocorrect_source(cop, '%Q(hi)')
# One could argue that the double quotes are not necessary for a static
Expand Down

0 comments on commit 40c724c

Please sign in to comment.