jslint icon indicating copy to clipboard operation
jslint copied to clipboard

error on for...of statement

Open n-a-m-e opened this issue 9 years ago • 4 comments

Firstly thanks for writing jslint, secondly I found this new for-of loop that javascript has https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of but js lint throws the error

Expected ';' and instead saw 'of'.
(function(){
    'use strict';
    let arr = [3, 5, 7];
    let i;
    arr.foo = "hello";

    for (i of arr) {
        console.log(i); // logs "3", "5", "7", "hello"
    }
}());

I could always use forEach instead but then I can't break out of the loop

(function () {
    'use strict';
    let arr = [3, 5, 7];
    arr.foo = "hello";
    arr.forEach(function (element, index) {
        console.log(element); // logs "3", "5", "7"
        console.log(index);   // logs "0", "1", "2"
    });
}());

what is your opinion, is the for of loop useful or should I use something else instead. Thanks

n-a-m-e avatar Feb 01 '16 04:02 n-a-m-e

The for of statement is a new ES6 feature that has not yet been proven to be a good part.

Try arr.every.

Recursion is becoming a better approach than looping.

douglascrockford avatar Feb 01 '16 04:02 douglascrockford

Could this perhaps be reconsidered? It's been 8 more years, and at least to me, for...of loops did turn out to be a good part. If maintainers agree, I might implement this during the next few weeks.

I'm new to JSLint, and really I like it: it is a self-contained, dependency-free tool that just works without npm hell. The one thing I don't like is that jslint cannot parse my loops.

Akuli avatar Oct 06 '24 01:10 Akuli

I think that is a good idea. We could also relax the restrictions on 'let' in scopes. Some of the JSLint rules were to get us thru some standard transitions, and I think those are way behind us now.

douglascrockford avatar Oct 06 '24 01:10 douglascrockford

  • took a stab at this feature early on as maintainer, but felt too unfamiliar with codebase at time
  • open to external pull-requests
  • good starting-point in codebase would be here @ https://github.com/jslint-org/jslint/blob/v2024.6.28/jslint.mjs#L6341
    function stmt_for() {
        let first;
        let the_for = token_now;
        if (!option_dict.for) {

// test_cause:
// ["for", "stmt_for", "unexpected_a", "for", 1]

            warn("unexpected_a", the_for);
        }
        check_not_top_level(the_for);
        functionage.loop += 1;
        advance("(");
        token_now.free = true;
        if (token_nxt.id === ";") {
...

kaizhu256 avatar Oct 07 '24 15:10 kaizhu256