/[drupal]/drupal/scripts/code-style.pl
ViewVC logotype

Contents of /drupal/scripts/code-style.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.12 - (hide annotations) (download) (as text)
Sat Oct 14 10:03:27 2006 UTC (3 years, 1 month ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-5-19, DRUPAL-5-18, DRUPAL-5-11, DRUPAL-5-13, DRUPAL-5-12, DRUPAL-5-15, DRUPAL-5-14, DRUPAL-5-16, DRUPAL-5-9, DRUPAL-5-8, DRUPAL-5-5, DRUPAL-5-4, DRUPAL-5-7, DRUPAL-5-6, DRUPAL-5-1, DRUPAL-5-0, DRUPAL-5-3, DRUPAL-5-0-BETA-1, DRUPAL-5-0-BETA-2, DRUPAL-5-10, DRUPAL-5-17, DRUPAL-5-0-RC-1, DRUPAL-5-0-RC-2, DRUPAL-5-2, DRUPAL-5-20
Branch point for: DRUPAL-5
Changes since 1.11: +3 -3 lines
File MIME type: text/x-perl
- Small correction to code style checker.
1 dries 1.1 #!/usr/bin/perl -w
2 dries 1.12 # $Id: code-style.pl,v 1.11 2006/02/21 18:46:54 dries Exp $
3 dries 1.1
4     # Author: Alexander Schwartz (alexander.schwartz@gmx.net)
5     # Licence: GPL
6     # First version: 2001-10-15
7    
8 dries 1.11 # Originally written for Drupal (http://drupal.org/) to ensure stylish
9 dries 1.2 # code. This program tries to show as many improvements as possible with
10 dries 1.1 # no false positives.
11    
12 dries 1.12 # $Id: code-style.pl,v 1.11 2006/02/21 18:46:54 dries Exp $
13 dries 1.1
14     $comment = 0;
15     $program = 0;
16     if ($ARGV[0] eq '-debug') {
17     $debug=1;
18     shift (@ARGV);
19     }
20     else {
21     $debug=0;
22     }
23     while (<>) {
24     $org=$_;
25     s/\\["']//g;
26     # please don't use nested comments for now... thanks!
27     # handles comments // style, but don't mess with http://
28 dries 1.3 s/\/\/[^:].*//;
29 dries 1.1 # handles comments /**/ on a single line
30     s/\/\*.*\*\///g;
31     # handles comments /**/ over several lines
32     if ($comment == 1) {
33     if (s/.*\*\///) {
34 dries 1.2 $comment = 0;
35     }
36 dries 1.1 else {
37 dries 1.2 next;
38 dries 1.1 }
39     }
40     if (s/\/\*.*//) {
41     $comment = 1;
42     }
43     if (/^\s*#/) {
44     next;
45     }
46    
47     if (s/<\?php//) {
48     $program = 1;
49     }
50     if (/\?>/) {
51     $program = 0;
52     }
53 dries 1.2
54 dries 1.9 # enforce "bar". foo() ."bar" syntax
55 dries 1.1 if (/^("[^"]*"|[^"])*("[^"]*")\.[^ ]/ && $program) {
56     $msg = "'\".' -> '\". '";
57     }
58     elsif (/^("[^"]*"|[^"])*("[^"]*")\s+\./ && $program) {
59     $msg = "'\" .' -> '\".'";
60     }
61 dries 1.9 # enforce "bar". foo() ."bar" syntax
62 dries 1.1 elsif (/^("[^"]*"|[^"])*[^ "]\.("[^"]*")/ && $program) {
63     $msg = "'.\"' -> '.\"'";
64     }
65     elsif (/^("[^"]*"|[^"])*[^ "]\.\s+("[^"]*")/ && $program) {
66     $msg = "'. \"' -> '.\"'";
67     }
68     # XHTML requires closing tag
69 dries 1.3 elsif (/<br>/i) {
70     $msg = "'<br>' -> '<br />'";
71 dries 1.1 }
72 dries 1.8 elsif (/\$REQUEST_URI/i) {
73     $msg = "the use of REQUEST_URI is prone to XSS exploits and does not work on IIS; use request_uri() instead";
74 dries 1.6 }
75 dries 1.8 elsif (/\"REQUEST_URI\"/i) {
76     $msg = "the use of REQUEST_URI is prone to XSS exploits and does not work on IIS; use request_uri() instead";
77 dries 1.6 }
78    
79 dries 1.2 # XHTML compatibility mode suggests a blank before /
80 dries 1.1 # i.e. <br />
81     elsif (/<[a-z][^>]*[^ >]\/>/i) {
82     $msg = "'<foo/".">' -> '<foo />'";
83     }
84     # we write '{' on the same line, not on the next
85     elsif (/^\s*{/ && $program) {
86 dries 1.2 $msg = "take '{' to previous line";
87 dries 1.1 }
88 dries 1.7 elsif (/([a-z])([A-Z])/) {
89     $msg = "no mixed case function or variable names, use lower case and _";
90 dries 1.1 }
91 dries 1.7 elsif (/<[\/]*[A-Z]+[^>]*>/) {
92     $msg = "XHTML demands tags to be lowercase";
93     }
94    
95 dries 1.1 # trying to recognize splitted lines
96     # there are only a few valid last characters in programming mode,
97     # only sometimes it is ( if you use if/else with a single statement
98 dries 1.2
99 dries 1.1 # from here on we need no more strings
100     while (s/^([^"]*)"[^"]*"/$1#/) {};
101     while (s/^([^']*)'[^']*'/$1#/) {};
102    
103     # it should be 'if (' all the time
104     if (/(^|[^a-zA-Z])(if|else|elseif|while|foreach|switch|return|for)\(/) {
105 dries 1.3 $msg = "'(' -> ' ('";
106 dries 1.1 }
107 dries 1.8 #elsif (/[^;{}:\s\n]\s*\n*$/ && $program && !/^[\s}]*(if|else)/) {
108     # $msg = "don't split lines";
109     #}
110 dries 1.1 elsif (/\}\s*else/) {
111     $msg = "'} else' -> '}\\nelse'";
112     }
113     elsif (/[^{\s\n]\s*\n*$/ && $program && /^\s*(if|else)/) {
114     $msg = "every if/else needs a { at eol";
115     }
116     elsif (/([\(\[]) / && $program) {
117     $msg = "'$1 ' -> '$1'";
118     }
119 dries 1.12 elsif (/\S ([\)\]])/ && $program) {
120 dries 1.1 $msg = "' $1' -> '$1'";
121     }
122 dries 1.2 # but no brackets
123 dries 1.1 elsif (/([a-z-A-Z_][a-zA-Z0-9_-]*)\s+\(/ && $program) {
124     if ($1 ne "switch" and $1 ne "if" and $1 ne "while" and $1 ne "foreach" and $1 ne "return" and $1 ne "for" and $1 ne "elseif") {
125     $msg = "'$1 (' -> '$1('";
126     }
127     }
128     # there should be a space before '{'
129 dries 1.3 if (/[^ ]{/ && $program) {
130 dries 1.1 $msg = "missing space before '{'";
131     }
132     # there should be a space after ','
133 kjartan 1.4 elsif (/[,][^ \n\r]/ && $program) {
134 dries 1.1 $msg = "missing space after ','";
135     }
136     # spaces before and after, only foreach may use $foo=>bar
137 dries 1.10 elsif (/[^ =|\-|\+](\+|\-)[^ =>|\-|\+]/ && $program && !/foreach/) {
138 kjartan 1.4 $msg = "'$1' -> ' $1 '";
139     }
140     elsif (/[^ =](\*|==|\.=|=>|=|\|\|)[^ =>]/ && $program && !/foreach/) {
141 dries 1.3 $msg = "'$1' -> ' $1 '";
142     }
143     # ensure $bar["foo"] and $bar[$foo] and $bar[0]
144     elsif (/\[[^#][^\]]*\]/ && !/\[[0-9\$][^\]]*\]/ && !/\[\]/) {
145     $msg = "only [\"foo\"], [\$foo] or [0] is allowed";
146     }
147     # first try to find missing quotes after = in (X)HTML tags
148     elsif (/<[^>]*=[a-zA-Z0-9][^>]*>/) {
149     $msg = "=... -> =\"...\"";
150 dries 1.1 }
151     if (defined $msg) {
152     if ($debug==0) {
153     print $ARGV .":". $. .": $msg : ". $org;
154     }
155     undef $msg;
156 dries 1.2 }
157 dries 1.1 elsif ($debug==1) {
158     print $org;
159     }
160     } continue {
161     close ARGV if eof;
162     }

  ViewVC Help
Powered by ViewVC 1.1.2