1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
package Perl::PrereqScanner::NotQuiteLite::Parser::AnyMoose;
use strict;
use warnings;
use Perl::PrereqScanner::NotQuiteLite::Util;
sub register {{
use => {
'Any::Moose' => 'parse_any_moose_args',
},
no => {
'Any::Moose' => 'remove_extends_and_with',
},
}}
sub parse_any_moose_args {
my ($class, $c, $used_module, $raw_tokens) = @_;
my $tokens = convert_string_tokens($raw_tokens);
if (is_version($tokens->[0])) {
$c->add($used_module => shift @$tokens);
}
while(my $token = shift @$tokens) {
next if ref $token;
# As Any::Moose falls back to Mouse, it's nice to have
# a Mouse variant, but that should not be required.
my $module = "Mouse$token";
$c->add_recommendation($module => 0) if is_module_name($module);
}
$c->register_keyword_parser(
'extends',
[$class, 'parse_extends_args', $used_module],
);
$c->register_keyword_parser(
'with',
[$class, 'parse_with_args', $used_module],
);
}
sub remove_extends_and_with {
my ($class, $c, $used_module, $raw_tokens) = @_;
$c->remove_keyword('extends');
$c->remove_keyword('with');
}
sub parse_extends_args { shift->_parse_loader_args(@_) }
sub parse_with_args { shift->_parse_loader_args(@_) }
sub _parse_loader_args {
my ($class, $c, $used_module, $raw_tokens) = @_;
my $tokens = convert_string_tokens($raw_tokens);
shift @$tokens; # discard extends, with;
my $prev;
my $saw_any_moose;
while(my $token = shift @$tokens) {
if (!ref $token) {
if ($saw_any_moose) {
my $module = "Mouse$token";
$c->add_recommendation($module => 0);
$prev = $module;
} else {
$c->add($token => 0);
$prev = $token;
}
next;
}
if ($token->[0] eq 'any_moose') {
$saw_any_moose = 1;
next;
}
my $desc = $token->[1] || '';
if ($desc eq '{}') {
my @hash_tokens = @{$token->[0] || []};
for(my $i = 0, my $len = @hash_tokens; $i < $len; $i++) {
if ($hash_tokens[$i][0] eq '-version' and $i < $len - 2) {
my $maybe_version_token = $hash_tokens[$i + 2];
my $maybe_version = $maybe_version_token->[0];
if (ref $maybe_version) {
$maybe_version = $maybe_version->[0];
}
if ($prev and is_version($maybe_version)) {
if ($saw_any_moose) {
$c->add_recommendation($prev => $maybe_version);
} else {
$c->add($prev => $maybe_version);
}
}
}
}
}
if ($saw_any_moose and $desc eq '()') {
my $tokens_in_parentheses = convert_string_tokens($token->[0]);
for my $token_in_parentheses (@$tokens_in_parentheses) {
next if ref $token_in_parentheses;
my $module = "Mouse$token_in_parentheses";
$c->add_recommendation($module => 0) if is_module_name($module);
}
}
}
}
1;
__END__
=encoding utf-8
=head1 NAME
Perl::PrereqScanner::NotQuiteLite::Parser::AnyMoose
=head1 DESCRIPTION
This parser is to deal with modules loaded by C<extends>
from L<Any::Moose> and its friends.
=head1 AUTHOR
Kenichi Ishigaki, E<lt>ishigaki@cpan.orgE<gt>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2017 by Kenichi Ishigaki.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|