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

Workaround for #65. Prevents fatal error when a non-string literal require is in a try/catch block #66

Closed
wants to merge 1 commit into from

Conversation

TikiTDO
Copy link

@TikiTDO TikiTDO commented Oct 5, 2017

Summary

This the simple workaround for #65, as mentioned by @cpojer

Test plan

Process:

  1. Configured react-native to use attached code for extract-dependencies.js

  2. Added failing moment version 2.18.1 to project package.json

  3. Ran: react-native bundle --entry-file index.ios.js --platform ios --bundle-output /tmp/test.js

Result

Without fix - Bundle fails

With fix - Bundle succeeded. Resulting bundle file contains all necessary code.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 5, 2017
@cpojer
Copy link
Contributor

cpojer commented Oct 6, 2017

Oh, this makes the visitor significantly more complex and possibly slower. I was thinking that we could instead just look at the parent node and check whether the type is a try-block. Would that not work?

@TikiTDO
Copy link
Author

TikiTDO commented Oct 6, 2017

The way traversal works is that every step through the AST the type of each node is used as an index into a visitors object. In modern JS engines the difference between an object with 1 and 3 keys shouldn't actually change the performance of accessing said object, since both will internally be identically sized hashmaps. Benchmark.

There will be a minor performance cost each time it hits a function declaration or a try block, but similarly there is a performance cost if we need to walk up the tree to search for a try block. However, in both situations the performance cost of running the extra steps will be negligible compared even to the cost of the of calling the traverse the tree in the first place.

Given that, I feel the visitor approach is more future-proof, because I expect the visitor logic to get more complex as better solutions are thought of (especially if there is any level of interest of detecting when someone aliases the require method). That said, I'm not particularly attached to my approach either; it was just the most obvious way for me to do it.

@cpojer
Copy link
Contributor

cpojer commented Oct 6, 2017

I think this is overly complex for the problem we are trying to solve, and just looking at the direct parent of the require call is sufficient. We can make it more complex when we actually need to, and apply YAGNI (you aren't gonna need it) here :)

@TikiTDO
Copy link
Author

TikiTDO commented Oct 6, 2017

I'll take a look at this next week when I have some time. However I would note that perhaps the proper approach for the primary gatekeeper to using react-native should adopt an approach that attempts to preempt issues before they happen, especially when it comes to fatal errors in systems that may be mission critical to a myriad of other organizations.

@cpojer
Copy link
Contributor

cpojer commented Oct 7, 2017

See #69

@cpojer cpojer closed this Oct 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants