Skip to content

Commit b190d50

Browse files
committed
Fix template-no-log: add isLocal check for block params
Prevent false positives when log is a locally-scoped block parameter. The original ember-template-lint rule checks isLocal before reporting; this was missing from the extraction. Also adds block-param scoping tests.
1 parent e20ba32 commit b190d50

2 files changed

Lines changed: 99 additions & 3 deletions

File tree

lib/rules/template-no-log.js

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,32 @@ module.exports = {
1515
},
1616

1717
create(context) {
18+
const localScopes = [];
19+
20+
function pushLocals(params) {
21+
localScopes.push(new Set(params || []));
22+
}
23+
24+
function popLocals() {
25+
localScopes.pop();
26+
}
27+
28+
function isLocal(name) {
29+
for (const scope of localScopes) {
30+
if (scope.has(name)) {
31+
return true;
32+
}
33+
}
34+
return false;
35+
}
36+
1837
function checkForLog(node) {
19-
if (node.path && node.path.type === 'GlimmerPathExpression' && node.path.original === 'log') {
38+
if (
39+
node.path &&
40+
node.path.type === 'GlimmerPathExpression' &&
41+
node.path.original === 'log' &&
42+
!isLocal('log')
43+
) {
2044
context.report({
2145
node,
2246
messageId: 'unexpected',
@@ -25,11 +49,30 @@ module.exports = {
2549
}
2650

2751
return {
28-
GlimmerMustacheStatement(node) {
52+
GlimmerBlockStatement(node) {
53+
if (node.program && node.program.blockParams) {
54+
pushLocals(node.program.blockParams);
55+
}
2956
checkForLog(node);
3057
},
58+
'GlimmerBlockStatement:exit'(node) {
59+
if (node.program && node.program.blockParams) {
60+
popLocals();
61+
}
62+
},
3163

32-
GlimmerBlockStatement(node) {
64+
GlimmerElementNode(node) {
65+
if (node.blockParams && node.blockParams.length > 0) {
66+
pushLocals(node.blockParams);
67+
}
68+
},
69+
'GlimmerElementNode:exit'(node) {
70+
if (node.blockParams && node.blockParams.length > 0) {
71+
popLocals();
72+
}
73+
},
74+
75+
GlimmerMustacheStatement(node) {
3376
checkForLog(node);
3477
},
3578
};

tests/lib/rules/template-no-log.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ ruleTester.run('template-no-log', rule, {
2828
`<template>
2929
<div data-test-log={{true}}></div>
3030
</template>`,
31+
// Block param: log as a yielded value should not be flagged
32+
`<template>
33+
{{#each this.logs as |log|}}{{log}}{{/each}}
34+
</template>`,
35+
`<template>
36+
{{#let this.log as |log|}}{{log}}{{/let}}
37+
</template>`,
38+
`<template>
39+
{{#let (component "my-log-component") as |log|}}{{#log}}message{{/log}}{{/let}}
40+
</template>`,
41+
`<template>
42+
<Logs @logs={{this.logs}} as |log|>{{log}}</Logs>
43+
</template>`,
44+
`<template>
45+
<Logs @logs={{this.logs}} as |log|><Log>{{log}}</Log></Logs>
46+
</template>`,
3147
],
3248

3349
invalid: [
@@ -71,5 +87,42 @@ ruleTester.run('template-no-log', rule, {
7187
},
7288
],
7389
},
90+
// log helper used inside a block that does NOT shadow it
91+
{
92+
code: `<template>
93+
{{#each this.messages as |message|}}{{log message}}{{/each}}
94+
</template>`,
95+
output: null,
96+
errors: [
97+
{
98+
message: 'Unexpected log statement in template.',
99+
type: 'GlimmerMustacheStatement',
100+
},
101+
],
102+
},
103+
{
104+
code: `<template>
105+
{{#let this.message as |message|}}{{log message}}{{/let}}
106+
</template>`,
107+
output: null,
108+
errors: [
109+
{
110+
message: 'Unexpected log statement in template.',
111+
type: 'GlimmerMustacheStatement',
112+
},
113+
],
114+
},
115+
{
116+
code: `<template>
117+
<Messages @messages={{this.messages}} as |message|>{{#log}}{{message}}{{/log}}</Messages>
118+
</template>`,
119+
output: null,
120+
errors: [
121+
{
122+
message: 'Unexpected log statement in template.',
123+
type: 'GlimmerBlockStatement',
124+
},
125+
],
126+
},
74127
],
75128
});

0 commit comments

Comments
 (0)