Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
id_dsa
id_rsa
/blib/
/.build/
_build/
cover_db/
inc/
Build
!Build/
Build.bat
.last_cover_stats
/Makefile
/Makefile.old
/MANIFEST.bak
/META.yml
/META.json
/MYMETA.*
nytprof.out
/pm_to_blib
*.o
*.bs
*.swp
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
dist: precise
group: deprecated-2017Q1
sudo: false
language: perl
matrix:
fast_finish: true
perl:
- '5.32'
- '5.30'
- '5.26'
- '5.24'
- '5.22'
- '5.20'
before_install:
- git config --global user.name "TravisCI"
- git config --global user.email $HOSTNAME":not-for-mail@travis-ci.org"
install:
- cpanm --quiet --notest Dist::Zilla Dist::Zilla::App::Command::cover App::cpm Devel::Cover::Report::Codecov
- cpm install -w 2 --mirror=http://cpan.cpantesters.org/ -g $(dzil authordeps --missing)
- cpm install -w 2 --mirror=http://cpan.cpantesters.org/ -g $(dzil listdeps --author --missing)

script:
- dzil smoke --release --author && dzil cover -test -report codecov && dzil xtest
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{$NEXT}}

0.001 2022-08-09 22:28:31+08:00 Asia/Shanghai
Initial release

400 changes: 379 additions & 21 deletions LICENSE

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.025.
use strict;
use warnings;

use 5.006;

use ExtUtils::MakeMaker;

my %WriteMakefileArgs = (
"ABSTRACT" => "Always make the package/Object::Pad class explicit.",
"AUTHOR" => "DERIV <DERIV\@cpan.org>",
"CONFIGURE_REQUIRES" => {
"ExtUtils::MakeMaker" => 0
},
"DISTNAME" => "Perl-Critic-Policy-Modules-RequireExplicitPackage-ObjectPad",
"LICENSE" => "perl",
"MIN_PERL_VERSION" => "5.006",
"NAME" => "Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad",
"PREREQ_PM" => {
"Class::Method::Modifiers" => "2.13",
"Perl::Critic::Policy::Modules::RequireExplicitPackage" => "1.134",
"indirect" => "0.37",
"parent" => 0,
"strict" => 0,
"warnings" => 0
},
"TEST_REQUIRES" => {
"ExtUtils::MakeMaker" => 0,
"File::Spec" => 0,
"IO::Handle" => 0,
"IPC::Open3" => 0,
"PPI" => 0,
"Scalar::Util" => 0,
"Test::CheckDeps" => "0.010",
"Test::More" => "0.98"
},
"VERSION" => "0.001",
"test" => {
"TESTS" => "t/*.t"
}
);


my %FallbackPrereqs = (
"Class::Method::Modifiers" => "2.13",
"ExtUtils::MakeMaker" => 0,
"File::Spec" => 0,
"IO::Handle" => 0,
"IPC::Open3" => 0,
"PPI" => 0,
"Perl::Critic::Policy::Modules::RequireExplicitPackage" => "1.134",
"Scalar::Util" => 0,
"Test::CheckDeps" => "0.010",
"Test::More" => "0.98",
"indirect" => "0.37",
"parent" => 0,
"strict" => 0,
"warnings" => 0
);


unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) {
delete $WriteMakefileArgs{TEST_REQUIRES};
delete $WriteMakefileArgs{BUILD_REQUIRES};
$WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs;
}

delete $WriteMakefileArgs{CONFIGURE_REQUIRES}
unless eval { ExtUtils::MakeMaker->VERSION(6.52) };

WriteMakefile(%WriteMakefileArgs);
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# perl-Perl-Critic-Policy-Modules-RequireExplicitPackage-ObjectPad
# NAME

Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad - Always make the package/Object::Pad class explicit.

# METHOLDS

## violates

