Fix TypeError: mw.user.isNamed is not a function

This is happening due to operator precedence. Taking a simpler
example:

  return i || typeof i === 'function' ? i() : (i + 3); where i is 1

Explanation from ChatGPT:

* First, the typeof operator is evaluated for i. Since i is 1, typeof i
returns the string 'number'. The result of this operation is `'number'.
* Then, the === operator is applied to 'number' and 'function'. The
result is false because these two strings are not equal.
* Now, we have the logical OR (||) operator. The i (which is 1) is
compared to the result of the typeof and === operations combined, which
is false. Since 1 is considered truthy, the || operation results in 1.
* After the || operation, we have the conditional (ternary) operator ? :.
In this case, the condition to the left of ? is 1, which is truthy, so
the expression after ? is evaluated.
* i() is the expression after ?. Since i is not a function, and the
condition to the left is truthy, it attempts to call i() but throws
a "TypeError: i is not a function" error.

Fiddle that demonstrates the issue: https://jsfiddle.net/t2v1w6ae/2/

Bug: T347847
Change-Id: If5e8405141dfafd53c4495c91265545ec01acab3
This commit is contained in:
Abijeet
2023-10-27 19:42:37 +05:30
committed by jenkins-bot
parent 3524de960d
commit d413fc2721

View File

@@ -196,7 +196,7 @@
function userCanChangeLanguage() {
return mw.config.get( 'wgULSAnonCanChangeLanguage' ) ||
// mw.user.isNamed() added in MW 1.40. Remove after MLEB drop support for MW < 1.40
typeof mw.user.isNamed === 'function' ? mw.user.isNamed() : !mw.user.isAnon();
( typeof mw.user.isNamed === 'function' ? mw.user.isNamed() : !mw.user.isAnon() );
}
/**