Skip to content

Commit 62fcda7

Browse files
Copilotjakebailey
andauthored
Fix JSDoc comment formatting with tab indentation (#1900)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 55cbead commit 62fcda7

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

internal/format/comment_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,43 @@ func TestCommentFormatting(t *testing.T) {
6464
assert.Check(t, !contains(secondFormatted, " sync x()"), "should not corrupt async to sync")
6565
assert.Check(t, contains(secondFormatted, "async"), "should preserve async keyword on second pass")
6666
})
67+
68+
t.Run("format JSDoc with tab indentation", func(t *testing.T) {
69+
t.Parallel()
70+
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
71+
EditorSettings: format.EditorSettings{
72+
TabSize: 4,
73+
IndentSize: 4,
74+
BaseIndentSize: 0,
75+
NewLineCharacter: "\n",
76+
ConvertTabsToSpaces: false, // Use tabs
77+
IndentStyle: format.IndentStyleSmart,
78+
TrimTrailingWhitespace: true,
79+
},
80+
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
81+
}, "\n")
82+
83+
// Original code with tab indentation (tabs represented as \t)
84+
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}"
85+
86+
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
87+
FileName: "/test.ts",
88+
Path: "/test.ts",
89+
}, originalText, core.ScriptKindTS)
90+
91+
// Apply formatting
92+
edits := format.FormatDocument(ctx, sourceFile)
93+
formatted := applyBulkEdits(originalText, edits)
94+
95+
// Check that tabs come before spaces (not spaces before tabs)
96+
// The comment lines should have format: tab followed by space and asterisk
97+
// NOT: space followed by tab and asterisk
98+
assert.Check(t, !contains(formatted, " \t*"), "should not have space before tab before asterisk")
99+
assert.Check(t, contains(formatted, "\t *"), "should have tab before space before asterisk")
100+
101+
// Verify console.log is properly indented with tabs
102+
assert.Check(t, contains(formatted, "\t\tconsole.log"), "console.log should be indented with two tabs")
103+
})
67104
}
68105

69106
func contains(s, substr string) bool {

internal/format/span.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ func getIndentationString(indentation int, options *FormatCodeSettings) string {
975975
spaces := indentation - (tabs * options.TabSize)
976976
res := strings.Repeat("\t", tabs)
977977
if spaces > 0 {
978-
res = strings.Repeat(" ", spaces) + res
978+
res = res + strings.Repeat(" ", spaces)
979979
}
980980

981981
return res

0 commit comments

Comments
 (0)