@@ -77,22 +77,21 @@ module.exports = {
7777 function processReference ( reference , domFragment ) {
7878 const parent = reference . parent ;
7979 const isAccessed = parent . type === 'MemberExpression' && parent . object === reference ;
80- const property = isAccessed ? parent . property : null ;
80+ if ( ! isAccessed ) {
81+ return false ;
82+ }
83+ const property = parent . property ;
8184 const grandParent = parent . parent ;
82- const isPropertyAssignment =
83- isAccessed && grandParent . type === 'AssignmentExpression' && grandParent . left === parent ;
85+ const isPropertyAssignment = grandParent . type === 'AssignmentExpression' && grandParent . left === parent ;
8486 const propertyValue = isPropertyAssignment ? grandParent . right : null ;
85- const isMethodCall = isAccessed && grandParent . type === 'CallExpression' && grandParent . callee === parent ;
86- const firstArg = isMethodCall ? grandParent . arguments [ 0 ] : null ;
87- const secondArg = isMethodCall ? grandParent . arguments [ 1 ] : null ;
87+ const isMethodCall = grandParent . type === 'CallExpression' && grandParent . callee === parent ;
8888 const grandGrandParent = grandParent . parent ;
89- const isPropertyMethodCall = isAccessed && grandParent . type === 'MemberExpression' &&
90- grandParent . object === parent && grandGrandParent . type === 'CallExpression' &&
91- grandGrandParent . callee === grandParent ;
89+ const isPropertyMethodCall = grandParent . type === 'MemberExpression' && grandParent . object === parent &&
90+ grandGrandParent . type === 'CallExpression' && grandGrandParent . callee === grandParent ;
9291 const propertyMethodArgument = isPropertyMethodCall ? grandGrandParent . arguments [ 0 ] : null ;
93- const isSubpropertyAssignment = isAccessed && grandParent . type === 'MemberExpression' &&
94- grandParent . object === parent && grandParent . property . type === 'Identifier ' &&
95- grandGrandParent . type === 'AssignmentExpression' && grandGrandParent . left === grandParent ;
92+ const isSubpropertyAssignment = grandParent . type === 'MemberExpression' && grandParent . object === parent &&
93+ grandParent . property . type === 'Identifier' && grandGrandParent . type === 'AssignmentExpression ' &&
94+ grandGrandParent . left === grandParent ;
9695 const subproperty =
9796 isSubpropertyAssignment && grandParent . property . type === 'Identifier' ? grandParent . property : null ;
9897 const subpropertyValue = isSubpropertyAssignment ? grandGrandParent . right : null ;
@@ -102,6 +101,8 @@ module.exports = {
102101 return true ;
103102 }
104103 } else if ( isMethodCall ) {
104+ const firstArg = grandParent . arguments [ 0 ] ;
105+ const secondArg = grandParent . arguments [ 1 ] ;
105106 if ( isIdentifier ( property , 'addEventListener' ) ) {
106107 const event = getEvent ( firstArg ) ;
107108 const value = secondArg ;
@@ -136,19 +137,21 @@ module.exports = {
136137 if ( ! reference . processed ) {
137138 continue ;
138139 }
139- const expression = getEnclosingExpression ( reference . node ) ;
140- if ( ! expression ) {
140+ const range = getEnclosingExpression ( reference . node ) ?. range ;
141+ if ( ! range ) {
141142 continue ;
142143 }
143- const range = expression . range ;
144144 ranges . push ( range ) ;
145145 for ( const child of domFragment . children ) {
146146 ranges . push ( ...getRangesToRemove ( child ) ) ;
147147 }
148148 }
149149
150150 if ( domFragment . initializer && domFragment . references . every ( r => r . processed ) ) {
151- ranges . push ( getEnclosingExpression ( domFragment . initializer ) . range ) ;
151+ const range = getEnclosingExpression ( domFragment . initializer ) ?. range ;
152+ if ( range ) {
153+ ranges . push ( range ) ;
154+ }
152155 }
153156 for ( const range of ranges ) {
154157 while ( [ ' ' , '\n' ] . includes ( sourceCode . text [ range [ 0 ] - 1 ] ) ) {
@@ -180,15 +183,15 @@ module.exports = {
180183 const template = 'html`' + domFragment . toTemplateLiteral ( sourceCode ) . join ( '' ) + '`' ;
181184 let replacementLocation = domFragment . replacementLocation ;
182185
183- if ( replacementLocation . type === 'VariableDeclarator' ) {
184- domFragment . initializer = null ;
186+ if ( replacementLocation ? .type === 'VariableDeclarator' ) {
187+ domFragment . initializer = undefined ;
185188 return [
186189 fixer . replaceText ( replacementLocation . init , template ) ,
187190 ...getRangesToRemove ( domFragment ) . map ( range => fixer . removeRange ( range ) ) ,
188191 ] ;
189192 }
190193
191- if ( replacementLocation . parent . type === 'ExportNamedDeclaration' ) {
194+ if ( replacementLocation ? .parent ? .type === 'ExportNamedDeclaration' ) {
192195 replacementLocation = replacementLocation . parent ;
193196 }
194197 const text = `
0 commit comments