From 393713bb6fd212baac65e11fc0f0f2edd1a9f294 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Wed, 18 Dec 2019 07:37:59 -0800 Subject: [PATCH] Improved semicolon deletion rules --- lib/Perl/Tidy/Formatter.pm | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index d1b1e320..634b5e8c 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -3025,26 +3025,36 @@ sub respace_tokens { ) { - my $has_side_comment; + # This looks like a deletable semicolon, but even if a + # semicolon can be deleted it is necessarily best to do so. + # We apply these additional rules for deletion: + # - Always ok to delete a ';' at the end of a line + # - Never delete a ';' before a '#' because it would + # promote it to a block comment. + # - If a semicolon is not at the end of line, then only + # delete if it is followed by another semicolon or closing + # token. This includes the comment rule. It may take + # two passes to get to a final state, but it is a little + # safer. For example, keep the first semicolon here: + # eval { sub bubba { ok(0) }; ok(0) } || ok(1); + # It is not required but adds some clarity. + my $ok_to_delete = 1; if ( $KK < $Klast ) { my $Kn = $self->K_next_nonblank($KK); my $next_nonblank_token_type = ""; if ( defined($Kn) && $Kn <= $Klast ) { $next_nonblank_token_type = $rLL->[$Kn]->[_TYPE_]; - $has_side_comment = - $next_nonblank_token_type eq '#'; + $ok_to_delete = $next_nonblank_token_type eq ';' + || $next_nonblank_token_type eq '}'; } } - # don't delete ; before a # because it would promote it - # to a block comment - if ( !$has_side_comment ) { + if ($ok_to_delete) { note_deleted_semicolon(); next; } else { - write_logfile_entry( - "Extra ';' at line $input_line_number\n"); + write_logfile_entry("Extra ';'\n"); } } } -- 2.39.5