javascript-lint.js 4.0 KB

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