1

EDIT: Syntastic is configured to use JSLint

EDIT2: I am using JavascriptLint, not JSLint

On the below code syntastic warns function inside does not always return a value

function(){
 switch(age){
 case 1:
  return 'won';
 case 2:
  return 'too';
 case 3:
  return 'tree';
 default:
  throw 'wow man, you are old!';
  break; //technically redundant
 }
}

I realize default does not return a value, but the throw ends the execution. Is this a Syntastic error, or should I change my coding style? Should I put a return statement after throw?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
puk
  • 16,318
  • 29
  • 119
  • 199
  • Syntastic just uses GCC to syntax check. If you compile with GCC using `-Wall` do you get the same warning? – Andrew Marshall Oct 30 '11 at 23:48
  • I built my syntastic to work with jslint (described [here](http://stackoverflow.com/questions/7926356/how-do-i-get-a-syntax-check-to-work-in-with-vim)). – puk Oct 30 '11 at 23:51
  • If I use `jsl -process` I get the same problems. Isn't `gcc` for C/C++? – puk Oct 30 '11 at 23:56
  • You're right, GCC is totally unrelated to your question. – Leonid Shevtsov Oct 30 '11 at 23:57
  • Whoops, I'm sorry about that. Point is since JSlint is giving you that warning, and that's what syntastic is using, it can't help it. It may be a bug in JSlint, though. – Andrew Marshall Oct 31 '11 at 00:00

1 Answers1

2

Syntactic uses JSLint under the hood to check Javascript syntax.

JSLint is known to be overcautious in order to avoid errors. You can either ignore the message to your better judgement, or turn off this particular warning, or add a redundant return there.

Personally I prefer to satisfy JSLint's requirements over turning off warnings. Just make sure to add a //Satisfying JSlint comment near the return so other people will understand the redundancy.

Leonid Shevtsov
  • 14,024
  • 9
  • 51
  • 82
  • Thanks. I have also noticed that JSLint is over-cautious . For example it complains when `++` is used as a part of a bigger statement like so `var x=0;flags={a:x++ , b:x++ , c:x++...}` – puk Oct 31 '11 at 00:04
  • Is there a way to turn off the warning in JSLint, just for that piece of code? I've seen many static analysis tools that have such a feature... – Merlyn Morgan-Graham Oct 31 '11 at 00:07
  • 1
    Actually, found my own answer: Yes it does - http://stackoverflow.com/questions/599859/jslint-control-comments-selective-ignore – Merlyn Morgan-Graham Oct 31 '11 at 00:07
  • 1
    Yes, because the global purpose of JSLint is to make you use only the 'good parts' of Javascript, by Crockford's definition. It's not just a syntax checker, but an *opinionated syntax* checker. – Leonid Shevtsov Oct 31 '11 at 00:09
  • @MerlynMorgan-Graham thanks for the link. I also realized I meant Javascript Lint, not JSLint (but your link has information on both) – puk Oct 31 '11 at 00:47