UI for Zipcoin Blue

detectnestedternary.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Usage: node detectnestedternary.js /path/to/some/directory
  2. // For more details, please read http://esprima.org/doc/#nestedternary
  3. /*jslint node:true sloppy:true plusplus:true */
  4. var fs = require('fs'),
  5. esprima = require('../esprima'),
  6. dirname = process.argv[2];
  7. // Executes visitor on the object and its children (recursively).
  8. function traverse(object, visitor) {
  9. var key, child;
  10. visitor.call(null, object);
  11. for (key in object) {
  12. if (object.hasOwnProperty(key)) {
  13. child = object[key];
  14. if (typeof child === 'object' && child !== null) {
  15. traverse(child, visitor);
  16. }
  17. }
  18. }
  19. }
  20. // http://stackoverflow.com/q/5827612/
  21. function walk(dir, done) {
  22. var results = [];
  23. fs.readdir(dir, function (err, list) {
  24. if (err) {
  25. return done(err);
  26. }
  27. var i = 0;
  28. (function next() {
  29. var file = list[i++];
  30. if (!file) {
  31. return done(null, results);
  32. }
  33. file = dir + '/' + file;
  34. fs.stat(file, function (err, stat) {
  35. if (stat && stat.isDirectory()) {
  36. walk(file, function (err, res) {
  37. results = results.concat(res);
  38. next();
  39. });
  40. } else {
  41. results.push(file);
  42. next();
  43. }
  44. });
  45. }());
  46. });
  47. }
  48. walk(dirname, function (err, results) {
  49. if (err) {
  50. console.log('Error', err);
  51. return;
  52. }
  53. results.forEach(function (filename) {
  54. var shortname, first, content, syntax;
  55. shortname = filename;
  56. first = true;
  57. if (shortname.substr(0, dirname.length) === dirname) {
  58. shortname = shortname.substr(dirname.length + 1, shortname.length);
  59. }
  60. function report(node, problem) {
  61. if (first === true) {
  62. console.log(shortname + ': ');
  63. first = false;
  64. }
  65. console.log(' Line', node.loc.start.line, ':', problem);
  66. }
  67. function checkConditional(node) {
  68. var condition;
  69. if (node.consequent.type === 'ConditionalExpression' ||
  70. node.alternate.type === 'ConditionalExpression') {
  71. condition = content.substring(node.test.range[0], node.test.range[1]);
  72. if (condition.length > 20) {
  73. condition = condition.substring(0, 20) + '...';
  74. }
  75. condition = '"' + condition + '"';
  76. report(node, 'Nested ternary for ' + condition);
  77. }
  78. }
  79. try {
  80. content = fs.readFileSync(filename, 'utf-8');
  81. syntax = esprima.parse(content, { tolerant: true, loc: true, range: true });
  82. traverse(syntax, function (node) {
  83. if (node.type === 'ConditionalExpression') {
  84. checkConditional(node);
  85. }
  86. });
  87. } catch (e) {
  88. }
  89. });
  90. });