Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix line returns segments being one character too long on Windows #1043

Merged
merged 3 commits into from
Aug 14, 2024
Merged
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
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