Loading ...
Sorry, an error occurred while loading the content.

Wrapping assignment in extra parentheses no longer working.

Expand Messages
  • thomasrutter
    The documentation states, If you really intend an assignment, wrap it in another set of parens: if ((a = b)) { ... } This used to work until recently, but now
    Message 1 of 6 , Jan 30, 2011
    • 0 Attachment
      The documentation states,

      If you really intend an assignment, wrap it in another set of parens:

      if ((a = b)) {
      ...
      }

      This used to work until recently, but now for me, JSLint is giving me this error:

      Problem at line 32 character 25: Expected a conditional expression and instead saw an assignment.

      while ((element = element.previousSibling)) {

      As you can see from my code, the assignment is wrapped in extra parentheses, which in my understanding (and the documentation) SHOULD be acceptable to JSLint. Indeed, this used to work in JSLint until recently.

      Cheers,
      Thomas
    • Douglas Crockford
      ... You are right. Thanks. That has been corrected. ... I found that there were cases where people would take the erroneous code and wrap parens around it to
      Message 2 of 6 , Jan 30, 2011
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "thomasrutter" <trr@...> wrote:
        >
        > The documentation states,
        >
        > If you really intend an assignment, wrap it in another set of parens:
        >
        > if ((a = b)) {
        > ...
        > }

        You are right. Thanks. That has been corrected.

        > This used to work until recently, but now for me, JSLint is giving me this error:
        >
        > Problem at line 32 character 25: Expected a conditional expression and instead saw an assignment.
        >
        > while ((element = element.previousSibling)) {
        >
        > As you can see from my code, the assignment is wrapped in extra parentheses, which in my understanding (and the documentation) SHOULD be acceptable to JSLint. Indeed, this used to work in JSLint until recently.


        I found that there were cases where people would take the erroneous code and wrap parens around it to avoid the warning.

        The assumptions behind JSLint call for subsetting the language so that the correct constructs are more easily distinguished from errors.
      • thomasrutter
        Unfortunately I m in the situation of having a large amount of code using the old jslint custom - a common pattern being while ((a = a.next)) { This code can
        Message 3 of 6 , Jan 30, 2011
        • 0 Attachment
          Unfortunately I'm in the situation of having a large amount of code using the old jslint custom - a common pattern being

          while ((a = a.next)) {

          This code can be rewritten eventually, though in the meantime, there are a lot of jslint errors coming up for code that I'm confident is working and well-tested for now.

          A jslint flag allowing it to use the old behaviour would be helpful. Otherwise, I guess there is
          - trying to use an earlier jslint
          - ignoring the jslint errors for a while

          As for rewriting this code, any equivalent that avoids repeating oneself? e.g.

          a = a.next;
          while (a) {
          //...
          a = a.next;
          }

          I'm wondering if I can reach out to other people on this list for suggestions.

          Thanks
        • luke.a.page
          for (a = a.next; a;) { //... a = a.next; } but generally where there is code repetition (a = a.next) I tend to find a bigger refactor is needed - why isn t the
          Message 4 of 6 , Jan 31, 2011
          • 0 Attachment
            for (a = a.next; a;) {
            //...
            a = a.next;
            }

            but generally where there is code repetition (a = a.next) I tend to find a
            bigger refactor is needed - why isn't the first a in the while loop? You are
            skipping the first element, so make the reason for that obvious by the
            structure of the code, instead of trying to disguise it by only executing
            the statement once. If you are skipping the head, can you make it more
            obvious, e.g.

            var head = a; tail = a.next;

            //process head

            while(tail) {
            // process tail
            tail = tail.next;
            }

            and then, rather than code repetition you have something that looks like it
            is - a statement to get the tail and a while loop to process everything in
            the tail.

            On 31 January 2011 02:53, thomasrutter <trr@...> wrote:

            >
            >
            > Unfortunately I'm in the situation of having a large amount of code using
            > the old jslint custom - a common pattern being
            >
            > while ((a = a.next)) {
            >
            > This code can be rewritten eventually, though in the meantime, there are a
            > lot of jslint errors coming up for code that I'm confident is working and
            > well-tested for now.
            >
            > A jslint flag allowing it to use the old behaviour would be helpful.
            > Otherwise, I guess there is
            > - trying to use an earlier jslint
            > - ignoring the jslint errors for a while
            >
            > As for rewriting this code, any equivalent that avoids repeating oneself?
            > e.g.
            >
            > a = a.next;
            > while (a) {
            > //...
            > a = a.next;
            > }
            >
            > I'm wondering if I can reach out to other people on this list for
            > suggestions.
            >
            > Thanks
            >
            >
            >


            [Non-text portions of this message have been removed]
          • Morgaut Alexandre Louis Marc
            ... Which can also be written like this: for (a = a.next; a; a = a.next) { //... } This notation show even more explicitly the iteration statement The second
            Message 5 of 6 , Jan 31, 2011
            • 0 Attachment
              > for (a = a.next; a;) {
              > //...
              > a = a.next;
              > }

              Which can also be written like this:

              for (a = a.next; a; a = a.next) {
              //...
              }

              This notation show even more explicitly the iteration statement

              The second example proposed by luke with head and tail is important,
              it gives the code a better semantic

              for the same objective, we can also write

              var
              current;

              for (current = a; current !== undefined; current = current.next) {
              // ...
              }


              PS:

              It could have been even better if JavaScript supported
              "current .= next;"
              as
              "current = current . next;"
              and then
              "current = current.next;"

              But it may be confusing for people coming from PHP ;-)
            • Chris
              ... JSLint does not complain about the following code, which features assignment inside a conditional: /*global document */ var p; if ((p =
              Message 6 of 6 , Feb 25, 2011
              • 0 Attachment
                --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
                >
                >
                > I found that there were cases where people would take the erroneous code and wrap parens around it to avoid the warning.
                >
                > The assumptions behind JSLint call for subsetting the language so that the correct constructs are more easily distinguished from errors.
                >

                JSLint does not complain about the following code, which features assignment inside a conditional:

                /*global document */

                var p;

                if ((p = document.body.lastChild).nodeName.toLowerCase() !== 'p') {
                p = document.body.appendChild(document.createElement('p'));
                }


                Should JSLint issue a warning for this?

                Thanks!
                - Chris
              Your message has been successfully submitted and would be delivered to recipients shortly.