Skip to content

Commit 8f1c05a

Browse files
metafeatherariya
authored andcommitted
Adapt esvalidate.js to run under PhantomJS as well as Node and Rhino
Added object checks and shims to enable esvalidate.js to load and run under PhantomJS 1.7+ http://code.google.com/p/esprima/issues/detail?id=373
1 parent f803e3b commit 8f1c05a

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

bin/esvalidate.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,38 @@
2424
*/
2525

2626
/*jslint sloppy:true plusplus:true node:true rhino:true */
27-
28-
var fs, esprima, options, fnames, count;
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');
27+
/*global phantom:true */
28+
29+
var fs, system, esprima, options, fnames, count;
30+
31+
if (typeof esprima === 'undefined') {
32+
// PhantomJS can only require() relative files
33+
if (typeof phantom === 'object') {
34+
fs = require('fs');
35+
system = require('system');
36+
esprima = require('./esprima');
37+
} else if (typeof require === 'function') {
38+
fs = require('fs');
39+
esprima = require('esprima');
40+
} else if (typeof load === 'function') {
41+
try {
42+
load('esprima.js');
43+
} catch (e) {
44+
load('../esprima.js');
45+
}
3846
}
3947
}
4048

49+
// Shims to Node.js objects when running under PhantomJS 1.7+.
50+
if (typeof phantom === 'object') {
51+
fs.readFileSync = fs.read;
52+
process = {
53+
argv: [].slice.call(system.args),
54+
exit: phantom.exit
55+
};
56+
process.argv.unshift('phantomjs');
57+
}
58+
4159
// Shims to Node.js objects when running under Rhino.
4260
if (typeof console === 'undefined' && typeof process === 'undefined') {
4361
console = { log: print };
@@ -164,3 +182,7 @@ if (options.format === 'junit') {
164182
if (count > 0) {
165183
process.exit(1);
166184
}
185+
186+
if (count === 0 && typeof phantom === 'object') {
187+
process.exit(0);
188+
}

0 commit comments

Comments
 (0)