summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2001-10-16 20:59:27 +0000
committerDries Buytaert <dries@buytaert.net>2001-10-16 20:59:27 +0000
commitaf6c2808fca8da9011e287a578a8607f2498dddc (patch)
tree8157a7dc0253dbe2f310e38ca6318de8c3f4c3f2 /scripts
parent33a3502dfee073325f5046baee655e6a2bdb29ec (diff)
downloadbrdo-af6c2808fca8da9011e287a578a8607f2498dddc.tar.gz
brdo-af6c2808fca8da9011e287a578a8607f2498dddc.tar.bz2
- Added a *cool* new Perl script by Alexander. You can use this script
to check your code against the Drupal coding style: "This program tries to show as many improvements as possible with no false positives."
Diffstat (limited to 'scripts')
-rw-r--r--scripts/code-style.pl142
1 files changed, 142 insertions, 0 deletions
diff --git a/scripts/code-style.pl b/scripts/code-style.pl
new file mode 100644
index 000000000..c4b022c5d
--- /dev/null
+++ b/scripts/code-style.pl
@@ -0,0 +1,142 @@
+#!/usr/bin/perl -w
+
+# Author: Alexander Schwartz (alexander.schwartz@gmx.net)
+# Licence: GPL
+# First version: 2001-10-15
+
+# Originally written for Drupal (http://www.drupal.org/) to ensure stylish
+# code. This program tries to show as many improvements as possible with
+# no false positives.
+
+# $id$
+
+$comment = 0;
+$program = 0;
+if ($ARGV[0] eq '-debug') {
+ $debug=1;
+ shift (@ARGV);
+}
+else {
+ $debug=0;
+}
+while (<>) {
+ $org=$_;
+ s/\\["']//g;
+ # please don't use nested comments for now... thanks!
+ # handles comments // style, but don't mess with http://
+ s/\/\/[^:]*//;
+ # handles comments /**/ on a single line
+ s/\/\*.*\*\///g;
+ # handles comments /**/ over several lines
+ if ($comment == 1) {
+ if (s/.*\*\///) {
+ $comment = 0;
+ }
+ else {
+ next;
+ }
+ }
+ if (s/\/\*.*//) {
+ $comment = 1;
+ }
+ if (/^\s*#/) {
+ next;
+ }
+
+ if (s/<\?php//) {
+ $program = 1;
+ }
+ if (/\?>/) {
+ $program = 0;
+ }
+
+ # enfoce "bar". foo() ."bar" syntax
+ if (/^("[^"]*"|[^"])*("[^"]*")\.[^ ]/ && $program) {
+ $msg = "'\".' -> '\". '";
+ }
+ elsif (/^("[^"]*"|[^"])*("[^"]*")\s+\./ && $program) {
+ $msg = "'\" .' -> '\".'";
+ }
+ # enfoce "bar". foo() ."bar" syntax
+ elsif (/^("[^"]*"|[^"])*[^ "]\.("[^"]*")/ && $program) {
+ $msg = "'.\"' -> '.\"'";
+ }
+ elsif (/^("[^"]*"|[^"])*[^ "]\.\s+("[^"]*")/ && $program) {
+ $msg = "'. \"' -> '.\"'";
+ }
+ # XHTML requires closing tag
+ elsif (/[<]br[>]/i) {
+ $msg = "'<b"."r>' -> '<b"."r />'";
+ }
+ # XHTML compatibility mode suggests a blank before /
+ # i.e. <br />
+ elsif (/<[a-z][^>]*[^ >]\/>/i) {
+ $msg = "'<foo/".">' -> '<foo />'";
+ }
+ # we write '{' on the same line, not on the next
+ elsif (/^\s*{/ && $program) {
+ $msg = "take '{' to previous line";
+ }
+ elsif (/function ([a-zA-Z_][a-zA-Z_0-9]*[A-Z][a-zA-Z_0-9]*)\(/) {
+ $msg = "no mixedcase, use lowercase and _";
+ }
+ elsif (/<[\/]*[A-Z]+[^>]*>/) {
+ $msg = "XHTML demands tags to be lowercase";
+ }
+ # trying to recognize splitted lines
+ # there are only a few valid last characters in programming mode,
+ # only sometimes it is ( if you use if/else with a single statement
+
+ # from here on we need no more strings
+ while (s/^([^"]*)"[^"]*"/$1#/) {};
+ while (s/^([^']*)'[^']*'/$1#/) {};
+
+ # it should be 'if (' all the time
+ if (/(^|[^a-zA-Z])(if|else|elseif|while|foreach|switch|return|for)\(/) {
+ $msg = "'$1(' -> '$1 ('";
+ }
+ elsif (/[^;{}:\s\n]\s*\n*$/ && $program && !/^[\s}]*(if|else)/) {
+ $msg = "don't split lines";
+ }
+ elsif (/\}\s*else/) {
+ $msg = "'} else' -> '}\\nelse'";
+ }
+ elsif (/[^{\s\n]\s*\n*$/ && $program && /^\s*(if|else)/) {
+ $msg = "every if/else needs a { at eol";
+ }
+ elsif (/([\(\[]) / && $program) {
+ $msg = "'$1 ' -> '$1'";
+ }
+ elsif (/ ([\)\]])/ && $program) {
+ $msg = "' $1' -> '$1'";
+ }
+ # but no brackets
+ elsif (/([a-z-A-Z_][a-zA-Z0-9_-]*)\s+\(/ && $program) {
+ 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") {
+ $msg = "'$1 (' -> '$1('";
+ }
+ }
+ # there should be a space before '{'
+ if (/[^ ][{]/ && $program) {
+ $msg = "missing space before '{'";
+ }
+ # there should be a space after ','
+ elsif (/[,][^ ]/ && $program) {
+ $msg = "missing space after ','";
+ }
+ # spaces before and after, only foreach may use $foo=>bar
+ elsif (/[^ =](==|\.=|=>|=|\|\|)[^ =>]/ && $program && !/foreach/) {
+ $msg = "'$1' -> ' $1 '"
+ }
+ if (defined $msg) {
+ if ($debug==0) {
+ print $ARGV .":". $. .": $msg : ". $org;
+ }
+ undef $msg;
+ }
+ elsif ($debug==1) {
+ print $org;
+ }
+} continue {
+ close ARGV if eof;
+}