4

I have just turned on 'JSLint' validation in Aptana Studio 3. In my web app, I have the following code:

Sessions.getVars = function()
{
    return $http.get(baseURL)
                .then(function(response) { return response.data; },
                      function(response) { /* TODO Error handling */ });    
};

This is throwing the following error Expected to see a statement but instead saw a block.

I have looked in this question, but that really only answers the question in relation to switch/case statements. Anyone able to help me understand why this error exists?

Community
  • 1
  • 1
Ben Wainwright
  • 4,224
  • 1
  • 18
  • 36
  • 1
    Funnily enough, a block is a statement. That error message is just silly. Ah, jslint. – Sacho May 05 '15 at 08:42
  • Consider posting your jslint options, because for that code, jslint generates a whole swathe of errors, *but not the one you mentioned*. – Sacho May 05 '15 at 08:44
  • Perhaps I should have asked before posted my answer: which line is the error being reported on? – Robert Rossmann May 05 '15 at 08:46

3 Answers3

2

This is likely because jslint enforces a convention of putting the opening curly brace of a function body on the same line as the function keyword, i.e.

function myFunc () { // Put it here
  // body...
}

It could also be a bug in that the parser fails to recognise the function's body starting on a new line.

PS. If JSLint suits your needs, then keep using it, but I feel obliged to introduce you to some alternatives:

jshint and eslint. jshint provides configurable rules for known pitfalls and enforces good coding habits, whereas eslint (in addition) provides rules to enforce a particular coding style (at the expense of seemingly overwhelming amount of configuration options).

Robert Rossmann
  • 11,931
  • 4
  • 42
  • 73
  • Is it possible to turn this off? That's not how I write a *lot* of my functions... – Ben Wainwright May 05 '15 at 08:03
  • Not sure about jslint (I hear it is not very configurable), but see my update about alternatives which allow configuration. – Robert Rossmann May 05 '15 at 08:06
  • 1
    It seems that isn't the problem. I moved the curly brace on the second line to the first line and the problem remains. In fact I just noticed that the squiggly for this error is on line 5, where the opening brace is definitely on the same line as the function statement... – Ben Wainwright May 05 '15 at 08:42
  • Re the other ones, I tried to install JShint first but it doesn't seem to do anything :/ – Ben Wainwright May 05 '15 at 08:44
  • @BenWainwright JSHint must be configured, this may take some time... http://jshint.com/docs/ – Christophe Roussy May 05 '15 at 09:47
  • JSLint isn't complaining about where you put the squigglies (if you check the box to tolerate whitespace or use the `white:true` directive). [Try it](http://old.jslint.com). Either way, the error reported here isn't a whitespace issue. It's because JSLint doesn't like empty blocks; see [my answer](http://stackoverflow.com/a/30053394/1028230). – ruffin May 05 '15 at 12:51
1

It's a much easier a problem than squiggly bracket placement. You have a particular type of block -- an empty block -- and JSLint doesn't like empty blocks. It wants statements.

Note that a function without a return value returns undefined anyway, so you can kludge this without changing function like this:

/*jslint sloppy:true, white:true */
/*global Sessions, $http, baseURL */
Sessions.getVars = function()
{
    return $http.get(baseURL)
                .then(function(response) { return response.data; },
                      function(response) { return undefined; });    
};

I think that's all you're seeing.

Note that JSLint is not necessarily quite as bad about where you put the squigglies as these other answers would have you believe! ;^) It's not a dealbreaker, anyhow, if you use JSLint directives.

I'm using two directives:

  • sloppy -- allows you to skip using "use strict";
  • white -- allows for any whitespace you want. Without this, you'd see the error I think other answers here are anticipating, but that error would be Expected exactly one space between ')' and '{'.

I'd say you can just run the snippet on JSLint.com to check, but it looks like Crockford is in the middle of his warned move to a new JSLint that's much more draconian than the old one. For now, I'd suggest testing snippets at old.jslint.com.

If you do, you'll see that, to make JSLint "fully happy", you'll need to remove response from that second function as well: function() { return "Something"; });. It doesn't like unused parameters either.

If you want to keep the TODO comment, you'll need to add the todo directive too.

Adding both of those changes gives us:

/*jslint sloppy:true, white:true, todo:true */
/*global Sessions, $http, baseURL */
Sessions.getVars = function()
{
    return $http.get(baseURL)
        .then(function(response) { return response.data; },
            function() {
                /* TODO Error handling; add `err` to parameters */ 
                return undefined; 
        });    
};
ruffin
  • 16,507
  • 9
  • 88
  • 138
  • Thanks! Modifying it to `return undefined` solved the problem! – Ben Wainwright May 08 '15 at 07:50
  • Great! Glad that worked. JSLint can be, um, particular, but it really can improve the quality of JavaScript code, especially when used in groups of coders. There's lots to disagree with, but I haven't found a situation where JSLint is indefensibly wrong. Good luck. – ruffin May 08 '15 at 12:46
0

Use the recommended JavaScript formatting style here other styles are mostly variations on this one. Most editors automatically format your JS code into this style or something that is really close.

The { (left curly brace) should be at the end of the line that begins the compound statement.

There are tons of nasty JavaScript bugs you can avoid by following the recommended formatting rules (example). It will also help other people to read this code.

JSLint is all about enforcing such guidelines. So bypassing them is often, but not always counterproductive this is why JSHint was invented.

Community
  • 1
  • 1
Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
  • 1
    Ok, thanks for the tip :) I don't think that is the problem that jslint is picking on though, see comment on above answer... – Ben Wainwright May 05 '15 at 08:43
  • This is not the **official** Javascript formatting style, because no such thing exists. – Sacho May 05 '15 at 08:47
  • I think Douglas Crockford is pretty much the 'official' one to recommend the formatting and most editors/IDEs use it by convention. You could call it 'recommended' instead of 'official'. – Christophe Roussy May 05 '15 at 08:53
  • @Sacho using recommended instead of official, official is indeed too strong. Many of those rules are found in guidelines because they avoid problems, it is not just about personal preferences. – Christophe Roussy May 05 '15 at 09:06