Skip to content

Commit 4f0db93

Browse files
committed
Fix pairity with template-lint
1 parent 6a11a73 commit 4f0db93

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

lib/rules/template-no-chained-this.js

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,46 @@ module.exports = {
2323
return {
2424
GlimmerPathExpression(node) {
2525
const text = sourceCode.getText(node);
26-
if (text.startsWith('this.this.')) {
27-
// Don't autofix if this PathExpression is the path of a BlockStatement,
28-
// because the closing tag wouldn't be updated and the template would break.
29-
const isBlockPath =
30-
node.parent?.type === 'GlimmerBlockStatement' && node.parent.path === node;
31-
32-
context.report({
33-
node,
34-
messageId: 'noChainedThis',
35-
fix: isBlockPath
36-
? undefined
37-
: (fixer) => {
38-
return fixer.replaceText(node, text.replace('this.this.', 'this.'));
39-
},
40-
});
26+
if (!text.startsWith('this.this.')) {
27+
return;
4128
}
29+
30+
const fixed = text.replace('this.this.', 'this.');
31+
32+
context.report({
33+
node,
34+
messageId: 'noChainedThis',
35+
fix(fixer) {
36+
const fixes = [fixer.replaceText(node, fixed)];
37+
38+
// Block statements have a closing tag path that must match
39+
const parent = node.parent;
40+
if (parent?.type === 'GlimmerBlockStatement' && parent.path === node) {
41+
const closingPathEnd = parent.range[1] - 2; // before '}}'
42+
const closingPathStart = closingPathEnd - text.length;
43+
fixes.push(fixer.replaceTextRange([closingPathStart, closingPathEnd], fixed));
44+
}
45+
46+
return fixes;
47+
},
48+
});
4249
},
4350
GlimmerElementNode(node) {
44-
if (node.tag?.startsWith('this.this.')) {
45-
context.report({
46-
node,
47-
messageId: 'noChainedThis',
48-
});
51+
if (!node.tag?.startsWith('this.this.')) {
52+
return;
4953
}
54+
55+
const fixedTag = node.tag.replace('this.this.', 'this.');
56+
57+
context.report({
58+
node,
59+
messageId: 'noChainedThis',
60+
fix(fixer) {
61+
// Replace the tag name after '<'
62+
const openStart = node.range[0] + 1;
63+
return fixer.replaceTextRange([openStart, openStart + node.tag.length], fixedTag);
64+
},
65+
});
5066
},
5167
};
5268
},

tests/lib/rules/template-no-chained-this.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ ruleTester.run('template-no-chained-this', rule, {
3434
},
3535
{
3636
code: '<template><this.this.Component /></template>',
37-
output: null,
37+
output: '<template><this.Component /></template>',
3838
errors: [{ messageId: 'noChainedThis' }],
3939
},
40-
41-
// Test cases ported from ember-template-lint
4240
{
4341
code: '<template>{{#this.this.value}}woo{{/this.this.value}}</template>',
44-
output: null,
42+
output: '<template>{{#this.value}}woo{{/this.value}}</template>',
4543
errors: [{ messageId: 'noChainedThis' }],
4644
},
4745
{

0 commit comments

Comments
 (0)