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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/createClassProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ function getDisplayName(Component) {
'Unknown';
}

// This is a simple way to sanitize the component name.
// It will prevent an exception if the component happens to use a name
// that is also not a valid identifier.
const SIMPLE_IDENT_PATTERN = /^[$\w]+$/
const SIMPLE_IDENT_FIX = /[^$\w]+/g

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.

return fn.replace(SIMPLE_IDENT_FIX, '_');
}

// This was originally a WeakMap but we had issues with React Native:
// https://github.com/gaearon/react-proxy/issues/50#issuecomment-192928066
let allProxies = [];
Expand Down Expand Up @@ -76,10 +89,11 @@ function proxyClass(InitialComponent) {
}

let displayName = getDisplayName(InitialComponent);
let componentName = sanitizeFunctionName(displayName);
try {
// Create a proxy constructor with matching name
ProxyComponent = new Function('factory', 'instantiate',
`return function ${displayName}() {
`return function ${componentName}() {
return instantiate(factory, this, arguments);
}`
)(() => CurrentComponent, instantiate);
Expand Down