javascript-lint.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("../../lib/codemirror"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["../../lib/codemirror"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. "use strict";
  10. // declare global: JSHINT
  11. var bogus = [ "Dangerous comment" ];
  12. var warnings = [ [ "Expected '{'",
  13. "Statement body should be inside '{ }' braces." ] ];
  14. var errors = [ "Missing semicolon", "Extra comma", "Missing property name",
  15. "Unmatched ", " and instead saw", " is not defined",
  16. "Unclosed string", "Stopping, unable to continue" ];
  17. function validator(text, options) {
  18. JSHINT(text, options);
  19. var errors = JSHINT.data().errors, result = [];
  20. if (errors) parseErrors(errors, result);
  21. return result;
  22. }
  23. CodeMirror.registerHelper("lint", "javascript", validator);
  24. function cleanup(error) {
  25. // All problems are warnings by default
  26. fixWith(error, warnings, "warning", true);
  27. fixWith(error, errors, "error");
  28. return isBogus(error) ? null : error;
  29. }
  30. function fixWith(error, fixes, severity, force) {
  31. var description, fix, find, replace, found;
  32. description = error.description;
  33. for ( var i = 0; i < fixes.length; i++) {
  34. fix = fixes[i];
  35. find = (typeof fix === "string" ? fix : fix[0]);
  36. replace = (typeof fix === "string" ? null : fix[1]);
  37. found = description.indexOf(find) !== -1;
  38. if (force || found) {
  39. error.severity = severity;
  40. }
  41. if (found && replace) {
  42. error.description = replace;
  43. }
  44. }
  45. }
  46. function isBogus(error) {
  47. var description = error.description;
  48. for ( var i = 0; i < bogus.length; i++) {
  49. if (description.indexOf(bogus[i]) !== -1) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. function parseErrors(errors, output) {
  56. for ( var i = 0; i < errors.length; i++) {
  57. var error = errors[i];
  58. if (error) {
  59. var linetabpositions, index;
  60. linetabpositions = [];
  61. // This next block is to fix a problem in jshint. Jshint
  62. // replaces
  63. // all tabs with spaces then performs some checks. The error
  64. // positions (character/space) are then reported incorrectly,
  65. // not taking the replacement step into account. Here we look
  66. // at the evidence line and try to adjust the character position
  67. // to the correct value.
  68. if (error.evidence) {
  69. // Tab positions are computed once per line and cached
  70. var tabpositions = linetabpositions[error.line];
  71. if (!tabpositions) {
  72. var evidence = error.evidence;
  73. tabpositions = [];
  74. // ugggh phantomjs does not like this
  75. // forEachChar(evidence, function(item, index) {
  76. Array.prototype.forEach.call(evidence, function(item,
  77. index) {
  78. if (item === '\t') {
  79. // First col is 1 (not 0) to match error
  80. // positions
  81. tabpositions.push(index + 1);
  82. }
  83. });
  84. linetabpositions[error.line] = tabpositions;
  85. }
  86. if (tabpositions.length > 0) {
  87. var pos = error.character;
  88. tabpositions.forEach(function(tabposition) {
  89. if (pos > tabposition) pos -= 1;
  90. });
  91. error.character = pos;
  92. }
  93. }
  94. var start = error.character - 1, end = start + 1;
  95. if (error.evidence) {
  96. index = error.evidence.substring(start).search(/.\b/);
  97. if (index > -1) {
  98. end += index;
  99. }
  100. }
  101. // Convert to format expected by validation service
  102. error.description = error.reason;// + "(jshint)";
  103. error.start = error.character;
  104. error.end = end;
  105. error = cleanup(error);
  106. if (error)
  107. output.push({message: error.description,
  108. severity: error.severity,
  109. from: CodeMirror.Pos(error.line - 1, start),
  110. to: CodeMirror.Pos(error.line - 1, end)});
  111. }
  112. }
  113. }
  114. });