From cb5c126e8282b8b63188cddb9f4f4eae07db6e33 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Wed, 15 May 2024 16:12:36 -0700 Subject: [PATCH] fix for git #143 --- bin/perltidy | 4 +- lib/Perl/Tidy/Formatter.pm | 50 +++++++++--- t/snippets/expect/git143.def | 7 ++ t/snippets/expect/git143.git143 | 7 ++ t/snippets/expect/wtc.wtc2 | 2 +- t/snippets/git143.in | 7 ++ t/snippets/git143.par | 1 + t/snippets/packing_list.txt | 4 +- t/snippets27.t | 2 +- t/snippets30.t | 135 ++++++++++++++++++++++++++++++++ 10 files changed, 204 insertions(+), 15 deletions(-) create mode 100644 t/snippets/expect/git143.def create mode 100644 t/snippets/expect/git143.git143 create mode 100644 t/snippets/git143.in create mode 100644 t/snippets/git143.par create mode 100644 t/snippets30.t diff --git a/bin/perltidy b/bin/perltidy index 77d21cf2..f639bbab 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -3870,8 +3870,8 @@ Here are some points to note regarding adding and deleting trailing commas: For the implementation of these parameters, a B is basically taken to be a container of items (parens, square brackets, or braces), which is not a code -block, with one or more commas. These parameters only apply to something that -fits this definition of a list. +block, with one or more commas or fat commas. These parameters only apply to +something that fits this definition of a list. Note that a paren-less list of parameters is not a list by this definition, so these parameters have no effect on a paren-less list. diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 849c3b64..146d7e98 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -10845,7 +10845,7 @@ sub respace_tokens_inner_loop { # if this is a list .. my $rtype_count = $rtype_count_by_seqno->{$type_sequence}; if ( $rtype_count - && $rtype_count->{','} + && ( $rtype_count->{','} || $rtype_count->{'=>'} ) && !$rtype_count->{';'} && !$rtype_count->{'f'} ) { @@ -11267,6 +11267,18 @@ EOM next; } } + + # remember input line index of first '=>' if -wtc is used + if (%trailing_comma_rules) { + my $seqno = $seqno_stack{ $depth_next - 1 }; + if ( defined($seqno) + && !defined( $self->[_rfirst_comma_line_index_]->{$seqno} ) + ) + { + $self->[_rfirst_comma_line_index_]->{$seqno} = + $rtoken_vars->[_LINE_INDEX_]; + } + } } # change 'LABEL :' to 'LABEL:' @@ -12395,7 +12407,10 @@ sub match_trailing_comma_rule { return unless ($type_sequence); my $closing_token = $rLL->[$KK]->[_TOKEN_]; my $rtype_count = $self->[_rtype_count_by_seqno_]->{$type_sequence}; - return unless ( defined($rtype_count) && $rtype_count->{','} ); + return unless defined($rtype_count); + my $comma_count = $rtype_count->{','}; + my $fat_comma_count = $rtype_count->{'=>'}; + return unless ( $comma_count || $fat_comma_count ); my $is_permanently_broken = $self->[_ris_permanently_broken_]->{$type_sequence}; @@ -12456,7 +12471,7 @@ sub match_trailing_comma_rule { # 'm' matches a Multiline list #----------------------------- elsif ( $trailing_comma_style eq 'm' ) { - $match = $is_multiline; + $match = $is_multiline && $comma_count; } #---------------------------------- @@ -12480,7 +12495,7 @@ sub match_trailing_comma_rule { # There must be no more than one comma per line for both 'h' and 'i' # The new_comma_count here will include the trailing comma. - my $new_comma_count = $rtype_count->{','}; + my $new_comma_count = $comma_count; $new_comma_count += 1 if ($if_add); my $excess_commas = $new_comma_count - $line_diff_commas - 1; if ( $excess_commas > 0 ) { @@ -12521,13 +12536,28 @@ sub match_trailing_comma_rule { } } - # a list of key=>value pairs with at least 2 fat commas is a match - # for both 'h' and 'i' - my $fat_comma_count = $rtype_count->{'=>'}; - if ( !$match && $fat_comma_count && $fat_comma_count >= 2 ) { + # check fat commas + if ( + !$match + && $fat_comma_count + && ( + + # - a list of key=>value pairs with at least 2 fat commas is a + # match for both 'h' and 'i' + $fat_comma_count >= 2 + + # - an isolated fat comma is a match for type 'h' + || ( $fat_comma_count == 1 + && $new_comma_count == 1 + && $if_add + && $trailing_comma_style eq 'h' ) + ) + ) + { - # comma count (including trailer) and fat comma count must differ by - # by no more than 1. This allows for some small variations. + # but comma count (including trailer) and fat comma count must + # differ by by no more than 1. This allows for some small + # variations. my $comma_diff = $new_comma_count - $fat_comma_count; $match = ( $comma_diff >= -1 && $comma_diff <= 1 ); } diff --git a/t/snippets/expect/git143.def b/t/snippets/expect/git143.def new file mode 100644 index 00000000..08b050c5 --- /dev/null +++ b/t/snippets/expect/git143.def @@ -0,0 +1,7 @@ + # include '=>' in comma count to allow adding trailing comma here + my %strips = ( + 1 => [ + [ [ 1750, 150, ], [ 1850, 150, ], ], + [ [ 1950, 150, ], [ 2050, 150, ], ], + ] + ); diff --git a/t/snippets/expect/git143.git143 b/t/snippets/expect/git143.git143 new file mode 100644 index 00000000..8a014220 --- /dev/null +++ b/t/snippets/expect/git143.git143 @@ -0,0 +1,7 @@ + # include '=>' in comma count to allow adding trailing comma here + my %strips = ( + 1 => [ + [ [ 1750, 150, ], [ 1850, 150, ], ], + [ [ 1950, 150, ], [ 2050, 150, ], ], + ], + ); diff --git a/t/snippets/expect/wtc.wtc2 b/t/snippets/expect/wtc.wtc2 index 57aa571c..a16c9df7 100644 --- a/t/snippets/expect/wtc.wtc2 +++ b/t/snippets/expect/wtc.wtc2 @@ -42,5 +42,5 @@ $dasm_frame->Button( my $no_index_1_1 = { 'map' => - { ':key' => { name => \&string, list => { value => \&string }, }, }, }; + { ':key' => { name => \&string, list => { value => \&string, }, }, }, }; diff --git a/t/snippets/git143.in b/t/snippets/git143.in new file mode 100644 index 00000000..61e88b21 --- /dev/null +++ b/t/snippets/git143.in @@ -0,0 +1,7 @@ +# include '=>' in comma count to allow adding trailing comma here + my %strips = ( + 1 => [ + [ [ 1750, 150, ], [ 1850, 150, ], ], + [ [ 1950, 150, ], [ 2050, 150, ], ], + ] + ); diff --git a/t/snippets/git143.par b/t/snippets/git143.par new file mode 100644 index 00000000..e3a1bbd6 --- /dev/null +++ b/t/snippets/git143.par @@ -0,0 +1 @@ +-atc -wtc=h diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index c51a0d7b..6e2ce47b 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -421,6 +421,7 @@ ../snippets29.t git137.git137 ../snippets29.t git138.def ../snippets29.t git138.git138 +../snippets29.t vsn.vsn3 ../snippets3.t ce_wn1.ce_wn ../snippets3.t ce_wn1.def ../snippets3.t colin.colin @@ -561,4 +562,5 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def -../snippets29.t vsn.vsn3 +../snippets30.t git143.def +../snippets30.t git143.git143 diff --git a/t/snippets27.t b/t/snippets27.t index f6c497d1..20a538ef 100644 --- a/t/snippets27.t +++ b/t/snippets27.t @@ -378,7 +378,7 @@ $dasm_frame->Button( my $no_index_1_1 = { 'map' => - { ':key' => { name => \&string, list => { value => \&string }, }, }, }; + { ':key' => { name => \&string, list => { value => \&string, }, }, }, }; #2........... }, diff --git a/t/snippets30.t b/t/snippets30.t new file mode 100644 index 00000000..fee06e9b --- /dev/null +++ b/t/snippets30.t @@ -0,0 +1,135 @@ +# Created with: ./make_t.pl + +# Contents: +#1 git143.def +#2 git143.git143 + +# To locate test #13 you can search for its name or the string '#13' + +use strict; +use Test::More; +use Carp; +use Perl::Tidy; +my $rparams; +my $rsources; +my $rtests; + +BEGIN { + + ########################################### + # BEGIN SECTION 1: Parameter combinations # + ########################################### + $rparams = { + 'def' => "", + 'git143' => "-atc -wtc=h", + }; + + ############################ + # BEGIN SECTION 2: Sources # + ############################ + $rsources = { + + 'git143' => <<'----------', +# include '=>' in comma count to allow adding trailing comma here + my %strips = ( + 1 => [ + [ [ 1750, 150, ], [ 1850, 150, ], ], + [ [ 1950, 150, ], [ 2050, 150, ], ], + ] + ); +---------- + }; + + #################################### + # BEGIN SECTION 3: Expected output # + #################################### + $rtests = { + + 'git143.def' => { + source => "git143", + params => "def", + expect => <<'#1...........', + # include '=>' in comma count to allow adding trailing comma here + my %strips = ( + 1 => [ + [ [ 1750, 150, ], [ 1850, 150, ], ], + [ [ 1950, 150, ], [ 2050, 150, ], ], + ] + ); +#1........... + }, + + 'git143.git143' => { + source => "git143", + params => "git143", + expect => <<'#2...........', + # include '=>' in comma count to allow adding trailing comma here + my %strips = ( + 1 => [ + [ [ 1750, 150, ], [ 1850, 150, ], ], + [ [ 1950, 150, ], [ 2050, 150, ], ], + ], + ); +#2........... + }, + }; + + my $ntests = 0 + keys %{$rtests}; + plan tests => $ntests; +} + +############### +# EXECUTE TESTS +############### + +foreach my $key ( sort keys %{$rtests} ) { + my $output; + my $sname = $rtests->{$key}->{source}; + my $expect = $rtests->{$key}->{expect}; + my $pname = $rtests->{$key}->{params}; + my $source = $rsources->{$sname}; + my $params = defined($pname) ? $rparams->{$pname} : ""; + my $stderr_string; + my $errorfile_string; + my $err = Perl::Tidy::perltidy( + source => \$source, + destination => \$output, + perltidyrc => \$params, + argv => '', # for safety; hide any ARGV from perltidy + stderr => \$stderr_string, + errorfile => \$errorfile_string, # not used when -se flag is set + ); + if ( $err || $stderr_string || $errorfile_string ) { + print STDERR "Error output received for test '$key'\n"; + if ($err) { + print STDERR "An error flag '$err' was returned\n"; + ok( !$err ); + } + if ($stderr_string) { + print STDERR "---------------------\n"; + print STDERR "<>\n$stderr_string\n"; + print STDERR "---------------------\n"; + ok( !$stderr_string ); + } + if ($errorfile_string) { + print STDERR "---------------------\n"; + print STDERR "<<.ERR file>>\n$errorfile_string\n"; + print STDERR "---------------------\n"; + ok( !$errorfile_string ); + } + } + else { + if ( !is( $output, $expect, $key ) ) { + my $leno = length($output); + my $lene = length($expect); + if ( $leno == $lene ) { + print STDERR +"#> Test '$key' gave unexpected output. Strings differ but both have length $leno\n"; + } + else { + print STDERR +"#> Test '$key' gave unexpected output. String lengths differ: output=$leno, expected=$lene\n"; + } + } + } +} -- 2.39.5