New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle comments and blank interpolations in regexp parsed_tree #8803
Changes from 1 commit
7693728
df4f6b4
cfec9ed
b25fd8e
cb8a04b
a73a64f
a6b3cd1
9b61289
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,16 @@ class << self | |
@parsed_cache = {} | ||
|
||
# @return [Regexp::Expression::Root, nil] | ||
def parsed_tree | ||
return if interpolation? | ||
def parsed_tree(interpolation: :ignore) | ||
unless %i[ignore blank].include?(interpolation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When thinking about the fact that That made me wonder if the best course of action isn't to remove the option altogether, and make blanking the default. What do you think? It should be the callers job to check for interpolation Should be documented, and marked as breaking change in the Changelog. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, I'm not really a fan of Having had a quick look at I'll take a look at making this change, and report back There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you're right about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So for now, to keep this PR focussed, I left |
||
raise ArgumentError, 'interpolation must be one of :ignore or :blank' | ||
end | ||
|
||
return if interpolation? && interpolation == :ignore | ||
|
||
str = content | ||
str = with_interpolations_blanked | ||
Ext::RegexpNode.parsed_cache[str] ||= begin | ||
Regexp::Parser.parse(str) | ||
Regexp::Parser.parse(str, options: options) | ||
rescue StandardError | ||
nil | ||
end | ||
|
@@ -40,6 +44,23 @@ def each_capture(named: ANY) | |
self | ||
end | ||
|
||
private | ||
|
||
def with_interpolations_blanked | ||
children.reject(&:regopt_type?).map do |child| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's only one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point - I don't think I'd realised that even for a Regexp without options the regopt node was still present. I think the original was better readability-wise, but the new version is probably fine with a comment 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about I add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, my slight concern is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I know. I would actually mark |
||
source = child.source | ||
|
||
# We don't want to consider the contents of interpolations as part of the pattern source, | ||
# but need to preserve their width, to allow offsets to correctly line up with the | ||
# original source: spaces have no effect, and preserve width. | ||
if child.begin_type? | ||
' ' * source.length | ||
else | ||
source | ||
end | ||
end.join | ||
end | ||
|
||
AST::RegexpNode.include self | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,9 +36,9 @@ Gem::Specification.new do |s| | |
s.add_runtime_dependency('parallel', '~> 1.10') | ||
s.add_runtime_dependency('parser', '>= 2.7.1.5') | ||
s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0') | ||
s.add_runtime_dependency('regexp_parser', '>= 1.7') | ||
s.add_runtime_dependency('regexp_parser', '>= 1.8') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To pick up ammar/regexp_parser#68 and ammar/regexp_parser#67 |
||
s.add_runtime_dependency('rexml') | ||
s.add_runtime_dependency('rubocop-ast', '>= 0.5.0') | ||
s.add_runtime_dependency('rubocop-ast', '>= 0.6.0') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To pick up rubocop/rubocop-ast#124 |
||
s.add_runtime_dependency('ruby-progressbar', '~> 1.7') | ||
s.add_runtime_dependency('unicode-display_width', '>= 1.4.0', '< 2.0') | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^interpolaton^interpolationAlso, please back-quote RegexpNode#parsed_tree
Might change altogether, read below