From 24c9fb611a2c90099ad39736642cb628f2d5fd98 Mon Sep 17 00:00:00 2001 From: Christian Walde Date: Mon, 25 Nov 2024 11:17:12 +0100 Subject: [PATCH] implement passing custom_feature_includes via %ENV --- Changes | 1 + lib/PPI/Document.pm | 15 +++++++++++++++ lib/PPI/Lexer.pm | 2 +- t/feature_tracking.t | 43 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/Changes b/Changes index 00d158b0..24a5a389 100644 --- a/Changes +++ b/Changes @@ -12,6 +12,7 @@ Revision history for Perl extension PPI - PPI::Document->new( feature_mods => ... ) - PPI::Document->new( custom_feature_includes => ... ) - PPI::Document->new( custom_feature_include_cb => ... ) + - $ENV{PPI_CUSTOM_FEATURE_INCLUDES} - Added ability to parse features: - signatures, as PPI::Structure::Signature - try catch, as PPI::Statement::Compound diff --git a/lib/PPI/Document.pm b/lib/PPI/Document.pm index 1ffed88e..a0bac381 100644 --- a/lib/PPI/Document.pm +++ b/lib/PPI/Document.pm @@ -155,6 +155,12 @@ which act like pragmas that enable parsing features within their scope. This is mostly useful when your work project has its own boilerplate module. +It can also be provided in the environment variable +PPI_CUSTOM_FEATURE_INCLUDES, like so: + + PPI_CUSTOM_FEATURE_INCLUDES='{strEct=>{signatures=>"perl"}}' \ + perlcritic lib/OurModule.pm + =head3 custom_feature_include_cb custom_feature_include_cb => sub { @@ -263,6 +269,15 @@ sub _setattr { $document->{feature_mods} = $attr{feature_mods}; $document->{custom_feature_includes} = $attr{custom_feature_includes}; $document->{custom_feature_include_cb} = $attr{custom_feature_include_cb}; + if ( $ENV{PPI_CUSTOM_FEATURE_INCLUDES} ) { + my $includes = eval $ENV{PPI_CUSTOM_FEATURE_INCLUDES}; + die "\$ENV{PPI_CUSTOM_FEATURE_INCLUDES} " + . "does not contain valid perl:\n" + . "val: '$ENV{PPI_CUSTOM_FEATURE_INCLUDES}'\nerr: $@" + if $@; + $document->{custom_feature_includes} = + { %{ $document->{custom_feature_includes} || {} }, %{$includes} }; + } return $document; } diff --git a/lib/PPI/Lexer.pm b/lib/PPI/Lexer.pm index fb39e33d..00b4363d 100644 --- a/lib/PPI/Lexer.pm +++ b/lib/PPI/Lexer.pm @@ -218,7 +218,7 @@ sub lex_tokenizer { # Create the empty document my $Document = PPI::Document->new; - ref($Document)->_setattr( $Document, %args ) if keys %args; + ref($Document)->_setattr( $Document, %args ); $Tokenizer->_document($Document); # Lex the token stream into the document diff --git a/t/feature_tracking.t b/t/feature_tracking.t index 71f8b389..7c126586 100644 --- a/t/feature_tracking.t +++ b/t/feature_tracking.t @@ -2,7 +2,7 @@ use lib 't/lib'; use PPI::Test::pragmas; -use Test::More tests => 8 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); +use Test::More tests => 9 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 ); use B 'perlstring'; @@ -239,6 +239,47 @@ END_PERL "simple custom boilerplate modules"; } +ENV_HOMEBREW_ARGS: { + local $ENV{PPI_CUSTOM_FEATURE_INCLUDES} = "{strEct=>{signatures=>'perl'}}"; + test_document + <<'END_PERL', + use strEct; + sub meep($) {} + sub marp($left, $right) {} +END_PERL + [ + 'PPI::Statement::Include', 'use strEct;', + 'PPI::Token::Word', 'use', + 'PPI::Token::Word', 'strEct', + 'PPI::Token::Structure', ';', + 'PPI::Statement::Sub', 'sub meep($) {}', + 'PPI::Token::Word', 'sub', + 'PPI::Token::Word', 'meep', + 'PPI::Structure::Signature', '($)', + 'PPI::Token::Structure', '(', + 'PPI::Statement::Expression', '$', + 'PPI::Token::Symbol', '$', + 'PPI::Token::Structure', ')', + 'PPI::Structure::Block', '{}', + 'PPI::Token::Structure', '{', + 'PPI::Token::Structure', '}', + 'PPI::Statement::Sub', 'sub marp($left, $right) {}', + 'PPI::Token::Word', 'sub', + 'PPI::Token::Word', 'marp', + 'PPI::Structure::Signature', '($left, $right)', + 'PPI::Token::Structure', '(', + 'PPI::Statement::Expression', '$left, $right', + 'PPI::Token::Symbol', '$left', + 'PPI::Token::Operator', ',', + 'PPI::Token::Symbol', '$right', + 'PPI::Token::Structure', ')', + 'PPI::Structure::Block', '{}', + 'PPI::Token::Structure', '{', + 'PPI::Token::Structure', '}', + ], + "simple custom boilerplate modules"; +} + HOMEBREW_CB: { test_document # [