-
Notifications
You must be signed in to change notification settings - Fork 178
Add Prism.node_for(Method|UnboundMethod|Proc) to get a Prism node for a callable #3808
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
base: main
Are you sure you want to change the base?
Conversation
|
Looks like Proc generated by |
dd02f75 to
5d510f5
Compare
|
The 2.7 failure is quite annoying: https://github.com/ruby/prism/actions/runs/20211767571/job/58018650394?pr=3808 |
This is something we should probably talk about sometime in the future. It was added for the convenience of rubocop as far as I'm aware and I appreciate that this happened. But I do not want to keep these long EOL versions supported forever. For now I would just put this in a separate file like you said. You shoudl be able to use the version-specific gemfile with |
24bdf87 to
3c0b91c
Compare
@tompng Do you mean: does not have a |
Implemented. |
|
Looking at things like |
* By comparing byte offsets which folds 3 branches into 1. * Also avoids allocation of Location objects.
966dda3 to
eafdef7
Compare
|
I found that {Method,UnboundMethod,Proc}#source_location returns columns in bytes and not in characters. EDIT: It also means |
| when DefNode | ||
| node.start_offset == start_offset && node.end_offset == end_offset | ||
| when LambdaNode | ||
| # Proc#source_location returns start_column 2 for `-> { ... }` (just after the `->`) |
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.
Filed https://bugs.ruby-lang.org/issues/21784 for this
lib/prism/parse_result.rb
Outdated
| end_offset = parse_result.source.line_to_byte_offset(end_line) + end_column | ||
| start_byte_column = start_offset - parse_result.source.line_start(start_offset) | ||
|
|
<
67ED
/tr>
||
| found = root.tunnel(start_line, start_byte_column).reverse.find do |node| |
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.
rfind would be nice here but of course it's only RUby 4+.
I guess we could do a polyfill but for this case I doubt it matters, most of the time is not spent here but in creating the Ruby objects for the AST, there are typically only a few nodes returned by tunnel.
|
Re the test-all failure, I'll fix it, makes it clear test-unit |
| end | ||
| root = parse_result.value | ||
| # CRuby currently returns the source_location columns in bytes and not characters | ||
| start_offset = parse_result.source.line_to_byte_offset(start_line) + start_column |
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.
I'm not understanding what you're doing here exactly. We already have line and column, and it looks like you're calculating offset, and then using that to recompute the column? The column is already in bytes, and that's what tunnel expects, so you should just be able to use that directly.
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.
I compute start_offset & end_offset so we can have fast comparisons below (2 comparisons vs 4) and without creating Location objects for most cases.
Right, since CRuby currently return the column in byte I can remove start_byte_column, will push a commit to do that.
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.
Done, I amended the last commit to simplify this part.
67ED
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.
BTW, it might be easier to review commit by commit, that would show the purpose of start_offset better.
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.
Okay last thing is I don't think we need the line_and_character_column_to_byte_offset API now.
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.
Yeah, it's what I mentioned in #3808 (comment).
I'll remove it in its own commit, so if we need it again it'll be just a git revert away.
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.
Pushed
|
For reference, this won't be available (at least in Ruby 4.0) unfortunately https://bugs.ruby-lang.org/issues/21783#note-8 |
|
@eregon we've marked this as blocked until and unless the source location thing gets resolved. Another option is requesting an official node_id API. Is that something you would consider implementing in TruffleRuby? |
and https://bugs.ruby-lang.org/issues/20999