Skip to content

Commit

Permalink
Fix export hoisting with inline TypeScript types (withastro#750)
Browse files Browse the repository at this point in the history
* fix: export hoisting with inline TypeScript types

* chore: format
  • Loading branch information
natemoo-re authored Mar 14, 2023
1 parent 102c19c commit 348840b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-peaches-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fix getStaticPaths export when used with a TypeScript type ([#4929](https://github.com/withastro/astro/issues/4929))
4 changes: 4 additions & 0 deletions internal/js_scanner/js_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ outer:
if flags["&"] || flags["="] {
continue
}
if pairs['('] > 0 {
continue
}

foundSemicolonOrLineTerminator = true
} else if js.IsPunctuator(next) {
if nextValue[0] == '{' || nextValue[0] == '(' || nextValue[0] == '[' {
Expand Down
20 changes: 20 additions & 0 deletions internal/js_scanner/js_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,26 @@ import { b } from "b";
const { a } = Astro.props;`,
want: `export async function getStaticPaths() {
const value = /2/g;
}`,
},
{
name: "getStaticPaths with TypeScript type",
source: `import { fn } from "package";
export async function getStaticPaths({
paginate
}: {
paginate: any
}) {
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `export async function getStaticPaths({
paginate
}: {
paginate: any
}) {
const content = Astro.fetchContent('**/*.md');
}`,
},
{
Expand Down
22 changes: 22 additions & 0 deletions packages/compiler/test/basic/get-static-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,26 @@ const { MdxContent, frontmatter, url, file } = Astro.props;
assert.match(result.code, '\nconst $$stdin = ', 'Expected getStaticPaths hoisting to maintain newlines');
});

test('getStaticPaths with types', async () => {
const FIXTURE = `---
export async function getStaticPaths({
paginate,
}: {
paginate: PaginateFunction;
}) {
const allPages = (
await getCollection(
"blog"
)
);
return paginate(allPages, { pageSize: 10 });
}
---
<div></div>
`;
const result = await transform(FIXTURE);
assert.match(result.code, `{\n paginate: PaginateFunction;\n}) {`, 'Expected output to contain getStaticPaths output');
});

test.run();

0 comments on commit 348840b

Please sign in to comment.