|
1 | 1 | package format_test |
2 | 2 |
|
3 | 3 | import ( |
4 | | -"testing" |
| 4 | + "testing" |
5 | 5 |
|
6 | | -"github.com/microsoft/typescript-go/internal/ast" |
7 | | -"github.com/microsoft/typescript-go/internal/core" |
8 | | -"github.com/microsoft/typescript-go/internal/format" |
9 | | -"github.com/microsoft/typescript-go/internal/parser" |
| 6 | + "github.com/microsoft/typescript-go/internal/ast" |
| 7 | + "github.com/microsoft/typescript-go/internal/core" |
| 8 | + "github.com/microsoft/typescript-go/internal/format" |
| 9 | + "github.com/microsoft/typescript-go/internal/parser" |
10 | 10 | ) |
11 | 11 |
|
12 | 12 | func TestTernaryWithTabs(t *testing.T) { |
13 | | -t.Parallel() |
| 13 | + t.Parallel() |
14 | 14 |
|
15 | | -// This is the original code with tabs for indentation |
16 | | -// Using literal tab characters |
17 | | -text := "const test = (a: string) => (\n\ta === '1' ? (\n\t\t10\n\t) : (\n\t\t12\n\t)\n)" |
| 15 | + // This is the original code with tabs for indentation |
| 16 | + // Using literal tab characters |
| 17 | + text := "const test = (a: string) => (\n\ta === '1' ? (\n\t\t10\n\t) : (\n\t\t12\n\t)\n)" |
18 | 18 |
|
19 | | -ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{ |
20 | | -EditorSettings: format.EditorSettings{ |
21 | | -TabSize: 4, |
22 | | -IndentSize: 4, |
23 | | -BaseIndentSize: 0, |
24 | | -NewLineCharacter: "\n", |
25 | | -ConvertTabsToSpaces: false, // Use tabs |
26 | | -IndentStyle: format.IndentStyleSmart, |
27 | | -TrimTrailingWhitespace: true, |
28 | | -}, |
29 | | -}, "\n") |
| 19 | + ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{ |
| 20 | + EditorSettings: format.EditorSettings{ |
| 21 | + TabSize: 4, |
| 22 | + IndentSize: 4, |
| 23 | + BaseIndentSize: 0, |
| 24 | + NewLineCharacter: "\n", |
| 25 | + ConvertTabsToSpaces: false, // Use tabs |
| 26 | + IndentStyle: format.IndentStyleSmart, |
| 27 | + TrimTrailingWhitespace: true, |
| 28 | + }, |
| 29 | + }, "\n") |
30 | 30 |
|
31 | | -sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ |
32 | | -FileName: "/test.ts", |
33 | | -Path: "/test.ts", |
34 | | -}, text, core.ScriptKindTS) |
| 31 | + sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ |
| 32 | + FileName: "/test.ts", |
| 33 | + Path: "/test.ts", |
| 34 | + }, text, core.ScriptKindTS) |
35 | 35 |
|
36 | | -edits := format.FormatDocument(ctx, sourceFile) |
37 | | -newText := applyBulkEdits(text, edits) |
| 36 | + edits := format.FormatDocument(ctx, sourceFile) |
| 37 | + newText := applyBulkEdits(text, edits) |
38 | 38 |
|
39 | | -// Check that we don't have mixed tabs and spaces |
40 | | -// The formatted text should only use tabs for indentation |
41 | | -lines := splitLines(newText) |
42 | | -for i, line := range lines { |
43 | | -if len(line) == 0 { |
44 | | -continue |
45 | | -} |
46 | | -// Check that the line doesn't have spaces for indentation followed by tabs |
47 | | -hasLeadingSpaces := false |
48 | | -for j := 0; j < len(line); j++ { |
49 | | -if line[j] == ' ' { |
50 | | -hasLeadingSpaces = true |
51 | | -} else if line[j] == '\t' { |
52 | | -// If we already saw spaces and now see a tab, that's a problem |
53 | | -if hasLeadingSpaces { |
54 | | -t.Errorf("Line %d has mixed tabs and spaces: %q", i, line) |
55 | | -} |
56 | | -} else { |
57 | | -// Hit non-whitespace |
58 | | -break |
59 | | -} |
60 | | -} |
61 | | -} |
| 39 | + // Check that we don't have mixed tabs and spaces |
| 40 | + // The formatted text should only use tabs for indentation |
| 41 | + lines := splitLines(newText) |
| 42 | + for i, line := range lines { |
| 43 | + if len(line) == 0 { |
| 44 | + continue |
| 45 | + } |
| 46 | + // Check that the line doesn't have spaces for indentation followed by tabs |
| 47 | + hasLeadingSpaces := false |
| 48 | + for j := range len(line) { |
| 49 | + if line[j] == ' ' { |
| 50 | + hasLeadingSpaces = true |
| 51 | + } else if line[j] == '\t' { |
| 52 | + // If we already saw spaces and now see a tab, that's a problem |
| 53 | + if hasLeadingSpaces { |
| 54 | + t.Errorf("Line %d has mixed tabs and spaces: %q", i, line) |
| 55 | + } |
| 56 | + } else { |
| 57 | + // Hit non-whitespace |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + } |
62 | 62 | } |
63 | 63 |
|
64 | 64 | func splitLines(text string) []string { |
65 | | -var lines []string |
66 | | -start := 0 |
67 | | -for i := 0; i < len(text); i++ { |
68 | | -if text[i] == '\n' { |
69 | | -lines = append(lines, text[start:i]) |
70 | | -start = i + 1 |
71 | | -} |
72 | | -} |
73 | | -if start < len(text) { |
74 | | -lines = append(lines, text[start:]) |
75 | | -} |
76 | | -return lines |
| 65 | + var lines []string |
| 66 | + start := 0 |
| 67 | + for i := range len(text) { |
| 68 | + if text[i] == '\n' { |
| 69 | + lines = append(lines, text[start:i]) |
| 70 | + start = i + 1 |
| 71 | + } |
| 72 | + } |
| 73 | + if start < len(text) { |
| 74 | + lines = append(lines, text[start:]) |
| 75 | + } |
| 76 | + return lines |
77 | 77 | } |
0 commit comments