Skip to content

Commit 4b3fafa

Browse files
authored
fix(tsx): improve sourcemaps on Windows (#601)
Co-authored-by: Nate Moore <[email protected]>
1 parent 5a8636e commit 4b3fafa

File tree

4 files changed

+128
-7
lines changed

4 files changed

+128
-7
lines changed

.changeset/rude-cherries-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/compiler': patch
3+
---
4+
5+
Fix TSX sourcemaps on Windows

internal/printer/printer.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ func (p *printer) printTextWithSourcemap(text string, l loc.Loc) {
6868
lastPos := -1
6969
for pos, c := range text {
7070
diff := pos - lastPos
71-
if c == '\r' {
72-
start += diff
73-
continue
74-
}
7571
p.addSourceMapping(loc.Loc{Start: start})
7672
p.print(string(c))
7773
start += diff

internal/sourcemap/sourcemap.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ func (offset *LineColumnOffset) AdvanceBytes(bytes []byte) {
209209
case '\r', '\n', '\u2028', '\u2029':
210210
// Handle Windows-specific "\r\n" newlines
211211
if c == '\r' && len(bytes) > 0 && bytes[0] == '\n' {
212-
columns++
213212
continue
214213
}
215214

@@ -235,7 +234,6 @@ func (offset *LineColumnOffset) AdvanceString(text string) {
235234
case '\r', '\n', '\u2028', '\u2029':
236235
// Handle Windows-specific "\r\n" newlines
237236
if c == '\r' && i+1 < len(text) && text[i+1] == '\n' {
238-
columns++
239237
continue
240238
}
241239

@@ -505,7 +503,6 @@ func GenerateLineOffsetTables(contents string, approximateLineCount int) []LineO
505503
case '\r', '\n', '\u2028', '\u2029':
506504
// Handle Windows-specific "\r\n" newlines
507505
if c == '\r' && i+1 < len(contents) && contents[i+1] == '\n' {
508-
column++
509506
continue
510507
}
511508

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { test } from 'uvu';
2+
import * as assert from 'uvu/assert';
3+
import { testSourcemap } from '../utils';
4+
5+
test('template expression basic', async () => {
6+
const input = `<div>{\r\nnonexistent\r\n}</div>`;
7+
8+
const output = await testSourcemap(input, 'nonexistent');
9+
assert.equal(output, {
10+
source: 'index.astro',
11+
line: 2,
12+
column: 1,
13+
name: null,
14+
});
15+
});
16+
17+
test('template expression has dot', async () => {
18+
const input = `<div>{\nconsole.log(hey)\n}</div>`;
19+
const output = await testSourcemap(input, 'log');
20+
assert.equal(output, {
21+
source: 'index.astro',
22+
line: 2,
23+
column: 9,
24+
name: null,
25+
});
26+
});
27+
28+
test('template expression has dot', async () => {
29+
const input = `<div>{\r\nconsole.log(hey)\r\n}</div>`;
30+
const output = await testSourcemap(input, 'log');
31+
assert.equal(output, {
32+
source: 'index.astro',
33+
line: 2,
34+
column: 9,
35+
name: null,
36+
});
37+
});
38+
39+
test('template expression with addition', async () => {
40+
const input = `{"hello" + \nhey}`;
41+
const output = await testSourcemap(input, 'hey');
42+
assert.equal(output, {
43+
source: 'index.astro',
44+
line: 2,
45+
column: 1,
46+
name: null,
47+
});
48+
});
49+
50+
test('template expression with addition', async () => {
51+
const input = `{"hello" + \r\nhey}`;
52+
const output = await testSourcemap(input, 'hey');
53+
assert.equal(output, {
54+
source: 'index.astro',
55+
line: 2,
56+
column: 1,
57+
name: null,
58+
});
59+
});
60+
61+
test('html attribute', async () => {
62+
const input = `<svg\nvalue="foo" color="#000"></svg>`;
63+
const output = await testSourcemap(input, 'color');
64+
assert.equal(output, {
65+
source: 'index.astro',
66+
name: null,
67+
line: 2,
68+
column: 12,
69+
});
70+
});
71+
72+
test('html attribute', async () => {
73+
const input = `<svg\r\nvalue="foo" color="#000"></svg>`;
74+
const output = await testSourcemap(input, 'color');
75+
assert.equal(output, {
76+
source: 'index.astro',
77+
name: null,
78+
line: 2,
79+
column: 12,
80+
});
81+
});
82+
83+
test('complex template expression', async () => {
84+
const input = `{[].map(ITEM => {\r\nv = "what";\r\nreturn <div>{ITEMS}</div>\r\n})}`;
85+
const item = await testSourcemap(input, 'ITEM');
86+
const items = await testSourcemap(input, 'ITEMS');
87+
assert.equal(item, {
88+
source: 'index.astro',
89+
name: null,
90+
line: 1,
91+
column: 8,
92+
});
93+
assert.equal(items, {
94+
source: 'index.astro',
95+
name: null,
96+
line: 3,
97+
column: 14,
98+
});
99+
});
100+
101+
test('attributes', async () => {
102+
const input = `<div\r\na="b" className="hello" />`;
103+
const className = await testSourcemap(input, 'className');
104+
assert.equal(className, {
105+
source: 'index.astro',
106+
name: null,
107+
line: 2,
108+
column: 6,
109+
});
110+
});
111+
112+
test('special attributes', async () => {
113+
const input = `<div\r\na="b" @on.click="fn" />`;
114+
const onClick = await testSourcemap(input, '@on.click');
115+
assert.equal(onClick, {
116+
source: 'index.astro',
117+
name: null,
118+
line: 2,
119+
column: 6,
120+
});
121+
});
122+
123+
test.run();

0 commit comments

Comments
 (0)