Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ruby/prism] Accept a newline after the defined? keyword [Backport #21197] #13327

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

Open
wants to merge 1 commit into
base: ruby_3_4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -664,13 +664,37 @@ def visit_def_node(node)
# defined?(a)
# ^^^^^^^^^^^
def visit_defined_node(node)
builder.keyword_cmd(
:defined?,
token(node.keyword_loc),
token(node.lparen_loc),
[visit(node.value)],
token(node.rparen_loc)
)
# Very weird circumstances here where something like:
#
# defined?
# (1)
#
# gets parsed in Ruby as having only the `1` expression but in parser
# it gets parsed as having a begin. In this case we need to synthesize
# that begin to match parser's behavior.
if node.lparen_loc && node.keyword_loc.join(node.lparen_loc).slice.include?("\n")
builder.keyword_cmd(
:defined?,
token(node.keyword_loc),
nil,
[
builder.begin(
token(node.lparen_loc),
visit(node.value),
token(node.rparen_loc)
)
],
nil
)
else
builder.keyword_cmd(
:defined?,
token(node.keyword_loc),
token(node.lparen_loc),
[visit(node.value)],
token(node.rparen_loc)
)
end
end

# if foo then bar else baz end
Expand Down
17 changes: 16 additions & 1 deletion lib/prism/translation/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1615,8 +1615,23 @@ def visit_def_node(node)
# defined?(a)
# ^^^^^^^^^^^
def visit_defined_node(node)
expression = visit(node.value)

# Very weird circumstances here where something like:
#
# defined?
# (1)
#
# gets parsed in Ruby as having only the `1` expression but in Ripper it
# gets parsed as having a parentheses node. In this case we need to
# synthesize that node to match Ripper's behavior.
if node.lparen_loc && node.keyword_loc.join(node.lparen_loc).slice.include?("\n")
bounds(node.lparen_loc.join(node.rparen_loc))
expression = on_paren(on_stmts_add(on_stmts_new, expression))
end

bounds(node.location)
on_defined(visit(node.value))
on_defined(expression)
end

# if foo then bar else baz end
Expand Down
19 changes: 14 additions & 5 deletions prism/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -19557,18 +19557,27 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
pm_token_t lparen;
pm_token_t rparen;
pm_node_t *expression;

context_push(parser, PM_CONTEXT_DEFINED);
bool newline = accept1(parser, PM_TOKEN_NEWLINE);

if (accept1(parser, PM_TOKEN_PARENTHESIS_LEFT)) {
lparen = parser->previous;
expression = parse_expression(parser, PM_BINDING_POWER_COMPOSITION, true, false, PM_ERR_DEFINED_EXPRESSION, (uint16_t) (depth + 1));

if (parser->recovering) {
if (newline && accept1(parser, PM_TOKEN_PARENTHESIS_RIGHT)) {
expression = (pm_node_t *) pm_parentheses_node_create(parser, &lparen, NULL, &parser->previous, 0);
lparen = not_provided(parser);
rparen = not_provided(parser);
} else {
accept1(parser, PM_TOKEN_NEWLINE);
expect1(parser, PM_TOKEN_PARENTHESIS_RIGHT, PM_ERR_EXPECT_RPAREN);
rparen = parser->previous;
expression = parse_expression(parser, PM_BINDING_POWER_COMPOSITION, true, false, PM_ERR_DEFINED_EXPRESSION, (uint16_t) (depth + 1));

if (parser->recovering) {
rparen = not_provided(parser);
} else {
accept1(parser, PM_TOKEN_NEWLINE);
expect1(parser, PM_TOKEN_PARENTHESIS_RIGHT, PM_ERR_EXPECT_RPAREN);
rparen = parser->previous;
}
}
} else {
lparen = not_provided(parser);
Expand Down
3 changes: 3 additions & 0 deletions test/prism/errors/defined_empty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defined?()
^ expected an expression after `defined?`

9 changes: 9 additions & 0 deletions test/prism/fixtures/defined.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ defined? 1

defined?("foo"
)

defined?
1

defined?
(1)

defined?
()
Loading