diff --git a/internal/format/comment_test.go b/internal/format/comment_test.go index b7063bfe5d..50762ab56d 100644 --- a/internal/format/comment_test.go +++ b/internal/format/comment_test.go @@ -64,6 +64,43 @@ func TestCommentFormatting(t *testing.T) { assert.Check(t, !contains(secondFormatted, " sync x()"), "should not corrupt async to sync") assert.Check(t, contains(secondFormatted, "async"), "should preserve async keyword on second pass") }) + + t.Run("format JSDoc with tab indentation", func(t *testing.T) { + t.Parallel() + ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{ + EditorSettings: format.EditorSettings{ + TabSize: 4, + IndentSize: 4, + BaseIndentSize: 0, + NewLineCharacter: "\n", + ConvertTabsToSpaces: false, // Use tabs + IndentStyle: format.IndentStyleSmart, + TrimTrailingWhitespace: true, + }, + InsertSpaceBeforeTypeAnnotation: core.TSTrue, + }, "\n") + + // Original code with tab indentation (tabs represented as \t) + originalText := "class Foo {\n\t/**\n\t * @param {string} argument - This is a param description.\n\t */\n\texample(argument) {\nconsole.log(argument);\n\t}\n}" + + sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ + FileName: "/test.ts", + Path: "/test.ts", + }, originalText, core.ScriptKindTS) + + // Apply formatting + edits := format.FormatDocument(ctx, sourceFile) + formatted := applyBulkEdits(originalText, edits) + + // Check that tabs come before spaces (not spaces before tabs) + // The comment lines should have format: tab followed by space and asterisk + // NOT: space followed by tab and asterisk + assert.Check(t, !contains(formatted, " \t*"), "should not have space before tab before asterisk") + assert.Check(t, contains(formatted, "\t *"), "should have tab before space before asterisk") + + // Verify console.log is properly indented with tabs + assert.Check(t, contains(formatted, "\t\tconsole.log"), "console.log should be indented with two tabs") + }) } func contains(s, substr string) bool { diff --git a/internal/format/span.go b/internal/format/span.go index 4657949f7e..c8d0096bed 100644 --- a/internal/format/span.go +++ b/internal/format/span.go @@ -975,7 +975,7 @@ func getIndentationString(indentation int, options *FormatCodeSettings) string { spaces := indentation - (tabs * options.TabSize) res := strings.Repeat("\t", tabs) if spaces > 0 { - res = strings.Repeat(" ", spaces) + res + res = res + strings.Repeat(" ", spaces) } return res