coding_guidelines.pl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env perl
  2. # SJCL coding guidelines:
  3. #
  4. # No tabs in any Javascript
  5. #
  6. # Indentation is two spaces. Alignment of stuff in multi-line statements is
  7. # encouraged.
  8. #
  9. # Braces everywhere, following jslint. I'm pretty sure the Closure compressor
  10. # removes them. Semicolons at the end of every statement.
  11. #
  12. # ++ and -- are allowed.
  13. #
  14. # Varibles are in camelCase. I prefer underscore_separated, but JavaScript uses
  15. # camelCase everywhere.
  16. #
  17. # Private members and methods are prefixed by underscores.
  18. #
  19. # Constants (and only constants) are UNDERSCORE_SEPARATED_UPPER_CASE. The
  20. # compression scripts rely on this.
  21. #
  22. # Classes begin with a capital letter (not yet implemented. Namespaces too?)
  23. #
  24. # Block comments are not on the same line as code.
  25. my $in_comment = 0;
  26. my $file = '';
  27. # for some reason, $. doesn't work.
  28. my $line = 0;
  29. while (<>) {
  30. if ($ARGV ne $file) {
  31. # opening a new file
  32. if ($in_comment) {
  33. print STDERR "Opening file $ARGV: comment from $file wasn't closed.\n";
  34. }
  35. $file = $ARGV;
  36. $in_comment = $line = 0;
  37. }
  38. $line ++;
  39. if (/\/\*(?:[^\*]|\*[^\/])*(\*\/\s*)?/) {
  40. # block comment on this line
  41. $in_comment = 1 unless defined $1;
  42. $ba = "$`$'";
  43. # shouldn't have code before or after it
  44. print STDERR "$file line $line: block comment and code together.\n" if $ba =~ /\S/;
  45. next;
  46. } elsif ($in_comment and /\*\//) {
  47. # leaving block comment
  48. $in_comment = 0;
  49. print STDERR "$file line $line: block comment and code together.\n" if $' =~ /\S/;
  50. }
  51. # don't enforce code style in a comment.
  52. next if $in_comment;
  53. reset;
  54. while (?[\.\s+\*/<>=,;:-]([a-zA-Z0-9_]+_[a-zA-Z0-9_]*)?) {
  55. # find variable names with underscores
  56. my $varname = $1;
  57. print STDERR "$file line $line: Variable name $varname contains an underscore\n"
  58. if $varname =~ /[a-z]/;
  59. }
  60. reset;
  61. print STDERR "$file line $line contains a tab\n" if /\t/;
  62. }