blib/lib/Perl/Critic/Policy/CodeLayout/ProhibitHardTabs.pm

filter: Statementstmt 100% 39 / 39 Branchbran 100% 14 / 14 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 13 / 13 Podpod 80.0% 4 / 5 totaltotal 98.8% 85 / 86 CC 11 SCAR 14.8
CC
11
Cov
100%
violates
9.0
_is_extended_regex
3.0
BEGIN
1.0
CRAP 4.4
« blib/lib/Perl/Critic/Policy/InputOutput/RequireCheckedSyscalls.pm ↑ Summary blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitNegativeExpressionsInUnlessAndUntilConditions.pm »
1 package Perl::Critic::Policy::CodeLayout::ProhibitHardTabs;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw( :severities );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 our $VERSION = '1.150';
12  
13 #-----------------------------------------------------------------------------
14  
15 40 Readonly::Scalar my $DESC => q{Hard tabs used};
16 40 Readonly::Scalar my $EXPL => [ 20 ];
17  
18 #-----------------------------------------------------------------------------
19  
20 # The following regex should probably be "qr{^ .* [^\t]+ \t}xms" but it doesn't
21 # match when I expect it to. I haven't figured out why, so I used "\S" to
22 # approximately mean "not a tab", and that seemed to work.
23  
24 40 Readonly::Scalar my $NON_LEADING_TAB_REGEX => qr{^ .* \S+ \t }xms;
25  
26 #-----------------------------------------------------------------------------
27  
28 sub supported_parameters {
29 return (
30 {
31 101 ▶ name => 'allow_leading_tabs',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
32 description => 'Allow hard tabs before first non-whitespace character.',
33 default_string => '1',
34 behavior => 'boolean',
35 },
36 );
37 }
38  
39 85 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
40 84 ▶sub default_themes { return qw( core cosmetic pbp ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
41 41 ▶sub applies_to { return 'PPI::Token' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
42  
43 #-----------------------------------------------------------------------------
44  
45 sub violates {
46 2385 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=9 SCAR=22.0
Pod documented
47 2385 ▶ $elem =~ m{ \t }xms || return;
Branch: 1 branch
BranchTrueFalseTotal
$elem =~ / \t /msx or return 2366 19 2385
48  
49 # The __DATA__ element is exempt
50 19 ▶ return if $elem->parent->isa('PPI::Statement::Data');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->parent->isa("PPI::Statement::Data") 1 18 19
51  
52 # If allowed, permit leading tabs in situations where whitespace s not significant.
53 18 ▶ if ( $self->_allow_leading_tabs() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_allow_leading_tabs) 12 6 18
54  
55 12 ▶ return if $elem->location->[1] == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $elem->location->[1] == 1 4 8 12
56  
57 8 ▶ return if _is_extended_regex($elem)
Branch: 1 branch
BranchTrueFalseTotal
if _is_extended_regex($elem) and not $elem =~ /$NON_LEADING_TAB_REGEX/ 2 6 8
Condition100%3/3
_is_extended_regex($elem) and not $elem =~ /$NON_LEADING_TAB_REGEX/
!ll&&!rl&&r
512
MC/DC100%2/2
_is_extended_regex($elem) and not $elem =~ /$NON_LEADING_TAB_REGEX/
_is_extended_regex($elem) not $elem =~ /$NON_LEADING_TAB_REGEX/
Truth table100%3/3
A and not(B)
A= _is_extended_regex($elem)B= not $elem =~ /$NON_LEADING_TAB_REGEX/
ABresult
0X0
100
111
58 && $elem !~ $NON_LEADING_TAB_REGEX;
59  
60 6 ▶ return if $elem->isa('PPI::Token::QuoteLike::Words')
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Token::QuoteLike::Words") and not $elem =~ /$NON_LEADING_TAB_REGEX/ 1 5 6
Condition100%3/3
$elem->isa("PPI::Token::QuoteLike::Words") and not $elem =~ /$NON_LEADING_TAB_REGEX/
!ll&&!rl&&r
411
MC/DC100%2/2
$elem->isa("PPI::Token::QuoteLike::Words") and not $elem =~ /$NON_LEADING_TAB_REGEX/
$elem->isa("PPI::Token::QuoteLike::Words") not $elem =~ /$NON_LEADING_TAB_REGEX/
Truth table100%3/3
A and not(B)
A= $elem->isa("PPI::Token::QuoteLike::Words")B= not $elem =~ /$NON_LEADING_TAB_REGEX/
ABresult
0X0
100
111
61 && $elem !~ $NON_LEADING_TAB_REGEX;
62 }
63  
64 # If we get here, then it must be a violation...
65 11 return $self->violation( $DESC, $EXPL, $elem );
66 }
67  
68 #-----------------------------------------------------------------------------
69  
70 sub _allow_leading_tabs {
71 18 ▶ my ( $self ) = @_;
Subroutine _allow_leading_tabs: called CC=1 SCAR=0.0
72 18 return $self->{_allow_leading_tabs};
73 }
74  
75 #-----------------------------------------------------------------------------
76  
77 sub _is_extended_regex {
78 8 ▶ my ($elem) = @_;
Subroutine _is_extended_regex: called CC=3 SCAR=11.0
79  
80 8 ▶ $elem->isa('PPI::Token::Regexp')
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Token::Regexp") or $elem->isa("PPI::Token::QuoteLike::Regexp") or return 3 5 8
Condition100%3/3
$elem->isa("PPI::Token::Regexp") or $elem->isa("PPI::Token::QuoteLike::Regexp")
l!l&&r!l&&!r
323
MC/DC100%2/2
$elem->isa("PPI::Token::Regexp") or $elem->isa("PPI::Token::QuoteLike::Regexp")
$elem->isa("PPI::Token::Regexp") $elem->isa("PPI::Token::QuoteLike::Regexp")
Truth table100%3/3
A or B
A= $elem->isa("PPI::Token::Regexp")B= $elem->isa("PPI::Token::QuoteLike::Regexp")
ABresult
1X1
011
000
81 || $elem->isa('PPI::Token::QuoteLike::Regexp')
82 || return;
83  
84 # Look for the /x modifier near the end
85 5 return $elem =~ m{\b [gimso]* x [gimso]* $}xms;
86 }
87  
88 40 1;
89  
90 __END__
91  
92 #-----------------------------------------------------------------------------
93  
94 =head1 NAME
95  
96 Perl::Critic::Policy::CodeLayout::ProhibitHardTabs - Use spaces instead of tabs.
97  
98  
99 =head1 AFFILIATION
100  
101 This Policy is part of the core L<Perl::Critic|Perl::Critic>
102 distribution.
103  
104  
105 =head1 DESCRIPTION
106  
107 Putting hard tabs in your source code (or POD) is one of the worst
108 things you can do to your co-workers and colleagues, especially if
109 those tabs are anywhere other than a leading position. Because
110 various applications and devices represent tabs differently, they can
111 cause you code to look vastly different to other people. Any decent
112 editor can be configured to expand tabs into spaces.
113 L<Perl::Tidy|Perl::Tidy> also does this for you.
114  
115 This Policy catches all tabs in your source code, including POD,
116 quotes, and HEREDOCs. The contents of the C<__DATA__> section are not
117 examined.
118  
119  
120 =head1 CONFIGURATION
121  
122 Hard tabs in a string are always forbidden (use "\t" instead). But
123 hard tabs in a leading position are allowed when they are used to indent
124 code statements, C<qw()> word lists, and regular expressions with the C</x>
125 modifier. However, if you want to forbid all tabs everywhere, then add
126 this to your F<.perlcriticrc> file:
127  
128 [CodeLayout::ProhibitHardTabs]
129 allow_leading_tabs = 0
130  
131  
132 =head1 NOTES
133  
134 Beware that Perl::Critic may report the location of the string that
135 contains the tab, not the actual location of the tab, so you may need
136 to do some hunting. I'll try and fix this in the future.
137  
138  
139 =head1 AUTHOR
140  
141 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
142  
143  
144 =head1 COPYRIGHT
145  
146 Copyright (c) 2005-2023 Imaginative Software Systems
147  
148 This program is free software; you can redistribute it and/or modify
149 it under the same terms as Perl itself. The full text of this license
150 can be found in the LICENSE file included with this module.
151  
152 =cut
153  
154 ##############################################################################
155 # Local Variables:
156 # mode: cperl
157 # cperl-indent-level: 4
158 # fill-column: 78
159 # indent-tabs-mode: nil
160 # c-indentation-style: bsd
161 # End:
162 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/InputOutput/RequireCheckedSyscalls.pm ↑ Summary blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitNegativeExpressionsInUnlessAndUntilConditions.pm »