Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize proxy component name to minimize exceptions. #58

Closed
wants to merge 1 commit into from

Conversation

leidegre
Copy link

Sanitize the display name to be able to act as a component name without causing issues if display name is not a valid identifier. This happens all the time with Redux components.

It's a very simple test that is more aggressive than the JavaScript identifier rules but it is also very simple.

Here's a Fiddle with some tests https://jsfiddle.net/rzyfr1hz/

J

function sanitizeFunctionName(fn) {
if (SIMPLE_IDENT_PATTERN.test(fn)) {
return isNaN(parseInt(fn)) ? fn : '_' + fn;
}
Copy link
Owner

Choose a reason for hiding this comment

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

Nitpick: too many spaces before return.

@gaearon
Copy link
Owner

gaearon commented Apr 25, 2016

Thanks! Can you please add some tests? I think displayName tests are in consistency.js.

@leidegre
Copy link
Author

I'll fix that together with the spaces later today.

@leidegre
Copy link
Author

I took a look at the things you mentioned.

  • The code is covered by existing tests (not every edge case).
  • There's no way to tell/assert (in it's current form whether these exceptions were thrown or not)

I'd much rather like to provide the function as a utility and tests that in isolation. Just to ensure that its behavior is as it should. I'm not overly familiar with the source code but from what I can tell though, the component name is overshadowed anyway.

try {
  Object.defineProperty(ProxyComponent, 'name', {
    value: displayName
  });
} catch (err) { }

Unless I'm missing something. This is only really necessary if we can't create a dynamic constructor and defineProperty 'name' does not work which seems like the most spectacular edge case. At this point, do we really care that we didn't set a component name for the proxy? Wouldn't just be simpler to do this.

let displayName = getDisplayName(InitialComponent);
ProxyComponent = function () {
  return instantiate(() => CurrentComponent, this, arguments);
};
try {
  Object.defineProperty(ProxyComponent, 'name', {
    value: displayName
  });
} catch (err) {
  // ...
}

Please correct me if I'm off here.

@gaearon
Copy link
Owner

gaearon commented Apr 26, 2016

if we can't create a dynamic constructor

We have a test for this. Let’s just give that function a weird name.

@leidegre
Copy link
Author

@gaearon like this #59 ?

@leidegre leidegre closed this Apr 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants