Skip to content
Open
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
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Revision history for App-perlimports

{{$NEXT}}
- Add experimental --skip-empty-imports flag, to allow following style
guides that do not want an empty `qw()` import list in use statements.
(GH#118) (Thomas Lamprecht)

0.000057 2025-04-30 12:12:23Z
- Update test expectations for MooseX::Types::Moose 0.51 (GH#122) (Olaf
Expand Down
7 changes: 7 additions & 0 deletions lib/App/perlimports/CLI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ sub _build_args {
'Pad imports: qw( foo bar ) vs qw(foo bar). Defaults to true',
],
[],
[
'skip-empty-imports!',
'Skip empty imports: Foo qw(); vs Foo;. Defaults to false',
],
[],
[
'read-stdin',
'Read statements to process from STDIN rather than the supplied file.',
Expand Down Expand Up @@ -266,6 +271,7 @@ sub _build_config {
log_level
never_export_modules_filename
padding
skip_empty_imports
preserve_duplicates
preserve_unused
tidy_whitespace
Expand Down Expand Up @@ -450,6 +456,7 @@ sub run {
lint => $self->_lint,
logger => $logger,
padding => $self->_config->padding,
skip_empty_imports => $self->_config->skip_empty_imports,
preserve_duplicates => $self->_config->preserve_duplicates,
preserve_unused => $self->_config->preserve_unused,
tidy_whitespace => $self->_config->tidy_whitespace,
Expand Down
8 changes: 8 additions & 0 deletions lib/App/perlimports/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ has padding => (
default => 1,
);

has skip_empty_imports => (
is => 'ro',
isa => Bool,
lazy => 1,
default => 0,
);

has preserve_duplicates => (
is => 'ro',
isa => Bool,
Expand Down Expand Up @@ -230,3 +237,4 @@ padding = true
preserve_duplicates = false
preserve_unused = false
tidy_whitespace = true
skip_empty_imports = false
10 changes: 9 additions & 1 deletion lib/App/perlimports/Document.pm
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ has _padding => (
default => 1,
);

has _skip_empty_imports => (
is => 'ro',
isa => Bool,
init_arg => 'skip_empty_imports',
default => 0,
);

has ppi_document => (
is => 'ro',
isa => Object,
Expand Down Expand Up @@ -897,7 +904,8 @@ INCLUDE:
logger => $self->logger,
original_imports => $self->original_imports->{ $include->module },
pad_imports => $self->_padding,
tidy_whitespace => $self->_tidy_whitespace,
skip_empty_imports => $self->_skip_empty_imports,
tidy_whitespace => $self->_tidy_whitespace,
);
my $elem;
try {
Expand Down
14 changes: 10 additions & 4 deletions lib/App/perlimports/Include.pm
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ has _pad_imports => (
default => sub { 1 },
);

has _skip_empty_imports => (
is => 'ro',
isa => Bool,
init_arg => 'skip_empty_imports',
default => sub { 0 },
);

has _tidy_whitespace => (
is => 'ro',
isa => Bool,
Expand Down Expand Up @@ -464,11 +471,10 @@ sub _build_formatted_ppi_statement {
if ( $self->_will_never_export
|| $self->_is_translatable
|| !@{ $self->_imports } ) {
my $template
= $self->_skip_empty_imports() ? 'use %s%s;' : 'use %s%s ();';
return $self->_maybe_get_new_include(
sprintf(
'use %s%s ();', $self->module_name, $maybe_module_version
)
);
sprintf( $template, $self->module_name, $maybe_module_version ) );
}

my $statement;
Expand Down
15 changes: 15 additions & 0 deletions script/perlimports
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,21 @@ parentheses.
# --no-padding
use Foo qw(bar baz);

=head2 --[no-]skip-empty-imports

C<--skip-empty-imports> is disabled by default. This means that empty import
statements will always get added to use statements. This setting skips adding
a import stanza if there is nothing to import.

# --no-skip-empty-imports
use Foo qw();

The C<--skip-empty-imports> arg allows you to disable outputting an empty
import list.

# --skip-empty-imports
use Foo;

=head2 --[no-]tidy-whitespace

C<--tidy-whitespace> is enabled by default. This means that use statements will
Expand Down
2 changes: 2 additions & 0 deletions t/cli-args.t
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ EOF
'--log-level' => 'info',
'--no-cache',
'--no-padding',
'--skip-empty-imports',
'--no-preserve-duplicates',
'--no-preserve-unused',
'--no-tidy-whitespace',
Expand Down Expand Up @@ -70,6 +71,7 @@ EOF
'never_export'
);
is( $c->padding, 0, 'padding' );
is( $c->skip_empty_imports, 1, 'skip_empty_imports' );
is( $c->preserve_duplicates, 0, 'preserve_duplicates' );
is( $c->tidy_whitespace, 0, 'tidy_whitespace' );
};
Expand Down
44 changes: 44 additions & 0 deletions t/cli.t
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,50 @@ EOF
is( $stderr, q{}, 'no STDERR' );
};

subtest '--skip-empty-imports' => sub {
my $expected = <<'EOF';
use strict;
use warnings;

use Carp;
use Data::Dumper;
use POSIX;
EOF

local @ARGV = (
'--no-config-file',
'--skip-empty-imports',
'-f' => 'test-data/original-imports.pl',
);
my $cli = App::perlimports::CLI->new;
my ( $stdout, $stderr ) = capture { $cli->run };
is( $stderr, q{}, 'no STDERR' );
is( $stdout, $expected, 'stdout' );
is( $stderr, q{}, 'no STDERR' );
};

subtest '--no-skip-empty-imports' => sub {
my $expected = <<'EOF';
use strict;
use warnings;

use Carp ();
use Data::Dumper ();
use POSIX ();
EOF

local @ARGV = (
'--no-config-file',
'--no-skip-empty-imports',
'-f' => 'test-data/original-imports.pl',
);
my $cli = App::perlimports::CLI->new;
my ( $stdout, $stderr ) = capture { $cli->run };
is( $stderr, q{}, 'no STDERR' );
is( $stdout, $expected, 'stdout' );
is( $stderr, q{}, 'no STDERR' );
};

subtest '--stdout' => sub {
my $expected = <<'EOF';
use strict;
Expand Down
Loading