Tslint: Support else / catch on next line

Created on 15 Aug 2013  ·  11Comments  ·  Source: palantir/tslint

It should be possible to enforce the position of trailing block headers like else/catch statements:

Currently this is expected:

if (myVar) {
    //...
} else {
    //...
}

Otherwise you get the 'misplaced else' warning.

But we want the trailing block bumped a line like this::

if (myVar) {
    //...
}
else {
    //...
}

Or for try/catch

try {
    //..
}
catch (e) {
    //..
}
finally {
    //..
}

An all other similar structures.

We find it easier to read as the keyword is tight on the left margin and the closing brace's position enforces a nearly blank line, just like in the starting block.

Formatting rule Enhancement

Most helpful comment

Let me clarify:

I'd like a rule which checks if braces are on the next line, and that it complains if they are on the same line.

As far as I could tell there is no way to enforce this.

All 11 comments

I can toggle it with the oneline rule.

I think it should be split to it's own rule: it is not the same thing as oneline's "opening brace to be on the same line as the expression preceding it".

sure, makes sense.

made all checks optional, fixed by 5cd203aa3c4b2de96c8a33d8107e9a8444aa4348

It would be great if next-line braces was a rule as well. Solving it by ignoring the "oneline" rule is not ideal.

Can you give us an example of what you mean, @eirikhm? If you want your braces to be on the next line, why do you want to one-line rule enabled at all?

Let me clarify:

I'd like a rule which checks if braces are on the next line, and that it complains if they are on the same line.

As far as I could tell there is no way to enforce this.

Is there any way to enforce the opposite of check-else? I want to enforce the following style:

if (expr) {
    // code
}
else {
    // code
}

and restrict the following style:

if (expr) {
    // code
} else {
    // code
}

EDIT - this can be accomplished with tslint-eslint-rules

Agreed, why not add the inverse to force blocks to their own line?

Is there any way to enforce the opposite of check-else? I want to enforce the following style:

if (expr) {
    // code
}
else {
    // code
}

and restrict the following style:

if (expr) {
    // code
} else {
    // code
}

This is a very common requirement - I've just found out it's not supported in tslint and I'm a bit surprised... I thought this was exactly the type of thing tslint was for?

@sam-s4s see #4534

Aww that's almost a bit sad :( lol

Was this page helpful?
0 / 5 - 0 ratings