From: Steve Hancock Date: Fri, 20 Jan 2023 22:19:32 +0000 (-0800) Subject: issue c179, rt #145706, multi-line class statement X-Git-Tag: 20221112.04~9 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=fa844fb302358ccda38953d71137ea9ecb849d97;p=perltidy.git issue c179, rt #145706, multi-line class statement This update improves the ability to automatically identify an older style class statement if it spans multiple lines. This avoids a needless warning message and allows the default to be --use-feature=class. --- diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index 1122f6a1..e50bf349 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -2269,13 +2269,16 @@ EOM # true otherwise (even if not sure) # We are trying to avoid problems with old uses of 'class' - # when --use-feature=class is set (rt145706). + # when --use-feature=class is set (rt145706). We look ahead + # see if this use of 'class' is obviously inconsistent with + # the syntax of use feature 'class'. This allows the default + # setting --use-feature=class to work for old syntax too. # Valid class declarations look like - # class NAME ATTRS VERSION BLOCK + # class NAME ?ATTRS ?VERSION ?BLOCK # where ATTRS VERSION and BLOCK are optional - # For example, this should cause a return of 'false': + # For example, this should produce a return of 'false': # # class ExtendsBasicAttributes is BasicAttributes{ @@ -2301,34 +2304,33 @@ EOM return; } - # TEST 3: look for invalid characters after NAME - if ( $input_line =~ m/\s*(\S)/gcx ) { + # TEST 3: look for valid characters after NAME + my $next_char = EMPTY_STRING; + if ( $input_line =~ m/\s*(\S)/gcx ) { $next_char = $1 } + if ( !$next_char || $next_char eq '#' ) { + ( $next_char, my $i_next ) = + find_next_nonblank_token( $max_token_index, + $rtokens, $max_token_index ); + } + return unless ($next_char); - my $char = $1; + # Must see one of: ATTRIBUTE, VERSION, BLOCK, or end stmt - # Must see one of: ATTRIBUTE, VERSION, BLOCK, or end stmt + # Possibly valid next token types: + # ':' could start ATTRIBUTE + # '\d' could start VERSION + # '{' cold start BLOCK + # ';' could end a statement + # '}' could end statement but would be strange - # Possibly valid next token types: - # ':' could start ATTRIBUTE - # '\d' could start VERSION - # '{' cold start BLOCK - # ';' could end a statement - # '}' could end statement but would be strange - if ( $char =~ /^[\:\d\{\;\}]/ ) { return 1 } - - # stop at a side comment - assume ok for now - if ( $char eq '#' ) { return 1 } + if ( $next_char !~ /^[\:\d\{\;\}]/ ) { - # Nothing else should be okay + # This does not match use feature 'class' syntax return; - - # non-dight letter - not ok - # ( this must be checked after \d ) - ##OLD: if ( $tok =~ /^\w/) { return } } - # TBD: Still uncertain; may be at end of line. - # We could continue will stop here and assume ok. + # We will stop here and assume that this is valid syntax for + # use feature 'class'. return 1; } diff --git a/t/snippets/expect/rt145706.def b/t/snippets/expect/rt145706.def index 64f81ef7..6876e311 100644 --- a/t/snippets/expect/rt145706.def +++ b/t/snippets/expect/rt145706.def @@ -20,10 +20,19 @@ ADJUST { method paint => sub { ...; }; + +method painter + + => sub { + ...; + }; is( ( method Pack "a", "b", "c" ), "method,a,b,c" ); class ExtendsBasicAttributes is BasicAttributes { ... } +class BrokenExtendsBasicAttributes is BasicAttributes { + ... +} class +Night with +Bad { public nine { return 'crazy' } }; diff --git a/t/snippets/rt145706.in b/t/snippets/rt145706.in index 542280f6..f3b39518 100644 --- a/t/snippets/rt145706.in +++ b/t/snippets/rt145706.in @@ -20,10 +20,19 @@ ADJUST { method paint => sub { ...; }; +method painter + + => sub { + ...; + }; is( ( method Pack "a", "b", "c" ), "method,a,b,c" ); class ExtendsBasicAttributes is BasicAttributes{ ... } +class BrokenExtendsBasicAttributes +is BasicAttributes{ + ... +} class +Night with +Bad { public nine { return 'crazy' } }; diff --git a/t/snippets27.t b/t/snippets27.t index c266b5fd..7d329702 100644 --- a/t/snippets27.t +++ b/t/snippets27.t @@ -179,10 +179,19 @@ ADJUST { method paint => sub { ...; }; +method painter + + => sub { + ...; + }; is( ( method Pack "a", "b", "c" ), "method,a,b,c" ); class ExtendsBasicAttributes is BasicAttributes{ ... } +class BrokenExtendsBasicAttributes +is BasicAttributes{ + ... +} class +Night with +Bad { public nine { return 'crazy' } }; @@ -913,10 +922,19 @@ ADJUST { method paint => sub { ...; }; + +method painter + + => sub { + ...; + }; is( ( method Pack "a", "b", "c" ), "method,a,b,c" ); class ExtendsBasicAttributes is BasicAttributes { ... } +class BrokenExtendsBasicAttributes is BasicAttributes { + ... +} class +Night with +Bad { public nine { return 'crazy' } };