Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Null guard all interpolated strings passed to _t #1035

Merged
merged 3 commits into from
Jun 6, 2017

Conversation

kegsay
Copy link
Member

@kegsay kegsay commented Jun 5, 2017

Comment should explain why.

Copy link
Member

@richvdh richvdh left a 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.

// 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. what's "" + here for? why would args[0] be truthy but not a string already?
  2. why are you even bothering with this? why not just if (args[1] && typeof args[1] === 'object') ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ""+ will stringify whatever args[0] is.
  • args[0] could be anything given it's truthy (an object, an array, a boolean) and only string has indexOf 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 touch args[1].

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

@richvdh richvdh assigned kegsay and unassigned richvdh Jun 5, 2017
@kegsay
Copy link
Member Author

kegsay commented Jun 5, 2017

Is it our intention to keep this forever?

Yes.

It seems like a shame to have to do this check every single time we do a translation

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.

a better solution is not to pass in null/undefined in the first place

If you want to add null guards to all interpolated values knock yourself out, I think it's a waste of time. null or undefined values are completely valid in ES6 template strings so we've never had a reason to check for them in the code previously. As a result, it's likely that there will be fun edge cases where they can be undefined resulting in the app crashing. Alternative suggestions welcome.

@richvdh
Copy link
Member

richvdh commented Jun 6, 2017

Performance is hardly going to be an issue here.

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.

@kegsay
Copy link
Member Author

kegsay commented Jun 6, 2017

warn when we're making this replacement

Done. Is there anything else for me to do here? Are we keeping the %( check or removing it?

@richvdh
Copy link
Member

richvdh commented Jun 6, 2017

Are we keeping the %( check or removing it?

I vote for removing it. Up to you at the end of the day, though.

LGTM otherwise.

@kegsay
Copy link
Member Author

kegsay commented Jun 6, 2017

Removed the check.

@kegsay kegsay merged commit 0b56d33 into develop Jun 6, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants