Skip to content
Merged
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
11 changes: 8 additions & 3 deletions include/prism/internal/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ pm_location_join(pm_location_t left, pm_location_t right) {
}

/*
* Return the sole expression in a statements node, if there is one.
* Return true if a statements node contains exactly one expression.
* A leading semicolon introduces an empty statement before the expression.
*/
static PRISM_INLINE bool
pm_statements_node_single_expression_p(const pm_statements_node_t *node) {
return node != NULL && node->body.size == 1 && !PM_NODE_FLAG_P(node, PM_STATEMENTS_FLAGS_LEADING_SEMICOLON);
}

/* Return the sole expression in a statements node, if there is one. */
static PRISM_INLINE pm_node_t *
pm_statements_node_single_expression(const pm_statements_node_t *node) {
if (node == NULL || node->body.size != 1 || PM_NODE_FLAG_P(node, PM_STATEMENTS_FLAGS_LEADING_SEMICOLON)) return NULL;
return node->body.nodes[0];
return pm_statements_node_single_expression_p(node) ? node->body.nodes[0] : NULL;
}

/* A string literal uses the source encoding unless an escape forced one. */
Expand Down
55 changes: 35 additions & 20 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,26 @@ pm_conditional_predicate_warn_write_literal(pm_parser_t *parser, const pm_node_t
}
}

/* Return the statements of a group whose last expression supplies the predicate. */
static const pm_statements_node_t *
pm_conditional_predicate_statements(const pm_node_t *node) {
switch (PM_NODE_TYPE(node)) {
case PM_PARENTHESES_NODE: {
const pm_node_t *body = ((const pm_parentheses_node_t *) node)->body;
if (body != NULL && PM_NODE_TYPE_P(body, PM_STATEMENTS_NODE)) return (const pm_statements_node_t *) body;
break;
}
case PM_BEGIN_NODE: {
const pm_begin_node_t *cast = (const pm_begin_node_t *) node;
if (cast->rescue_clause == NULL && cast->else_clause == NULL && cast->ensure_clause == NULL) return cast->statements;
break;
}
default:
break;
}
return NULL;
}

