Skip to content
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

Skip printing async if await is not used #1004

Merged
merged 1 commit into from
May 1, 2024
Merged
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/warm-zebras-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Skips printing `async` for component functions if `await` is not used
9 changes: 8 additions & 1 deletion internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,15 @@ func (p *printer) printFuncPrelude(opts transform.TransformOptions, printAstroGl
return
}
componentName := getComponentName(opts.Filename)

// Decide whether to print `async` if top-level await is used. Use a loose check for now.
funcPrefix := ""
if strings.Contains(p.sourcetext, "await") {
funcPrefix = "async "
}

p.addNilSourceMapping()
p.println(fmt.Sprintf("const %s = %s(async (%s, $$props, %s) => {", componentName, CREATE_COMPONENT, RESULT, SLOTS))
p.println(fmt.Sprintf("const %s = %s(%s(%s, $$props, %s) => {", componentName, CREATE_COMPONENT, funcPrefix, RESULT, SLOTS))
if printAstroGlobal {
p.addNilSourceMapping()
p.println(fmt.Sprintf("const Astro = %s.createAstro($$Astro, $$props, %s);", RESULT, SLOTS))
Expand Down
13 changes: 9 additions & 4 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ var INTERNAL_IMPORTS = fmt.Sprintf("import {\n %s\n} from \"%s\";\n", strings.J
"renderScript as " + RENDER_SCRIPT,
"createMetadata as " + CREATE_METADATA,
}, ",\n "), "http://localhost:3000/")
var PRELUDE = fmt.Sprintf(`const $$Component = %s(async ($$result, $$props, %s) => {`, CREATE_COMPONENT, SLOTS)
var PRELUDE = fmt.Sprintf(`const $$Component = %s(($$result, $$props, %s) => {`, CREATE_COMPONENT, SLOTS)
var PRELUDE_WITH_ASYNC = fmt.Sprintf(`const $$Component = %s(async ($$result, $$props, %s) => {`, CREATE_COMPONENT, SLOTS)
var PRELUDE_ASTRO_GLOBAL = fmt.Sprintf(`const Astro = $$result.createAstro($$Astro, $$props, %s);
Astro.self = $$Component;`, SLOTS)
var RETURN = fmt.Sprintf("return %s%s", TEMPLATE_TAG, BACKTICK)
Expand Down Expand Up @@ -3662,11 +3663,15 @@ const meta = { title: 'My App' };
if len(tt.want.getStaticPaths) > 0 {
toMatch += strings.TrimSpace(test_utils.Dedent(tt.want.getStaticPaths)) + "\n\n"
}
if printAstroGlobal {
toMatch += test_utils.Dedent(PRELUDE) + test_utils.Dedent(PRELUDE_ASTRO_GLOBAL) + "\n"
if strings.Contains(tt.source, "await") {
toMatch += test_utils.Dedent(PRELUDE_WITH_ASYNC)
} else {
toMatch += test_utils.Dedent(PRELUDE) + "\n"
toMatch += test_utils.Dedent(PRELUDE)
}
if printAstroGlobal {
toMatch += test_utils.Dedent(PRELUDE_ASTRO_GLOBAL)
}
toMatch += "\n"
if len(tt.want.frontmatter) > 1 {
toMatch += strings.TrimSpace(test_utils.Dedent(tt.want.frontmatter[1]))
}
Expand Down