Skip to content

Commit 703bf15

Browse files
committed
fix: ignore comment + whitespace nodes in template-no-yield-only
The rule flagged yield-only templates by asserting `templateNodes.length === 1 && isYieldOnly(templateNodes[0])`. That works only while the parser strips template comments out of the body, which ember-estree 0.4.2 happened to do. Upstream ember-estree 0.4.3 (#31) began keeping MustacheCommentStatement nodes in the body, so a template like <template>{{! some comment }}{{yield}}</template> now yields a body of length 2 and silently stops being flagged — which is also why PR #2735 (upgrade to ember-eslint-parser 0.11) fails this rule's CI. Ignoring comment and whitespace-only text nodes before the length check aligns with: - upstream ember-template-lint's `no-yield-only.js` (unchanged there since comments-in-body is the native Glimmer AST shape anyway) - the sibling rule `template-no-bare-yield.js`, which already uses the same `isEmptyNode` filter The fix is a no-op on the currently-pinned ember-eslint-parser@0.10 and fixes the rule under any parser version that preserves comment nodes in the template body. The rule also now correctly catches yield-only templates that contain HTML comments (`<!-- x -->{{yield}}`), which it was silently missing before.
1 parent 7676bb3 commit 703bf15

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

lib/rules/template-no-yield-only.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
function isEmptyNode(node) {
2+
return (
3+
node.type === 'GlimmerMustacheCommentStatement' ||
4+
node.type === 'GlimmerCommentStatement' ||
5+
(node.type === 'GlimmerTextNode' && !node.chars.trim())
6+
);
7+
}
8+
19
function isYieldOnly(node) {
210
return (
311
node.type === 'GlimmerMustacheStatement' &&
@@ -44,7 +52,8 @@ module.exports = {
4452
? node.body[0].children
4553
: node.body;
4654

47-
if (templateNodes.length === 1 && isYieldOnly(templateNodes[0])) {
55+
const nonEmptyNodes = templateNodes.filter((n) => !isEmptyNode(n));
56+
if (nonEmptyNodes.length === 1 && isYieldOnly(nonEmptyNodes[0])) {
4857
isOnlyYield = true;
4958
}
5059
},

tests/lib/rules/template-no-yield-only.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ const invalidHbs = [
3030
output: null,
3131
errors: [{ messageId: 'noYieldOnly' }],
3232
},
33+
{
34+
code: '{{!-- long-form comment --}}{{yield}}',
35+
output: null,
36+
errors: [{ messageId: 'noYieldOnly' }],
37+
},
38+
{
39+
code: '<!-- html comment -->{{yield}}',
40+
output: null,
41+
errors: [{ messageId: 'noYieldOnly' }],
42+
},
3343
];
3444

3545
function wrapTemplate(entry) {

0 commit comments

Comments
 (0)