Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / istanbul / node_modules / esprima / bin / esparse.js
1 #!/usr/bin/env node
2 /*
3   Copyright (c) jQuery Foundation, Inc. and Contributors, All Rights Reserved.
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright
9       notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13
14   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17   ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
18   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 /*jslint sloppy:true node:true rhino:true */
27
28 var fs, esprima, fname, content, options, syntax;
29
30 if (typeof require === 'function') {
31     fs = require('fs');
32     esprima = require('esprima');
33 } else if (typeof load === 'function') {
34     try {
35         load('esprima.js');
36     } catch (e) {
37         load('../esprima.js');
38     }
39 }
40
41 // Shims to Node.js objects when running under Rhino.
42 if (typeof console === 'undefined' && typeof process === 'undefined') {
43     console = { log: print };
44     fs = { readFileSync: readFile };
45     process = { argv: arguments, exit: quit };
46     process.argv.unshift('esparse.js');
47     process.argv.unshift('rhino');
48 }
49
50 function showUsage() {
51     console.log('Usage:');
52     console.log('   esparse [options] file.js');
53     console.log();
54     console.log('Available options:');
55     console.log();
56     console.log('  --comment      Gather all line and block comments in an array');
57     console.log('  --loc          Include line-column location info for each syntax node');
58     console.log('  --range        Include index-based range for each syntax node');
59     console.log('  --raw          Display the raw value of literals');
60     console.log('  --tokens       List all tokens in an array');
61     console.log('  --tolerant     Tolerate errors on a best-effort basis (experimental)');
62     console.log('  -v, --version  Shows program version');
63     console.log();
64     process.exit(1);
65 }
66
67 if (process.argv.length <= 2) {
68     showUsage();
69 }
70
71 options = {};
72
73 process.argv.splice(2).forEach(function (entry) {
74
75     if (entry === '-h' || entry === '--help') {
76         showUsage();
77     } else if (entry === '-v' || entry === '--version') {
78         console.log('ECMAScript Parser (using Esprima version', esprima.version, ')');
79         console.log();
80         process.exit(0);
81     } else if (entry === '--comment') {
82         options.comment = true;
83     } else if (entry === '--loc') {
84         options.loc = true;
85     } else if (entry === '--range') {
86         options.range = true;
87     } else if (entry === '--raw') {
88         options.raw = true;
89     } else if (entry === '--tokens') {
90         options.tokens = true;
91     } else if (entry === '--tolerant') {
92         options.tolerant = true;
93     } else if (entry.slice(0, 2) === '--') {
94         console.log('Error: unknown option ' + entry + '.');
95         process.exit(1);
96     } else if (typeof fname === 'string') {
97         console.log('Error: more than one input file.');
98         process.exit(1);
99     } else {
100         fname = entry;
101     }
102 });
103
104 if (typeof fname !== 'string') {
105     console.log('Error: no input file.');
106     process.exit(1);
107 }
108
109 // Special handling for regular expression literal since we need to
110 // convert it to a string literal, otherwise it will be decoded
111 // as object "{}" and the regular expression would be lost.
112 function adjustRegexLiteral(key, value) {
113     if (key === 'value' && value instanceof RegExp) {
114         value = value.toString();
115     }
116     return value;
117 }
118
119 try {
120     content = fs.readFileSync(fname, 'utf-8');
121     syntax = esprima.parse(content, options);
122     console.log(JSON.stringify(syntax, adjustRegexLiteral, 4));
123 } catch (e) {
124     console.log('Error: ' + e.message);
125     process.exit(1);
126 }