Skip to content

Commit 523802b

Browse files
committed
Add shim for missing window object in JavaScript context
Workaround for jshint/jshint#1038
1 parent f2d0979 commit 523802b

File tree

1 file changed

+25
-17
lines changed
  • bundles/com.eclipsesource.jshint/src/com/eclipsesource/jshint

1 file changed

+25
-17
lines changed

bundles/com.eclipsesource.jshint/src/com/eclipsesource/jshint/JSHint.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,8 @@ private void load( Reader reader ) throws IOException {
176176
context.setOptimizationLevel( 9 );
177177
context.setLanguageVersion( Context.VERSION_1_5 );
178178
scope = context.initStandardObjects();
179+
context.evaluateString( scope, createShimCode(), "shim", 1, null );
179180
context.evaluateReader( scope, reader, "jshint library", 1, null );
180-
String code = "console = {log:function(){},error:function(){},trace:function(){}};";
181-
context.evaluateString( scope, code, "fake console", 1, null );
182181
jshint = findJSHintFunction( scope );
183182
} catch( RhinoException exception ) {
184183
throw new IllegalArgumentException( "Could not evaluate JavaScript input", exception );
@@ -200,21 +199,6 @@ private boolean checkCode( Context context, String code ) {
200199
}
201200
}
202201

203-
private Function findJSHintFunction( ScriptableObject scope ) throws IllegalArgumentException {
204-
Object object;
205-
if( ScriptableObject.hasProperty( scope, "JSHINT" ) ) {
206-
object = scope.get( "JSHINT", scope );
207-
} else if( ScriptableObject.hasProperty( scope, "JSLINT" ) ) {
208-
object = scope.get( "JSLINT", scope );
209-
} else {
210-
throw new IllegalArgumentException( "Global JSHINT or JSLINT function missing in input" );
211-
}
212-
if( !( object instanceof Function ) ) {
213-
throw new IllegalArgumentException( "Global JSHINT or JSLINT is not a function" );
214-
}
215-
return (Function)object;
216-
}
217-
218202
private void handleProblems( ProblemHandler handler, Text text ) {
219203
NativeArray errors = (NativeArray)jshint.get( "errors", jshint );
220204
long length = errors.getLength();
@@ -255,6 +239,30 @@ private int fixPosition( Text text, int line, int character ) {
255239
return charIndex;
256240
}
257241

242+
private static String createShimCode() {
243+
// Create shims to prevent problems with JSHint accessing objects that are not available in
244+
// Rhino, e.g. https://github.com/jshint/jshint/issues/1038
245+
return "console = {log:function(){},error:function(){},trace:function(){}};"
246+
+ "window = {};";
247+
}
248+
249+
private static Function findJSHintFunction( ScriptableObject scope )
250+
throws IllegalArgumentException
251+
{
252+
Object object;
253+
if( ScriptableObject.hasProperty( scope, "JSHINT" ) ) {
254+
object = scope.get( "JSHINT", scope );
255+
} else if( ScriptableObject.hasProperty( scope, "JSLINT" ) ) {
256+
object = scope.get( "JSLINT", scope );
257+
} else {
258+
throw new IllegalArgumentException( "Global JSHINT or JSLINT function missing in input" );
259+
}
260+
if( !( object instanceof Function ) ) {
261+
throw new IllegalArgumentException( "Global JSHINT or JSLINT is not a function" );
262+
}
263+
return (Function)object;
264+
}
265+
258266
private static String getPropertyAsString( ScriptableObject object,
259267
String name,
260268
String defaultValue )

0 commit comments

Comments
 (0)