Commit c6b0b16
Fix unexpected empty source map stacks
This fixes two unrelated issues that both previously presented with a
similar symptom, namely the following error:
RangeError: cannot get the first element of beginless range
The first issue is that some constructs, such as `{} => {}` and `foo
in bar` get parsed by Ripper as a `case` AST node. However, without a
matching `case` keyword, the YARD parser doesn't know where it starts.
The second issue is that tokens following the `def` keyword
are (correctly) never treated as keywords themselves, because they are
used as the method name instead. However, this logic erroneously also
applied to keywords following the `:def` _symbol_ - again making the
YARD parser unable to determine where the keyword (following the
`:def` symbol) starts.
In both cases, the problematic constructs by themselves did not cause
a crash but merely provided wrong source ranges. However, in specific
parser states, the corresponding `@map` entry is the empty array
rather than nil (because a similar, valid, construct was encountered
before, which initializes the map entry). This was causing crashes
since the beginning of `line_range` and `source_range` becomes nil as
a result.
Here we're fixing the underlying issue and we're also adding specs to
ensure that source ranges are correct, and that crashes in the
presence of pre-initializing constructs are eliminated.
Note that another fix would have been to treat empty `@map` entries
the same as nil `@map` entries in `visit_event`. Doing so would also
prevent the crashes, and improve robustness more generally, however it
would mask potential similar problems with other constructs.
Therefore I have refrained from making that change -- a debatable
decision that might be revisited in a separate change. However, I've
added a better error message in this case.
DISCLOSURE: Claude wrote large parts of the specs and helped me
understand the problems and create the fixes, but I'm fully adopting
its output as my own (after polishing it manually and reviewing it.)
Fixes #1603.
Co-Authored-By: Claude <[email protected]>1 parent e96ae99 commit c6b0b16
File tree
2 files changed
+112
-2
lines changed- lib/yard/parser/ruby
- spec/parser/ruby
2 files changed
+112
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
236 | 236 | | |
237 | 237 | | |
238 | 238 | | |
239 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
240 | 251 | | |
241 | 252 | | |
242 | 253 | | |
| |||
259 | 270 | | |
260 | 271 | | |
261 | 272 | | |
262 | | - | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
263 | 277 | | |
264 | 278 | | |
265 | 279 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
553 | 553 | | |
554 | 554 | | |
555 | 555 | | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
556 | 652 | | |
557 | 653 | | |
0 commit comments