Skip to content

fix: export w/ curly brace on next line, extra curly brace before def #1075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-lemons-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/compiler": patch
---

Improves detection of function body opening curly brace for exported functions.
16 changes: 15 additions & 1 deletion internal/js_scanner/js_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ outer:
// a specifier is found, and a line terminator has been found
if token == js.ExportToken {
flags := make(map[string]bool)
tokensFound := make(map[string]bool)
foundIdent := false
foundSemicolonOrLineTerminator := false
foundBody := false
start := i
i += len(value)
for {
Expand All @@ -94,6 +96,7 @@ outer:
}
i += len(nextValue)
flags[string(nextValue)] = true
tokensFound[string(nextValue)] = true

if next == js.ErrorToken && l.Err() == io.EOF {
foundSemicolonOrLineTerminator = true
Expand All @@ -113,7 +116,7 @@ outer:
if next == js.LineTerminatorToken && i < len(source) && (source[i] == '&' || source[i] == '|') {
continue
}
if (flags["function"] || flags["=>"] || flags["interface"]) && !flags["{"] {
if (flags["function"] || flags["=>"] || flags["interface"]) && !foundBody {
continue
}
if flags["&"] || flags["="] {
Expand All @@ -125,6 +128,17 @@ outer:

foundSemicolonOrLineTerminator = true
} else if js.IsPunctuator(next) {
if nextValue[0] == '{' {
if flags["function"] {
// Curly braces can occur in a function parameter destructuring, which we don't want to consider
foundBody = foundBody || pairs['('] == 0
} else if flags["=>"] {
// Arrow can also occur in type definition before arrow function body (which we don't want to consider), but `=` cannot
foundBody = foundBody || tokensFound["="]
} else {
foundBody = true
}
}
if nextValue[0] == '{' || nextValue[0] == '(' || nextValue[0] == '[' {
flags[string(nextValue[0])] = true
pairs[nextValue[0]]++
Expand Down
110 changes: 110 additions & 0 deletions internal/js_scanner/js_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,116 @@ export async function getStaticPaths() {
const b = await fetch()`,
want: `export async function getStaticPaths() {
const content = Astro.fetchContent('**/*.md');
}`,
},
{
name: "getStaticPaths with curly brace on next line and destructured props",
source: `import { fn } from "package";
export async function getStaticPaths({ paginate })
{
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `export async function getStaticPaths({ paginate })
{
const content = Astro.fetchContent('**/*.md');
}`,
},
{
name: "getStaticPaths with curly brace on next line and param definition type in curly braces",
source: `import { fn } from "package";
export async function getStaticPaths(input: { paginate: any })
{
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `export async function getStaticPaths(input: { paginate: any })
{
const content = Astro.fetchContent('**/*.md');
}`,
},
{
name: "getStaticPaths with curly brace on next line and param definition type in square braces",
source: `import { fn } from "package";
export async function getStaticPaths([{ stuff }])
{
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `export async function getStaticPaths([{ stuff }])
{
const content = Astro.fetchContent('**/*.md');
}`,
},
{
name: "getStaticPaths with curly brace on next line and type specified with square braces 1",
source: `import { fn } from "package";
export const getStaticPaths: () => { params: any }[]
= () =>
{
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `export const getStaticPaths: () => { params: any }[]
= () =>
{
const content = Astro.fetchContent('**/*.md');
}`,
},
{
name: "getStaticPaths with curly brace on next line and type specified with square braces 2",
source: `import { fn } from "package";
export const getStaticPaths: () => { params: any }[] =
() =>
{
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `export const getStaticPaths: () => { params: any }[] =
() =>
{
const content = Astro.fetchContent('**/*.md');
}`,
},
{
name: "getStaticPaths with curly brace on next line and type specified with square braces 3",
source: `import { fn } from "package";
export const getStaticPaths: () => { params: any }[] = ()
=>
{
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `export const getStaticPaths: () => { params: any }[] = ()
=>
{
const content = Astro.fetchContent('**/*.md');
}`,
},
{
name: "getStaticPaths with curly brace on next line and type specified with square braces 4",
source: `import { fn } from "package";
export const getStaticPaths: () => { params: any }[] = () =>
{
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `export const getStaticPaths: () => { params: any }[] = () =>
{
const content = Astro.fetchContent('**/*.md');
}`,
},
{
name: "getStaticPaths with curly brace on next line and definition specified by anonymous function with destructured parameter",
source: `import { fn } from "package";
export const getStaticPaths = function({ paginate })
{
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `export const getStaticPaths = function({ paginate })
{
const content = Astro.fetchContent('**/*.md');
}`,
},
{
Expand Down
23 changes: 23 additions & 0 deletions packages/compiler/test/basic/get-static-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ export async function getStaticPaths()
);
});

test('getStaticPaths with braces on newline and destructured params', async () => {
const FIXTURE = `---
import A from './A.astro';
export async function getStaticPaths({ paginate })
{
return [
{ params: { id: '1' } },
{ params: { id: '2' } },
{ params: { id: '3' } }
];
}
---

<div></div>
`;
const result = await transform(FIXTURE);
assert.match(
result.code,
'export async function getStaticPaths({ paginate })\n{',
'Expected output to contain getStaticPaths output'
);
});

test('getStaticPaths as const without braces', async () => {
const FIXTURE = `---
import A from './A.astro';
Expand Down
Loading