Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
RegexpNode#each_capture
and parsed_tree
- Loading branch information
1 parent
50b798d
commit 7efae1c
Showing
4 changed files
with
83 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Ext | ||
# Extensions to AST::RegexpNode for our cached parsed regexp info | ||
module RegexpNode | ||
ANY = Object.new | ||
def ANY.==(_) | ||
true | ||
end | ||
private_constant :ANY | ||
|
||
class << self | ||
attr_reader :parsed_cache | ||
end | ||
@parsed_cache = {} | ||
|
||
# @return [Regexp::Expression::Root, nil] | ||
def parsed_tree | ||
return if interpolation? | ||
|
||
str = content | ||
Ext::RegexpNode.parsed_cache[str] ||= begin | ||
Regexp::Parser.parse(str) | ||
rescue StandardError | ||
nil | ||
end | ||
end | ||
|
||
def each_capture(named: ANY) | ||
return enum_for(__method__, named: named) unless block_given? | ||
|
||
parsed_tree&.traverse do |event, exp, _index| | ||
yield(exp) if event == :enter && | ||
named == exp.respond_to?(:name) && | ||
exp.respond_to?(:capturing?) && | ||
exp.capturing? | ||
end | ||
|
||
self | ||
end | ||
|
||
AST::RegexpNode.include self | ||
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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'timeout' | ||
|
||
RSpec.describe RuboCop::Ext::RegexpNode do | ||
let(:source) { '/(hello)(?<foo>world)(?:not captured)/' } | ||
let(:processed_source) { parse_source(source) } | ||
let(:ast) { processed_source.ast } | ||
let(:node) { ast } | ||
|
||
describe '#each_capture' do | ||
subject(:captures) { node.each_capture(**arg).to_a } | ||
|
||
let(:named) { be_instance_of(Regexp::Expression::Group::Named) } | ||
let(:positional) { be_instance_of(Regexp::Expression::Group::Capture) } | ||
|
||
context 'when called without argument' do | ||
let(:arg) { {} } | ||
|
||
it { is_expected.to match [positional, named] } | ||
end | ||
|
||
context 'when called with a `named: false`' do | ||
let(:arg) { { named: false } } | ||
|
||
it { is_expected.to match [positional] } | ||
end | ||
|
||
context 'when called with a `named: true`' do | ||
let(:arg) { { named: true } } | ||
|
||
it { is_expected.to match [named] } | ||
end | ||
end | ||
end |