Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 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
17 changes: 14 additions & 3 deletions internal/format/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -1055,9 +1055,12 @@ func (w *formatSpanWorker) consumeTokenAndAdvanceScanner(currentTokenInfo tokenI
indentNextTokenOrTrivia := true
if len(currentTokenInfo.leadingTrivia) > 0 {
commentIndentation := dynamicIndenation.getIndentationForComment(currentTokenInfo.token.Kind, tokenIndentation, container)
indentNextTokenOrTrivia = w.indentTriviaItems(currentTokenInfo.leadingTrivia, commentIndentation, indentNextTokenOrTrivia, func(item TextRangeWithKind) {
w.insertIndentation(item.Loc.Pos(), commentIndentation, false)
})
// Only indent comments if we have a valid indentation value
if commentIndentation != -1 {
indentNextTokenOrTrivia = w.indentTriviaItems(currentTokenInfo.leadingTrivia, commentIndentation, indentNextTokenOrTrivia, func(item TextRangeWithKind) {
w.insertIndentation(item.Loc.Pos(), commentIndentation, false)
})
}
}

// indent token only if is it is in target range and does not overlap with any error ranges
Expand Down Expand Up @@ -1091,6 +1094,10 @@ func (i *dynamicIndenter) getIndentationForComment(kind ast.Kind, tokenIndentati
// // comment
// }
case ast.KindCloseBraceToken, ast.KindCloseBracketToken, ast.KindCloseParenToken:
// If indentation is -1 (Unknown), we should not add delta to it
if i.indentation == -1 {
return -1
}
return i.indentation + i.getDelta(container)
}
if tokenIndentation != -1 {
Expand All @@ -1113,6 +1120,10 @@ func (i *dynamicIndenter) getIndentationForComment(kind ast.Kind, tokenIndentati
//
// > yValue;
func (i *dynamicIndenter) getIndentationForToken(line int, kind ast.Kind, container *ast.Node, suppressDelta bool) int {
// If indentation is -1 (Unknown), we should not add delta to it
if i.indentation == -1 {
return -1
}
if !suppressDelta && i.shouldAddDelta(line, kind, container) {
return i.indentation + i.getDelta(container)
}
Expand Down
77 changes: 77 additions & 0 deletions internal/format/ternary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package format_test

import (
"testing"

"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/format"
"github.com/microsoft/typescript-go/internal/parser"
)

func TestTernaryWithTabs(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot just put this in internal/format/format_test.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in cbc5012. Moved the test to format_test.go.

t.Parallel()

// This is the original code with tabs for indentation
// Using literal tab characters
text := "const test = (a: string) => (\n\ta === '1' ? (\n\t\t10\n\t) : (\n\t\t12\n\t)\n)"

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,
},
}, "\n")

sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
FileName: "/test.ts",
Path: "/test.ts",
}, text, core.ScriptKindTS)

edits := format.FormatDocument(ctx, sourceFile)
newText := applyBulkEdits(text, edits)

// Check that we don't have mixed tabs and spaces
// The formatted text should only use tabs for indentation
lines := splitLines(newText)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Just assert a specific output string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in cbc5012. Changed to assert the specific expected output string.

for i, line := range lines {
if len(line) == 0 {
continue
}
// Check that the line doesn't have spaces for indentation followed by tabs
hasLeadingSpaces := false
for j := range len(line) {
if line[j] == ' ' {
hasLeadingSpaces = true
} else if line[j] == '\t' {
// If we already saw spaces and now see a tab, that's a problem
if hasLeadingSpaces {
t.Errorf("Line %d has mixed tabs and spaces: %q", i, line)
}
} else {
// Hit non-whitespace
break
}
}
}
}

func splitLines(text string) []string {
var lines []string
start := 0
for i := range len(text) {
if text[i] == '\n' {
lines = append(lines, text[start:i])
start = i + 1
}
}
if start < len(text) {
lines = append(lines, text[start:])
}
return lines
}