@@ -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 } ,
0 commit comments