Login
Fixed ProhibitHardTabs so that it would allow leading tabs
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / CodeLayout / ProhibitHardTabs.pm
CommitLineData
6036a254 1##############################################################################
dff08b70
JRT
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6036a254 6##############################################################################
dff08b70 7
59b05e08
JRT
8package Perl::Critic::Policy::CodeLayout::ProhibitHardTabs;
9
df6dee2b 10use 5.006001;
59b05e08
JRT
11use strict;
12use warnings;
c680a9c9
ES
13use Readonly;
14
bbf4108c 15use Perl::Critic::Utils qw{ :booleans :severities };
59b05e08
JRT
16use base 'Perl::Critic::Policy';
17
be4331b3 18our $VERSION = '1.090';
59b05e08 19
fd5bd7b5
JRT
20#-----------------------------------------------------------------------------
21
c680a9c9
ES
22Readonly::Scalar my $DESC => q{Hard tabs used};
23Readonly::Scalar my $EXPL => [ 20 ];
59b05e08 24
fd5bd7b5
JRT
25my $DEFAULT_ALLOW_LEADING_TABS = $TRUE;
26
6036a254 27#-----------------------------------------------------------------------------
59b05e08 28
c7c74a79
JRT
29# The following regex should be "qr{^ [^\t]+ \t}xms"
30# but it doesn't match when I expect it to. I
31# haven't figured out why. So I used [^\s]
32# as a substitue to mean "not a tab".
33
34my $NON_LEADING_TAB_REGEX = qr{^ [^\s]+ \t}xms;
35
36#-----------------------------------------------------------------------------
37
3d0b0e24
ES
38sub supported_parameters {
39 return (
40 {
41 name => 'allow_leading_tabs',
42 description => 'Allow hard tabs before first non-whitespace character.',
43 default_string => '1',
44 behavior => 'boolean',
45 },
46 );
47}
48
c680a9c9
ES
49sub default_severity { return $SEVERITY_MEDIUM }
50sub default_themes { return qw( core cosmetic ) }
51sub applies_to { return 'PPI::Token' }
dff08b70 52
6036a254 53#-----------------------------------------------------------------------------
dff08b70 54
59b05e08 55sub violates {
e5f6c18d 56 my ( $self, $elem, undef ) = @_;
a0dcf06f 57 $elem =~ m{ \t }xms || return;
59b05e08 58
5fb3b6ed 59 # The __DATA__ element is exempt
04654e65 60 return if $elem->parent->isa('PPI::Statement::Data');
5fb3b6ed
JRT
61
62 # Permit leading tabs, if allowed
c7c74a79 63 return if $self->_allow_leading_tabs() && $elem !~ $NON_LEADING_TAB_REGEX;
5dc3db7a 64
5fb3b6ed 65 # Must be a violation...
c680a9c9 66 return $self->violation( $DESC, $EXPL, $elem );
59b05e08
JRT
67}
68
628facda
ES
69#-----------------------------------------------------------------------------
70
71sub _allow_leading_tabs {
72 my ( $self ) = @_;
73
74 return $self->{_allow_leading_tabs};
75}
76
59b05e08
JRT
771;
78
79__END__
80
fd5bd7b5
JRT
81#-----------------------------------------------------------------------------
82
59b05e08
JRT
83=head1 NAME
84
9c338374 85Perl::Critic::Policy::CodeLayout::ProhibitHardTabs - Use spaces instead of tabs.
59b05e08 86
11f53956 87
af93c316
ES
88=head1 AFFILIATION
89
11f53956
ES
90This Policy is part of the core L<Perl::Critic|Perl::Critic>
91distribution.
af93c316
ES
92
93
59b05e08
JRT
94=head1 DESCRIPTION
95
96Putting hard tabs in your source code (or POD) is one of the worst
97things you can do to your co-workers and colleagues, especially if
98those tabs are anywhere other than a leading position. Because
821d0eb5 99various applications and devices represent tabs differently, they can
59b05e08 100cause you code to look vastly different to other people. Any decent
11f53956
ES
101editor can be configured to expand tabs into spaces.
102L<Perl::Tidy|Perl::Tidy> also does this for you.
59b05e08 103
5fb3b6ed 104This Policy catches all tabs in your source code, including POD,
9e3b1511 105quotes, and HEREDOCs. The contents of the C<__DATA__> section are not
fd5bd7b5 106examined.
59b05e08 107
11f53956 108
fd5bd7b5
JRT
109=head1 CONFIGURATION
110
111Tabs in a leading position are allowed, but if you want to forbid all tabs
112everywhere, put this to your F<.perlcriticrc> file:
113
114 [CodeLayout::ProhibitHardTabs]
115 allow_leading_tabs = 0
116
11f53956 117
fd5bd7b5 118=head1 NOTES
59b05e08
JRT
119
120Beware that Perl::Critic may report the location of the string that
121contains the tab, not the actual location of the tab, so you may need
122to do some hunting. I'll try and fix this in the future.
123
11f53956 124
59b05e08
JRT
125=head1 AUTHOR
126
127Jeffrey Ryan Thalhammer <thaljef@cpan.org>
128
11f53956 129
59b05e08
JRT
130=head1 COPYRIGHT
131
20dfddeb 132Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
59b05e08
JRT
133
134This program is free software; you can redistribute it and/or modify
135it under the same terms as Perl itself. The full text of this license
136can be found in the LICENSE file included with this module.
137
138=cut
737d3b65 139
fd5bd7b5 140##############################################################################
737d3b65
CD
141# Local Variables:
142# mode: cperl
143# cperl-indent-level: 4
144# fill-column: 78
145# indent-tabs-mode: nil
146# c-indentation-style: bsd
147# End:
96fed375 148# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :