user3116402
user3116402

Reputation: 363

How to make JSLint ignore pieces of code?

My php file has mainly several javascript functions which I wanted to debug. The file starts with:

/*ignore jslint start*/
<? Header("content-type: application/x-javascript");
include('../country files/' . $_GET['country'] . '.php');
?>
/*ignore jslint end*/
  ...
  javascript code

Though, JSLint gives error on line 1!

Upvotes: 1

Views: 1000

Answers (1)

jokeyrhyme
jokeyrhyme

Reputation: 3638

You can change JSLint options for a few lines, with comments.

For example, JSLint will complain about you using Underscore.JS or Lo-Dash, because it isn't good practice to have underscores at the ends of variables names. But I can happily use Underscore's each() function by temporarily preventing nomen warnings:

/*jslint nomen:true*/ // suppress dangling underscore warnings
_.each( ... ); // example use of Underscore.JS
/*jslint nomen:false*/ // re-enable dangling underscore warnings

However, there isn't a way to use comments to temporarily disable JSLint completely.

It is good practice to move your JavaScript out into separate files. Ideally, you shouldn't be running JSLint on HTML or PHP files, only on pure JavaScript files.

If you need to dynamically generate JavaScript code with PHP, then run JSlint on a sample of the output, of you like. Generally, you minimise the cases where this is necessary.

You could have PHP generate a small amount of JavaScript to establish operational context (e.g. a few global constants), and leave the rest of your front end functionality to separate pure JavaScript files.

Upvotes: 3

Related Questions