Skip to content

Commit

Permalink
implement passing custom_feature_includes via %ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
wchristian committed Nov 25, 2024
1 parent a730887 commit d5b834f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/PPI/Document.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/PPI/Lexer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 42 additions & 1 deletion t/feature_tracking.t
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 #
[
Expand Down

0 comments on commit d5b834f

Please sign in to comment.