Skip to content
Closed
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
10 changes: 9 additions & 1 deletion lib/rules/template-no-yield-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ module.exports = {

return {
GlimmerTemplate(node) {
// In GJS/GTS, `node.body` is a one-element list wrapping a
// `<template>` element whose `children` is the actual template
// content. In classic HBS, `node.body` is the template content
// directly. Require `body.length === 1` before descending so we
// don't mistake a native HTML `<template>` element that happens
// to be the first child of an HBS template for the GJS wrapper —
// e.g. `<template>{{yield}}</template>{{#each items}}…{{/each}}`
// would otherwise be wrongly flagged as yield-only.
const templateNodes =
node.body[0] &&
node.body.length === 1 &&
node.body[0].type === 'GlimmerElementNode' &&
node.body[0].tag === 'template'
? node.body[0].children
Expand Down
12 changes: 11 additions & 1 deletion tests/lib/rules/template-no-yield-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ const validHbs = [
'<yield/>',
];

// HBS-only cases: these use a native HTML `<template>` element as the first
// child, which parses as a nested `<template>` in GJS/GTS (invalid syntax —
// GJS reserves `<template>` for the module-level component wrapper).
const validHbsOnly = [
// The rule must NOT descend into the native `<template>` and flag the
// inner `{{yield}}` as yield-only; the outer template has additional
// content (the `{{#each}}` block).
'<template>{{yield}}</template>{{#each items}}item{{/each}}',
];

const invalidHbs = [
{
code: '{{yield}}',
Expand Down Expand Up @@ -63,6 +73,6 @@ const hbsRuleTester = new RuleTester({
});

hbsRuleTester.run('template-no-yield-only', rule, {
valid: validHbs,
valid: [...validHbs, ...validHbsOnly],
invalid: invalidHbs,
});
Loading