Skip to content

Bugfix/require module regexp #368

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

Closed

Conversation

daviskoh
Copy link
Contributor

Resolves #316. Also updated the spec for the Haste Dependency Resolver. Not sure if these changes are the ones desired so feedback would be welcome!

@amasad
Copy link
Contributor

amasad commented Mar 28, 2015

Awesome, thanks!

@tadeuzagallo
Copy link
Contributor

I think the regex should be

/\brequire\s*?\(\s*?([\'"])([^"\']+)\1\s*?\)/g

and then

code.replace(REL_REQUIRE_STMT, function(codeMatch, _, depName) {

Even before this diff it already accepted require('module")
And \s* has a very poor performance when you expect \s to be very small, \s*? should perform much better.

@daviskoh
Copy link
Contributor Author

👍 updated

@amasad
Copy link
Contributor

amasad commented Mar 28, 2015

If you updated this, let's update the one in DependencyGraph/index.js as well. Ideally let's export it from DependencyGraph as a a constant to be used here

@daviskoh
Copy link
Contributor Author

Hmm. Maybe I am missing something, but I am facing some issues when I try to export the requireRe regex from DependencyGraph and into DependencyResolver.

I changed the requireRe in the DependencyGraph to

var requireRe = /\brequire\s*?\(\s*?([\'"])([^"\']+)\1\s*?\)/g;

Then changed the exports

exports.requireRe = requireRe;
exports.graph = DependecyGraph;

Then in the DependencyResolver/index.js

var DependencyGraph = require('./DependencyGraph').graph;
var requireRe = require('./DependencyGraph').requireRe;
console.log(requireRe);

Jest output
jest

The requireRe regex does not seem to be importing properly as you can see in the Jest output. I will keep digging into this. Any help would be appreciated as well!

@daviskoh
Copy link
Contributor Author

I changed the requireRe in the DependencyGraph to match REL_REQUIRE_STMT.

As noted above, I was having issues when I tried to import regex into the DependencyResolver. As @amasad said, ideally we would export the const, however in the meantime this should provide a fix for the whitespace issues.

I can submit another PR later exporting the requireRe when I can look at this code more objectively than I can now. What do you think?

@amasad
Copy link
Contributor

amasad commented Mar 28, 2015

That's strange, maybe the problem is with jest console.log. Can you try console.log(requireRe.toString())?

@daviskoh
Copy link
Contributor Author

This is the output from DependencyResolver/index.js:

screen shot 2015-03-28 at 5 22 50 pm

It does not seem limited to importing from DependencyGraph either. I tried creating a dummy file that exports only that regex and I get the same issue. I also tried exporting some simple regex:

/\s+/g

but it seems to get imported as

/(?:)/

as well. T.T

@daviskoh
Copy link
Contributor Author

It does not seem to be an issue w/ the console.log of jest because when I

console.log(requireRe);

right after it is declared within DependencyGraph, the output is:

requirere

* don't mock regex.js in jest tests
@daviskoh
Copy link
Contributor Author

The issue was related to how Jest seems to auto-mock its dependencies (though I am not too familiar w/ Jest). When I tried to export the regex, jest would try to mock it and resulting in that strange looking regex that broke unit tests (even when I tried mocking it as a function that returns the regex). However, I tested the regex exporting manually and it was working just fine.

I decided that the regex value should not be mocked in the unit tests. However, keeping this value inside the DependencyGraph/index.js was problematic as we still want to mock the rest of the DependencyGraph when testing the HasteDependencyResolver. Thus, I decided to move the regex value into a separate file that gets imported by both DependencyGraph/index.js & DependencyResolver/index.js allowing me to prevent the value from being mocked using jest.dontMock.

I named it regex.js as I thought there may be other regex where sharing across files is desired. The shared regex is now successfully exported and the unit tests are now passing.

@amasad thoughts?

@pilwon
Copy link
Contributor

pilwon commented Mar 31, 2015

Related to #386

@amasad
Copy link
Contributor

amasad commented Mar 31, 2015

That looks good, I'll pull it.

@daviskoh
Copy link
Contributor Author

👍

@pilwon
Copy link
Contributor

pilwon commented Apr 1, 2015

Do we really need to force convert quotation style (' or ") to single quotations when we relativize code? Wouldn't it be better we preserve whatever style developer chose?

@amasad
Copy link
Contributor

amasad commented Apr 1, 2015

@pilwon why do you care about generated code?

@pilwon
Copy link
Contributor

pilwon commented Apr 1, 2015

@amasad I agree it doesn't matter for code that is never meant to be read by humans.

I imagined more potential complex case similar to and developed from how I implemented #386. The original code passes through multiple replace steps until it reaches the final relativized code. The complexity in this replace pipeline could potentially grow (though it's highly unlikely beyond supporting import syntaxes for now) and it may be a better design choice to replace only whatever's absolutely necessary in each replace step. Either choice definitely works fine but forced conversion of quotation style didn't seem like a necessary one for me. (I'm probably overthinking this...)

@amasad
Copy link
Contributor

amasad commented Apr 1, 2015

OK, this already landed internally though. @pilwon you can fix it in your "import" syntax pull requests because I haven't looked at it yet.

@daviskoh
Copy link
Contributor Author

daviskoh commented Apr 1, 2015

Hmm...that explains the merge conflicts lol. I guess there must be a problem w/ Legal when taking PR's from random internet guys ☔ hehe

@pilwon
Copy link
Contributor

pilwon commented Apr 1, 2015

@daviskoh Related info here

@amasad I'm not sure how to rebase your internal code onto PR #386. The branch must have been diverged since this PR and my PR touched the same files. When you bring the changes to your internal code, is it usually a manual process? Should I wait until this PR makes its way back to react-native? (Updated PR #386)

@sahrens
Copy link
Contributor

sahrens commented Apr 1, 2015

Merged internally and synced.

@sahrens sahrens closed this Apr 1, 2015
vjeux pushed a commit to vjeux/react-native that referenced this pull request Apr 13, 2015
Summary:
Resolves facebook#316. Also updated the spec for the Haste Dependency Resolver. Not sure if these changes are the ones desired so feedback would be welcome!
Closes facebook#368
Github Author: daviskoh <koh.davis.0@gmail.com>

Test Plan: ./runJestTests
vjeux pushed a commit to vjeux/react-native that referenced this pull request Apr 14, 2015
Summary:
Resolves facebook#316. Also updated the spec for the Haste Dependency Resolver. Not sure if these changes are the ones desired so feedback would be welcome!
Closes facebook#368
Github Author: daviskoh <koh.davis.0@gmail.com>

Test Plan: ./runJestTests
vjeux pushed a commit to vjeux/react-native that referenced this pull request Apr 15, 2015
Summary:
Resolves facebook#316. Also updated the spec for the Haste Dependency Resolver. Not sure if these changes are the ones desired so feedback would be welcome!
Closes facebook#368
Github Author: daviskoh <koh.davis.0@gmail.com>

Test Plan: ./runJestTests
cpojer pushed a commit to facebook/metro that referenced this pull request Jan 26, 2017
Summary:
Resolves facebook/react-native#316. Also updated the spec for the Haste Dependency Resolver. Not sure if these changes are the ones desired so feedback would be welcome!
Closes facebook/react-native#368
Github Author: daviskoh <koh.davis.0@gmail.com>

Test Plan: ./runJestTests
jfrolich pushed a commit to jfrolich/react-native that referenced this pull request Apr 22, 2020
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.

Require function whitespace problem.
5 participants