react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / node_modules / esprima-fb / bin / esvalidate.js
80556 views#!/usr/bin/env node1/*2Copyright (C) 2012 Ariya Hidayat <[email protected]>34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are met:67* Redistributions of source code must retain the above copyright8notice, this list of conditions and the following disclaimer.9* Redistributions in binary form must reproduce the above copyright10notice, this list of conditions and the following disclaimer in the11documentation and/or other materials provided with the distribution.1213THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"14AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY17DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES18(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;19LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND20ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/2425/*jslint sloppy:true plusplus:true node:true rhino:true */26/*global phantom:true */2728var fs, system, esprima, options, fnames, count;2930if (typeof esprima === 'undefined') {31// PhantomJS can only require() relative files32if (typeof phantom === 'object') {33fs = require('fs');34system = require('system');35esprima = require('./esprima');36} else if (typeof require === 'function') {37fs = require('fs');38esprima = require('esprima');39} else if (typeof load === 'function') {40try {41load('esprima.js');42} catch (e) {43load('../esprima.js');44}45}46}4748// Shims to Node.js objects when running under PhantomJS 1.7+.49if (typeof phantom === 'object') {50fs.readFileSync = fs.read;51process = {52argv: [].slice.call(system.args),53exit: phantom.exit54};55process.argv.unshift('phantomjs');56}5758// Shims to Node.js objects when running under Rhino.59if (typeof console === 'undefined' && typeof process === 'undefined') {60console = { log: print };61fs = { readFileSync: readFile };62process = { argv: arguments, exit: quit };63process.argv.unshift('esvalidate.js');64process.argv.unshift('rhino');65}6667function showUsage() {68console.log('Usage:');69console.log(' esvalidate [options] file.js');70console.log();71console.log('Available options:');72console.log();73console.log(' --format=type Set the report format, plain (default) or junit');74console.log(' -v, --version Print program version');75console.log();76process.exit(1);77}7879if (process.argv.length <= 2) {80showUsage();81}8283options = {84format: 'plain'85};8687fnames = [];8889process.argv.splice(2).forEach(function (entry) {9091if (entry === '-h' || entry === '--help') {92showUsage();93} else if (entry === '-v' || entry === '--version') {94console.log('ECMAScript Validator (using Esprima version', esprima.version, ')');95console.log();96process.exit(0);97} else if (entry.slice(0, 9) === '--format=') {98options.format = entry.slice(9);99if (options.format !== 'plain' && options.format !== 'junit') {100console.log('Error: unknown report format ' + options.format + '.');101process.exit(1);102}103} else if (entry.slice(0, 2) === '--') {104console.log('Error: unknown option ' + entry + '.');105process.exit(1);106} else {107fnames.push(entry);108}109});110111if (fnames.length === 0) {112console.log('Error: no input file.');113process.exit(1);114}115116if (options.format === 'junit') {117console.log('<?xml version="1.0" encoding="UTF-8"?>');118console.log('<testsuites>');119}120121count = 0;122fnames.forEach(function (fname) {123var content, timestamp, syntax, name;124try {125content = fs.readFileSync(fname, 'utf-8');126127if (content[0] === '#' && content[1] === '!') {128content = '//' + content.substr(2, content.length);129}130131timestamp = Date.now();132syntax = esprima.parse(content, { tolerant: true });133134if (options.format === 'junit') {135136name = fname;137if (name.lastIndexOf('/') >= 0) {138name = name.slice(name.lastIndexOf('/') + 1);139}140141console.log('<testsuite name="' + fname + '" errors="0" ' +142' failures="' + syntax.errors.length + '" ' +143' tests="' + syntax.errors.length + '" ' +144' time="' + Math.round((Date.now() - timestamp) / 1000) +145'">');146147syntax.errors.forEach(function (error) {148var msg = error.message;149msg = msg.replace(/^Line\ [0-9]*\:\ /, '');150console.log(' <testcase name="Line ' + error.lineNumber + ': ' + msg + '" ' +151' time="0">');152console.log(' <error type="SyntaxError" message="' + error.message + '">' +153error.message + '(' + name + ':' + error.lineNumber + ')' +154'</error>');155console.log(' </testcase>');156});157158console.log('</testsuite>');159160} else if (options.format === 'plain') {161162syntax.errors.forEach(function (error) {163var msg = error.message;164msg = msg.replace(/^Line\ [0-9]*\:\ /, '');165msg = fname + ':' + error.lineNumber + ': ' + msg;166console.log(msg);167++count;168});169170}171} catch (e) {172++count;173if (options.format === 'junit') {174console.log('<testsuite name="' + fname + '" errors="1" failures="0" tests="1" ' +175' time="' + Math.round((Date.now() - timestamp) / 1000) + '">');176console.log(' <testcase name="' + e.message + '" ' + ' time="0">');177console.log(' <error type="ParseError" message="' + e.message + '">' +178e.message + '(' + fname + ((e.lineNumber) ? ':' + e.lineNumber : '') +179')</error>');180console.log(' </testcase>');181console.log('</testsuite>');182} else {183console.log('Error: ' + e.message);184}185}186});187188if (options.format === 'junit') {189console.log('</testsuites>');190}191192if (count > 0) {193process.exit(1);194}195196if (count === 0 && typeof phantom === 'object') {197process.exit(0);198}199200201