Skip to content

Commit 2a41760

Browse files
authored
fix: use utf8.DecodeLastRuneInString to correctly handle multi-byte runes in setLastNonTriviaPosition (#3712)
1 parent 3b78168 commit 2a41760

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

internal/printer/changetrackerwriter.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package printer
22

33
import (
4+
"unicode/utf8"
5+
46
"github.com/microsoft/typescript-go/internal/ast"
57
"github.com/microsoft/typescript-go/internal/core"
68
"github.com/microsoft/typescript-go/internal/scanner"
@@ -89,10 +91,16 @@ func (ct *ChangeTrackerWriter) setLastNonTriviaPosition(s string, force bool) {
8991
if force || scanner.SkipTrivia(s, 0) != len(s) {
9092
ct.lastNonTriviaPosition = ct.textWriter.GetTextPos()
9193
// trim trailing whitespaces
92-
pos := len(s) - 1
93-
for ; pos >= 0 && stringutil.IsWhiteSpaceLike(rune(s[pos])); pos-- {
94+
pos := len(s)
95+
for pos > 0 {
96+
r, size := utf8.DecodeLastRuneInString(s[:pos])
97+
if stringutil.IsWhiteSpaceLike(r) {
98+
pos -= size
99+
} else {
100+
break
101+
}
94102
}
95-
ct.lastNonTriviaPosition -= len(s) - 1 - pos
103+
ct.lastNonTriviaPosition -= len(s) - pos
96104
}
97105
}
98106

0 commit comments

Comments
 (0)