]> git.donarmstrong.com Git - perltidy.git/commitdiff
remove unknown config options with leading '---', see git #146
authorSteve Hancock <perltidy@users.sourceforge.net>
Sat, 25 May 2024 22:36:05 +0000 (15:36 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Sat, 25 May 2024 22:39:27 +0000 (15:39 -0700)
lib/Perl/Tidy.pm
t/snippets/git146.in [new file with mode: 0644]
t/snippets/git146.par [new file with mode: 0644]

index ac669a2040637702ff1e9570563e1dc60ea5c556..75798c9aa2d3ac1e3266ad90777d2ebba8e14f01 100644 (file)
@@ -4584,8 +4584,11 @@ EOM
 "exiting because profile '$config_file' could not be opened\n"
                 );
             }
+            filter_unknown_options(
+                $rconfig_string, $roption_category,
+                $rexpansion,     $rconfig_file_chatter
+            );
         }
-
         if ($saw_dump_profile) {
             dump_config_file( $rconfig_string, $config_file,
                 $rconfig_file_chatter );
@@ -4608,7 +4611,7 @@ EOM
 
                 if ( !GetOptions( \%Opts, @{$roption_string} ) ) {
                     Die(
-"Error in this config file: $config_file  \nUse -npro to ignore this file, -h for help'\n"
+"Error in this config file: $config_file  \nUse -npro to ignore this file, -dpro to dump it, -h for help'\n"
                     );
                 }
 
@@ -5582,6 +5585,66 @@ sub dump_config_file {
     return;
 } ## end sub dump_config_file
 
+sub filter_unknown_options {
+
+    my (
+        $rconfig_string, $roption_category,
+        $rexpansion,     $rconfig_file_chatter
+    ) = @_;
+
+    # Look through the configuration file for lines beginning with '---' and
+    # - remove the line if the option is unknown, or
+    # - remove the extra dash if the option is known
+    # See git #146 for discussion
+
+    # Given:
+    #   $rconfig_string = string ref to a .perltidyrc configuration file
+    #   $roption_category = ref to hash with long_names as key
+    #   $rexpansion = ref to hash with abbreviations as key
+    #   $rconfig_file_chatter = messages displayed in --dump-profile
+    #
+    # Update: $rconfig_string and $rconfig_file_chatter
+
+    # quick check to skip most files
+    if ( ${$rconfig_string} !~ /^\s*---\w/m ) { return }
+
+    my @lines = split /^/, ${$rconfig_string};
+    my $new_config_string;
+    my $change_notices = EMPTY_STRING;
+    while ( defined( my $line = shift @lines ) ) {
+        chomp $line;
+
+        # look for lines beginning with '---'
+        if ( $line && $line =~ /^\s*---(\w[\w-]*)/ ) {
+            my $word = $1;
+
+            # first look for a long name or an abbreviation
+            my $is_known = $roption_category->{$word} || $rexpansion->{$word};
+
+            # then look for prefix 'no' or 'no-' on a long name
+            if ( !$is_known && $word =~ s/^no-?// ) {
+                $is_known = $roption_category->{$word};
+            }
+
+            if ( !$is_known ) {
+                $change_notices .= "#  removing unknown option line $line\n";
+                next;
+            }
+            else {
+                $change_notices .= "#  accepting and fixing line $line\n";
+                $line =~ s/-//;
+            }
+        }
+        $new_config_string .= $line . "\n";
+    }
+
+    if ($change_notices) {
+        ${$rconfig_file_chatter} .= "# Filter operations:\n" . $change_notices;
+        ${$rconfig_string} = $new_config_string;
+    }
+    return;
+} ## end sub filter_unknown_options
+
 sub read_config_file {
 
     my ( $rconfig_string, $config_file, $rexpansion ) = @_;
diff --git a/t/snippets/git146.in b/t/snippets/git146.in
new file mode 100644 (file)
index 0000000..8d766e4
--- /dev/null
@@ -0,0 +1,6 @@
+            my %strips = (
+                1 => [
+                    [ [ 1750, 150, ], [ 1850, 150, ], ],
+                    [ [ 1950, 150, ], [ 2050, 150, ], ],
+                ]
+            );
diff --git a/t/snippets/git146.par b/t/snippets/git146.par
new file mode 100644 (file)
index 0000000..117ba07
--- /dev/null
@@ -0,0 +1,4 @@
+# testing three dash parameters
+---add-trailing-commas 
+---unknown-future-option
+---wtc=h