Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSLint - ignore sections of code

Tags:

jslint

I have a huge script that passes JSLint (including avoidance of all bad parts). Except for one stretch, which is some very convoluted obfuscated code that is embedded within the larger context. JSLint generates quite a bit of complaints for this section, and I'd like to selectively disable it for that stretch of code. I already do use selective

  /*jlsint xxx:true/false*/

directives to disable particular warnings for certain tiny stretches of code that do things contrary to the struct interpretation.

The obfuscated code I have uses constructs that I don't know if they can be disabled.

For instance:

for(L=(117.>

causes this message:

Problem at line 1 character 57: A trailing decimal point can be confused with a dot '117.

Sure, I know that I could clean up the code, but it is emitted from an obfuscator, and I really don't want to have to clean obfuscated code!

So, is there a way to tell JSLint to completely ignore a stretch off code?

I am aware of this query JSLint: control comments (selective ignore) but it wasn't answered.

like image 759
Zhami Avatar asked Aug 23 '10 18:08

Zhami


1 Answers

You can add this yourself to JSLint if you want, though that's borderline Evil.

Here's one quick and dirty way with the current version:

The route I'm going to take is to hijack the token function's switch block for /* style comments. That's at line 1276 currently:

case '/*':
    for (;;) {
        i = source_row.search(lx);
...

Let's change that to look for comments that look like /*ignore:true */ on a line by themselves (although technically the true half can be anywhere on the line in this case, though the /*ignore:false */ line has to be on a line by itself, so let's pretend that holds for both).

Example bad, lint-failing code:

function spam()
{
    var sand = "sand";
/*ignore:true */
    var spud = "spud";
/*ignore:false */
    window.console.log(sand);
}

If we find /*ignore:true */, let's skip lines until we find one with /*ignore:false */ with /*ignore:... as the very first characters on the line. Until that false statement on a line by itself, we ignore everything.

case '/*':
    // Opening /* has already been sliced.
    if (source_row.startsWith("ignore:true"))    {
        do  {
            if (console.log) { console.log(source_row) };
        } while (next_line() && !source_row.trim().startsWith("/*ignore:false"));
    }   else    {
        // Put in the code that was originally there
    }
    break;
  • Pastee of JSLint hack here (12 Mar 2015).
  • Simplest case JSLint HTML wrapper here.

That's ugly, but seems to be working.

Now this can cause issues. For example, if you have a var declaration in a section you ignore and use it later, JSLint_Hacked will complain that myVar was used before it was defined. Example:

/*jslint white:true, sloppy:true, browser:true */
function spam()
{
    var sand = "spam";
/*ignore:true */
    var spud = "spud";
/*ignore:false */
    window.console.log(sand + spud);
}

So that kind of stuff could get nasty.

And I'd only use this in cases where you're inanely forced to lint everything, but for some reason you don't have the power to fix what's in every file, though you do have the ability to edit it, strangely, as in this case with obfuscated code. This whole ignore thing is waaay seedy.

I need to spend more time inside of JSLint to know how it really works, but the next_line() function seems to be non-destructive. That is, you could (and should) handle this in the do_jslint() function with "real" /*jslint ignore:true */ style directives, but then you have to handle side effects when you call the advance() function. The hack I'm using here was much easier, but is also much uglier.

like image 94
ruffin Avatar answered Sep 23 '22 00:09

ruffin