diff --git a/tsc/internal/api/encoder/encoder_test.go b/tsc/internal/api/encoder/encoder_test.go index 2652a44212051..415c2e77c94ea 100644 --- a/tsc/internal/api/encoder/encoder_test.go +++ b/tsc/internal/api/encoder/encoder_test.go @@ -37,6 +37,38 @@ func TestEncodeSourceFile(t *testing.T) { }) } +func TestEncodeSourceFileWithNormalizedNumericLiterals(t *testing.T) { + t.Parallel() + for _, tt := range []struct { + source string + text string + }{ + {".1", "0.1"}, + {"1e3", "1000"}, + {"1e9", "1000000000"}, + {"1e2", "100"}, + {"1", "1"}, + {"0", "0"}, + {" /* leading trivia */ .1", "0.1"}, + {" /* leading trivia */ 1e9", "1000000000"}, + {"1_000", "1000"}, + {"0x10", "16"}, + } { + t.Run(tt.source, func(t *testing.T) { + t.Parallel() + sourceFile := parseSourceFile(tt.source) + buf, _, err := encoder.EncodeSourceFile(sourceFile) + assert.NilError(t, err) + + decoded, err := encoder.DecodeSourceFile(buf) + assert.NilError(t, err) + assert.Equal(t, decoded.Text(), tt.source) + literal := decoded.Statements.Nodes[0].AsExpressionStatement().Expression.AsNumericLiteral() + assert.Equal(t, literal.Text, tt.text) + }) + } +} + func TestEncodeContentMapperSourceFileMetadata(t *testing.T) { t.Parallel() if encoder.ProtocolVersion != 9 { diff --git a/tsc/internal/api/encoder/stringtable.go b/tsc/internal/api/encoder/stringtable.go index 46cd674f32444..6f76673af787a 100644 --- a/tsc/internal/api/encoder/stringtable.go +++ b/tsc/internal/api/encoder/stringtable.go @@ -38,8 +38,7 @@ 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 { + if start >= 0 && t.fileText[start:end] == text { t.offsets = append(t.offsets, uint32(start), uint32(end)) return index }