Please see [Perl::Critic::Policy::Modules::RequireExplicitPackage::violates](https://metacpan.org/pod/Perl%3A%3ACritic%3A%3APolicy%3A%3AModules%3A%3ARequireExplicitPackage%3A%3Aviolates)

## \_replace\_class

replace 'use Object::Pad' and 'class XXXX' with \`package XXX\` in [PPI::Document](https://metacpan.org/pod/PPI%3A%3ADocument) object, to make it be processable by [Perl::Critic::Policy::Modules::RequireExplicitPackage::violates](https://metacpan.org/pod/Perl%3A%3ACritic%3A%3APolicy%3A%3AModules%3A%3ARequireExplicitPackage%3A%3Aviolates)

Argument: PPI::Document object
Return: cloned PPI::document object
11 changes: 11 additions & 0 deletions cpanfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
requires 'indirect', '>= 0.37';
requires 'Class::Method::Modifiers', '>= 2.13';
requires 'Perl::Critic::Policy::Modules::RequireExplicitPackage', '>= 1.134';
on test => sub {
requires 'Test::More', '>= 0.98';
};

on develop => sub {
requires 'Devel::Cover', '>= 1.23';
requires 'Devel::Cover::Report::Codecov', '>= 0.14';
};
11 changes: 11 additions & 0 deletions dist.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = Perl-Critic-Policy-Modules-RequireExplicitPackage-ObjectPad
author = DERIV <DERIV@cpan.org>
license = Perl_5
copyright_holder = Deriv Services Ltd
copyright_year = 2021

[@Author::DERIV]
[PerlTidy]
perltidyrc = t/rc/.perltidyrc
[Test::Perl::Critic]
critic_config = t/rc/.perlcriticrc
70 changes: 70 additions & 0 deletions lib/Perl/Critic/Policy/Modules/RequireExplicitPackage/ObjectPad.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad;
use strict;
use warnings;
use parent qw(Perl::Critic::Policy::Modules::RequireExplicitPackage);
use Class::Method::Modifiers;
our $VERSION = "0.001";

=head1 NAME

Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad - Always make the package/Object::Pad class explicit.

=cut

=head1 METHOLDS

=head2 violates

Please see L<Perl::Critic::Policy::Modules::RequireExplicitPackage::violates>

=cut

# PODNAME: Perl::Critic::Policy::Modules::RequireExplicitPackage

around violates => sub {
my $orig = shift;
my ($self, $elem, $doc) = @_;
$doc = _replace_class($doc);
return $orig->($self, $elem, $doc);
};

=head2 _replace_class

replace 'use Object::Pad' and 'class XXXX' with `package XXX` in L<PPI::Document> object, to make it be processable by L<Perl::Critic::Policy::Modules::RequireExplicitPackage::violates>

Argument: PPI::Document object
Return: cloned PPI::document object

=cut

sub _replace_class {
my $doc = shift;
my $cloned_doc = $doc->clone();
my $object_pad = $cloned_doc->find_first(
sub {
$_[1]->parent == $_[0]
and $_[1]->isa('PPI::Statement::Include')
and ($_[1]->type // '') eq 'use'
and ($_[1]->module // '') eq 'Object::Pad';
});
return $cloned_doc unless $object_pad;
my $class = $cloned_doc->find_first(
sub {
$_[1]->parent == $_[0]
and $_[1]->isa('PPI::Statement')
and $_[1]->child(0)->isa('PPI::Token::Word')
and $_[1]->child(0)->literal eq 'class';
});
return $cloned_doc unless $class;
return $cloned_doc unless "$class" =~ /class\s+(\w+)/;
my $class_name = $1;
$cloned_doc->remove_child($object_pad);
my $package_code = "package $class_name;";
my $package_doc = PPI::Document->new(\$package_code);
my $package_statement = $package_doc->find_first(sub { $_[1]->isa('PPI::Statement::Package') });
$package_doc->remove_child($package_statement);
$class->insert_before($package_statement);
return $cloned_doc;
}
1;

139 changes: 139 additions & 0 deletions t/ObjectPad.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use strict;
use warnings;
use Test::More;
use PPI;
use Scalar::Util qw(refaddr);
use Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad;

my ($code, $expected_code, $doc, $processed_doc);
$code = <<EOF;
# some commit
package Abc;
use Def;
EOF
$expected_code = $code;
$doc = PPI::Document->new(\$code);
$processed_doc = Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad::_replace_class($doc);
isnt(refaddr($doc), refaddr($processed_doc), "not same doc");
is("$doc", $code, "the original doc not changed");
is("$processed_doc", $code, "the source code not changed because there is no class");

$code = <<EOF;
# Some comment
use Object::Pad;
# other comment
class Abc ;
# comment 3
EOF
$expected_code = <<EOF;
# Some comment

# other comment
package Abc;class Abc ;
# comment 3
EOF
$doc = PPI::Document->new(\$code);
$processed_doc = Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad::_replace_class($doc);
isnt(refaddr($doc), refaddr($processed_doc), "not same doc");
is("$doc", $code, "the original doc not changed");
is("$processed_doc", $expected_code, "Object::Pad and class is replaced");

$code = <<'EOF';
# Some comment
use Object::Pad;
# other comment
class Abc :isa("P1") {
has $abc :reader :writer ;
};
# comment 3
EOF
$expected_code = <<'EOF';
# Some comment

# other comment
package Abc;class Abc :isa("P1") {
has $abc :reader :writer ;
};
# comment 3
EOF
$doc = PPI::Document->new(\$code);
$processed_doc = Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad::_replace_class($doc);
isnt(refaddr($doc), refaddr($processed_doc), "not same doc");
is("$doc", $code, "the original doc not changed");
is("$processed_doc", $expected_code, "Object::Pad and class is replaced");

$code = <<'EOF';
# Some comment
use Object::Pad;
# other comment
class Abc :attr {
has $abc :reader :writer ;
};
# comment 3
EOF
$expected_code = <<'EOF';
# Some comment

# other comment
package Abc;class Abc :attr {
has $abc :reader :writer ;
};
# comment 3
EOF
$doc = PPI::Document->new(\$code);
$processed_doc = Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad::_replace_class($doc);
isnt(refaddr($doc), refaddr($processed_doc), "not same doc");
is("$doc", $code, "the original doc not changed");
is("$processed_doc", $expected_code, "Object::Pad and class is replaced");

$code = <<'EOF';
# Some comment
use Object::Pad;
# other comment
class Abc {
has $abc :reader :writer ;
};
# comment 3
EOF
$expected_code = <<'EOF';
# Some comment

# other comment
package Abc;class Abc {
has $abc :reader :writer ;
};
# comment 3
EOF
$doc = PPI::Document->new(\$code);
$processed_doc = Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad::_replace_class($doc);
isnt(refaddr($doc), refaddr($processed_doc), "not same doc");
is("$doc", $code, "the original doc not changed");
is("$processed_doc", $expected_code, "Object::Pad and class is replaced");

$code = <<'EOF';
# Some comment
use Object::Pad;
# other comment
# comment 3
EOF
$expected_code = $code;
$doc = PPI::Document->new(\$code);
$processed_doc = Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad::_replace_class($doc);
isnt(refaddr($doc), refaddr($processed_doc), "not same doc");
is("$doc", $code, "the original doc not changed");
is("$processed_doc", $expected_code, "code not changed becasue there is no class");

$code = <<'EOF';
# Some comment
# other comment
class Abc ;
# comment 3
EOF
$expected_code = $code;
$doc = PPI::Document->new(\$code);
$processed_doc = Perl::Critic::Policy::Modules::RequireExplicitPackage::ObjectPad::_replace_class($doc);
isnt(refaddr($doc), refaddr($processed_doc), "not same doc");
is("$doc", $code, "the original doc not changed");
is("$processed_doc", $expected_code, "code not changed becasue there is no Object::Pad");

done_testing();
Loading