Codestyle.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. var Fs = require('fs');
  16. var nThen = require('nthen');
  17. var Semaphore = require('../tools/lib/Semaphore');
  18. var Child = require('child_process');
  19. var headerLines = [
  20. '/* vim: set expandtab ts=4 sw=4: */',
  21. '/*',
  22. ' * You may redistribute this program and/or modify it under the terms of',
  23. ' * the GNU General Public License as published by the Free Software Foundation,',
  24. ' * either version 3 of the License, or (at your option) any later version.',
  25. ' *',
  26. ' * This program is distributed in the hope that it will be useful,',
  27. ' * but WITHOUT ANY WARRANTY; without even the implied warranty of',
  28. ' * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the',
  29. ' * GNU General Public License for more details.',
  30. ' *',
  31. ' * You should have received a copy of the GNU General Public License',
  32. ' * along with this program. If not, see <https://www.gnu.org/licenses/>.',
  33. ' */'
  34. ];
  35. var parseFile = function (fileName, fileContent) {
  36. var output = '';
  37. var parenthCount = 0;
  38. var functionParenthCount = 0;
  39. var expectBracket = 0;
  40. var name = fileName.replace(/^.*\//, '').replace(/\..*$/,'');
  41. var lines = fileContent.split('\n');
  42. var lineInfo = '';
  43. var ignore = false;
  44. var error = function(msg) {
  45. if (!ignore) {
  46. output += lineInfo + ' ' + msg + '\n';
  47. }
  48. };
  49. for (var lineNum = 0; lineNum < lines.length; lineNum++) {
  50. var line = lines[lineNum];
  51. // switch to 1 indexing for human readability
  52. lineInfo = fileName + ":" + (lineNum+1);
  53. ignore = false;
  54. if (lineNum < headerLines.length) {
  55. var expectedLine = headerLines[lineNum];
  56. if (line !== headerLines[lineNum]) {
  57. error("missing header\n" + expectedLine + "\n" + line);
  58. }
  59. } else if (/\.h$/.test(fileName) && lineNum < headerLines.length + 1) {
  60. if (line !== '#ifndef ' + name + "_H") {
  61. error("expected #ifndef " + name + "_H found " + line);
  62. }
  63. } else if (/\.h$/.test(fileName) && lineNum < headerLines.length + 2) {
  64. if (line !== '#define ' + name + "_H") {
  65. error("expected #define " + name + "_H found " + line);
  66. }
  67. }
  68. ignore = /CHECKFILES_IGNORE/.test(line);
  69. if (expectBracket === 1) {
  70. expectBracket = 0;
  71. if (!(/^[\s]*{/.test(line))) {
  72. error("expecting a { bracket " + line);
  73. }
  74. }
  75. // implementations.. TUNConfigurator_Linux contains TUNConfigurator_doStuff...
  76. var n = name.replace(/_.*/, '');
  77. if ((/^\w+\s.*\(/).test(line)) {
  78. if (!(/^int main\(/.test(line)
  79. || / CJDNS_/.test(line)
  80. || line.indexOf(' '+n) > -1
  81. || /^[ ]?static /.test(line)
  82. || /^typedef /.test(line)))
  83. {
  84. error("all globally visible functions must begin with the name of the file.");
  85. }
  86. }
  87. var matches;
  88. if (functionParenthCount === 0) {
  89. matches = /^\w+\s.*(\(.*)$/.exec(line);
  90. }
  91. if (functionParenthCount > 0 || matches) {
  92. var txt = (functionParenthCount > 0) ? line : matches[1];
  93. functionParenthCount += (txt.match(/\(/g)||[]).length;
  94. functionParenthCount -= (txt.match(/\)/g)||[]).length;
  95. if (functionParenthCount === 0) {
  96. txt = txt.substring(txt.lastIndexOf(')') + 1);
  97. if (/{/.test(txt)) {
  98. error("please put the opening bracket on the next line.");
  99. }
  100. }
  101. }
  102. if (/[\w]*int[\w]*\s+\*+\w/.test(line) || /[\w]*struct\s+[\w]+\s+\*+\w/.test(line)) {
  103. error("int* blah; means int pointer named blah, int *blah; means int names splatblah");
  104. }
  105. if (line.length > 100) {
  106. error("cjd's editor window is only 100 characters wide");
  107. }
  108. if (/\.h$/.test(fileName) && fileName.indexOf('util/platform/libc/') === -1) {
  109. // If the name is CryptoAuth_pvt.h, it's ok to make a structure called CryptoAuth
  110. var nameRe = name.replace(/_pvt$/, '').replace(/_impl$/, '');
  111. if (/^struct /.test(line) && line.indexOf('struct ' + nameRe) !== 0 && !(/\(/.test(line))) {
  112. error("all structures must begin with the name of the file.");
  113. }
  114. if (/#define /.test(line) && line.indexOf('#define ' + nameRe) === -1) {
  115. error("all defines must begin with the name of the file.");
  116. }
  117. }
  118. if (/\t/.test(line)) {
  119. error("tabs are not allowed, use 4 spaces.");
  120. }
  121. if (/\s$/.test(line)) {
  122. error("trailing whitespace.");
  123. }
  124. if (/[^A-Z](TODO|FIXME|XXX)[^A-Z]/.test(line)) {
  125. if (/[^A-Z](TODO|FIXME|XXX)[^\(A-Z]/.test(line)) {
  126. error("Please take responsibility for your TODO: eg: // TODO(cjd): make this work");
  127. } else {
  128. console.log(lineInfo + ' ' + line.replace(/[ \/]*/, ''));
  129. }
  130. }
  131. if (/(if|for|while)\(/.test(line)) {
  132. error("If/for/while statements must be followed by whitespace.");
  133. }
  134. matches = null;
  135. if (parenthCount === 0) {
  136. matches = /[^\w#](if|for|while) (\(.*$)/.exec(line);
  137. }
  138. if (parenthCount > 0 || matches) {
  139. var txt1 = (parenthCount > 0) ? line : matches[2];
  140. parenthCount += (txt1.match(/\(/g)||[]).length;
  141. parenthCount -= (txt1.match(/\)/g)||[]).length;
  142. if (parenthCount === 0) {
  143. txt1 = txt1.substring(txt1.lastIndexOf(')') + 1);
  144. // for (x; y; z) ; <-- ok
  145. // for (x; y; z) { <-- ok
  146. // for (x; y; z) { \ <-- ok (in preprocessor macro)
  147. // for (x; y; z) <-- ok but you better put a bracket on the next line
  148. // for (x; y; z) { j++; } <-- ok
  149. // for (x; y; z) j++; <-- BZZZZZZZZZZT
  150. if (!(/^[\s]*[;{].*$/.test(txt1)) && !(/^[\s]+{[\s]*\\$/).test(txt1)) {
  151. if (/[\s]*$/.test(txt1)) {
  152. expectBracket = 1;
  153. } else {
  154. error(parenthCount + ' ' + line);
  155. }
  156. }
  157. }
  158. }
  159. }
  160. return output;
  161. };
  162. var checkFile = module.exports.checkFile = function (file, callback) {
  163. Fs.readFile(file, function (err, ret) {
  164. if (err) { throw err; }
  165. callback(parseFile(file, ret.toString()));
  166. });
  167. };
  168. var lint = module.exports.lint = function (fileName, fileContent, callback) {
  169. var out = parseFile(fileName, fileContent);
  170. callback(out, !!out);
  171. };
  172. var checkFiles = module.exports.checkFiles = function (files, callback) {
  173. var sema = Semaphore.create(64);
  174. var errors = '';
  175. nThen(function (waitFor) {
  176. files.forEach(function (file) {
  177. sema.take(waitFor(function (returnAfter) {
  178. checkFile(file, waitFor(returnAfter(function (err) {
  179. if (err) {
  180. errors += file + '\n' + err + '\n';
  181. }
  182. })));
  183. }));
  184. });
  185. }).nThen(function (waitFor) {
  186. callback(errors);
  187. });
  188. };
  189. var checkDir = module.exports.checkDir = function (dir, runInFork, callback) {
  190. var gitIgnoreLines;
  191. if (runInFork) {
  192. var err = '';
  193. var out = '';
  194. var proc = Child.spawn(process.execPath, [__filename]);
  195. proc.stdout.on('data', function (data) { err += data.toString('utf8'); });
  196. proc.stderr.on('data', function (data) { err += data.toString('utf8'); });
  197. proc.on('close', function (ret) {
  198. out += err;
  199. var error;
  200. if (ret !== 0) { error = new Error(out); }
  201. callback(error, out);
  202. });
  203. return;
  204. }
  205. var output = '';
  206. nThen(function (waitFor) {
  207. Fs.readFile('.gitignore', waitFor(function (err, ret) {
  208. if (err) { throw err; }
  209. gitIgnoreLines = ret.toString('utf8').split('\n');
  210. }));
  211. }).nThen(function (waitFor) {
  212. var addDir = function (dir) {
  213. Fs.readdir(dir, waitFor(function (err, files) {
  214. if (err) { throw err; }
  215. files.forEach(function (file) {
  216. Fs.stat(dir + '/' + file, waitFor(function (err, stat) {
  217. if (err) { throw err; }
  218. if (file === '.git') {
  219. } else if (file === 'contrib') {
  220. } else if (file === 'dependencies') {
  221. } else if (gitIgnoreLines.indexOf(file) !== -1) {
  222. } else {
  223. if (stat.isDirectory()) {
  224. addDir(dir + '/' + file);
  225. } else if (/.*\.[ch]$/.test(file)) {
  226. checkFile(dir + '/' + file, waitFor(function (ret) {
  227. output += ret;
  228. }));
  229. }
  230. }
  231. }));
  232. });
  233. }));
  234. };
  235. addDir(dir);
  236. }).nThen(function (waitFor) {
  237. callback(output);
  238. });
  239. };
  240. if (module.parent === null) {
  241. checkDir('.', false, function(output) {
  242. if (output !== '') {
  243. console.log(output);
  244. process.exit(1);
  245. }
  246. });
  247. }