microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.44k stars 12.42k forks source link

Language services formatting for `else` on newline #41777

Open sheam opened 3 years ago

sheam commented 3 years ago

Search Terms

Suggestion

When the formatting option for the opening brace on newline for control blocks is true, it would make sense to have the else on it's online as well.

Use Cases

Using typescript-formatter with VS Code, and from command line.

Examples

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 "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');
}

Checklist

My suggestion meets these guidelines:

vsashidh commented 3 years ago

We are facing the same issue, as reported here and was directed to here by the visual studio code team: https://github.com/microsoft/vscode/issues/111829