-
Notifications
You must be signed in to change notification settings - Fork 757
Fix multiline ternary expression formatting with tabs #2203
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
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-multiline-ternary-formatting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+43
−3
Draft
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
58384d7
Initial plan
Copilot b3f97f7
Add failing test for ternary expression tab formatting issue
Copilot 417b52e
Fix multiline ternary expression tab formatting issue
Copilot 835673e
Remove accidental patch files
Copilot a38bd7a
Run format and fix lint issues
Copilot cbc5012
Move test to format_test.go and assert specific output
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
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,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) { | ||
| 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) | ||
|
||
| 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 | ||
| } | ||
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.
@copilot just put this in
internal/format/format_test.goThere 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.
Done in cbc5012. Moved the test to format_test.go.