Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Whitespace before curly braces #746

Closed
richardm opened this issue Oct 19, 2015 · 18 comments
Closed

Whitespace before curly braces #746

richardm opened this issue Oct 19, 2015 · 18 comments

Comments

@richardm
Copy link

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?

@glen-84
Copy link
Contributor

glen-84 commented Jan 19, 2016

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

@mrsimonemms
Copy link

+1. I like to enforce whitespace after function declarations

function foo () {
}

@danvk
Copy link
Contributor

danvk commented Aug 19, 2016

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

@glen-84
Copy link
Contributor

glen-84 commented Aug 19, 2016

check-brace maybe?

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

@ollwenjones
Copy link

+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';

@fmontes
Copy link

fmontes commented Apr 25, 2017

Hi guys,

Is this still no possible?

Regards.

@ospfranco
Copy link

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.

@glen-84
Copy link
Contributor

glen-84 commented Jun 25, 2017

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.

@byrondover
Copy link

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?

@yubaoquan
Copy link

Space after closing brace: Still not found the solution.

@jcreamer898
Copy link

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

@shesko
Copy link

shesko commented May 15, 2018

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

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

@jcreamer898
Copy link

nice. thanks

@MattShaile
Copy link

@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!)

@MattShaile
Copy link

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

@johnwiseheart
Copy link
Contributor

johnwiseheart commented Jul 28, 2018

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

@wclr
Copy link

wclr commented Nov 19, 2018

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

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

@JoshuaKGoldberg
Copy link
Contributor

@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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests