Skip to content

Commit 9a9ccdd

Browse files
committed
Widen branch scoping to all control flow helpers, not just if/unless
1 parent 131cd12 commit 9a9ccdd

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

lib/rules/template-no-duplicate-id.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ function isControlFlowHelper(node) {
77
return false;
88
}
99

10-
function isIfUnless(node) {
11-
if (node.type === 'GlimmerBlockStatement' && node.path?.type === 'GlimmerPathExpression') {
12-
return ['if', 'unless'].includes(node.path.original);
13-
}
14-
return false;
15-
}
16-
1710
// Walk up the parent chain to find an ancestor element/block whose blockParams
1811
// include headName. Used to make block-param-derived IDs unique per invocation.
1912
function findBlockParamAncestor(node, headName) {
@@ -236,14 +229,14 @@ module.exports = {
236229

237230
GlimmerBlock(node) {
238231
const parent = node.parent;
239-
if (parent && isIfUnless(parent)) {
232+
if (parent && isControlFlowHelper(parent)) {
240233
enterConditionalBranch();
241234
}
242235
},
243236

244237
'GlimmerBlock:exit'(node) {
245238
const parent = node.parent;
246-
if (parent && isIfUnless(parent)) {
239+
if (parent && isControlFlowHelper(parent)) {
247240
exitConditionalBranch();
248241
}
249242
},

tests/lib/rules/template-no-duplicate-id.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ ruleTester.run('template-no-duplicate-id', rule, {
113113
<Input id={{inputProperties.id}} />
114114
</MyComponent>
115115
</template>`,
116+
// IDs in different branches of {{#each}} should not conflict
117+
`<template>
118+
{{#each items as |item|}}
119+
<div id="x"></div>
120+
{{else}}
121+
<div id="x"></div>
122+
{{/each}}
123+
</template>`,
116124
],
117125
invalid: [
118126
// blockParams on an element must not isolate static IDs from outer scope
@@ -423,6 +431,12 @@ hbsRuleTester.run('template-no-duplicate-id (hbs)', rule, {
423431
<MyComponent as |inputProperties|>
424432
<Input id={{inputProperties.id}} />
425433
</MyComponent>`,
434+
// IDs in different branches of {{#each}} should not conflict
435+
`{{#each items as |item|}}
436+
<div id="x"></div>
437+
{{else}}
438+
<div id="x"></div>
439+
{{/each}}`,
426440
],
427441
invalid: [
428442
// blockParams on an element must not isolate static IDs from outer scope

0 commit comments

Comments
 (0)