]> git.donarmstrong.com Git - perltidy.git/commitdiff
fix for git #143
authorSteve Hancock <perltidy@users.sourceforge.net>
Wed, 15 May 2024 23:12:36 +0000 (16:12 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Wed, 15 May 2024 23:12:36 +0000 (16:12 -0700)
bin/perltidy
lib/Perl/Tidy/Formatter.pm
t/snippets/expect/git143.def [new file with mode: 0644]
t/snippets/expect/git143.git143 [new file with mode: 0644]
t/snippets/expect/wtc.wtc2
t/snippets/git143.in [new file with mode: 0644]
t/snippets/git143.par [new file with mode: 0644]
t/snippets/packing_list.txt
t/snippets27.t
t/snippets30.t [new file with mode: 0644]

index 77d21cf2bf5048ce799a43c4f8f546321385c539..f639bbabf2d657e6b03c6dc62ac3b46d80b05261 100755 (executable)
@@ -3870,8 +3870,8 @@ Here are some points to note regarding adding and deleting trailing commas:
 
 For the implementation of these parameters, a B<list> 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.
index 849c3b64d6d3e4bc11b6ad2828aa1eaf2d5ff513..146d7e987e5fdcac4c2ef23185a2099406d599f3 100644 (file)
@@ -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 (file)
index 0000000..08b050c
--- /dev/null
@@ -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 (file)
index 0000000..8a01422
--- /dev/null
@@ -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, ], ],
+                ],
+            );
index 57aa571c4c2ef92794d142a26c644bf0dbb0f9b8..a16c9df7b16b0e3244f78922b2e6fc5911dcba92 100644 (file)
@@ -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 (file)
index 0000000..61e88b2
--- /dev/null
@@ -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 (file)
index 0000000..e3a1bbd
--- /dev/null
@@ -0,0 +1 @@
+-atc -wtc=h
index c51a0d7b77eb5a3837d236b23da3a325dd13bbfd..6e2ce47bf87414aedac5959035dce5cd708a3d44 100644 (file)
 ../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
 ../snippets9.t rt98902.def
 ../snippets9.t rt98902.rt98902
 ../snippets9.t rt99961.def
-../snippets29.t        vsn.vsn3
+../snippets30.t        git143.def
+../snippets30.t        git143.git143
index f6c497d1a73222cf89f15a6df44aaf10a564b048..20a538ef5e6346830b6919b3c6615e2751244616 100644 (file)
@@ -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 (file)
index 0000000..fee06e9
--- /dev/null
@@ -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 "<<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";
+            }
+        }
+    }
+}