]> git.donarmstrong.com Git - perltidy.git/commitdiff
issue c179, rt #145706, multi-line class statement
authorSteve Hancock <perltidy@users.sourceforge.net>
Fri, 20 Jan 2023 22:19:32 +0000 (14:19 -0800)
committerSteve Hancock <perltidy@users.sourceforge.net>
Fri, 20 Jan 2023 22:19:32 +0000 (14:19 -0800)
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.

lib/Perl/Tidy/Tokenizer.pm
t/snippets/expect/rt145706.def
t/snippets/rt145706.in
t/snippets27.t

index 1122f6a16bab8d37f5f1f86a596e6cc309aff741..e50bf3495f60eb552a984911b60a2f2663ffa486 100644 (file)
@@ -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;
     }
 
index 64f81ef72a1decf8df4570fb3b374909ed158861..6876e31172584033e5afd605aacecb779296ee2f 100644 (file)
@@ -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' }
 };
index 542280f64a168c9ad54fd127e3deaf4707abf667..f3b395182da457ed5a0cf8686deb1c0fbc7cd7d0 100644 (file)
@@ -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' }
 };
index c266b5fd6a8f51dfe4da69f6f5c37adc2f6f8bcd..7d3297020207dc7653b272e4427b990ef62aba30 100644 (file)
@@ -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' }
 };