Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tsc/internal/compiler/emit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// generateLongLineTS generates TypeScript source code that produces a single very long line.
// This simulates generated code (e.g., from code generators) that has no line breaks,
// which triggers O(n²) behavior in source map generation due to
// GetECMALineAndUTF16CharacterOfPosition scanning from line start for each position.
// the emit writer scanning from line start for each generated position.
func generateLongLineTS(numProperties int) string {
// Build a large object literal all on one line, with no line breaks.
var b strings.Builder
Expand Down
13 changes: 10 additions & 3 deletions tsc/internal/printer/textwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type textWriter struct {
lineStart bool
lineCount int
linePos int
columnPos int
column core.UTF16Offset
hasTrailingCommentState bool
}

Expand All @@ -41,9 +43,10 @@ func (w *textWriter) GetColumn() core.UTF16Offset {
if w.lineStart {
return core.UTF16Offset(w.indent * w.indentSize)
}
// Count UTF-16 code units from the last line start.
// For ASCII-only output (the common case), this equals the byte count.
return core.UTF16Len(w.builder.String()[w.linePos:])
textPos := w.builder.Len()
w.column += core.UTF16Len(w.builder.String()[w.columnPos:textPos])
w.columnPos = textPos
return w.column
}

func (w *textWriter) GetIndent() int {
Expand Down Expand Up @@ -107,6 +110,8 @@ func (w *textWriter) updateLineCountAndPosFor(s string) {
w.lineCount += count - 1
curLen := w.builder.Len()
w.linePos = curLen - len(s) + int(lastLineStart)
w.columnPos = w.linePos
w.column = 0
w.lineStart = (w.linePos - curLen) == 0
return
}
Expand Down Expand Up @@ -163,6 +168,8 @@ func (w *textWriter) writeLineRaw() {
w.lastWritten = w.newLine
w.lineCount++
w.linePos = w.builder.Len()
w.columnPos = w.linePos
w.column = 0
w.lineStart = true
w.hasTrailingCommentState = false
}
Expand Down
38 changes: 38 additions & 0 deletions tsc/internal/printer/textwriter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package printer

import (
"testing"

"github.com/microsoft/TypeScript/tsc/internal/core"
"gotest.tools/v3/assert"
)

func TestTextWriterCachesUTF16Column(t *testing.T) {
t.Parallel()

w := NewTextWriter("\n", 4).(*textWriter)
w.Write("a𝟘")
assert.Equal(t, w.GetColumn(), core.UTF16Offset(3))
assert.Equal(t, w.columnPos, w.builder.Len())

w.Write("𝟙b")
assert.Equal(t, w.GetColumn(), core.UTF16Offset(6))
assert.Equal(t, w.columnPos, w.builder.Len())
}

func TestTextWriterResetsCachedColumnAfterNewLine(t *testing.T) {
t.Parallel()

w := NewTextWriter("\n", 4).(*textWriter)
w.Write("previous line")
w.GetColumn()
w.RawWrite("\n𝟘")
assert.Equal(t, w.GetColumn(), core.UTF16Offset(2))
assert.Equal(t, w.columnPos, w.builder.Len())

w.WriteLine()
w.IncreaseIndent()
assert.Equal(t, w.GetColumn(), core.UTF16Offset(4))
w.Write("x")
assert.Equal(t, w.GetColumn(), core.UTF16Offset(5))
}