| 1 | package Perl::Critic::Policy::ValuesAndExpressions::ProhibitLiteralArithmetic 0.001; | ||
| 2 | |||
| 3 | # ABSTRACT: Write a number as the number it is, not as arithmetic on other numbers. | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings FATAL => 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use re '/aa'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 0 | ▶ | use Readonly; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use Perl::Critic::Utils qw{ :severities }; | ||
| 15 | use parent qw{Perl::Critic::Policy}; | ||
| 16 | |||
| 17 | |||
| 18 | Readonly::Scalar my $DESC => q{Arithmetic on literal numbers}; | ||
| 19 | Readonly::Scalar my $EXPL => q{Write the value it computes, with digit separators where they help}; | ||
| 20 | |||
| 21 | # How tightly each operator binds, as perlop lists it: a higher number binds | ||
| 22 | # more tightly. ** is the one that is right associative. | ||
| 23 | Readonly::Hash my %PRECEDENCE => ( | ||
| 24 | q{**} => 4, | ||
| 25 | q{*} => 3, | ||
| 26 | q{/} => 3, | ||
| 27 | q{%} => 3, | ||
| 28 | q{x} => 3, | ||
| 29 | q{+} => 2, | ||
| 30 | q{-} => 2, | ||
| 31 | q{.} => 2, | ||
| 32 | q{<<} => 1, | ||
| 33 | q{>>} => 1, | ||
| 34 | ); | ||
| 35 | Readonly::Hash my %RIGHT_ASSOCIATIVE => ( q{**} => 1 ); | ||
| 36 | |||
| 37 | # The operators that are arithmetic. x and . bind like * and +, so they are in | ||
| 38 | # the table above, but they make strings. | ||
| 39 | Readonly::Hash my %ARITHMETIC => map { $_ => 1 } qw{ ** * / % + - << >> }; | ||
| 40 | |||
| 41 | # Signs that PPI leaves as an operator before a number, as in "5 - - 3". | ||
| 42 | Readonly::Hash my %SIGN => map { $_ => 1 } qw{ + - }; | ||
| 43 | |||
| 44 | |||
| 45 | sub supported_parameters { return () } | ||
| 46 | sub default_severity { return $SEVERITY_LOW } | ||
| 47 | sub default_themes { return qw{ maintenance } } | ||
| 48 | sub applies_to { return 'PPI::Token::Number' } | ||
| 49 | |||
| 50 | # A number, and not a version string. | ||
| 51 | my $is_number = sub { | ||
| 52 | my ($elem) = @_; | ||
| 53 | return $elem && $elem->isa('PPI::Token::Number') && !$elem->isa('PPI::Token::Number::Version'); | ||
| 54 | }; | ||
| 55 | |||
| 56 | my $is_operator = sub { | ||
| 57 | my ( $elem, $table ) = @_; | ||
| 58 | return $elem && $elem->isa('PPI::Token::Operator') && $table->{ $elem->content }; | ||
| 59 | }; | ||
| 60 | |||
| 61 | # The next significant element in the direction that $step names, | ||
| 62 | # sprevious_sibling or snext_sibling, past any signs. A sign is a + or a - with | ||
| 63 | # nothing before it that could be its left operand. | ||
| 64 | my $step_past_signs = sub { | ||
| 65 | my ( $elem, $step ) = @_; | ||
| 66 | my $found = $elem->$step; | ||
| 67 | while ( $is_operator->( $found, \%SIGN ) ) { | ||
| 68 | my $before = $found->sprevious_sibling; | ||
| 69 | last if $before && !$before->isa('PPI::Token::Operator'); | ||
| 70 | $found = $found->$step; | ||
| 71 | } | ||
| 72 | return $found; | ||
| 73 | }; | ||
| 74 | |||
| 75 | # Whether the operand of $op on one side reaches past the run: every operator | ||
| 76 | # between them, and the one at the edge, binds tightly enough to take $op's | ||
| 77 | # operand into its own expression. | ||
| 78 | my $reaches = sub { | ||
| 79 | my ( $op, $right, @between ) = @_; | ||
| 80 | my $mine = $PRECEDENCE{$op}; | ||
| 81 | foreach my $other (@between) { | ||
| 82 | my $theirs = $PRECEDENCE{$other} // return 0; | ||
| 83 | return 0 if $theirs < $mine; | ||
| 84 | return 0 if $theirs == $mine && ( $right ? !$RIGHT_ASSOCIATIVE{$op} : $RIGHT_ASSOCIATIVE{$op} ); | ||
| 85 | } | ||
| 86 | return 1; | ||
| 87 | }; | ||
| 88 | |||
| 89 | |||
| 90 | sub violates { | ||
| 91 | my ( $self, $elem, undef ) = @_; | ||
| 92 | return unless $is_number->($elem); | ||
| 93 | |||
| 94 | # One violation for each expression, from its first number. A number that | ||
| 95 | # an arithmetic operator joins to a literal before it is not first. | ||
| 96 | my $before = $step_past_signs->( $elem, 'sprevious_sibling' ); | ||
| 97 | return if $is_operator->( $before, \%ARITHMETIC ) && $is_number->( $step_past_signs->( $before, 'sprevious_sibling' ) ); | ||
| 98 | |||
| 99 | # The run of literals that arithmetic joins, and the operators at its edges. | ||
| 100 | my @operators; | ||
| 101 | my $last = $elem; | ||
| 102 | while ( my $op = $last->snext_sibling ) { | ||
| 103 | last unless $is_operator->( $op, \%ARITHMETIC ); | ||
| 104 | my $number = $step_past_signs->( $op, 'snext_sibling' ); | ||
| 105 | last unless $is_number->($number); | ||
| 106 | push @operators, $op->content; | ||
| 107 | $last = $number; | ||
| 108 | } | ||
| 109 | return unless @operators; | ||
| 110 | |||
| 111 | my $after = $last->snext_sibling; | ||
| 112 | my $left = $is_operator->( $before, \%PRECEDENCE ) ? $before->content : undef; | ||
| 113 | my $right = $is_operator->( $after, \%PRECEDENCE ) ? $after->content : undef; | ||
| 114 | |||
| 115 | # An operator in the run makes a literal value when neither of its operands | ||
| 116 | # reaches past the run to the operand beyond an edge operator. | ||
| 117 | foreach my $i ( 0 .. $#operators ) { | ||
| 118 | my $op = $operators[$i]; | ||
| 119 | next if defined $left && $reaches->( $op, 0, @operators[ 0 .. $i - 1 ], $left ); | ||
| 120 | next if defined $right && $reaches->( $op, 1, @operators[ $i + 1 .. $#operators ], $right ); | ||
| 121 | return $self->violation( $DESC, $EXPL, $elem ); | ||
| 122 | } | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; | ||
| 127 | |||
| 128 | __END__ | ||