From 266e29b93e2859109d72868b14147beec8292159 Mon Sep 17 00:00:00 2001 From: Josue Barros Date: Mon, 16 Nov 2026 16:04:48 -0300 Subject: [PATCH] Guard the string table reuse bounds for normalized literals A numeric literal such as .1 or 1e3 normalizes to a longer text than its source spelling. recordExtendedData_NumericLiteral passes that normalized text with the original end position, and stringTable.add computed a negative start before slicing fileText. Check the start bound so the existing fallback to otherStrings is used. Adds encoder coverage for .1, 1e3, 1e9, 1e2, 1 and 0. --- tsc/internal/api/encoder/encoder_test.go | 12 ++++++++++++ tsc/internal/api/encoder/stringtable.go | 10 ++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/tsc/internal/api/encoder/encoder_test.go b/tsc/internal/api/encoder/encoder_test.go index 2652a44212051..4f8b72a037953 100644 --- a/tsc/internal/api/encoder/encoder_test.go +++ b/tsc/internal/api/encoder/encoder_test.go @@ -215,3 +215,15 @@ func formatEncodedSourceFile(encoded []byte) string { } return result.String() } + +func TestEncodeSourceFileWithNormalizedNumericLiterals(t *testing.T) { + t.Parallel() + for _, text := range []string{".1", "1e3", "1e9", "1e2", "1", "0"} { + sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{ + FileName: "/test.ts", + PathKey: "/test.ts", + }, text, core.ScriptKindTS) + _, _, err := encoder.EncodeSourceFile(sourceFile) + assert.NilError(t, err, "text %q", text) + } +} diff --git a/tsc/internal/api/encoder/stringtable.go b/tsc/internal/api/encoder/stringtable.go index 46cd674f32444..e0e047862f502 100644 --- a/tsc/internal/api/encoder/stringtable.go +++ b/tsc/internal/api/encoder/stringtable.go @@ -38,10 +38,12 @@ func (t *stringTable) add(text string, kind ast.Kind, pos int, end int) uint32 { } end = end - endOffset start := end - length - fileSlice := t.fileText[start:end] - if fileSlice == text { - t.offsets = append(t.offsets, uint32(start), uint32(end)) - return index + if start >= 0 { + fileSlice := t.fileText[start:end] + if fileSlice == text { + t.offsets = append(t.offsets, uint32(start), uint32(end)) + return index + } } } // no exact match, so we need to add it to the string table