Tslint: indent rule enhancements

Created on 19 Aug 2015  ·  37Comments  ·  Source: palantir/tslint

Similar to ESLint, we should be able to enforce 2-space or 4-space indentation via options like so:

  • [x] [true, "spaces", 2]
  • [x] [true, "spaces", 4]

In addition, it'd be nice to have a "detect" option (as suggested in #122) which tries to guess the indentation style in a file and enforce consistency:

  • [ ] [true, "detect"]
P1 Fixed Enhancement

Most helpful comment

@dimitriy-k #2723 just merged, it'll be in the next release.

All 37 comments

+1

+1 for enforcing number of spaces.

+1

Would this only work if the indentation is divisible by the number of spaces configured?

{
    theObject.something()
             .more(); // 13 spaces.
}

The above (align the periods) method wouldn't be supported?

@glen-84 I see two possibilities:

  • A line by line check of indentation (where your example wouldn't be allowed
  • A statement by statement check of indentation. Since in your example, theObject.something().more() is all one statement, it would only check the indentation level at the beginning of it.

I think they both have pros and cons, but the second one is probably preferred here.

:+1:

+1
@JKillian -> I would vote for option #2 where statements are checked, not every line.

+1 option 2, checking statement-by-statement

@JKillian Thanks. The second option looks good. =)

Edit: I changed my mind.

+1 option 1

A good way is the ESLINT one http://eslint.org/docs/rules/indent

+9000 SSJ3
Please, guys, implement 4-spaces indent option ASAP. I started programming in typescript a lot and 2-spaces default indent feels so bad for me. For some people it's really hard to see indent level of some code with only 2 spaces...

@radarsu you might want to try use .editorconfig for the time being.

@radarsu Right now the indentation rule just figures out if you have an indentation error based upon how your code aligns with the previous statement. You're able to use four space indentation and make the rule happy (in fact, that's what the TSLint source code does)

@JKillian any idea how hard this would be to tackle? i would love to start enforcing the indent size. I'm open to hacking away at it if it's not too much of a time sink (only have so much time over the next week).

Hard to say how difficult it would be - I expect it wouldn't be simple at the least haha. Definitely go for it though!

@mcriss @strike-sbolel Please stop adding +1 comments.

Looks like similar efforts are being made here https://github.com/buzinas/tslint-eslint-rules/issues/74

I've changed my mind, as option 2 would allow code like:

{
    theObject.something()
             .more() // 13 spaces.
           .wrong()
                    .moreWrong();
}

In an ideal world, we'd use option 1 with a setting to allow (or enforce) "wrapped" lines to be positioned below the last period in the previous line.

{
    theObject.something()
             .more()  // 13 spaces.
             .right() // Must be the same as above now, because the previous line used this indentation mode.
             .moreRight();
}

Or:

{
    theObject.something()
        .more() // (4 x 2) spaces.
        .right()
        .moreRight();
}

Can we release an update please?

@owais the enhancements haven't been implemented in core TSLint yet. That link is to an external repo with custom rules.

Oh. I didn't realize I was looking at another repo. :) Thanks for the link. I think I might use that.

@adidahiya Is this still something that is outstanding, and is it worth me opening a PR? Or would it be better to use the custom rules you've linked to?

@pudgeball definitely would be happy to accept a PR (as indicated by the label)

note that #2723 doesn't fix this issue. It fixes tabs -> x spaces and x spaces -> tabs, but doesn't fix x spaces -> y spaces

still no updates on indent size?... request is from 2015

@dimitriy-k #2723 just merged, it'll be in the next release.

What will this rule be called? 😄

@dolanmiu it's not a new rule. It's implemented exactly as noted in the original issue description. Just click through to the PR I linked.

@adidahiya I am using the current latest version of tslint 5.3.2

And using the following:

    "indent": [
      true,
      "spaces",
      4
    ],

I have a file which is 2 all 2 spaces, but it did not detect it! Am I doing something wrong, or is there a bug?

@dolanmiu yeah that looks like a new issue, I also noticed #2723 did not work like I expected. I filed https://github.com/palantir/tslint/issues/2814

any updates?

"indent": [true, "spaces", 4] not working for me

@liul85 try install tslint-eslint-rules and use the terIndentRule

@leocaseiro thanks for sharing, I will give a try.

Still not working in tslint 5.11.0

"indent": [true, "spaces", 4] still not working...

Was this page helpful?
0 / 5 - 0 ratings