Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions internal/format/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/format/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down