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
60 changes: 60 additions & 0 deletions internal/format/format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package format_test

import (
"strings"
"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"
"gotest.tools/v3/assert"
)

func TestFormatNoTrailingNewline(t *testing.T) {
t.Parallel()
// Issue: Formatter adds extra space at end of line
// When formatting a file that has content "1;" with no trailing newline,
// an extra space should NOT be added at the end of the line

testCases := []struct {
name string
text string
}{
{"simple statement without trailing newline", "1;"},
{"function call without trailing newline", "console.log('hello');"},
{"variable declaration without trailing newline", "const x = 1;"},
{"multiple statements without trailing newline", "const x = 1;\nconst y = 2;"},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
EditorSettings: format.EditorSettings{
TabSize: 4,
IndentSize: 4,
BaseIndentSize: 4,
NewLineCharacter: "\n",
ConvertTabsToSpaces: true,
IndentStyle: format.IndentStyleSmart,
TrimTrailingWhitespace: true,
},
}, "\n")
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
FileName: "/test.ts",
Path: "/test.ts",
}, tc.text, core.ScriptKindTS)
edits := format.FormatDocument(ctx, sourceFile)
newText := applyBulkEdits(tc.text, edits)

// The formatted text should not add extra space at the end
// It may add proper spacing within the code, but not after the last character
assert.Assert(t, !strings.HasSuffix(newText, " "), "Formatter should not add trailing space")
// Also check that no space was added at EOF position if text didn't end with newline
if !strings.HasSuffix(tc.text, "\n") {
assert.Assert(t, !strings.HasSuffix(newText, " "), "Formatter should not add space before EOF")
}
})
}
}
4 changes: 3 additions & 1 deletion internal/format/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
func getAllRules() []ruleSpec {
allTokens := make([]ast.Kind, 0, ast.KindLastToken-ast.KindFirstToken+1)
for token := ast.KindFirstToken; token <= ast.KindLastToken; token++ {
allTokens = append(allTokens, token)
if token != ast.KindEndOfFile {
allTokens = append(allTokens, token)
}
Comment on lines +12 to +14
Copy link
Member

Choose a reason for hiding this comment

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

This is correct, reading Strada.

}

anyTokenExcept := func(tokens ...ast.Kind) tokenRange {
Expand Down