Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 17, 2025

Problem

When formatting TypeScript/JavaScript files with tab indentation (ConvertTabsToSpaces: false), the formatter incorrectly placed spaces before tabs in JSDoc comment lines, resulting in malformed indentation.

For example, formatting this code:

class Foo {
	/**
	 * @param {string} argument - This is a param description.
	 */
	example(argument) {
		console.log(argument);
	}
}

Would produce incorrect output with spaces before tabs:

class Foo {
	/**
 	* @param {string} argument - This is a param description.
 	*/
	example(argument) {
		console.log(argument);
	}
}

Notice how the comment lines now have the pattern space tab * instead of the correct tab space *.

Root Cause

The bug was in the getIndentationString function in internal/format/span.go. When generating indentation with tabs, the function was concatenating spaces before tabs instead of tabs before spaces:

// WRONG:
res = strings.Repeat(" ", spaces) + res  // spaces + tabs

This was opposite to the TypeScript reference implementation in src/services/formatting/formatting.ts:

return spaces ? tabString + repeatString(" ", spaces) : tabString;  // tabs + spaces

Solution

Fixed the concatenation order to match the TypeScript implementation:

// CORRECT:
res = res + strings.Repeat(" ", spaces)  // tabs + spaces

This ensures that tabs always come before spaces in indentation strings, which is the standard convention and matches editor expectations.

Testing

Added a comprehensive test case format JSDoc with tab indentation that validates:

  • Comment lines have the pattern \t * (tab, space, asterisk)
  • The incorrect pattern \t* (space, tab, asterisk) is not present
  • Other code indentation remains correct

All existing tests continue to pass, confirming no regressions.

Fixes the issue described in the problem statement where JSDoc comments with tab indentation were being incorrectly formatted.

Original prompt

This section details on the original issue you should resolve

<issue_title>Incorrect formatting of JSDoc comment with tab indentation</issue_title>
<issue_description>For a file that uses tab indentation, try running format:

class Foo {
	/**
	 * @param {string} argument - This is a param description.
	 */
	example(argument) {
console.log(argument);
	}
}

Bug
This incorrectly moves the spaces to the start of the doc comment lines so you end up with space tab *.

class Foo {
	/**
 	* @param {string} argument - This is a param description.
 	*/
	example(argument) {
		console.log(argument);
	}
}
```</issue_description>

<agent_instructions>Make a formatting test first, and make sure you look at the original formatting code in the submodule.</agent_instructions>

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

<comments>
</comments>

Fixes #1899


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI self-assigned this Oct 17, 2025
@Copilot Copilot AI changed the title [WIP] Fix incorrect formatting of JSDoc comment with tab indentation Fix JSDoc comment formatting with tab indentation Oct 17, 2025
@Copilot Copilot AI requested a review from jakebailey October 17, 2025 21:05
Copilot finished work on behalf of jakebailey October 17, 2025 21:05
@jakebailey jakebailey marked this pull request as ready for review October 17, 2025 21:57
@Copilot Copilot AI review requested due to automatic review settings October 17, 2025 21:57
Copy link
Contributor

@Copilot 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 in the TypeScript formatter where JSDoc comments with tab indentation were incorrectly formatted, placing spaces before tabs instead of tabs before spaces.

Key changes:

  • Fixed concatenation order in getIndentationString function to place tabs before spaces
  • Added comprehensive test case to validate JSDoc formatting with tab indentation
  • Ensured alignment with TypeScript reference implementation behavior

Reviewed Changes

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

File Description
internal/format/span.go Fixed concatenation order in getIndentationString to place tabs before spaces
internal/format/comment_test.go Added test case to verify JSDoc formatting with tab indentation works correctly

@jakebailey jakebailey enabled auto-merge October 18, 2025 08:43
@jakebailey jakebailey added this pull request to the merge queue Oct 21, 2025
Merged via the queue into main with commit 62fcda7 Oct 21, 2025
22 checks passed
@jakebailey jakebailey deleted the copilot/fix-jsdoc-tab-indentation branch October 21, 2025 15:48
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.

Incorrect formatting of JSDoc comment with tab indentation

4 participants