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
16 changes: 12 additions & 4 deletions internal/printer/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,15 +910,23 @@ func newLineCharacterCache(source sourcemap.Source) *lineCharacterCache {
// offset from the start of that line for the given byte position.
func (c *lineCharacterCache) getLineAndCharacter(pos int) (line int, character core.UTF16Offset) {
line = scanner.ComputeLineOfPosition(c.lineMap, pos)
if c.hasCached && line == c.cachedLine && pos >= c.cachedPos {
lineStart := int(c.lineMap[line])
// When pos is beyond the source text (e.g., for error-recovery tokens like
// missing closing braces), we can't slice past the text end. Compute the
// UTF-16 length up to EOF and add the remaining byte offset arithmetically,
// matching TypeScript's computeLineAndCharacterOfPosition which uses
// arithmetic (position - lineStarts[lineNumber]) and handles this implicitly.
endPos := min(pos, len(c.text))
if c.hasCached && line == c.cachedLine && endPos >= c.cachedPos {
// Incremental: only count UTF-16 code units from the last cached position.
character = c.cachedChar + core.UTF16Len(c.text[c.cachedPos:pos])
character = c.cachedChar + core.UTF16Len(c.text[c.cachedPos:endPos])
} else {
// Full computation from line start.
character = core.UTF16Len(c.text[c.lineMap[line]:pos])
character = core.UTF16Len(c.text[lineStart:endPos])
}
character += core.UTF16Offset(pos - endPos)
c.cachedLine = line
c.cachedPos = pos
c.cachedPos = endPos
c.cachedChar = character
c.hasCached = true
return line, character
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sourceMapUnclosedBlock.ts(1,2): error TS1005: '}' expected.


==== sourceMapUnclosedBlock.ts (1 errors) ====
{

!!! error TS1005: '}' expected.
!!! related TS1007 sourceMapUnclosedBlock.ts:1:1: The parser expected to find a '}' to match the '{' token here.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
===================================================================
JsFile: sourceMapUnclosedBlock.js
mapUrl: sourceMapUnclosedBlock.js.map
sourceRoot:
sources: sourceMapUnclosedBlock.ts
===================================================================
-------------------------------------------------------------------
emittedFile:sourceMapUnclosedBlock.js
sourceFile:sourceMapUnclosedBlock.ts
-------------------------------------------------------------------
>>>"use strict";
>>>{ }
1 >
2 >^
3 > ^
4 > ^
5 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
2 >{
3 >
4 >
1 >Emitted(2, 1) Source(1, 1) + SourceIndex(0)
2 >Emitted(2, 2) Source(1, 2) + SourceIndex(0)
3 >Emitted(2, 3) Source(1, 2) + SourceIndex(0)
4 >Emitted(2, 4) Source(1, 3) + SourceIndex(0)
---
>>>//# sourceMappingURL=sourceMapUnclosedBlock.js.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//// [tests/cases/compiler/sourceMapUnclosedBlock.ts] ////

=== sourceMapUnclosedBlock.ts ===
{
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//// [tests/cases/compiler/sourceMapUnclosedBlock.ts] ////

=== sourceMapUnclosedBlock.ts ===
{
2 changes: 2 additions & 0 deletions testdata/tests/cases/compiler/sourceMapUnclosedBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @sourceMap: true
{