esvalidate.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env node
  2. /*
  3. Copyright (c) jQuery Foundation, Inc. and Contributors, All Rights Reserved.
  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. /*global phantom:true */
  24. var fs, system, esprima, options, fnames, count;
  25. if (typeof esprima === 'undefined') {
  26. // PhantomJS can only require() relative files
  27. if (typeof phantom === 'object') {
  28. fs = require('fs');
  29. system = require('system');
  30. esprima = require('./esprima');
  31. } else if (typeof require === 'function') {
  32. fs = require('fs');
  33. esprima = require('esprima');
  34. } else if (typeof load === 'function') {
  35. try {
  36. load('esprima.js');
  37. } catch (e) {
  38. load('../esprima.js');
  39. }
  40. }
  41. }
  42. // Shims to Node.js objects when running under PhantomJS 1.7+.
  43. if (typeof phantom === 'object') {
  44. fs.readFileSync = fs.read;
  45. process = {
  46. argv: [].slice.call(system.args),
  47. exit: phantom.exit
  48. };
  49. process.argv.unshift('phantomjs');
  50. }
  51. // Shims to Node.js objects when running under Rhino.
  52. if (typeof console === 'undefined' && typeof process === 'undefined') {
  53. console = { log: print };
  54. fs = { readFileSync: readFile };
  55. process = { argv: arguments, exit: quit };
  56. process.argv.unshift('esvalidate.js');
  57. process.argv.unshift('rhino');
  58. }
  59. function showUsage() {
  60. console.log('Usage:');
  61. console.log(' esvalidate [options] file.js');
  62. console.log();
  63. console.log('Available options:');
  64. console.log();
  65. console.log(' --format=type Set the report format, plain (default) or junit');
  66. console.log(' -v, --version Print program version');
  67. console.log();
  68. process.exit(1);
  69. }
  70. if (process.argv.length <= 2) {
  71. showUsage();
  72. }
  73. options = {
  74. format: 'plain'
  75. };
  76. fnames = [];
  77. process.argv.splice(2).forEach(function (entry) {
  78. if (entry === '-h' || entry === '--help') {
  79. showUsage();
  80. } else if (entry === '-v' || entry === '--version') {
  81. console.log('ECMAScript Validator (using Esprima version', esprima.version, ')');
  82. console.log();
  83. process.exit(0);
  84. } else if (entry.slice(0, 9) === '--format=') {
  85. options.format = entry.slice(9);
  86. if (options.format !== 'plain' && options.format !== 'junit') {
  87. console.log('Error: unknown report format ' + options.format + '.');
  88. process.exit(1);
  89. }
  90. } else if (entry.slice(0, 2) === '--') {
  91. console.log('Error: unknown option ' + entry + '.');
  92. process.exit(1);
  93. } else {
  94. fnames.push(entry);
  95. }
  96. });
  97. if (fnames.length === 0) {
  98. console.log('Error: no input file.');
  99. process.exit(1);
  100. }
  101. if (options.format === 'junit') {
  102. console.log('<?xml version="1.0" encoding="UTF-8"?>');
  103. console.log('<testsuites>');
  104. }
  105. count = 0;
  106. fnames.forEach(function (fname) {
  107. var content, timestamp, syntax, name;
  108. try {
  109. content = fs.readFileSync(fname, 'utf-8');
  110. if (content[0] === '#' && content[1] === '!') {
  111. content = '//' + content.substr(2, content.length);
  112. }
  113. timestamp = Date.now();
  114. syntax = esprima.parse(content, { tolerant: true });
  115. if (options.format === 'junit') {
  116. name = fname;
  117. if (name.lastIndexOf('/') >= 0) {
  118. name = name.slice(name.lastIndexOf('/') + 1);
  119. }
  120. console.log('<testsuite name="' + fname + '" errors="0" ' +
  121. ' failures="' + syntax.errors.length + '" ' +
  122. ' tests="' + syntax.errors.length + '" ' +
  123. ' time="' + Math.round((Date.now() - timestamp) / 1000) +
  124. '">');
  125. syntax.errors.forEach(function (error) {
  126. var msg = error.message;
  127. msg = msg.replace(/^Line\ [0-9]*\:\ /, '');
  128. console.log(' <testcase name="Line ' + error.lineNumber + ': ' + msg + '" ' +
  129. ' time="0">');
  130. console.log(' <error type="SyntaxError" message="' + error.message + '">' +
  131. error.message + '(' + name + ':' + error.lineNumber + ')' +
  132. '</error>');
  133. console.log(' </testcase>');
  134. });
  135. console.log('</testsuite>');
  136. } else if (options.format === 'plain') {
  137. syntax.errors.forEach(function (error) {
  138. var msg = error.message;
  139. msg = msg.replace(/^Line\ [0-9]*\:\ /, '');
  140. msg = fname + ':' + error.lineNumber + ': ' + msg;
  141. console.log(msg);
  142. ++count;
  143. });
  144. }
  145. } catch (e) {
  146. ++count;
  147. if (options.format === 'junit') {
  148. console.log('<testsuite name="' + fname + '" errors="1" failures="0" tests="1" ' +
  149. ' time="' + Math.round((Date.now() - timestamp) / 1000) + '">');
  150. console.log(' <testcase name="' + e.message + '" ' + ' time="0">');
  151. console.log(' <error type="ParseError" message="' + e.message + '">' +
  152. e.message + '(' + fname + ((e.lineNumber) ? ':' + e.lineNumber : '') +
  153. ')</error>');
  154. console.log(' </testcase>');
  155. console.log('</testsuite>');
  156. } else {
  157. console.log('Error: ' + e.message);
  158. }
  159. }
  160. });
  161. if (options.format === 'junit') {
  162. console.log('</testsuites>');
  163. }
  164. if (count > 0) {
  165. process.exit(1);
  166. }
  167. if (count === 0 && typeof phantom === 'object') {
  168. process.exit(0);
  169. }