Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 22, 2025

Problem

The formatter was incorrectly adding a space at the end of files that don't have a trailing newline. For example, formatting a file containing just 1; (with no trailing newline) would result in 1; (with a space added after the semicolon).

Root Cause

The EndOfFileToken was incorrectly included in the allTokens array, which caused the "SpaceAfterSemicolon" formatting rule to match between semicolon and EOF tokens. TypeScript explicitly excludes EOF from its allTokens array to prevent this.

Solution

internal/format/rules.go: Modified getAllRules() to exclude ast.KindEndOfFile when building the allTokens array, matching TypeScript's behavior.

internal/format/format_test.go: Added comprehensive test cases to verify the fix works for various code patterns including simple statements, function calls, variable declarations, and multiple statements without trailing newlines.

Testing

All tests pass, including:

  • New test cases specifically for files without trailing newlines
  • Existing format tests continue to pass
  • Language service integration tests pass
  • Code passes linter and formatter checks

The fix is minimal and focused - it only changes the token rules to exclude EOF from allTokens, preventing the "SpaceAfterSemicolon" rule from incorrectly applying between semicolon and EOF tokens.

Fixes #1926

Original prompt

This section details on the original issue you should resolve

<issue_title>Formatter adds extra space at end of line</issue_title>
<issue_description>Format a a file that has this content (no trailing new line):

1;

Bug
An extra space is added at the end of the line</issue_description>

<agent_instructions>Create and commit a failing test FIRST. Then compare against the original formatter in _submodules/TypeScript/src/services/formatting/formatting.ts.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@jakebailey An extra space, or an extra newline? @mjbvz Seems like an extra space in this case:
Trace - 11:40:37 AM] Sending notification 'textDocument/didChange'.
Params: {
    "textDocument": {
        "uri": "file:///Users/matb/projects/sandbox/x.ts",
        "version": 130
    },
    "contentChanges": [
        {
            "range": {
                "start": {
                    "line": 0,
                    "character": 2
                },
                "end": {
                    "line": 0,
                    "character": 2
                }
            },
            "rangeLength": 0,
            "text": " "
        }
    ]
}
```</body></comment_new>
</comments>

Fixes #1926


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits October 22, 2025 19:24
The issue was that EndOfFileToken was incorrectly included in the `allTokens` array, which caused the "SpaceAfterSemicolon" rule to match between semicolon and EOF tokens. This resulted in a space being inserted at the end of files without trailing newlines.

Also fixed GetECMAEndLinePosition to correctly return the index of the last character (length-1) for the last line, matching the TypeScript implementation.

Co-authored-by: jakebailey <[email protected]>
Co-authored-by: jakebailey <[email protected]>
Added test cases for various code patterns to ensure the formatter doesn't add trailing spaces before EOF.

Co-authored-by: jakebailey <[email protected]>
Copilot AI changed the title [WIP] Fix formatter issue causing extra space at end of line Fix formatter adding extra space at end of line without trailing newline Oct 22, 2025
Copilot AI requested a review from jakebailey October 22, 2025 19:50
Copilot finished work on behalf of jakebailey October 22, 2025 19:50
@iisaduan
Copy link
Member

This probably fixes the extra newlines that @andrewbranch found when working on autoimports formatting?

Comment on lines +12 to +14
if token != ast.KindEndOfFile {
allTokens = append(allTokens, token)
}
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.

Moved the test from api_test.go to a new format_test.go file for better organization. The api_test.go file should contain tests for the main API/integration tests, while specific formatting edge case tests belong in their own file.

Co-authored-by: jakebailey <[email protected]>
Copilot finished work on behalf of jakebailey October 22, 2025 20:10
Copilot AI requested a review from jakebailey October 22, 2025 20:10
The changes to GetECMAEndLinePosition were not needed to fix the issue. The fix in rules.go to exclude EOF from allTokens is sufficient on its own.

Co-authored-by: jakebailey <[email protected]>
Copilot finished work on behalf of jakebailey October 22, 2025 20:21
@jakebailey jakebailey marked this pull request as ready for review October 22, 2025 20:25
Copilot AI review requested due to automatic review settings October 22, 2025 20:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug where the formatter incorrectly added a trailing space to files without a trailing newline. The root cause was that EndOfFileToken was being included in the allTokens array, causing the "SpaceAfterSemicolon" formatting rule to trigger between semicolons and EOF.

Key changes:

  • Excluded ast.KindEndOfFile from the allTokens array in the rule builder
  • Added comprehensive test coverage for formatting files without trailing newlines

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
internal/format/rules.go Modified token array building to exclude EOF token, preventing incorrect spacing rules
internal/format/format_test.go Added new test file with test cases verifying no trailing spaces are added to files without trailing newlines

@jakebailey jakebailey added this pull request to the merge queue Oct 22, 2025
Merged via the queue into main with commit 5d349b6 Oct 22, 2025
42 checks passed
@jakebailey jakebailey deleted the copilot/fix-extra-space-issue branch October 22, 2025 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Formatter adds extra space at end of line

4 participants