Skip to content

Commit f314cb0

Browse files
committed
Squash down parens and begins for conditional predicates
1 parent d25a431 commit f314cb0

4 files changed

Lines changed: 100 additions & 23 deletions

File tree

‎include/prism/internal/node.h‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ pm_location_join(pm_location_t left, pm_location_t right) {
3333
}
3434

3535
/*
36-
* Return the sole expression in a statements node, if there is one.
36+
* Return true if a statements node contains exactly one expression.
3737
* A leading semicolon introduces an empty statement before the expression.
3838
*/
39+
static PRISM_INLINE bool
40+
pm_statements_node_single_expression_p(const pm_statements_node_t *node) {
41+
return node != NULL && node->body.size == 1 && !PM_NODE_FLAG_P(node, PM_STATEMENTS_FLAGS_LEADING_SEMICOLON);
42+
}
43+
44+
/* Return the sole expression in a statements node, if there is one. */
3945
static PRISM_INLINE pm_node_t *
4046
pm_statements_node_single_expression(const pm_statements_node_t *node) {
41-
if (node == NULL || node->body.size != 1 || PM_NODE_FLAG_P(node, PM_STATEMENTS_FLAGS_LEADING_SEMICOLON)) return NULL;
42-
return node->body.nodes[0];
47+
return pm_statements_node_single_expression_p(node) ? node->body.nodes[0] : NULL;
4348
}
4449

4550
/* A string literal uses the source encoding unless an escape forced one. */

‎src/prism.c‎

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,26 @@ pm_conditional_predicate_warn_write_literal(pm_parser_t *parser, const pm_node_t
15691569
}
15701570
}
15711571

