Skip to content

Commit

Permalink
Skip printing async if await is not used (#1004)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored May 1, 2024
1 parent e8b6cdf commit ecd7e90
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
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

0 comments on commit ecd7e90

Please sign in to comment.