javascript-lint.js 4.3 KB

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