| 1 | // Copyright 2026 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package astutil_test |
| 6 | |
| 7 | import ( |
| 8 | "go/ast" |
| 9 | "go/parser" |
| 10 | "go/token" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "golang.org/x/tools/internal/astutil" |
| 16 | "golang.org/x/tools/internal/testenv" |
| 17 | ) |
| 18 | |
| 19 | func TestPosInStringLiteral(t *testing.T) { |
| 20 | // Each string is Go source for a string literal with ^ marker at expected Pos. |
| 21 | tests := []string{ |
| 22 | `"^abc"`, |
| 23 | `"a^bc"`, |
| 24 | `"ab^c"`, |
| 25 | `"abc^"`, |
| 26 | `"a\n^b"`, |
| 27 | `"\n^"`, |
| 28 | `"a\000^"`, |
| 29 | `"\x61^"`, |
| 30 | `"\u0061^"`, |
| 31 | `"\U00000061^"`, |
| 32 | `"€^"`, |
| 33 | `"a€^b"`, |
| 34 | "`abc^`", |
| 35 | "`a\n^b`", |
| 36 | // normalization of \r carriage returns: |
| 37 | "`a\r\n^b`", |
| 38 | "`a\r\nb\r\nc\r\n^d`", |
| 39 | } |
| 40 | for _, test := range tests { |
| 41 | // The workaround for \r requires the go1.26 fix for https://go.dev/issue/76031. |
| 42 | if strings.Contains(test, "\r") && testenv.Go1Point() < 26 { |
| 43 | continue |
| 44 | } |
| 45 | |
| 46 | t.Logf("input: %#q", test) |
| 47 | |
| 48 | // Parse. |
| 49 | const prefix = "package p; const _ = " |
| 50 | src := prefix + test |
| 51 | fset := token.NewFileSet() |
| 52 | f, err := parser.ParseFile(fset, "p.go", src, 0) |
| 53 | if err != nil { |
| 54 | t.Errorf("Parse: %v", err) |
| 55 | continue |
| 56 | } |
| 57 | |
| 58 | // Find literal. |
| 59 | var lit *ast.BasicLit |
| 60 | ast.Inspect(f, func(n ast.Node) bool { |
| 61 | if b, ok := n.(*ast.BasicLit); ok { |
| 62 | lit = b |
| 63 | return false |
| 64 | } |
| 65 | return true |
| 66 | }) |
| 67 | if lit == nil { |
| 68 | t.Errorf("No literal") |
| 69 | continue |
| 70 | } |
| 71 | |
| 72 | // Find index of marker within logical value. |
| 73 | value, err := strconv.Unquote(lit.Value) |
| 74 | if err != nil { |
| 75 | t.Errorf("Unquote: %v", err) |
| 76 | continue |
| 77 | } |
| 78 | index := strings.Index(value, "^") |
| 79 | if index < 0 { |
| 80 | t.Errorf("Value %q contains no marker", value) |
| 81 | continue |
| 82 | } |
| 83 | |
| 84 | // Convert logical index to file position. |
| 85 | pos, err := astutil.PosInStringLiteral(lit, index) |
| 86 | if err != nil { |
| 87 | t.Errorf("PosInStringLiteral(%d): %v", index, err) |
| 88 | continue |
| 89 | } |
| 90 | |
| 91 | // Check that cut offset in original src file is before marker. |
| 92 | offset := fset.Position(pos).Offset |
| 93 | before, after := src[:offset], src[offset:] |
| 94 | t.Logf("\t%q :: %q", before, after) |
| 95 | if !strings.HasPrefix(after, "^") { |
| 96 | t.Errorf("no marker at cut point") |
| 97 | continue |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestOffsetInStringLiteral(t *testing.T) { |
| 103 | // Each string is Go source for a string literal with | marker at expected |
| 104 | // physical pos, and $ marker at the expected logical unquoted index. |
| 105 | tests := []string{ |
| 106 | `"$|abc"`, |
| 107 | `"a$|bc"`, |
| 108 | `"ab$|c"`, |
| 109 | `"abc$|"`, |
| 110 | `"a\n$|b"`, |
| 111 | `"\n$|"`, |
| 112 | `"a\000$|"`, |
| 113 | `"\x61$|"`, |
| 114 | `"\u0061$|"`, |
| 115 | `"\U00000061$|"`, |
| 116 | `"$\U00|000061"`, |
| 117 | `"€$|"`, |
| 118 | `"a€$|b"`, |
| 119 | "`abc$|`", |
| 120 | "`a\n$|b`", |
| 121 | // normalization of \r carriage returns: |
| 122 | "`a\r\n$|b`", |
| 123 | "`a\r\nb\r\nc\r\n$|d`", |
| 124 | } |
| 125 | for _, test := range tests { |
| 126 | // The workaround for \r requires the go1.26 fix for https://go.dev/issue/76031. |
| 127 | if strings.Contains(test, "\r") && testenv.Go1Point() < 26 { |
| 128 | continue |
| 129 | } |
| 130 | |
| 131 | t.Logf("input: %#q", test) |
| 132 | |
| 133 | const prefix = "package p; const _ = " |
| 134 | src := prefix + test |
| 135 | |
| 136 | // Remove the physical marker to produce valid Go source. |
| 137 | offset := strings.Index(src, "|") |
| 138 | if offset < 0 { |
| 139 | t.Errorf("Source %q contains no marker", src) |
| 140 | continue |
| 141 | } |
| 142 | src = src[:offset] + src[offset+1:] |
| 143 | |
| 144 | // Parse. |
| 145 | fset := token.NewFileSet() |
| 146 | f, err := parser.ParseFile(fset, "p.go", src, 0) |
| 147 | if err != nil { |
| 148 | t.Errorf("Parse: %v", err) |
| 149 | continue |
| 150 | } |
| 151 | |
| 152 | // Find literal. |
| 153 | var lit *ast.BasicLit |
| 154 | ast.Inspect(f, func(n ast.Node) bool { |
| 155 | if b, ok := n.(*ast.BasicLit); ok { |
| 156 | lit = b |
| 157 | return false |
| 158 | } |
| 159 | return true |
| 160 | }) |
| 161 | if lit == nil { |
| 162 | t.Errorf("No literal") |
| 163 | continue |
| 164 | } |
| 165 | |
| 166 | tfile := fset.File(f.Pos()) |
| 167 | pos := tfile.Pos(offset) |
| 168 | |
| 169 | // Convert file position to logical index. |
| 170 | index, err := astutil.OffsetInStringLiteral(lit, pos) |
| 171 | if err != nil { |
| 172 | t.Errorf("OffsetInStringLiteral(%d): %v", pos, err) |
| 173 | continue |
| 174 | } |
| 175 | |
| 176 | value, err := strconv.Unquote(lit.Value) |
| 177 | if err != nil { |
| 178 | t.Errorf("Unquote: %v", err) |
| 179 | continue |
| 180 | } |
| 181 | |
| 182 | if index < 0 || index > len(value) { |
| 183 | t.Errorf("Returned index %d is out of bounds for logical string of length %d", index, len(value)) |
| 184 | continue |
| 185 | } |
| 186 | |
| 187 | // Check that cut offset in value is before marker. |
| 188 | before, after := value[:index], value[index:] |
| 189 | t.Logf("\t%q :: %q", before, after) |
| 190 | if !strings.HasSuffix(before, "$") { |
| 191 | t.Errorf("no marker at logical cut point, got before=%q", before) |
| 192 | continue |
| 193 | } |
| 194 | } |
| 195 | } |