1572+
/* Return the statements of a group whose last expression supplies the predicate. */
1573+
static const pm_statements_node_t *
1574+
pm_conditional_predicate_statements(const pm_node_t *node) {
1575+
switch (PM_NODE_TYPE(node)) {
1576+
case PM_PARENTHESES_NODE: {
1577+
const pm_node_t *body = ((const pm_parentheses_node_t *) node)->body;
1578+
if (body != NULL && PM_NODE_TYPE_P(body, PM_STATEMENTS_NODE)) return (const pm_statements_node_t *) body;
1579+
break;
1580+
}
1581+
case PM_BEGIN_NODE: {
1582+
const pm_begin_node_t *cast = (const pm_begin_node_t *) node;
1583+
if (cast->rescue_clause == NULL && cast->else_clause == NULL && cast->ensure_clause == NULL) return cast->statements;
1584+
break;
1585+
}
1586+
default:
1587+
break;
1588+
}
1589+
return NULL;
1590+
}
1591+
15721592
/**
15731593
* The predicate of conditional nodes can change what would otherwise be regular
15741594
* nodes into specialized nodes. For example:
@@ -1582,7 +1602,18 @@ pm_conditional_predicate_warn_write_literal(pm_parser_t *parser, const pm_node_t
15821602
* predicate or writing a static literal as the predicate.
15831603
*/
15841604
static void
1585-
pm_conditional_predicate(pm_parser_t *parser, pm_node_t *node, pm_conditional_predicate_type_t type) {
1605+
pm_conditional_predicate(pm_parser_t *parser, pm_node_t *predicate, pm_conditional_predicate_type_t type) {
1606+
pm_node_t *node = predicate;
1607+
bool allow_flip_flop = true;
1608+
const pm_statements_node_t *statements;
1609+
1610+
while ((statements = pm_conditional_predicate_statements(node)) != NULL) {
1611+
/* A direct range becomes a flip-flop only when it is the sole
1612+
* expression in every enclosing group. Logical operands start fresh. */
1613+
if (!pm_statements_node_single_expression_p(statements)) allow_flip_flop = false;
1614+
node = statements->body.nodes[statements->body.size - 1];
1615+
}
1616+
15861617
switch (PM_NODE_TYPE(node)) {
15871618
case PM_AND_NODE: {
15881619
pm_and_node_t *cast = (pm_and_node_t *) node;
@@ -1596,25 +1627,8 @@ pm_conditional_predicate(pm_parser_t *parser, pm_node_t *node, pm_conditional_pr
15961627
pm_conditional_predicate(parser, cast->right, PM_CONDITIONAL_PREDICATE_TYPE_CONDITIONAL);
15971628
break;
15981629
}
1599-
case PM_PARENTHESES_NODE: {
1600-
pm_parentheses_node_t *cast = (pm_parentheses_node_t *) node;
1601-
1602-
if ((cast->body != NULL) && PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE)) {
1603-
pm_statements_node_t *statements = (pm_statements_node_t *) cast->body;
1604-
if (statements->body.size == 1) pm_conditional_predicate(parser, statements->body.nodes[0], type);
1605-
}
1606-
1607-
break;
1608-
}
1609-
case PM_BEGIN_NODE: {
1610-
pm_begin_node_t *cast = (pm_begin_node_t *) node;
1611-
if (cast->statements != NULL) {
1612-
pm_statements_node_t *statements = cast->statements;
1613-
if (statements->body.size == 1) pm_conditional_predicate(parser, statements->body.nodes[0], type);
1614-
}
1615-
break;
1616-
}
16171630
case PM_RANGE_NODE: {
1631+
if (!allow_flip_flop) break;
16181632
pm_range_node_t *cast = (pm_range_node_t *) node;
16191633

16201634
if (cast->left != NULL) pm_conditional_predicate(parser, cast->left, PM_CONDITIONAL_PREDICATE_TYPE_FLIP_FLOP);
@@ -1664,7 +1678,8 @@ pm_conditional_predicate(pm_parser_t *parser, pm_node_t *node, pm_conditional_pr
16641678
pm_parser_warn_conditional_predicate_literal(parser, node, type, PM_WARN_LITERAL_IN_CONDITION_VERBOSE, PM_STRING_LITERAL("symbol "));
16651679
break;
16661680
case PM_INTEGER_NODE:
1667-
if (type == PM_CONDITIONAL_PREDICATE_TYPE_FLIP_FLOP) {
1681+
/* Only a bare integer endpoint is compared with the line number. */
1682+
if (type == PM_CONDITIONAL_PREDICATE_TYPE_FLIP_FLOP && node == predicate) {
16681683
if (!PM_PARSER_COMMAND_LINE_OPTION_E(parser)) {
16691684
pm_parser_warn_node(parser, node, PM_WARN_INTEGER_IN_FLIP_FLOP);
16701685
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../test_helper"
4+
5+
module Prism
6+
class ConditionalPredicateTest < TestCase
7+
def test_range_in_parentheses
8+
assert_kind_of RangeNode, Prism.parse_statement("not (; ..stop)").receiver.body.body.last
9+
assert_kind_of FlipFlopNode, Prism.parse_statement("not (..stop;)").receiver.body.body.last
10+
11+
group = Prism.parse_statement("if (setup; (start...stop)); end").predicate
12+
assert_kind_of RangeNode, group.body.body.last.body.body.last
13+
end
14+
15+
def test_logical_operands_in_group_tail
16+
group = Prism.parse_statement("not (()\ngate and ..stop)").receiver
17+
assert_kind_of FlipFlopNode, group.body.body.last.right
18+
19+
group = Prism.parse_statement("if begin setup; /start/ or ..stop end; end").predicate
20+
tail = group.statements.body.last
21+
assert_kind_of MatchLastLineNode, tail.left
22+
assert_kind_of FlipFlopNode, tail.right
23+
end
24+
25+
def test_regexp_in_group_tail
26+
group = Prism.parse_statement("if (/first/; /last/); end").predicate
27+
first, last = group.body.body
28+
assert_kind_of RegularExpressionNode, first
29+
assert_kind_of MatchLastLineNode, last
30+
31+
group = Prism.parse_statement('if begin setup; /#{pattern}/ end; end').predicate
32+
assert_kind_of InterpolatedMatchLastLineNode, group.statements.body.last
33+
end
34+
35+
def test_range_in_begin
36+
group = Prism.parse_statement("if begin; start..stop end; end").predicate
37+
assert_kind_of RangeNode, group.statements.body.last
38+
39+
group = Prism.parse_statement("if begin start..stop; end; end").predicate
40+
assert_kind_of FlipFlopNode, group.statements.body.last
41+
end
42+
43+
def test_begin_with_exception_clauses
44+
["rescue", "ensure"].each do |clause|
45+
group = Prism.parse_statement("if begin /body/; #{clause}; end; end").predicate
46+
assert_kind_of RegularExpressionNode, group.statements.body.last, clause
47+
end
48+
end
49+
end
50+
end

‎test/prism/result/warnings_test.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ def test_literal_predicate_warnings
274274
refute_warning("if !:s; end")
275275
end
276276

277+
def test_grouped_predicate_warnings
278+
assert_warning('if (setup; "s"); end', "string literal in condition", compare: RUBY_VERSION >= "3.3")
279+
warnings = Prism.parse("if (setup; /r/)..(setup; 1); end").warnings
280+
assert_equal ["regex literal in flip-flop", "literal in flip-flop"], warnings.map(&:message)
281+
refute_warning("if (; 1..2); end")
282+
end
283+
277284
def test_literal_in_conditionals
278285
sources = [
279286
"if (a = 2); a; end",

0 commit comments

Comments
 (0)