Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
66 changes: 66 additions & 0 deletions internal/format/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,72 @@ func TestCommentFormatting(t *testing.T) {
// Verify console.log is properly indented with tabs
assert.Check(t, contains(formatted, "\t\tconsole.log"), "console.log should be indented with two tabs")
})

t.Run("format comment inside multi-line argument list", 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 proper indentation
originalText := "console.log(\n\t\"a\",\n\t// the second arg\n\t\"b\"\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)

// The comment should remain indented with a tab
assert.Check(t, contains(formatted, "\t// the second arg"), "comment should be indented with tab")
// The comment should not lose its indentation
assert.Check(t, !contains(formatted, "\n// the second arg"), "comment should not lose indentation")
})

t.Run("format comment in chained method calls", 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 proper indentation
originalText := "foo\n\t.bar()\n\t// A second call\n\t.baz();"

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)

// The comment should remain indented
assert.Check(t, contains(formatted, "\t// A second call") || contains(formatted, " // A second call"), "comment should be indented")
// The comment should not lose its indentation
assert.Check(t, !contains(formatted, "\n// A second call"), "comment should not lose indentation")
})
}

func contains(s, substr string) bool {
Expand Down
3 changes: 3 additions & 0 deletions internal/format/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,9 @@ func (i *dynamicIndenter) getIndentationForComment(kind ast.Kind, tokenIndentati
case ast.KindCloseBraceToken, ast.KindCloseBracketToken, ast.KindCloseParenToken:
return i.indentation + i.getDelta(container)
}
if tokenIndentation != -1 {
return tokenIndentation
}
return i.indentation
}

Expand Down