Skip to content

Put else on newline #158

@sheam

Description

@sheam

When having opening curly braces on new lines, it is pretty normal to have the else on it's own line.

My tsfmt.json is:

{
    "baseIndentSize": 0,
    "indentSize": 4,
    "tabSize": 4,
    "indentStyle": 2,
    "newLineCharacter": "\n",
    "convertTabsToSpaces": true,
    "insertSpaceAfterCommaDelimiter": true,
    "insertSpaceAfterSemicolonInForStatements": true,
    "insertSpaceBeforeAndAfterBinaryOperators": true,
    "insertSpaceAfterConstructor": false,
    "insertSpaceAfterKeywordsInControlFlowStatements": true,
    "insertSpaceAfterFunctionKeywordForAnonymousFunctions": false,
    "insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": false,
    "insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
    "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": true,
    "insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
    "insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
    "insertSpaceAfterTypeAssertion": false,
    "insertSpaceBeforeFunctionParenthesis": false,
    "insertSpaceBeforeTypeAnnotation": false,
    "placeOpenBraceOnNewLineForFunctions": true,
    "placeOpenBraceOnNewLineForControlBlocks": true
}

I get results like this:

if(condition)
{
     console.log('condition true');
} else if(otherCondition)
{
     console.log('other condition is true');
} else 
{
     console.log('condition not true');
}

For people using "placeOpenBraceOnNewLineForFunctions": true and "placeOpenBraceOnNewLineForControlBlocks": true, the expected behaviour (IMHO) would be:

if(condition)
{
     console.log('condition true');
} 
else if(otherCondition)
{
     console.log('other condition is true');
} 
else 
{
     console.log('condition not true');
}

I am happy to do a PR (potentially for a new setting) if someone could point me in the right direction.

Cheers.

ps - opened issue with the TS project here: microsoft/TypeScript#41777

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions