JSLint showing Undeclared 'context'. (undeclared_a) for js file in resources

Former Community Member
Not applicable

I am using JSLint to check the quality of my javascript files.

For js code starting like this:

var clientAuthHeader = context.proxyRequest.headers['X-AR-Authorization']; context.targetRequest.headers.Authorization = "Bearer " + clientAuthHeader;

I am seeing Undeclared 'context'. (undeclared_a).

Is there a way to allow this in JSLint to do static code analysis of my code or if i declare context will it effect how APIGEE runs the js code?

@Siddharth Barahalikar

0 3 785
3 REPLIES 3

yes

There are some predefined global variables available for JS callouts. Context is one of them. httpClient is another. To make jslint happy you can tell it about those globals, with a specially formatted comment near the top of the file. Like this:

/* global context, httpClient, properties */

var clientAuthHeader = context.proxyRequest.headers['X-AR-Authorization']; 
context.targetRequest.headers.Authorization = "Bearer " + clientAuthHeader;

You may alternatively be able to use a jslintrc file. What that means is, the "globals" declaration could go into a separate file, and you wouldn't need to use that special comment.

I myself use eslint or jshint, not jslint. And I generally use .jshintrc files.

LMGTFY.

Former Community Member
Not applicable

This is useful Dino.

Is there any advantage using eslint vs jshint vs jslint?

Jslint was a great idea, but it was pretty opinionated, not very flexible. For example it was difficult to turn off some of the warnings. Not everybody wanted that.

JSHint evolved as a more flexible jslint, and I adopted JSHint for that reason.

ESLint is yet even more flexible.

I am not an expert on the comparison, but here's a good article.