-
Notifications
You must be signed in to change notification settings - Fork 734
Fix formatter adding extra space at end of line without trailing newline #1933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cb516f8
Initial plan
Copilot a404358
Add failing test for formatter trailing space bug
Copilot 4bd8be2
Fix formatter adding extra space at end of line
Copilot 9659ae4
Apply formatting fixes
Copilot e2dde0e
Add more comprehensive tests for EOF formatting
Copilot ca11f1f
Move TestFormatNoTrailingNewline to dedicated test file
Copilot 1b03d7a
Revert unnecessary scanner.go changes
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.