3

I have a for loop in JavaScript that I have run through JSLint a few times. In the past I received the unexpected++ error, I decided to refactor to make my code more readable. A month or so later JSLint came out with an update and is now showing the warning...

Unexpected expression 'i' in statement position. for (i; i < scope.formData.tabs.length; i = i + 1) {

//See JSLint.com for why I pulled out i initialization and i = i+1 instead of i++
//and http://stackoverflow.com/questions/3000276/the-unexpected-error-in-jslint
var i = 0;
for (i; i < scope.formData.tabs.length; i += 1) {
    scope.formData.tabs[i].show = false; // hide all the other tabs 

    if (scope.formData.tabs[i].title === title) {
        scope.formData.tabs[i].show = true; // show the new tab 
    }
}

Reverting to var i = 0 and i++ does not get improve the warnings, JSLint just stops processing.

JabberwockyDecompiler
  • 3,068
  • 2
  • 34
  • 51
  • 4
    There's no reason to have that `i` there in the first part of the for loop, if you do the declaration & initialization elsewhere then just leave that part blank and keep the semicolon- – SeinopSys May 15 '15 at 17:04
  • Removing that causing warnings directly in Visual Studio and shows as `Unexpected 'for'.` and `Expected ';' and instead saw ')'.` in JSLint. I am pretty sure the for needs all three components to work correctly. – JabberwockyDecompiler May 15 '15 at 17:09
  • Visual Studio is a terrible IDE in my persional opinion. `for(;i – SeinopSys May 15 '15 at 17:12
  • I did not have the `;` in front, but with that in, both old.jslint.com and jslint.com report the following when the `;` is in. `Expected ';' and instead saw 'i'. for (;i < scope.formData.tabs.length; i = i + 1) { line 18 character 14Expected ')' and instead saw ' – JabberwockyDecompiler May 15 '15 at 17:18
  • 1
    If you still don't believe me, see the [MDN docs for the `for` loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for#Optional_for_expressions) – SeinopSys May 15 '15 at 17:23
  • 1
    Where did you see that `var i=0; for (i; ...` is preferred over `var i; for (i=0; ...` in JSLint? I didn't necessarily see that in the [linked question](http://stackoverflow.com/questions/3000276/the-unexpected-error-in-jslint), nor at the JSLint instructions. – ruffin May 18 '15 at 18:05
  • @ruffin I made that comment in my code probably around a year ago, I am not sure where I saw that. I do not see it in the code for the other question or in the JSLint instructions either, but since they have upgraded their site it is possible that changed. It may have been an old message that JSLint gave related to [declaring multiple variables together](http://stackoverflow.com/questions/694102/declaring-multiple-variables-in-javascript), I know that I refactored due to that often. – JabberwockyDecompiler May 19 '15 at 14:09

2 Answers2

5

Looks like the follow-up problem isn't [just?] due to JSLint being in beta. It's because Crockford no longer allows for statements by default. Looks like I'm going to need to set aside a weekend to read the new instructions and source. Strange things are afoot at the Circle K, man.

The most important new feature of ES6 is proper tail calls. This has no new syntax, so JSLint doesn't see it. But it makes recursion much more attractive, which makes loops, particularly for loops, much less attractive.

Then this in the /*jslint */ directive section's main table:

Description: Tolerate for statement
Option: for
Meaning: true if the for statement should be allowed.

There's a little more explanation below the table:

JSLint does not recommend use of the for statement. Use array methods like forEach instead. The for option will suppress some warnings. The forms of for that JSLint accepts are restricted, excluding the new ES6 forms.

So to make this lint in the new JSLint, you need at least this code (with the for directive set):

/*jslint white:true, for:true */
/*global scope, title */

function test()
{
    "use strict";
    var i;

    for (i=0; i < scope.formData.tabs.length; i = i + 1) {
        scope.formData.tabs[i].show = false; // hide all the other tabs 

        if (scope.formData.tabs[i].title === title) {
            scope.formData.tabs[i].show = true; // show the new tab 
        }
    }
}

Note that I did still have to move i's initialization, so you might still have an issue worth reporting. I'll also admit I'm with Stephen at the question you link; I'm not sure why i+= 1 is better. But now it looks like a hard requirement. No plusplus option.

Notice also that if the code isn't wrapped in a function (I wrapped in test, above), you'll get Unexpected 'for' at top level., which is a new error.

Community
  • 1
  • 1
ruffin
  • 13,513
  • 8
  • 72
  • 118
  • These questions may be better for the jslint forum or reading the docs, but I thought I would check to see if you happen to know. I checked Assume ES6, Tolerate 'for statement' and 'messy white space'. Is there another check that I should do using the interface that will interact better with the 'unexpected expression' message or were you using custom /*jslint ignore*/ tags? – JabberwockyDecompiler May 19 '15 at 14:26
  • 1
    Just used the code above. I did try adding `es6:true` to the `jslint` directives comment, and it worked fine as well. – ruffin May 20 '15 at 02:50
2

EDIT: It seems as if I was mistaken. See ruffin's answer for a more detailed explanation.

It seems as if the problem is with jslint.com. Remember, it is still in beta. If you use the old version (old.jslint.com), the problem seems to go away.

Here's the output for old.jslint.com: enter image description here
It's mostly all about scope not being defined... etc.

Here's the output from jslint.com: enter image description here It's about scope not getting defined... etc AND

Unexpected 'for'.

along with

Unexpected expression 'i' in statement position.

For now, I suppose you should just use the old version of jslint.com.

Nathan
  • 934
  • 1
  • 11
  • 26