Skip to content
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

Fix an incompatible behavior for Prism::Translation::Parser #2552

Closed

Commits on Apr 2, 2024

  1. Fix an incompatible behavior for Prism::Translation::Parser

    This PR fixes the following incompatible behavior for `Prism::Translation::Parser`
    when using `case`/`when`/`end` with `;` expression.
    
    ## Expected
    
    It returns the range of the semicolon even if there is a space before the semicolon:
    
    ```console
    $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \
    'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin'
    ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
    #<Parser::Source::Range (string) 15...16>
    $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \
    'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin.source'
    ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
    ";"
    
    $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \
    'p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin'
    ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
    #<Parser::Source::Range (string) 16...17>
    $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \
    'p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin.source'
    ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
    ";"
    ```
    
    ## Actual
    
    It returns `nil` if there is a space before the semicolon:
    
    ```console
    $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \
    'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin'
    ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
    #<Parser::Source::Range (string) 15...16>
    $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \
    'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin.source'
    ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
    ";"
    
    $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \
    'p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin'
    ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
    nil
    $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \
    'p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin.source'
    ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
    -e:1:in `<main>': undefined method `source' for nil (NoMethodError)
    
    p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin.source
                                                                                            ^^^^^^^
    ```
    koic committed Apr 2, 2024
    Configuration menu
    Copy the full SHA
    9625805 View commit details
    Browse the repository at this point in the history