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
32 changes: 32 additions & 0 deletions tsc/internal/api/encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions tsc/internal/api/encoder/stringtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down