2
0

lint.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Simple linter, based on the Acorn [1] parser module
  3. All of the existing linters either cramp my style or have huge
  4. dependencies (Closure). So here's a very simple, non-invasive one
  5. that only spots
  6. - missing semicolons and trailing commas
  7. - variables or properties that are reserved words
  8. - assigning to a variable you didn't declare
  9. [1]: https://github.com/marijnh/acorn/
  10. */
  11. var fs = require("fs"), acorn = require("./acorn.js"), walk = require("./walk.js");
  12. var scopePasser = walk.make({
  13. ScopeBody: function(node, prev, c) { c(node, node.scope); }
  14. });
  15. function checkFile(fileName) {
  16. var file = fs.readFileSync(fileName, "utf8"), notAllowed;
  17. if (notAllowed = file.match(/[\x00-\x08\x0b\x0c\x0e-\x19\uFEFF\t]|[ \t]\n/)) {
  18. var msg;
  19. if (notAllowed[0] == "\t") msg = "Found tab character";
  20. else if (notAllowed[0].indexOf("\n") > -1) msg = "Trailing whitespace";
  21. else msg = "Undesirable character " + notAllowed[0].charCodeAt(0);
  22. var info = acorn.getLineInfo(file, notAllowed.index);
  23. fail(msg + " at line " + info.line + ", column " + info.column, {source: fileName});
  24. }
  25. try {
  26. var parsed = acorn.parse(file, {
  27. locations: true,
  28. ecmaVersion: 3,
  29. strictSemicolons: true,
  30. allowTrailingCommas: false,
  31. forbidReserved: true,
  32. sourceFile: fileName
  33. });
  34. } catch (e) {
  35. fail(e.message, {source: fileName});
  36. return;
  37. }
  38. var scopes = [];
  39. walk.simple(parsed, {
  40. ScopeBody: function(node, scope) {
  41. node.scope = scope;
  42. scopes.push(scope);
  43. }
  44. }, walk.scopeVisitor, {vars: Object.create(null)});
  45. var ignoredGlobals = Object.create(null);
  46. function inScope(name, scope) {
  47. for (var cur = scope; cur; cur = cur.prev)
  48. if (name in cur.vars) return true;
  49. }
  50. function checkLHS(node, scope) {
  51. if (node.type == "Identifier" && !(node.name in ignoredGlobals) &&
  52. !inScope(node.name, scope)) {
  53. ignoredGlobals[node.name] = true;
  54. fail("Assignment to global variable", node.loc);
  55. }
  56. }
  57. walk.simple(parsed, {
  58. UpdateExpression: function(node, scope) {checkLHS(node.argument, scope);},
  59. AssignmentExpression: function(node, scope) {checkLHS(node.left, scope);},
  60. Identifier: function(node, scope) {
  61. // Mark used identifiers
  62. for (var cur = scope; cur; cur = cur.prev)
  63. if (node.name in cur.vars) {
  64. cur.vars[node.name].used = true;
  65. return;
  66. }
  67. },
  68. FunctionExpression: function(node) {
  69. if (node.id) fail("Named function expression", node.loc);
  70. }
  71. }, scopePasser);
  72. for (var i = 0; i < scopes.length; ++i) {
  73. var scope = scopes[i];
  74. for (var name in scope.vars) {
  75. var info = scope.vars[name];
  76. if (!info.used && info.type != "catch clause" && info.type != "function name" && name.charAt(0) != "_")
  77. fail("Unused " + info.type + " " + name, info.node.loc);
  78. }
  79. }
  80. }
  81. var failed = false;
  82. function fail(msg, pos) {
  83. if (pos.start) msg += " (" + pos.start.line + ":" + pos.start.column + ")";
  84. console.log(pos.source + ": " + msg);
  85. failed = true;
  86. }
  87. function checkDir(dir) {
  88. fs.readdirSync(dir).forEach(function(file) {
  89. var fname = dir + "/" + file;
  90. if (/\.js$/.test(file)) checkFile(fname);
  91. else if (fs.lstatSync(fname).isDirectory()) checkDir(fname);
  92. });
  93. }
  94. exports.checkDir = checkDir;
  95. exports.checkFile = checkFile;
  96. exports.success = function() { return !failed; };