@@ -125,37 +125,22 @@ function peek(arr) {
125125}
126126
127127/**
128- * Gets the full object name for a MemberExpression or Identifier
129- * @param {import('estree').MemberExpression | import('estree').Identifier } node
130- * @returns {string }
128+ * Gets the root object name for a MemberExpression or Identifier.
129+ * @param {Node } node
130+ * @returns {string | undefined }
131131 */
132- function getFullObjectName ( node ) {
132+ function getRootObjectName ( node ) {
133133 if ( node . type === 'Identifier' ) {
134134 return node . name
135135 }
136- const objectName = getFullObjectName ( node . object )
137- const propertyName = node . property . name
138- return `${ objectName } ${ node . computed ? '[' : '.' } ${ propertyName } ${ node . computed ? ']' : '' } `
139- }
140-
141- /**
142- * Checks if the given name starts with an ignored variable
143- * @param {string } name
144- * @param {string[] } ignoredVars
145- * @returns {boolean }
146- */
147- function startsWithIgnoredVar ( name , ignoredVars ) {
148- return ignoredVars . some (
149- ( ignoredVar ) =>
150- name === ignoredVar ||
151- ( name . startsWith ( ignoredVar ) &&
152- ( name . charAt ( ignoredVar . length ) === '.' ||
153- name . charAt ( ignoredVar . length ) === '[' ) ) ,
154- )
136+ // istanbul ignore else (fallback)
137+ if ( node . type === 'MemberExpression' ) {
138+ return getRootObjectName ( node . object )
139+ }
155140}
156141
157142/**
158- * Checks if the node is an assignment to an ignored variable
143+ * Checks if the node is an assignment to an ignored variable. Use getRootObjectName to get the variable name.
159144 * @param {Node } node
160145 * @param {string[] } ignoredVars
161146 * @returns {boolean }
@@ -165,14 +150,8 @@ function isIgnoredAssignment(node, ignoredVars) {
165150 const expr = node . expression
166151 if ( expr . type !== 'AssignmentExpression' ) return false
167152 const left = expr . left
168- // istanbul ignore else
169- if ( left . type === 'MemberExpression' ) {
170- const fullName = getFullObjectName ( left . object )
171- return startsWithIgnoredVar ( fullName , ignoredVars )
172- }
173- // istanbul ignore next
174- // fallback
175- return false
153+ const rootName = getRootObjectName ( left )
154+ return ignoredVars . includes ( rootName )
176155}
177156
178157module . exports = {
@@ -210,8 +189,9 @@ module.exports = {
210189 const options = context . options [ 0 ] || { }
211190 const ignoreLastCallback = ! ! options . ignoreLastCallback
212191 const ignoreAssignmentVariable = options . ignoreAssignmentVariable || [
213- 'window ' ,
192+ 'globalThis ' ,
214193 ]
194+
215195 /**
216196 * @typedef {object } FuncInfo
217197 * @property {string[] } branchIDStack This is a stack representing the currently
@@ -306,13 +286,14 @@ module.exports = {
306286 return
307287 }
308288
309- if ( ignoreAssignmentVariable . length && isLastCallback ( node ) ) {
310- // istanbul ignore else
311- if ( node . body ?. type === 'BlockStatement' ) {
312- for ( const statement of node . body . body ) {
313- if ( isIgnoredAssignment ( statement , ignoreAssignmentVariable ) ) {
314- return
315- }
289+ if (
290+ ignoreAssignmentVariable . length &&
291+ isLastCallback ( node ) &&
292+ node . body ?. type === 'BlockStatement'
293+ ) {
294+ for ( const statement of node . body . body ) {
295+ if ( isIgnoredAssignment ( statement , ignoreAssignmentVariable ) ) {
296+ return
316297 }
317298 }
318299 }
0 commit comments