-
-
Notifications
You must be signed in to change notification settings - Fork 831
Null guard all interpolated strings passed to _t #1035
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment at element-hq/element-web#4195 (comment): if the problem is about missing properties (as the description on that PR implies), this does nothing to fix it.
Is it our intention to keep this forever? It seems like a shame to have to do this check every single time we do a translation, and a better solution is not to pass in null/undefined in the first place.
src/languageHandler.js
Outdated
// if there are no existing null guards. To avoid this making the app completely inoperable, | ||
// we'll check all the values for undefined/null and stringify them here. | ||
if (args[0]) { | ||
const isInterpolation = ("" + args[0]).indexOf("%(") !== -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- what's
"" +
here for? why wouldargs[0]
be truthy but not a string already? - why are you even bothering with this? why not just
if (args[1] && typeof args[1] === 'object')
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
""+
will stringify whateverargs[0]
is.args[0]
could be anything given it's truthy (an object, an array, a boolean) and onlystring
hasindexOf
so it'll crash if you try to call that on anything else.- Annoyingly
counterpart
changes its behaviour depending on what args you pass it. The%(
check is an attempt to detect when we're using it for interpolating strings, so we don't needlessly touchargs[1]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, but do we ever actually use any of those alternative behaviours? Feels to me like the check for %(
is costing more than it buys.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if we use those alternative behaviours, as we call this function in a bazillion places so I can't reasonably check them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, but what's the worst case? I can't imagine a case where if (args[1] && typeof args[1] === 'object')
will be true, and replacing the null/undef members will be harmful.
Yes.
It needs to be done either here or further up the stack in the actual code. Performance is hardly going to be an issue here.
If you want to add null guards to all interpolated values knock yourself out, I think it's a waste of time. |
uhhh... debatable. My only other suggestion (which apparently I forgot to write) would be to warn when we're making this replacement, so that we can try to fix them further up the stack. I agree it's marginal though. Also: tests have failed, though I suspect it's not your fault. |
Done. Is there anything else for me to do here? Are we keeping the |
I vote for removing it. Up to you at the end of the day, though. LGTM otherwise. |
Removed the check. |
Comment should explain why.