Skip to content

Fix templateExportDefault: true not adding export default to bare module-level templates#422

Merged
evoactivity merged 2 commits intomainfrom
copilot/fix-export-default-issue
Apr 17, 2026
Merged

Fix templateExportDefault: true not adding export default to bare module-level templates#422
evoactivity merged 2 commits intomainfrom
copilot/fix-export-default-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

Bare <template> tags in .gjs files (or .gts files without a TypeScript type annotation) were not getting export default prepended when templateExportDefault: true, leaving the file unchanged.

Root Cause

The preprocessing converts a bare <template> at module level into a BlockStatement directly inside Program. The existing export default injection logic lives in print() and only fires for nodes matched by isGlimmerTemplateParent — which covers ExportDefaultDeclaration and TypeScript ExpressionStatement > TSAsExpression > template, but not a BlockStatement that is a direct child of Program. Because embed() handles these template nodes before print() ever sees them, the injection was silently skipped.

Fix

Added the export default injection into embed(), where bare module-level templates are actually handled:

// src/printers.ts
const printed = printTemplateTag(content);

if (options.templateExportDefault && path.parent?.type === 'Program') {
  printed.unshift('export ', 'default ');
}

The path.parent?.type === 'Program' guard ensures only truly bare module-level templates are affected — templates nested inside ExportDefaultDeclaration, VariableDeclarator, class bodies, etc. are untouched.

Snapshot Updates

Four snapshots corrected in the templateExportDefault: true config suite:

File Before After
gjs/simple.gjs <template>what</template> export default <template>what</template>
gjs/simple-with-tag.gjs bare top-level template unchanged bare template gets export default; const-assigned templates unaffected
gjs/preceded-by-object.gjs bare template unchanged bare template gets export default
gts/issue-191-b.gts <template>💩</template> export default <template>💩</template>
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug] templateExportDefault: true does not add export default correctly</issue_title>
<issue_description>When templateExportDefault: true, I would expect this gjs file:

<template>Hello world</template>

to be transformed to

export default <template>Hello world</template>

However, this does not happen. The file remains unchanged.

This issue is also shown in the snapshot tests in this repo:

exports[`config > templateExportDefault: true > it formats ../cases/gjs/simple.gjs 1`] = `
"<template>what</template>
"
`;

^^ this assertion should be export default <template>what</template>

🌍 Environment

  • prettier-plugin-ember-template-tag version: 2.1.0
    </issue_description>

<agent_instructions>Do we have any tests for this behavior? If not, create a failing test for it.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…ule-level templates

Co-authored-by: gitKrystan <14152574+gitKrystan@users.noreply.github.com>
Copilot AI changed the title [WIP] [Bug] Fix templateExportDefault not adding export default correctly Fix templateExportDefault: true not adding export default to bare module-level templates Mar 13, 2026
Copilot AI requested a review from gitKrystan March 13, 2026 19:12
@evoactivity
Copy link
Copy Markdown
Member

This looks ok, @gitKrystan any reason to keep this a draft or can we merge to get it in next release?

@gitKrystan gitKrystan marked this pull request as ready for review April 17, 2026 17:30
@evoactivity evoactivity added the bug Something isn't working label Apr 17, 2026
@evoactivity evoactivity merged commit 8f01348 into main Apr 17, 2026
15 checks passed
@github-actions github-actions Bot mentioned this pull request Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] templateExportDefault: true does not add export default correctly

3 participants