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

combining Closure Compiler type casting with JSLint

Expand Messages
  • Jos van den Oever
    In WebODF, we ve been happily using JSLint for some time. It is a terrific tool for non-trivial JavaScript project. In addition to JSLint we are using Closure
    Message 1 of 3 , Mar 26 1:33 PM
    • 0 Attachment
      In WebODF, we've been happily using JSLint for some time. It is a
      terrific tool for non-trivial JavaScript project. In addition to JSLint
      we are using Closure Compiler, mainly for type checking.

      Closure Compiler can keep track of data types and allows casting between
      data types. Here is an example:

      var a = /**@type{!number}*/(b);

      Here, the variable a is assigned the value of b, cast to a non-null
      number (a !== null && a !== undefined).

      When setting JSLint loose on this fragment, the following error occurs:
      error: Unexpected '('.

      JSLint is not aware of the Closure Compiler idiom of specifying types. A
      smaller example that results in the same error is this:

      function x(b) {
      "use strict";
      var a = (b);
      }

      In Closure Compiler, the parentheses are obligatory; in JSLint (version
      2013-03-19) they are forbidden.

      Is there a way to make JSLint accept the unneeded parentheses?
      Is there an archive of older JSLint versions we could try to narrow down
      the problem?

      Best regards,
      Jos van den Oever
    • douglascrockford
      ... I may add option.closure. Is there any more weirdness?
      Message 2 of 3 , Mar 26 3:23 PM
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, Jos van den Oever <jos.van.den.oever@...> wrote:
        >
        > In WebODF, we've been happily using JSLint for some time. It is a
        > terrific tool for non-trivial JavaScript project. In addition to JSLint
        > we are using Closure Compiler, mainly for type checking.
        >
        > Closure Compiler can keep track of data types and allows casting between
        > data types. Here is an example:
        >
        > var a = /**@type{!number}*/(b);
        >
        > Here, the variable a is assigned the value of b, cast to a non-null
        > number (a !== null && a !== undefined).
        >
        > When setting JSLint loose on this fragment, the following error occurs:
        > error: Unexpected '('.
        >
        > JSLint is not aware of the Closure Compiler idiom of specifying types. A
        > smaller example that results in the same error is this:
        >
        > function x(b) {
        > "use strict";
        > var a = (b);
        > }
        >
        > In Closure Compiler, the parentheses are obligatory; in JSLint (version
        > 2013-03-19) they are forbidden.
        >
        > Is there a way to make JSLint accept the unneeded parentheses?

        I may add option.closure. Is there any more weirdness?
      • Jos van den Oever
        ... Writing var a = /**@type{!number}*/(b); also has effect on the white-space. JSLint expects the b to be much closer to the = . This is a check that can
        Message 3 of 3 , Mar 27 12:18 AM
        • 0 Attachment
          On 03/26/2013 11:23 PM, douglascrockford wrote:
          > --- In jslint_com@yahoogroups.com <mailto:jslint_com%40yahoogroups.com>,
          > Jos van den Oever <jos.van.den.oever@...> wrote:
          > >
          > > In WebODF, we've been happily using JSLint for some time. It is a
          > > terrific tool for non-trivial JavaScript project. In addition to JSLint
          > > we are using Closure Compiler, mainly for type checking.
          > >
          > > Closure Compiler can keep track of data types and allows casting between
          > > data types. Here is an example:
          > >
          > >
          > >
          > > Here, the variable a is assigned the value of b, cast to a non-null
          > > number (a !== null && a !== undefined).
          > >
          > > When setting JSLint loose on this fragment, the following error occurs:
          > > error: Unexpected '('.
          > >
          > > JSLint is not aware of the Closure Compiler idiom of specifying types. A
          > > smaller example that results in the same error is this:
          > >
          > > function x(b) {
          > > "use strict";
          > > var a = (b);
          > > }
          > >
          > > In Closure Compiler, the parentheses are obligatory; in JSLint (version
          > > 2013-03-19) they are forbidden.
          > >
          > > Is there a way to make JSLint accept the unneeded parentheses?
          >
          > I may add option.closure. Is there any more weirdness?

          Writing
          var a = /**@type{!number}*/(b);
          also has effect on the white-space. JSLint expects the 'b' to be much
          closer to the '='. This is a check that can be disabled.

          Type definitions in Closure Compiler are empty statements:

          /** @typedef {(string|number)} */
          goog.NumberLike;

          For simple types, one can get around this by adding an initializer or
          doing type definitions in a small file that is excluded from the JSLint
          step.

          Cheers,
          Jos

          https://developers.google.com/closure/compiler/docs/js-for-compiler
        Your message has been successfully submitted and would be delivered to recipients shortly.