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

Re: Indentation quirk?

Expand Messages
  • douglascrockford
    JSLint accepts two conventions for block-like structures, open and compact. Your code conforms to neither. Try /*global g */ /*jslint indent: 2 */ (function ()
    Message 1 of 4 , Jan 22, 2012
    • 0 Attachment
      JSLint accepts two conventions for block-like structures, open and compact. Your code conforms to neither. Try

      /*global g */
      /*jslint indent: 2 */
      (function () {
      ~~"use strict";
      ~~return function () {
      ~~~~return g
      ~~~~~~.method({
      ~~~~~~~~a: 1,
      ~~~~~~~~b: {
      ~~~~~~~~~~c: function () {}
      ~~~~~~~~}
      ~~~~~~});
      ~~};
      }());
    • caerusx
      Thank you for a quick answer! It s a honor to get an answer directly from JSLint author. So JSLint does not allow to mix open and compact forms. If I get right
      Message 2 of 4 , Jan 22, 2012
      • 0 Attachment
        Thank you for a quick answer! It's a honor to get an answer directly
        from JSLint author.

        So JSLint does not allow to mix open and compact forms. If I get right
        I used compact form for outer block and open for inner block?

        I think it's better to make a warning not to mix styles, rather then
        suggest wrong indentation.

        On other hand I don't see anything wrong in mixing styles. What if
        after adding 'b' key I realize that line won't fit in 79 characters in
        compact form? Naturally I rewrite contents of 'b' key from compact to
        open form (actually mixed styles appeared in that way, while code
        evolved). I don't see a reason to rewrite outer blocks to open form
        too, as code is perfectly readable already.

        Here is my original code with leading spaces substituted by
        tilde. (Thanks for idea, how to show indents in variable width font,
        by the way).

        /*global g */
        /*jslint indent: 2 */
        (function () {
        ~~"use strict";
        ~~return function () {
        ~~~~return g
        ~~~~~~.method({ a: 1,
        ~~~~~~~~~~~~~~~~b: {
        ~~~~~~~~~~~~~~~~~~c: function () {}
        ~~~~~~~~~~~~~~~~}
        ~~~~~~~~~~~~~~});
        ~~};
        }());

        --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
        >
        > JSLint accepts two conventions for block-like structures, open and compact. Your code conforms to neither. Try
        >
        > /*global g */
        > /*jslint indent: 2 */
        > (function () {
        > ~~"use strict";
        > ~~return function () {
        > ~~~~return g
        > ~~~~~~.method({
        > ~~~~~~~~a: 1,
        > ~~~~~~~~b: {
        > ~~~~~~~~~~c: function () {}
        > ~~~~~~~~}
        > ~~~~~~});
        > ~~};
        > }());
        >
      • caerusx
        Good morning! I was thinking about mixing open and compact forms. And I think disallowing to use open inside compact and vice verse brings inconveniences. It s
        Message 3 of 4 , Jan 23, 2012
        • 0 Attachment
          Good morning!

          I was thinking about mixing open and compact forms. And I think
          disallowing to use open inside compact and vice verse brings
          inconveniences.

          It's fine for lint to use function parameters in compact form:

          func(1, 2, 3,
          ~~~~~4, 5, 6, function () {});

          But when one parameter listed in compact form internally contains open
          form, lint fails to validate:

          func(1, 2, 3,
          ~~~~~4, 5, 6, function () {
          ~~~~~});

          To pass validation, it's needed to rewrite outer block to open
          form. Do you agree that code below, which is correct from lint point
          of view, looks worse for a human?

          func(
          ~~1,
          ~~2,
          ~~3,
          ~~4,
          ~~5,
          ~~6,
          ~~function () {
          ~~}
          );
        Your message has been successfully submitted and would be delivered to recipients shortly.