diff --git a/.changeset/flat-peaches-design.md b/.changeset/flat-peaches-design.md new file mode 100644 index 000000000..753c7b4df --- /dev/null +++ b/.changeset/flat-peaches-design.md @@ -0,0 +1,5 @@ +--- +'@astrojs/compiler': patch +--- + +Fix getStaticPaths export when used with a TypeScript type ([#4929](https://github.com/withastro/astro/issues/4929)) diff --git a/internal/js_scanner/js_scanner.go b/internal/js_scanner/js_scanner.go index 0130e4667..f2a119f07 100644 --- a/internal/js_scanner/js_scanner.go +++ b/internal/js_scanner/js_scanner.go @@ -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] == '[' { diff --git a/internal/js_scanner/js_scanner_test.go b/internal/js_scanner/js_scanner_test.go index c8d034da3..a2208caa1 100644 --- a/internal/js_scanner/js_scanner_test.go +++ b/internal/js_scanner/js_scanner_test.go @@ -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'); }`, }, { diff --git a/packages/compiler/test/basic/get-static-paths.ts b/packages/compiler/test/basic/get-static-paths.ts index b24392560..382f97af9 100644 --- a/packages/compiler/test/basic/get-static-paths.ts +++ b/packages/compiler/test/basic/get-static-paths.ts @@ -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 }); +} +--- + +
+`; + const result = await transform(FIXTURE); + assert.match(result.code, `{\n paginate: PaginateFunction;\n}) {`, 'Expected output to contain getStaticPaths output'); +}); + test.run();