Tslint: Whitespace before curly braces

Created on 19 Oct 2015  ·  18Comments  ·  Source: palantir/tslint

When check-branch is true, whitespace is required after an if / then, but not before a corresponding opening curly brace or after a closing curly brace.

As a result, we have an awkward scenario where check-branch only requires code to look like this:

function(str){
  if (str && str.length > 20){
    return str.substr(0, 20) + '...';
  }else {
    return str;
  }
}

In my experience, when teams want to enforce spaces after the branch, they also want to enforce spaces before the opening curly on line 2 and after the closing curly on line 4, like this:

function(str){
  if (str && str.length > 20) { // require space before opening curly
    return str.substr(0, 20) + '...';
  } else { // require space after closing curly
    return str;
  }
}

In addition, there is no way to require function(str){ to be formatted with whitespace as function (str) {.

Would pull requests adding this functionality be accepted? Any suggestions for naming those rule options?

Accepting PRs Enhancement

Most helpful comment

+1 also import statements. if trying to enforce:

import { MyThing } from './the.module';

over

import {MyThing} from './the.module';

or else you inevitably end up with

import { MyThing} from './the.module';

All 18 comments

+1 This is really needed. Somewhat related is #628.

+1. I like to enforce whitespace after function declarations

function foo () {
}

@adidahiya @JKillian I'd be interested in implementing this. What would your preference on syntax be? "whitespace": [true, "check-curly"]?

check-brace maybe?

Edit: I just noticed that this is what @adidahiya suggested here.

+1 also import statements. if trying to enforce:

import { MyThing } from './the.module';

over

import {MyThing} from './the.module';

or else you inevitably end up with

import { MyThing} from './the.module';

Hi guys,

Is this still no possible?

Regards.

Any word on this? it would be really good for my team since we are migrating from a pure JS ESLinted with airbnb style guide, and the new TS code does not automatically do this for us.

Of the 3 issues mentioned in the initial comment:

  1. Space before opening brace: Handled by one-line rule (check-whitespace) and whitespace rule (check-preblock)
  2. Space after closing brace: No available rules (although I've only looked up until TSLint 4.4.2)
  3. Space before function parenthesis: Handled by space-before-function-paren rule.

Whitespace rules around destructuring assignment and import / export statements, as @ollwenjones mentioned, is what I'm really after.

You can achieve this today for TypeScript with tslint-eslint-rules by adding an object-curly-spacing linting rule. Perhaps this is worth porting over to TSLint core rules?

Space after closing brace: Still not found the solution.

The one I wanna fix is the import {module} from "foo"; Anybody have a good solve for that?

@jcreamer898 the following rule will sort it out for you.

"whitespace": [true, "check-module"]

nice. thanks

@shesko as far as I can see that rule isn't working. Is it working for you? (just to check it's not just me!)

Sorry, this does work on 5.10.0, I just needed to upgrade

Closing this as it seems to be fixed using the whitespace rule. Please re-open or create a new issue if you disagree.

Someone knows how to allow space between function and bracket?
image

I have whitespace: [true], but it tells to removes the space.

@whitecolor if you could, please don't post unrelated questions on old issues. This one was closed in July. StackOverflow and Gitter are better channels for miscellaneous TSLint questions.

Your screenshot makes it look like you're using Prettier or a ruleset related to it, so you could start by checking your Prettier configuration.

Was this page helpful?
0 / 5 - 0 ratings