Danny Beckett
Danny Beckett

Reputation: 20748

How to disable the Weird Condition check in JSLint?

I have some code that maps keyboard keys to blocks of code of varying length, from something as simple as return false, to mainly 2-3 line snippets, to complex functions:

var k = window.event.keycode;

switch(true){
    case(k === 9):
       //
    break;

    case(k === 13):
       //
    break;

    case(k === 38 && selectedRecord > 1):
       //
    break;
}

I'd rather not create an object with a list of functions and map it that way, because they're mainly short blocks of code and there is the odd complex case.

Is there an option to disable the check for switch(true) (as there are for other checks)?

Related, but not a duplicate: Is a reversed switch statement acceptable JavaScript?

Upvotes: 0

Views: 1095

Answers (1)

elixenide
elixenide

Reputation: 44831

Two thoughts:

1) You can make the linter happy by adding this comment at the top of the file: /*jshint eqeqeq: true */ That said, I still get a This 'switch' should be an 'if'. warning. It may be that jslint/jshint just won't ignore this kind of construct.

2) Instead of turning of linter options, I would respectfully suggest that you take this opportunity to clean up rather strange code and rewrite your switch as follows:

var k = window.event.keycode;

if(k === 9) {
       //
} else if(k === 13) {
       //
} else if(k === 38 && selectedRecord > 1) {
       //
}

Upvotes: 3

Related Questions