Skip to content

Commit

Permalink
Merge pull request #1241 from ruby/fix-ternary
Browse files Browse the repository at this point in the history
Fix ternary operator immediately followed by underscore
  • Loading branch information
kddnewton authored Aug 11, 2023
2 parents d413210 + e907303 commit 1c01957
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/yarp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5411,13 +5411,14 @@ lex_question_mark(yp_parser_t *parser) {
} else {
size_t encoding_width = parser->encoding.char_width(parser->current.end, parser->end - parser->current.end);

// We only want to return a character literal if there's exactly one
// alphanumeric character right after the `?`
// Ternary operators can have a ? immediately followed by an identifier which starts with
// an underscore. We check for this case
if (
!parser->encoding.alnum_char(parser->current.end, parser->end - parser->current.end) ||
!(parser->encoding.alnum_char(parser->current.end, parser->end - parser->current.end) ||
*parser->current.end == '_') ||
(
(parser->current.end + encoding_width >= parser->end) ||
!parser->encoding.alnum_char(parser->current.end + encoding_width, parser->end - (parser->current.end + encoding_width))
!char_is_identifier(parser, parser->current.end + encoding_width)
)
) {
lex_state_set(parser, YP_LEX_STATE_END);
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/ternary_operator.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ empty??nil:nil
a??nil:nil

a ?var1 : var2

nil??_a =2:1
35 changes: 32 additions & 3 deletions test/snapshots/ternary_operator.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1c01957

Please sign in to comment.