From c831f366ce43d601abf49edf57b7c75992b625e1 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Sat, 10 Feb 2024 18:32:18 -0800 Subject: [PATCH] update -drc to delete repeated fat commas --- CHANGES.md | 11 +++++++++++ bin/perltidy | 34 +++++++++++++++++++++++++++++++++- lib/Perl/Tidy/Formatter.pm | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 122432b7..719c42c8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,17 @@ ## 2024 02 02.01 + - The option --delete-repeated-commas, -drc has been expanded as follows: + - Repeated commas like ',,' on the same line are removed with a warning + - Repeated fat commas like '=> =>' on the same line are removed with a + warning + - Repeated commas and fat commas on different lines remain unchanged + but produce a warning + - The combination '=>,' produces a warning but is not changed (it is + likely a serious error but only its author would know how to fix it). + These warnings are only output if the --warnings flag is set. + The -drc option is off by default; this could change in the future. + - Added control --delete-interbracket-arrows, or -dia, to delete optional hash ref and array ref arrows between brackets as in the following expression (see git #131) diff --git a/bin/perltidy b/bin/perltidy index 97e4b865..ab431fb0 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -3707,8 +3707,40 @@ we can remove it with -drc # perltidy -drc: ignoreSpec( $file, "file", \%spec, \%Rspec ); -Since the default is not to add or delete commas, this feature is off by default and must be requested. +This parameter also deletes repeated fat commas, '=>'. The complete list of +actions taken when this flag is set are as follows: +=over 4 + +=item * + +Repeated commas like ',,' on the same line are removed with a warning. + +=item * +Repeated fat commas like '=> =>' on the same line are removed with a warning. + +=item * + +Repeated commas and fat commas on different lines remain unchanged +but produce a warning. + +=item * + +The combination '=>,' produces a warning but is not changed (it is +likely an error but only its author would know how to fix it). + +=item * + +Note that the special combination ',=>' ('winking fat comma') is ignored +by this parameter. + +=item * + +These warnings are only output if the B<--warnings> flag is set. + +=back + +This feature is currently off by default and must be requested. =item B<--want-trailing-commas=s> or B<-wtc=s>, B<--add-trailing-commas> or B<-atc>, and B<--delete-trailing-commas> or B<-dtc> diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 173a029e..4dd82808 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -11329,15 +11329,26 @@ EOM # do not delete a comma repeated on a different line - # this can cause problems, such as promoting a side comment # to a block comment. See test 'mangle4.in' + my $lno = 1 + $rLL->[$KK]->[_LINE_INDEX_]; if ( $KK == $Kfirst ) { - ## but a warning could be issued + complain( + "repeated commas across multiple lines, not deleted\n", + $lno + ); } else { # Could note this deletion as a possible future update: ## $self->note_deleted_comma($input_line_number); + complain( "deleted repeated ','\n", $lno ); next; } } + elsif ($last_nonblank_code_type eq '=>' + && $rOpts->{'delete-repeated-commas'} ) + { + my $lno = 1 + $rLL->[$KK]->[_LINE_INDEX_]; + complain( "found '=>,' ... error?\n", $lno ); + } # remember input line index of first comma if -wtc is used if (%trailing_comma_rules) { @@ -11351,6 +11362,29 @@ EOM } } } + elsif ( $type eq '=>' ) { + if ( $last_nonblank_code_type eq '=>' + && $rOpts->{'delete-repeated-commas'} ) + { + + # Check for repeated '=>'s + # Note that ',=>' is useful and called a winking fat comma + + # Do not delete a fat comma repeated on a different line - + # this can cause problems, such as promoting a side comment + # to a block comment. See test 'mangle4.in' + my $lno = 1 + $rLL->[$KK]->[_LINE_INDEX_]; + if ( $KK == $Kfirst ) { + complain( + "-repeated '=>' acriss multiple lines, not deleted\n", + $lno ); + } + else { + complain( "deleted repeated '=>'\n", $lno ); + next; + } + } + } # change 'LABEL :' to 'LABEL:' elsif ( $type eq 'J' ) { -- 2.39.5