forked from rubocop/rubocop
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
118 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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# Do not mix named captures and numbered captures in a Regexp literal | ||
# because numbered capture is ignored if they're mixed. | ||
# Replace numbered captures with non-capturing groupings or | ||
# named captures. | ||
# | ||
# # bad | ||
# /(?<foo>FOO)(BAR)/ | ||
# | ||
# # good | ||
# /(?<foo>FOO)(?<bar>BAR)/ | ||
# | ||
# # good | ||
# /(?<foo>FOO)(?:BAR)/ | ||
# | ||
class MixedRegexpCaptureTypes < Cop | ||
MSG = 'Do not mix named captures and numbered captures ' \ | ||
'in a Regexp literal.' | ||
|
||
def on_regexp(node) | ||
tree = Regexp::Parser.parse(node.content) | ||
return unless named_capture?(tree) | ||
return unless numbered_capture?(tree) | ||
|
||
add_offense(node) | ||
end | ||
|
||
private | ||
|
||
def named_capture?(tree) | ||
each_regexp_expr(tree).any? do |e| | ||
e.instance_of?(Regexp::Expression::Group::Capture) | ||
end | ||
end | ||
|
||
def numbered_capture?(tree) | ||
each_regexp_expr(tree).any? do |e| | ||
e.instance_of?(Regexp::Expression::Group::Named) | ||
end | ||
end | ||
|
||
def each_regexp_expr(tree) | ||
tree.to_enum(:each_expression) | ||
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
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::MixedRegexpCaptureTypes do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
it 'registers an offense when both of named and numbered captures are used' do | ||
expect_offense(<<~RUBY) | ||
/(?<foo>bar)(baz)/ | ||
^^^^^^^^^^^^^^^^^^ Do not mix named captures and numbered captures in a Regexp literal. | ||
RUBY | ||
end | ||
|
||
it 'does not register offense to a regexp with named capture only' do | ||
expect_no_offenses(<<~RUBY) | ||
/(?<foo>bar)/ | ||
RUBY | ||
end | ||
|
||
it 'does not register offense to a regexp with numbered capture only' do | ||
expect_no_offenses(<<~RUBY) | ||
/(bar)/ | ||
RUBY | ||
end | ||
|
||
it 'does not register offense to a regexp with named capture and ' \ | ||
'non-capturing group' do | ||
expect_no_offenses(<<~RUBY) | ||
/(<foo>bar)(?:bar)/ | ||
RUBY | ||
end | ||
end |