typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

Change in formatting for anonymous generators

Open mjbvz opened this issue 2 months ago • 6 comments

Format the code:

const a = {
	b: function* () { }
}

function* bar() { } 

Expected

Using stable TS, the formatting keeps the space after the * in b: function* () {}

Actual

Current tsgo removes the space:

const a = {
	b: function*() { }
}

function* bar() { } 

This makes it harder to use a mix of stable and tsgo in a codebase

mjbvz avatar Oct 20 '25 20:10 mjbvz

Does tsserver add the space, or does it simply not remove the space?

jakebailey avatar Oct 20 '25 20:10 jakebailey

It adds the space in my testing

mjbvz avatar Oct 20 '25 20:10 mjbvz

This is because VS Code itself appears to override our defaults: https://github.com/microsoft/vscode/blob/986516b4bf9f6418ce7e5a551ba7edc02bc3183a/extensions/typescript-language-features/package.json#L1070 (and I suspect the same will be true for things you had to also override when using our APIs https://github.com/microsoft/vscode/blob/986516b4bf9f6418ce7e5a551ba7edc02bc3183a/tsfmt.json#L9)

And we don't yet handle user preferences set by the editor.

jakebailey avatar Oct 20 '25 21:10 jakebailey

Good catch. I can change this in the VS Code codebase but changing defaults for everyone is going to be a pain as these have been around for a long time (I don't remember why we override them)

Do all of these formatting settings default to false on your side?

mjbvz avatar Oct 22 '25 17:10 mjbvz

They aren't all false; in Strada the defaults are:

export function getDefaultFormatCodeSettings(newLineCharacter?: string): FormatCodeSettings {
    return {
        indentSize: 4,
        tabSize: 4,
        newLineCharacter: newLineCharacter || "\n",
        convertTabsToSpaces: true,
        indentStyle: IndentStyle.Smart,
        insertSpaceAfterConstructor: false,
        insertSpaceAfterCommaDelimiter: true,
        insertSpaceAfterSemicolonInForStatements: true,
        insertSpaceBeforeAndAfterBinaryOperators: true,
        insertSpaceAfterKeywordsInControlFlowStatements: true,
        insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
        insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
        insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
        insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
        insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
        insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
        insertSpaceBeforeFunctionParenthesis: false,
        placeOpenBraceOnNewLineForFunctions: false,
        placeOpenBraceOnNewLineForControlBlocks: false,
        semicolons: SemicolonPreference.Ignore,
        trimTrailingWhitespace: true,
        indentSwitchCase: true,
    };
}

Which matches the Go code:

func GetDefaultFormatCodeSettings(newLineCharacter string) *FormatCodeSettings {
	return &FormatCodeSettings{
		EditorSettings: EditorSettings{
			IndentSize:             4,
			TabSize:                4,
			NewLineCharacter:       newLineCharacter,
			ConvertTabsToSpaces:    true,
			IndentStyle:            IndentStyleSmart,
			TrimTrailingWhitespace: true,
		},
		InsertSpaceAfterConstructor:                                 core.TSFalse,
		InsertSpaceAfterCommaDelimiter:                              core.TSTrue,
		InsertSpaceAfterSemicolonInForStatements:                    core.TSTrue,
		InsertSpaceBeforeAndAfterBinaryOperators:                    core.TSTrue,
		InsertSpaceAfterKeywordsInControlFlowStatements:             core.TSTrue,
		InsertSpaceAfterFunctionKeywordForAnonymousFunctions:        core.TSFalse,
		InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis:  core.TSFalse,
		InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets:     core.TSFalse,
		InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces:       core.TSTrue,
		InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: core.TSFalse,
		InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces:  core.TSFalse,
		InsertSpaceBeforeFunctionParenthesis:                        core.TSFalse,
		PlaceOpenBraceOnNewLineForFunctions:                         core.TSFalse,
		PlaceOpenBraceOnNewLineForControlBlocks:                     core.TSFalse,
		Semicolons:                                                  SemicolonPreferenceIgnore,
		IndentSwitchCase:                                            core.TSTrue,
	}
}

I don't think we need to change the status quo here, we just need to start respecting the options we pull from the client.

jakebailey avatar Oct 22 '25 17:10 jakebailey

This is blocked on #1729.

Perhaps we should adopt VS Code's defaults as our own for now?

jakebailey avatar Oct 22 '25 19:10 jakebailey