Skip to content

Commit c23fa18

Browse files
NullVoxPopuliclaude
andcommitted
Fix false positive: don't flag this in class component templates
The rule was incorrectly flagging `this` usage in ALL `<template>` tags, including those inside class bodies (class components). It should only flag `this` in template-only components (standalone `<template>` tags). Also adds missing BUILTIN_PROPERTIES from the original ember-template-lint rule: action, element, parentView, attrs, isDestroying, isDestroyed. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent d007394 commit c23fa18

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

lib/rules/template-no-this-in-template-only-components.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ module.exports = {
2424
create(context) {
2525
// Properties that should not be auto-fixed (built-in component properties)
2626
const BUILTIN_PROPERTIES = new Set([
27+
'action',
28+
'element',
29+
'parentView',
30+
'attrs',
2731
'elementId',
2832
'tagName',
2933
'ariaRole',
@@ -32,10 +36,26 @@ module.exports = {
3236
'classNameBindings',
3337
'attributeBindings',
3438
'isVisible',
39+
'isDestroying',
40+
'isDestroyed',
3541
]);
3642

3743
return {
3844
GlimmerPathExpression(node) {
45+
// Only flag template-only components, not class components.
46+
// Walk ancestors to check if the <template> is inside a class body.
47+
const sourceCode = context.sourceCode ?? context.getSourceCode();
48+
const ancestors = sourceCode.getAncestors
49+
? sourceCode.getAncestors(node)
50+
: context.getAncestors();
51+
const isInsideClass = ancestors.some(
52+
(ancestor) =>
53+
ancestor.type === 'ClassBody' || ancestor.type === 'ClassDeclaration'
54+
);
55+
if (isInsideClass) {
56+
return;
57+
}
58+
3959
// In gjs/gts files with <template> tags, check for this.* usage
4060
if (node.head?.type === 'ThisHead' && node.tail?.length > 0) {
4161
const original = node.original;

tests/lib/rules/template-no-this-in-template-only-components.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ ruleTester.run('template-no-this-in-template-only-components', rule, {
1212
'<template><WelcomePage /></template>',
1313
'<template><MyComponent @prop={{can "edit" @model}} /></template>',
1414
'<template>{{my-component model=model}}</template>',
15+
// Class components should not be flagged (not template-only)
16+
'class MyComponent extends Component { <template>{{this.foo}}</template> }',
17+
'class MyComponent extends Component { <template>{{this.bar}} {{this.baz}}</template> }',
1518
],
1619
invalid: [
1720
{

0 commit comments

Comments
 (0)