Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Add support for custom factory methods #10

Merged
merged 7 commits into from
Sep 23, 2015

Conversation

aaronjensen
Copy link
Contributor

Fixes #8

@aaronjensen aaronjensen force-pushed the configurable-factory-methods branch 2 times, most recently from b4afb12 to dec29db Compare September 5, 2015 15:07
@aaronjensen
Copy link
Contributor Author

@gaearon rebased on master and fixed the test, lmk if there are any tweaks you'd like me to make.

@aaronjensen
Copy link
Contributor Author

@gaearon hm any love on this?

@aaronjensen aaronjensen force-pushed the configurable-factory-methods branch from dec29db to 25e8ec0 Compare September 18, 2015 02:24
@gaearon
Copy link
Owner

gaearon commented Sep 19, 2015

Apologies for dragging my feet on this. I got sick and had a lot of catching up to do.
Looking into this now.

@aaronjensen
Copy link
Contributor Author

No problem, hope you are feeling better! Thanks. 

Aaron

On Sat, Sep 19, 2015 at 8:36 AM, Dan Abramov notifications@github.com
wrote:

Apologies for dragging my feet on this. I got sick and had a lot of catching up to do.

Looking into this now.

Reply to this email directly or view it on GitHub:
#10 (comment)

@gaearon
Copy link
Owner

gaearon commented Sep 19, 2015

There is in fact something else which is tangential to this PR but I'd love to see it here.

Currently we run all test cases with the same config, which is a problem for testing different configurations. For example, we can't test that the same code is treated differently with different configurations.

What I'd like to see is Babel config being moved into separate .babelrc files in each fixture folder, which would be read by the test runner. This way we can check different configurations easily.

Would you be willing to add this?

function isCreateClassCallExpression(node, factoryMethods) {
for (const method of factoryMethods) {
if (method.indexOf('.') !== -1) {
if (t.buildMatchMemberExpression(method)(node.callee)) {
Copy link
Owner

Choose a reason for hiding this comment

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

This appears to have worse performance. (I haven't tested but the code looks so.) The point of buildMatchMemberExpression is to build the matching function and save it. However in this code we build the matching function on every invocation.

I think what we really need here is buildMatchCreateClassCallExpression(factoryMethods) which would return a node-accepting function. This way we should be able to create (and memoize) the matching function once per plugin invocation. Does this make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's definitely faster to pre-build it, though I'm not sure how much the difference matters when just about everything is still in the single digit microsecond or faster range:

build inline x 2,421,358 ops/sec ±1.20% (91 runs sampled)
prebuilt x 11,400,386 ops/sec ±1.20% (92 runs sampled)
Fastest is prebuilt
build inline x 2,678,951 ops/sec ±1.01% (95 runs sampled)
prebuilt x 11,239,894 ops/sec ±1.51% (90 runs sampled)
Fastest is prebuilt
build inline x 2,634,932 ops/sec ±1.24% (94 runs sampled)
prebuilt x 10,768,092 ops/sec ±1.17% (94 runs sampled)
Fastest is prebuilt
build inline x 296,499 ops/sec ±1.74% (88 runs sampled)
prebuilt x 2,769,726 ops/sec ±0.94% (92 runs sampled)
Fastest is prebuilt
build inline x 1,382,369 ops/sec ±0.97% (93 runs sampled)
prebuilt x 2,686,110 ops/sec ±1.29% (93 runs sampled)
Fastest is prebuilt
build inline x 2,558,054 ops/sec ±1.10% (88 runs sampled)
prebuilt x 11,474,870 ops/sec ±1.19% (93 runs sampled)
Fastest is prebuilt
build inline x 2,553,266 ops/sec ±1.39% (95 runs sampled)
prebuilt x 10,736,413 ops/sec ±1.36% (91 runs sampled)
Fastest is prebuilt
build inline x 1,379,023 ops/sec ±1.25% (93 runs sampled)
prebuilt x 2,820,422 ops/sec ±1.03% (94 runs sampled)
Fastest is prebuilt

I'll tweak it to prebuild though, may as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aaronjensen
Copy link
Contributor Author

Would you be willing to add this?

Sure! ad8fb28

@aaronjensen
Copy link
Contributor Author

@gaearon All feedback addressed, let me know if there's anything else.

const factoryMethods = options.factoryMethods || ['React.createClass', 'createClass'];
this.state[optionsKey] = options;
this.state[cacheKey] = {
isCreateClassCallExpression: buildIsCreateClassCallExpression(factoryMethods),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted to build the matchers up front instead of memoizing them off of the method name. It seemed a little simpler/more efficient in the common case.

@gaearon
Copy link
Owner

gaearon commented Sep 21, 2015

This is good, thank you. Will merge later today.

@aaronjensen
Copy link
Contributor Author

@gaearon just merged master in to get back up to date, it was getting too painful to rebase all of the commits w/ all the conflicting readme changes. This still good to merge?

@gaearon
Copy link
Owner

gaearon commented Sep 23, 2015

Let's add a backward compatibility mode. I feel like changing all examples in boilerplates and READMEs etc is going to be a world of royal pain. I'd say let's allow array as a configuration, but print something like Warning: you're using an outdated format of React Transform configuration. Please update your configuration to the new format. See README for details: https://github.com/gaearon/babel-plugin-react-transform.

@aaronjensen
Copy link
Contributor Author

Do we still want to rename target to transform?

@gaearon
Copy link
Owner

gaearon commented Sep 23, 2015

Meh, let's keep target then, it's not too bad.

@aaronjensen
Copy link
Contributor Author

Ok, added backwards compat mode, including the transform/target rename (got your msg too late). Let me know what you think or if you'd like me to remove the rename.

@gaearon
Copy link
Owner

gaearon commented Sep 23, 2015

What do you think is better? I think target was a bad choice because it doesn't really explain anything.

@aaronjensen
Copy link
Contributor Author

Yeah, I didn't really get target either. I'd consider target to mean the files that it'd transform, not the transform it'd use. I'm good w/ the rename. IMO release a new major, remove backwards compat and release another major (maybe not all today 😄)

@gaearon
Copy link
Owner

gaearon commented Sep 23, 2015

I think the right way is to put support for this in a minor, wait for people to migrate, then update the docs everywhere, and then release a major.

@aaronjensen
Copy link
Contributor Author

yeah that sounds like a better idea. Works for me.

gaearon added a commit that referenced this pull request Sep 23, 2015
@gaearon gaearon merged commit f2a61e6 into gaearon:master Sep 23, 2015
@gaearon
Copy link
Owner

gaearon commented Sep 23, 2015

This is out in 1.1.0, thank you very much.

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