From 93abe8412064f0072663fa3c0f3254fb0b7821fa Mon Sep 17 00:00:00 2001 From: Princesseuh <3019731+Princesseuh@users.noreply.github.com> Date: Tue, 13 Aug 2024 18:08:43 +0200 Subject: [PATCH 1/3] fix: maybe@ "@" --- internal/printer/printer.go | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/internal/printer/printer.go b/internal/printer/printer.go index 6e2af9f3..32d2797d 100644 --- a/internal/printer/printer.go +++ b/internal/printer/printer.go @@ -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)) @@ -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 } From d375e2042b45ce0142f298efaf69df0f6bd0d464 Mon Sep 17 00:00:00 2001 From: Princesseuh <3019731+Princesseuh@users.noreply.github.com> Date: Wed, 14 Aug 2024 00:53:24 +0200 Subject: [PATCH 2/3] test: add test --- .../compiler/test/tsx-sourcemaps/template-windows.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/compiler/test/tsx-sourcemaps/template-windows.ts b/packages/compiler/test/tsx-sourcemaps/template-windows.ts index b0d7a995..59498ee8 100644 --- a/packages/compiler/test/tsx-sourcemaps/template-windows.ts +++ b/packages/compiler/test/tsx-sourcemaps/template-windows.ts @@ -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 = '