-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathesvalidate.js
155 lines (122 loc) · 3.46 KB
/
esvalidate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*global log:true esprima:true fs:true */
var options, fnames, count, formatter, dieLoudly, formatter;
function tryGet() {
'use strict';
var args = [].slice.apply(arguments),
valueToGet = null,
path = null;
if (args.length) {
path = args.splice(0, 1)[0];
try {
valueToGet = require(path);
} catch (e) {
log(e);
return tryGet.apply(this, args);
}
}
return valueToGet;
}
function showUsage() {
'use strict';
log('Usage:');
log(' esvalidate [options] file.js');
log();
log('Available options:');
log();
log(' --format=type Set the report format, plain (default) or junit');
log(' --formatter=file Path to a formatter.js file');
log(' -v, --version Print program version');
log(' -q, --quiet If an error occurs during parsing, do not return an error code');
log();
process.exit(1);
}
if (process.argv.length <= 2) {
showUsage();
}
options = {
format: 'plain'
};
fnames = [];
dieLoudly = true;
process.argv.splice(2).forEach(function (entry) {
'use strict';
if (entry === '-h' || entry === '--help') {
showUsage();
} else if (entry === '-v' || entry === '--version') {
log('ECMAScript Validator (using Esprima version', esprima.version, ')');
log();
process.exit(0);
} else if (entry === '-q' || entry === '--quiet') {
dieLoudly = false;
} else if (entry.slice(0, 9) === '--format=') {
options.format = entry.slice(9);
} else if (entry.slice(0, 12) === '--formatter=') {
options.format = entry.slice(12);
} else if (entry.slice(0, 2) === '--') {
log('Error: unknown option ' + entry + '.');
process.exit(1);
} else {
fnames.push(entry);
}
});
if (options.format.slice(options.format.length - 3).toLowerCase() !== '.js') {
options.format = options.format + '.js';
}
var tempFormatter = tryGet('../formats/' + options.format, options.format, './' + options.format);
if (!formatter && tempFormatter) {
formatter = tempFormatter;
}
if (!formatter) {
log('Error: unknown report format ' + options.format);
process.exit(1);
} else {
formatter = formatter(log);
}
if (fnames.length === 0) {
log('Error: no input file.');
process.exit(1);
}
formatter.startLog();
count = 0;
fnames.forEach(function (fname) {
'use strict';
var content, timestamp, syntax, name, errors, failures, tests, time;
timestamp = Date.now();
try {
content = fs.readFileSync(fname, 'utf-8');
if (content[0] === '#' && content[1] === '!') {
content = '//' + content.substr(2, content.length);
}
syntax = esprima.parse(content, { tolerant: true });
name = fname;
if (name.lastIndexOf('/') >= 0) {
name = name.slice(name.lastIndexOf('/') + 1);
}
errors = 0;
failures = syntax.errors.length;
tests = syntax.errors.length;
time = Math.round((Date.now() - timestamp) / 1000);
formatter.startSection(name, errors, failures, tests, time);
syntax.errors.forEach(function (error) {
formatter.writeError(name, error, "SyntaxError");
++count;
});
formatter.endSection();
} catch (e) {
++count;
errors = 1;
failures = 0;
tests = 1;
time = Math.round((Date.now() - timestamp) / 1000);
formatter.startSection(fname, errors, failures, tests, time);
formatter.writeError(fname, e, "ParseError");
formatter.endSection();
}
});
formatter.endLog();
if ((count > 0) && dieLoudly) {
process.exit(1);
}
if (count === 0 && typeof phantom === 'object') {
process.exit(0);
}