Don't create tokens by hand#1696
Conversation
|
|
||
| def test_markup_code | ||
| tokens = [ | ||
| { :line_no => 0, :char_no => 0, :kind => :on_const, :text => 'CONSTANT' }, |
There was a problem hiding this comment.
This testcase didn't make much sense since only methods have their source code shown. I simply removed it.
|
🚀 Preview deployment available at: https://baaa25b1.rdoc-6cd.pages.dev (commit: 45fa797) |
There was a problem hiding this comment.
Pull request overview
This PR removes “synthetic” tokens (file/line comment, newline, indent) from Ruby parser token streams and moves location/line-number decoration into RDoc::MethodAttr#markup_code, with tests updated to build token streams via RipperStateLex instead of hand-crafted hashes.
Changes:
- Stop prepending non-source tokens in
RDoc::Parser::Ruby#visible_tokens_from_locationand related paths. - Rework
RDoc::MethodAttr#markup_codeto dedent, optionally add line numbers, and prepend a generated location comment. - Update
any_method_testto useRipperStateLex.parseand assert the new HTML output format.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
test/rdoc/code_object/any_method_test.rb |
Updates expectations and replaces hand-built token hashes with RipperStateLex.parse output. |
lib/rdoc/parser/ruby.rb |
Removes construction of extra non-source tokens from token streams; returns only sliced source tokens. |
lib/rdoc/generator/markup.rb |
Moves location comment and line-number generation into markup_code, and adjusts dedent logic. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| meth.start_collecting_tokens(:ruby) | ||
| node = @line_nodes[line_no] | ||
| tokens = node ? visible_tokens_from_location(node.location) : [file_line_comment_token(start_line)] | ||
| tokens = node ? visible_tokens_from_location(node.location) : [] | ||
| tokens.each { |token| meth.token_stream << token } | ||
|
|
There was a problem hiding this comment.
Seems correct. Previously they were not empty because it contained the comment
| line_no = node.location.start_line | ||
| else | ||
| tokens = [file_line_comment_token(line_no)] | ||
| tokens = [] |
There is some awkward code that dances around the fact that the tokens for a method actually contain a 3 extra tokens that don't exist in the source code. Now `RipperStateLex` is only referenced to actually parse, rest is kept internal
45fa797 to
187755a
Compare
There is some awkward code that dances around the fact that the tokens for a method actually contain a 3 extra tokens that don't exist in the source code.
Now
RipperStateLexis only referenced to actually parse, rest is kept internal