Нет описания

esvalidate 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env node
  2. /*
  3. Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  15. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  18. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  20. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. /*jslint sloppy:true plusplus:true node:true rhino:true */
  23. var fs, esprima, options, fnames, count;
  24. if (typeof require === 'function') {
  25. fs = require('fs');
  26. esprima = require('esprima');
  27. } else if (typeof load === 'function') {
  28. try {
  29. load('esprima.js');
  30. } catch (e) {
  31. load('../esprima.js');
  32. }
  33. }
  34. // Shims to Node.js objects when running under Rhino.
  35. if (typeof console === 'undefined' && typeof process === 'undefined') {
  36. console = { log: print };
  37. fs = { readFileSync: readFile };
  38. process = { argv: arguments, exit: quit };
  39. process.argv.unshift('esvalidate.js');
  40. process.argv.unshift('rhino');
  41. }
  42. function showUsage() {
  43. console.log('Usage:');
  44. console.log(' esvalidate [options] file.js');
  45. console.log();
  46. console.log('Available options:');
  47. console.log();
  48. console.log(' --format=type Set the report format, plain (default) or junit');
  49. console.log(' -v, --version Print program version');
  50. console.log();
  51. process.exit(1);
  52. }
  53. if (process.argv.length <= 2) {
  54. showUsage();
  55. }
  56. options = {
  57. format: 'plain'
  58. };
  59. fnames = [];
  60. process.argv.splice(2).forEach(function (entry) {
  61. if (entry === '-h' || entry === '--help') {
  62. showUsage();
  63. } else if (entry === '-v' || entry === '--version') {
  64. console.log('ECMAScript Validator (using Esprima version', esprima.version, ')');
  65. console.log();
  66. process.exit(0);
  67. } else if (entry.slice(0, 9) === '--format=') {
  68. options.format = entry.slice(9);
  69. if (options.format !== 'plain' && options.format !== 'junit') {
  70. console.log('Error: unknown report format ' + options.format + '.');
  71. process.exit(1);
  72. }
  73. } else if (entry.slice(0, 2) === '--') {
  74. console.log('Error: unknown option ' + entry + '.');
  75. process.exit(1);
  76. } else {
  77. fnames.push(entry);
  78. }
  79. });
  80. if (fnames.length === 0) {
  81. console.log('Error: no input file.');
  82. process.exit(1);
  83. }
  84. if (options.format === 'junit') {
  85. console.log('<?xml version="1.0" encoding="UTF-8"?>');
  86. console.log('<testsuites>');
  87. }
  88. count = 0;
  89. fnames.forEach(function (fname) {
  90. var content, timestamp, syntax, name;
  91. try {
  92. content = fs.readFileSync(fname, 'utf-8');
  93. if (content[0] === '#' && content[1] === '!') {
  94. content = '//' + content.substr(2, content.length);
  95. }
  96. timestamp = Date.now();
  97. syntax = esprima.parse(content, { tolerant: true });
  98. if (options.format === 'junit') {
  99. name = fname;
  100. if (name.lastIndexOf('/') >= 0) {
  101. name = name.slice(name.lastIndexOf('/') + 1);
  102. }
  103. console.log('<testsuite name="' + fname + '" errors="0" ' +
  104. ' failures="' + syntax.errors.length + '" ' +
  105. ' tests="' + syntax.errors.length + '" ' +
  106. ' time="' + Math.round((Date.now() - timestamp) / 1000) +
  107. '">');
  108. syntax.errors.forEach(function (error) {
  109. var msg = error.message;
  110. msg = msg.replace(/^Line\ [0-9]*\:\ /, '');
  111. console.log(' <testcase name="Line ' + error.lineNumber + ': ' + msg + '" ' +
  112. ' time="0">');
  113. console.log(' <error type="SyntaxError" message="' + error.message + '">' +
  114. error.message + '(' + name + ':' + error.lineNumber + ')' +
  115. '</error>');
  116. console.log(' </testcase>');
  117. });
  118. console.log('</testsuite>');
  119. } else if (options.format === 'plain') {
  120. syntax.errors.forEach(function (error) {
  121. var msg = error.message;
  122. msg = msg.replace(/^Line\ [0-9]*\:\ /, '');
  123. msg = fname + ':' + error.lineNumber + ': ' + msg;
  124. console.log(msg);
  125. ++count;
  126. });
  127. }
  128. } catch (e) {
  129. ++count;
  130. if (options.format === 'junit') {
  131. console.log('<testsuite name="' + fname + '" errors="1" failures="0" tests="1" ' +
  132. ' time="' + Math.round((Date.now() - timestamp) / 1000) + '">');
  133. console.log(' <testcase name="' + e.message + '" ' + ' time="0">');
  134. console.log(' <error type="ParseError" message="' + e.message + '">' +
  135. e.message + '(' + fname + ((e.lineNumber) ? ':' + e.lineNumber : '') +
  136. ')</error>');
  137. console.log(' </testcase>');
  138. console.log('</testsuite>');
  139. } else {
  140. console.log('Error: ' + e.message);
  141. }
  142. }
  143. });
  144. if (options.format === 'junit') {
  145. console.log('</testsuites>');
  146. }
  147. if (count > 0) {
  148. process.exit(1);
  149. }