You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
7.6 KiB

  1. #!/usr/bin/env node
  2. /*
  3. Copyright JS Foundation and other contributors, https://js.foundation/
  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, forceFile, 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. try {
  34. esprima = require('esprima');
  35. } catch (e) {
  36. esprima = require('../');
  37. }
  38. } else if (typeof load === 'function') {
  39. try {
  40. load('esprima.js');
  41. } catch (e) {
  42. load('../esprima.js');
  43. }
  44. }
  45. }
  46. // Shims to Node.js objects when running under PhantomJS 1.7+.
  47. if (typeof phantom === 'object') {
  48. fs.readFileSync = fs.read;
  49. process = {
  50. argv: [].slice.call(system.args),
  51. exit: phantom.exit,
  52. on: function (evt, callback) {
  53. callback();
  54. }
  55. };
  56. process.argv.unshift('phantomjs');
  57. }
  58. // Shims to Node.js objects when running under Rhino.
  59. if (typeof console === 'undefined' && typeof process === 'undefined') {
  60. console = { log: print };
  61. fs = { readFileSync: readFile };
  62. process = {
  63. argv: arguments,
  64. exit: quit,
  65. on: function (evt, callback) {
  66. callback();
  67. }
  68. };
  69. process.argv.unshift('esvalidate.js');
  70. process.argv.unshift('rhino');
  71. }
  72. function showUsage() {
  73. console.log('Usage:');
  74. console.log(' esvalidate [options] [file.js...]');
  75. console.log();
  76. console.log('Available options:');
  77. console.log();
  78. console.log(' --format=type Set the report format, plain (default) or junit');
  79. console.log(' -v, --version Print program version');
  80. console.log();
  81. process.exit(1);
  82. }
  83. options = {
  84. format: 'plain'
  85. };
  86. fnames = [];
  87. process.argv.splice(2).forEach(function (entry) {
  88. if (forceFile || entry === '-' || entry.slice(0, 1) !== '-') {
  89. fnames.push(entry);
  90. } else if (entry === '-h' || entry === '--help') {
  91. showUsage();
  92. } else if (entry === '-v' || entry === '--version') {
  93. console.log('ECMAScript Validator (using Esprima version', esprima.version, ')');
  94. console.log();
  95. process.exit(0);
  96. } else if (entry.slice(0, 9) === '--format=') {
  97. options.format = entry.slice(9);
  98. if (options.format !== 'plain' && options.format !== 'junit') {
  99. console.log('Error: unknown report format ' + options.format + '.');
  100. process.exit(1);
  101. }
  102. } else if (entry === '--') {
  103. forceFile = true;
  104. } else {
  105. console.log('Error: unknown option ' + entry + '.');
  106. process.exit(1);
  107. }
  108. });
  109. if (fnames.length === 0) {
  110. fnames.push('');
  111. }
  112. if (options.format === 'junit') {
  113. console.log('<?xml version="1.0" encoding="UTF-8"?>');
  114. console.log('<testsuites>');
  115. }
  116. count = 0;
  117. function run(fname, content) {
  118. var timestamp, syntax, name;
  119. try {
  120. if (typeof content !== 'string') {
  121. throw content;
  122. }
  123. if (content[0] === '#' && content[1] === '!') {
  124. content = '//' + content.substr(2, content.length);
  125. }
  126. timestamp = Date.now();
  127. syntax = esprima.parse(content, { tolerant: true });
  128. if (options.format === 'junit') {
  129. name = fname;
  130. if (name.lastIndexOf('/') >= 0) {
  131. name = name.slice(name.lastIndexOf('/') + 1);
  132. }
  133. console.log('<testsuite name="' + fname + '" errors="0" ' +
  134. ' failures="' + syntax.errors.length + '" ' +
  135. ' tests="' + syntax.errors.length + '" ' +
  136. ' time="' + Math.round((Date.now() - timestamp) / 1000) +
  137. '">');
  138. syntax.errors.forEach(function (error) {
  139. var msg = error.message;
  140. msg = msg.replace(/^Line\ [0-9]*\:\ /, '');
  141. console.log(' <testcase name="Line ' + error.lineNumber + ': ' + msg + '" ' +
  142. ' time="0">');
  143. console.log(' <error type="SyntaxError" message="' + error.message + '">' +
  144. error.message + '(' + name + ':' + error.lineNumber + ')' +
  145. '</error>');
  146. console.log(' </testcase>');
  147. });
  148. console.log('</testsuite>');
  149. } else if (options.format === 'plain') {
  150. syntax.errors.forEach(function (error) {
  151. var msg = error.message;
  152. msg = msg.replace(/^Line\ [0-9]*\:\ /, '');
  153. msg = fname + ':' + error.lineNumber + ': ' + msg;
  154. console.log(msg);
  155. ++count;
  156. });
  157. }
  158. } catch (e) {
  159. ++count;
  160. if (options.format === 'junit') {
  161. console.log('<testsuite name="' + fname + '" errors="1" failures="0" tests="1" ' +
  162. ' time="' + Math.round((Date.now() - timestamp) / 1000) + '">');
  163. console.log(' <testcase name="' + e.message + '" ' + ' time="0">');
  164. console.log(' <error type="ParseError" message="' + e.message + '">' +
  165. e.message + '(' + fname + ((e.lineNumber) ? ':' + e.lineNumber : '') +
  166. ')</error>');
  167. console.log(' </testcase>');
  168. console.log('</testsuite>');
  169. } else {
  170. console.log(fname + ':' + e.lineNumber + ': ' + e.message.replace(/^Line\ [0-9]*\:\ /, ''));
  171. }
  172. }
  173. }
  174. fnames.forEach(function (fname) {
  175. var content = '';
  176. try {
  177. if (fname && (fname !== '-' || forceFile)) {
  178. content = fs.readFileSync(fname, 'utf-8');
  179. } else {
  180. fname = '';
  181. process.stdin.resume();
  182. process.stdin.on('data', function(chunk) {
  183. content += chunk;
  184. });
  185. process.stdin.on('end', function() {
  186. run(fname, content);
  187. });
  188. return;
  189. }
  190. } catch (e) {
  191. content = e;
  192. }
  193. run(fname, content);
  194. });
  195. process.on('exit', function () {
  196. if (options.format === 'junit') {
  197. console.log('</testsuites>');
  198. }
  199. if (count > 0) {
  200. process.exit(1);
  201. }
  202. if (count === 0 && typeof phantom === 'object') {
  203. process.exit(0);
  204. }
  205. });