/**
* The predicate of conditional nodes can change what would otherwise be regular
* nodes into specialized nodes. For example:
Expand All @@ -1582,7 +1602,18 @@ pm_conditional_predicate_warn_write_literal(pm_parser_t *parser, const pm_node_t
* predicate or writing a static literal as the predicate.
*/
static void
pm_conditional_predicate(pm_parser_t *parser, pm_node_t *node, pm_conditional_predicate_type_t type) {
pm_conditional_predicate(pm_parser_t *parser, pm_node_t *predicate, pm_conditional_predicate_type_t type) {
pm_node_t *node = predicate;
bool allow_flip_flop = true;
const pm_statements_node_t *statements;

while ((statements = pm_conditional_predicate_statements(node)) != NULL) {
/* A direct range becomes a flip-flop only when it is the sole
* expression in every enclosing group. Logical operands start fresh. */
if (!pm_statements_node_single_expression_p(statements)) allow_flip_flop = false;
node = statements->body.nodes[statements->body.size - 1];
}

switch (PM_NODE_TYPE(node)) {
case PM_AND_NODE: {
pm_and_node_t *cast = (pm_and_node_t *) node;
Expand All @@ -1596,25 +1627,8 @@ pm_conditional_predicate(pm_parser_t *parser, pm_node_t *node, pm_conditional_pr
pm_conditional_predicate(parser, cast->right, PM_CONDITIONAL_PREDICATE_TYPE_CONDITIONAL);
break;
}
case PM_PARENTHESES_NODE: {
pm_parentheses_node_t *cast = (pm_parentheses_node_t *) node;

if ((cast->body != NULL) && PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE)) {
pm_statements_node_t *statements = (pm_statements_node_t *) cast->body;
if (statements->body.size == 1) pm_conditional_predicate(parser, statements->body.nodes[0], type);
}

break;
}
case PM_BEGIN_NODE: {
pm_begin_node_t *cast = (pm_begin_node_t *) node;
if (cast->statements != NULL) {
pm_statements_node_t *statements = cast->statements;
if (statements->body.size == 1) pm_conditional_predicate(parser, statements->body.nodes[0], type);
}
break;
}
case PM_RANGE_NODE: {
if (!allow_flip_flop) break;
pm_range_node_t *cast = (pm_range_node_t *) node;

if (cast->left != NULL) pm_conditional_predicate(parser, cast->left, PM_CONDITIONAL_PREDICATE_TYPE_FLIP_FLOP);
Expand Down Expand Up @@ -1664,7 +1678,8 @@ pm_conditional_predicate(pm_parser_t *parser, pm_node_t *node, pm_conditional_pr
pm_parser_warn_conditional_predicate_literal(parser, node, type, PM_WARN_LITERAL_IN_CONDITION_VERBOSE, PM_STRING_LITERAL("symbol "));
break;
case PM_INTEGER_NODE:
if (type == PM_CONDITIONAL_PREDICATE_TYPE_FLIP_FLOP) {
/* Only a bare integer endpoint is compared with the line number. */
if (type == PM_CONDITIONAL_PREDICATE_TYPE_FLIP_FLOP && node == predicate) {
if (!PM_PARSER_COMMAND_LINE_OPTION_E(parser)) {
pm_parser_warn_node(parser, node, PM_WARN_INTEGER_IN_FLIP_FLOP);
}
Expand Down
50 changes: 50 additions & 0 deletions test/prism/result/conditional_predicate_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require_relative "../test_helper"

module Prism
class ConditionalPredicateTest < TestCase
def test_range_in_parentheses
assert_kind_of RangeNode, Prism.parse_statement("not (; ..stop)").receiver.body.body.last
assert_kind_of FlipFlopNode, Prism.parse_statement("not (..stop;)").receiver.body.body.last

group = Prism.parse_statement("if (setup; (start...stop)); end").predicate
assert_kind_of RangeNode, group.body.body.last.body.body.last
end

def test_logical_operands_in_group_tail
group = Prism.parse_statement("not (()\ngate and ..stop)").receiver
assert_kind_of FlipFlopNode, group.body.body.last.right

group = Prism.parse_statement("if begin setup; /start/ or ..stop end; end").predicate
tail = group.statements.body.last
assert_kind_of MatchLastLineNode, tail.left
assert_kind_of FlipFlopNode, tail.right
end

def test_regexp_in_group_tail
group = Prism.parse_statement("if (/first/; /last/); end").predicate
first, last = group.body.body
assert_kind_of RegularExpressionNode, first
assert_kind_of MatchLastLineNode, last

group = Prism.parse_statement('if begin setup; /#{pattern}/ end; end').predicate
assert_kind_of InterpolatedMatchLastLineNode, group.statements.body.last
end

def test_range_in_begin
group = Prism.parse_statement("if begin; start..stop end; end").predicate
assert_kind_of RangeNode, group.statements.body.last

group = Prism.parse_statement("if begin start..stop; end; end").predicate
assert_kind_of FlipFlopNode, group.statements.body.last
end

def test_begin_with_exception_clauses
["rescue", "ensure"].each do |clause|
group = Prism.parse_statement("if begin /body/; #{clause}; end; end").predicate
assert_kind_of RegularExpressionNode, group.statements.body.last, clause
end
end
end
end
7 changes: 7 additions & 0 deletions test/prism/result/warnings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ def test_literal_predicate_warnings
refute_warning("if !:s; end")
end

def test_grouped_predicate_warnings
assert_warning('if (setup; "s"); end', "string literal in condition", compare: RUBY_VERSION >= "3.3")
warnings = Prism.parse("if (setup; /r/)..(setup; 1); end").warnings
assert_equal ["regex literal in flip-flop", "literal in flip-flop"], warnings.map(&:message)
refute_warning("if (; 1..2); end")
end

def test_literal_in_conditionals
sources = [
"if (a = 2); a; end",
Expand Down
Loading