Skip to content

Commit aaca0a4

Browse files
authored
Check the preceding rune before recognizing conflict markers (#65464)
1 parent 21ba970 commit aaca0a4

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎tsc/internal/scanner/scanner.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,8 +2471,8 @@ func isConflictMarkerTrivia(text string, pos int) bool {
24712471

24722472
// Conflict markers must be at the start of a line.
24732473
atLineStart := pos == 0 || stringutil.IsLineBreak(rune(text[pos-1]))
2474-
if !atLineStart && pos >= 2 {
2475-
prev, _ := utf8.DecodeLastRuneInString(text[:pos-2])
2474+
if !atLineStart && text[pos-1] > maxAsciiCharacter {
2475+
prev, _ := utf8.DecodeLastRuneInString(text[:pos])
24762476
atLineStart = stringutil.IsLineBreak(prev)
24772477
}
24782478
if atLineStart {

‎tsc/internal/scanner/scanner_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,45 @@ func TestScanSourceKeyword(t *testing.T) {
110110
assert.Equal(t, s.TokenValue(), "sourceValue")
111111
}
112112

113+
func TestConflictMarkerLineStart(t *testing.T) {
114+
t.Parallel()
115+
116+
tests := []struct {
117+
name string
118+
prefix string
119+
expected bool
120+
}{
121+
{name: "start of file", expected: true},
122+
{name: "line feed", prefix: "\n", expected: true},
123+
{name: "carriage return", prefix: "\r", expected: true},
124+
{name: "CRLF", prefix: "\r\n", expected: true},
125+
{name: "line separator", prefix: "\u2028", expected: true},
126+
{name: "paragraph separator", prefix: "\u2029", expected: true},
127+
{name: "text then line separator", prefix: "a\u2028", expected: true},
128+
{name: "one ASCII byte", prefix: "a"},
129+
{name: "two ASCII bytes", prefix: "ab"},
130+
{name: "ASCII after line feed", prefix: "\nab"},
131+
{name: "spaces after line feed", prefix: "\n "},
132+
{name: "two byte rune after line feed", prefix: "\né"},
133+
{name: "three byte rune after line feed", prefix: "\n€"},
134+
{name: "four byte rune after line feed", prefix: "\n🦀"},
135+
{name: "ASCII after line separator", prefix: "\u2028ab"},
136+
{name: "ASCII after paragraph separator", prefix: "\u2029ab"},
137+
{name: "invalid UTF-8 byte", prefix: "\xff"},
138+
{name: "invalid UTF-8 after line feed", prefix: "\n\xff\xfe"},
139+
}
140+
141+
for _, test := range tests {
142+
t.Run(test.name, func(t *testing.T) {
143+
t.Parallel()
144+
for _, marker := range []string{"<<<<<<< x", "======= x", ">>>>>>> x", "||||||| x"} {
145+
text := test.prefix + marker
146+
assert.Equal(t, isConflictMarkerTrivia(text, len(test.prefix)), test.expected, "%q", text)
147+
}
148+
})
149+
}
150+
}
151+
113152
func TestLookupKeywordMatchesKeywordMap(t *testing.T) {
114153
t.Parallel()
115154
for text, kind := range textToKeyword {

0 commit comments

Comments
 (0)