Skip to content

Commit

Permalink
Merge pull request shadow-dot-cat#14 from zmughal/definedor-assign-pr…
Browse files Browse the repository at this point in the history
…ecedence

DefinedOr: operator precedence for assignment + ConditionalExpression
  • Loading branch information
zmughal committed Sep 2, 2022
2 parents d3a749b + bc1706f commit 00862db
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/Babble/Plugin/DefinedOr.pm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ sub transform_to_plain {
my ($before, $after) = $m->subtexts(qw(before after));
s/^\s+//, s/\s+$// for ($before, $after);
if ($m->submatches->{op}->text =~ /=$/) {
$after = '$_ = '.$after;
$after = '($_ = '.$after.')';
}
$m->replace_text('(map +(defined($_) ? $_ : '.$after.'), '.$before.')[0]');
};
Expand All @@ -39,9 +39,9 @@ sub transform_to_plain {
[ after => '(?>(?&PerlPrefixPostfixTerm))' ],
] => $tf);
$top->each_match_within(Assignment => [
[ before => '(?>(?&PerlPrefixPostfixTerm))' ],
[ before => '(?>(?&PerlConditionalExpression))' ],
[ op => '(?>(?&PerlOWS) //=)' ], '(?>(?&PerlOWS))',
[ after => '(?>(?&PerlPrefixPostfixTerm))' ],
[ after => '(?>(?&PerlConditionalExpression))' ],
] => $tf);
}

Expand Down
4 changes: 3 additions & 1 deletion t/plugin-definedor.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ my @cand = (
[ 'my $x = $y // $z;',
'my $x = (map +(defined($_) ? $_ : $z), $y)[0];', ],
[ 'my $x = ($y //= $z);',
'my $x = ((map +(defined($_) ? $_ : $_ = $z), $y)[0]);', ],
'my $x = ((map +(defined($_) ? $_ : ($_ = $z)), $y)[0]);', ],
[ 'my $x; my $y = 3; $x //= $y; say $x;',
'my $x; my $y = 3; defined($_) or $_ = $y for $x; say $x;', ],
[ 'my $x; my $y = 3; $x //= $y if $z; say $x;',
'my $x; my $y = 3; do { defined($_) or $_ = $y for $x } if $z; say $x;', ],
[ 'sub foo { return $x // 3 }',
'sub foo { return (map +(defined($_) ? $_ : 3), $x)[0] }', ],
[ '$lhs ? $i : $j //= $rhs ? $x : $y',
'(map +(defined($_) ? $_ : ($_ = $rhs ? $x : $y)), $lhs ? $i : $j)[0]', ],
);

foreach my $cand (@cand) {
Expand Down

0 comments on commit 00862db

Please sign in to comment.