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

Processing of Tabs

Expand Messages
  • Merlin
    JSLint does not handle tabs correctly. Mostly, this does not matter, but it does matter when checking indentation. Some text editors allow tab-stops to be
    Message 1 of 4 , May 28, 2008
    View Source
    • 0 Attachment
      JSLint does not handle tabs correctly.

      Mostly, this does not matter, but it does matter when checking indentation.

      Some text editors allow tab-stops to be placed at arbitrary positions.
      As it is virtually impossible to handle that, I restrict the following to cases
      where tab-stops are placed at regular intervals, by default at intervals of 4.

      If the user of a text editor intends to use indentation of 3 spaces,
      it is likely that she will set tab-stops at intervals of 3.

      To handle text containing tab-stops, JSLint must then be told that tabs occur at intervals
      of 3.

      Here is code for JSLint's lexical processing that handles tabs better.

      The constant 4 needs to be replaced by the required tab interval.
      That could be set to the indent value, but it would be more general to have a separate
      option setting.

      // Private lex methods

      function detab(data, tabspace) {
      var i, c, col = 0, out = "";
      for (i = 0; i < data.length; i += 1) {
      c = data[i];
      if (c === "\t") {
      do {
      out += " ";
      col += 1;
      } while ((i % tabspace) !== 0);
      } else {
      out += c;
      col += 1;
      }
      }
      return out;
      }

      function nextLine() {
      var at;
      line += 1;
      if (line >= lines.length) {
      return false;
      }
      character = 0;
      s = detab(lines[line], 4); // The 4 should be an option.
      at = s.search(cx);
      if (at >= 0) {
      warningAt("Unsafe character.", line, at);
      }
      return true;
      }
    • Jakob Kruse
      I think it would be simpler, and probably better, if jslint could enforce (check that is) and either-or policy: either indentation is performed using spaces
      Message 2 of 4 , May 28, 2008
      View Source
      • 0 Attachment
        I think it would be simpler, and probably better, if jslint could
        "enforce" (check that is) and either-or policy: either indentation is
        performed using spaces alone, or it is performed using tabs alone.

        In case of spaces we now already have an option to adjust the amount
        of spaces used per level. In the case of tabs, exactly one tab
        character per indent level should be the only acceptable content.

        Mixing tab and space characters for indentation is a bad, bad practice
        that will always lead to incorrectly indented code as people edit with
        different tab stop settings. If tabs and spaces are not mixed, there
        is really no reason for jslint to "detab" a file.

        /Jakob
      • Merlin
        It is a good idea when offering code to get it right! Here is a corrected version of my recent post. function detab(data, tabspace) { var i, c, col = 0, out =
        Message 3 of 4 , May 28, 2008
        View Source
        • 0 Attachment
          It is a good idea when offering code to get it right!

          Here is a corrected version of my recent post.

          function detab(data, tabspace) {
          var i, c, col = 0, out = "";
          for (i = 0; i < data.length; i += 1) {
          c = data[i];
          if (c === "\t") {
          do {
          out += " ";
          col += 1;
          } while ((col % tabspace) !== 0); // corrected!
          } else {
          out += c;
          col += 1;
          }
          }
          return out;
          }

          function nextLine() {
          var at;
          line += 1;
          if (line >= lines.length) {
          return false;
          }
          character = 0;
          s = detab(lines[line], 4); // was: s = lines[line].replace(/\t/g, ' ');
          at = s.search(cx);
          if (at >= 0) {
          warningAt("Unsafe character.", line, at);
          }
          return true;
          }
        • Merlin
          ... Tabs are not just bad , they are awful. They have been a source of trouble from the early days of computers. Unless you know where the tab-stops were
          Message 4 of 4 , May 28, 2008
          View Source
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "Jakob Kruse" <kruse@...> wrote:
            >
            > I think it would be simpler, and probably better, if jslint could
            > "enforce" (check that is) and either-or policy: either indentation is
            > performed using spaces alone, or it is performed using tabs alone.
            >
            > In case of spaces we now already have an option to adjust the amount
            > of spaces used per level. In the case of tabs, exactly one tab
            > character per indent level should be the only acceptable content.
            >
            > Mixing tab and space characters for indentation is a bad, bad practice
            > that will always lead to incorrectly indented code as people edit with
            > different tab stop settings. If tabs and spaces are not mixed, there
            > is really no reason for jslint to "detab" a file.
            >
            > /Jakob
            >

            Tabs are not just bad , they are awful. They have been a source of trouble from the early
            days of computers. Unless you know where the tab-stops were placed while the text was
            being created, you can't reconstruct the layout of a line of text. Word processors
            remember where the tab-stops were placed, plain text editors (in general) do not.

            However, most text editors allow the use of tabs, and it is not apparent whether a
            particular visible indentation was caused by tabs or spaces or some combination of the
            two.

            If my text looks correct in my favorite text editor, I want it to be accepted as such by
            other programs.

            One solution would be for JSLint to regard tabs as illegal and to insist on spaces only.
            In that case, I could detab my text before presenting it to JSLint. My favorite editor will do
            that for me - it knows where the tab-stops were placed.

            I greatly dislike the idea of having single tabs denote indentation. To use that reliably, I
            would have to have the editor make tabs visible. I really do not want that.

            The "standard" detab solution has been around for about 40 years. I much prefer it,
            although it does mean that you need to know the tab-stops or tab interval.

            In text to be presented to JSLint it would be simple enough to have a

            /*jslint tabstops: 4 */

            declaration comment.

            Less satisfactory would be to assume that the tab interval was the same as the indentation
            interval.
          Your message has been successfully submitted and would be delivered to recipients shortly.