Skip to content

Commit

Permalink
Fix line returns segments being one character too long on Windows (#1043
Browse files Browse the repository at this point in the history
)

* fix: maybe@

"@"

* test: add test

* chore: changeset
  • Loading branch information
Princesseuh authored Aug 14, 2024
1 parent f55a2af commit 5d0023d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-planets-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/compiler": patch
---

Fixes sourcemapping for CRLF line endings wrongfully including the last character
31 changes: 23 additions & 8 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,22 @@ func (p *printer) addTSXStyle(start int, end int, content string, styleType stri

func (p *printer) printTextWithSourcemap(text string, l loc.Loc) {
start := l.Start
skipNext := false
for pos, c := range text {
// Handle Windows-specific "\r\n" newlines
if skipNext {
skipNext = false
continue
}

// If we encounter a CRLF, map both characters to the same location
if c == '\r' && len(text[pos:]) > 1 && text[pos+1] == '\n' {
// tiny optimization: avoid calling `utf8.DecodeRuneInString`
// if we know the next char is `\n`
start++
p.addSourceMapping(loc.Loc{Start: start})
p.print("\r\n")
start += 2
skipNext = true
continue
}

_, nextCharByteSize := utf8.DecodeRuneInString(text[pos:])
p.addSourceMapping(loc.Loc{Start: start})
p.print(string(c))
Expand All @@ -124,12 +132,19 @@ func (p *printer) printTextWithSourcemap(text string, l loc.Loc) {

func (p *printer) printEscapedJSXTextWithSourcemap(text string, l loc.Loc) {
start := l.Start
skipNext := false
for pos, c := range text {
// Handle Windows-specific "\r\n" newlines
if skipNext {
skipNext = false
continue
}

// If we encounter a CRLF, map both characters to the same location
if c == '\r' && len(text[pos:]) > 1 && text[pos+1] == '\n' {
// tiny optimization: avoid calling `utf8.DecodeRuneInString`
// if we know the next char is `\n`
start++
p.addSourceMapping(loc.Loc{Start: start})
p.print("\r\n")
start += 2
skipNext = true
continue
}

Expand Down
11 changes: 11 additions & 0 deletions packages/compiler/test/tsx-sourcemaps/template-windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { testTSXSourcemap } from '../utils.js';

test('last character does not end up in middle of CRLF', async () => {
const input = "---\r\nimport { Meta } from '$lib/components/Meta.astro';\r\n---\r\n";
const output = await testTSXSourcemap(input, ';');
assert.equal(output, {
source: 'index.astro',
line: 2,
column: 50,
name: null,
});
});

test('template expression basic', async () => {
const input = '<div>{\r\nnonexistent\r\n}</div>';

Expand Down

0 comments on commit 5d0023d

Please sign in to comment.