Skip to content

Commit a6081c7

Browse files
committed
fix: export w/ curly brace on next line, extra curly brace before def
1 parent 394ef06 commit a6081c7

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed

internal/js_scanner/js_scanner.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ outer:
8080
// a specifier is found, and a line terminator has been found
8181
if token == js.ExportToken {
8282
flags := make(map[string]bool)
83+
tokensFound := make(map[string]bool)
8384
foundIdent := false
8485
foundSemicolonOrLineTerminator := false
86+
foundBody := false
8587
start := i
8688
i += len(value)
8789
for {
@@ -94,6 +96,7 @@ outer:
9496
}
9597
i += len(nextValue)
9698
flags[string(nextValue)] = true
99+
tokensFound[string(nextValue)] = true
97100

98101
if next == js.ErrorToken && l.Err() == io.EOF {
99102
foundSemicolonOrLineTerminator = true
@@ -113,7 +116,7 @@ outer:
113116
if next == js.LineTerminatorToken && i < len(source) && (source[i] == '&' || source[i] == '|') {
114117
continue
115118
}
116-
if (flags["function"] || flags["=>"] || flags["interface"]) && !flags["{"] {
119+
if (flags["function"] || flags["=>"] || flags["interface"]) && !foundBody {
117120
continue
118121
}
119122
if flags["&"] || flags["="] {
@@ -125,6 +128,17 @@ outer:
125128

126129
foundSemicolonOrLineTerminator = true
127130
} else if js.IsPunctuator(next) {
131+
if nextValue[0] == '{' {
132+
if flags["function"] {
133+
// Curly braces can occur in a function parameter destructuring, which we don't want to consider
134+
foundBody = foundBody || pairs['('] == 0
135+
} else if flags["=>"] {
136+
// Arrow can also occur in type definition before arrow function body (which we don't want to consider), but `=` cannot
137+
foundBody = foundBody || tokensFound["="]
138+
} else {
139+
foundBody = true
140+
}
141+
}
128142
if nextValue[0] == '{' || nextValue[0] == '(' || nextValue[0] == '[' {
129143
flags[string(nextValue[0])] = true
130144
pairs[nextValue[0]]++

internal/js_scanner/js_scanner_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,116 @@ export async function getStaticPaths() {
260260
const b = await fetch()`,
261261
want: `export async function getStaticPaths() {
262262
const content = Astro.fetchContent('**/*.md');
263+
}`,
264+
},
265+
{
266+
name: "getStaticPaths with curly brace on next line and destructured props",
267+
source: `import { fn } from "package";
268+
export async function getStaticPaths({ paginate })
269+
{
270+
const content = Astro.fetchContent('**/*.md');
271+
}
272+
const b = await fetch()`,
273+
want: `export async function getStaticPaths({ paginate })
274+
{
275+
const content = Astro.fetchContent('**/*.md');
276+
}`,
277+
},
278+
{
279+
name: "getStaticPaths with curly brace on next line and param definition type in curly braces",
280+
source: `import { fn } from "package";
281+
export async function getStaticPaths(input: { paginate: any })
282+
{
283+
const content = Astro.fetchContent('**/*.md');
284+
}
285+
const b = await fetch()`,
286+
want: `export async function getStaticPaths(input: { paginate: any })
287+
{
288+
const content = Astro.fetchContent('**/*.md');
289+
}`,
290+
},
291+
{
292+
name: "getStaticPaths with curly brace on next line and param definition type in square braces",
293+
source: `import { fn } from "package";
294+
export async function getStaticPaths([{ stuff }])
295+
{
296+
const content = Astro.fetchContent('**/*.md');
297+
}
298+
const b = await fetch()`,
299+
want: `export async function getStaticPaths([{ stuff }])
300+
{
301+
const content = Astro.fetchContent('**/*.md');
302+
}`,
303+
},
304+
{
305+
name: "getStaticPaths with curly brace on next line and type specified with square braces 1",
306+
source: `import { fn } from "package";
307+
export const getStaticPaths: () => { params: any }[]
308+
= () =>
309+
{
310+
const content = Astro.fetchContent('**/*.md');
311+
}
312+
const b = await fetch()`,
313+
want: `export const getStaticPaths: () => { params: any }[]
314+
= () =>
315+
{
316+
const content = Astro.fetchContent('**/*.md');
317+
}`,
318+
},
319+
{
320+
name: "getStaticPaths with curly brace on next line and type specified with square braces 2",
321+
source: `import { fn } from "package";
322+
export const getStaticPaths: () => { params: any }[] =
323+
() =>
324+
{
325+
const content = Astro.fetchContent('**/*.md');
326+
}
327+
const b = await fetch()`,
328+
want: `export const getStaticPaths: () => { params: any }[] =
329+
() =>
330+
{
331+
const content = Astro.fetchContent('**/*.md');
332+
}`,
333+
},
334+
{
335+
name: "getStaticPaths with curly brace on next line and type specified with square braces 3",
336+
source: `import { fn } from "package";
337+
export const getStaticPaths: () => { params: any }[] = ()
338+
=>
339+
{
340+
const content = Astro.fetchContent('**/*.md');
341+
}
342+
const b = await fetch()`,
343+
want: `export const getStaticPaths: () => { params: any }[] = ()
344+
=>
345+
{
346+
const content = Astro.fetchContent('**/*.md');
347+
}`,
348+
},
349+
{
350+
name: "getStaticPaths with curly brace on next line and type specified with square braces 4",
351+
source: `import { fn } from "package";
352+
export const getStaticPaths: () => { params: any }[] = () =>
353+
{
354+
const content = Astro.fetchContent('**/*.md');
355+
}
356+
const b = await fetch()`,
357+
want: `export const getStaticPaths: () => { params: any }[] = () =>
358+
{
359+
const content = Astro.fetchContent('**/*.md');
360+
}`,
361+
},
362+
{
363+
name: "getStaticPaths with curly brace on next line and definition specified by anonymous function with destructured parameter",
364+
source: `import { fn } from "package";
365+
export const getStaticPaths = function({ paginate })
366+
{
367+
const content = Astro.fetchContent('**/*.md');
368+
}
369+
const b = await fetch()`,
370+
want: `export const getStaticPaths = function({ paginate })
371+
{
372+
const content = Astro.fetchContent('**/*.md');
263373
}`,
264374
},
265375
{

packages/compiler/test/basic/get-static-paths.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ export async function getStaticPaths()
2525
);
2626
});
2727

28+
test('getStaticPaths with braces on newline and destructured params', async () => {
29+
const FIXTURE = `---
30+
import A from './A.astro';
31+
export async function getStaticPaths({ paginate })
32+
{
33+
return [
34+
{ params: { id: '1' } },
35+
{ params: { id: '2' } },
36+
{ params: { id: '3' } }
37+
];
38+
}
39+
---
40+
41+
<div></div>
42+
`;
43+
const result = await transform(FIXTURE);
44+
assert.match(
45+
result.code,
46+
'export async function getStaticPaths({ paginate })\n{',
47+
'Expected output to contain getStaticPaths output'
48+
);
49+
});
50+
2851
test('getStaticPaths as const without braces', async () => {
2952
const FIXTURE = `---
3053
import A from './A.astro';

0 commit comments

Comments
 (0)