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

Reordering is breaking statements with side-effects. #34

Closed
mbostock opened this issue Jul 6, 2015 · 6 comments
Closed

Reordering is breaking statements with side-effects. #34

mbostock opened this issue Jul 6, 2015 · 6 comments

Comments

@mbostock
Copy link
Contributor

mbostock commented Jul 6, 2015

Here’s linear.js (abridged):

export function Linear() {};
Linear.prototype.lineStart = function() {};
Linear.prototype.lineEnd = function() {};
Linear.prototype.point = function(x, y) {};

export function LinearClosed() {};
LinearClosed.prototype = Object.create(Linear.prototype);
LinearClosed.prototype.lineEnd = function() {};

The resulting rolled-up file:

function Linear() {}
Linear.prototype.lineStart = function() {};
Linear.prototype.lineEnd = function() {};
Linear.prototype.point = function(x, y) {};

function LinearClosed() {}
LinearClosed.prototype.lineEnd = function() {};
LinearClosed.prototype = Object.create(Linear.prototype);

Note that the last two lines have been reversed, so the assignment to LinearClosed.prototype.lineEnd gets overwritten. This code is in d3-shape. I can work on creating an isolated test case if you need it.

@Rich-Harris
Copy link
Contributor

Thanks. No need for a more isolated test, I reckon this is small enough to work with - will look into it as soon as I get a chance. Not immediately sure what's causing it but am sure it's fixable (he said confidently...)

@Rich-Harris
Copy link
Contributor

@mbostock Turns out I'm struggling to reproduce this - all the d3-shape tests are passing for me, and the statement ordering looks to be correct. Has anything changed since this issue was opened?

@mbostock
Copy link
Contributor Author

mbostock commented Jul 8, 2015

Yeah, there’s a workaround I’ve been using.

I’ll try to pare down a standalone test case.

@mbostock
Copy link
Contributor Author

mbostock commented Jul 8, 2015

Here ya go:

https://gist.github.com/mbostock/bc1ffc78c3f933eff312

Note that if you reverse the order of statements in index.js, the test passes!

@Rich-Harris
Copy link
Contributor

Ah, I see what's going on here. The (new Foo) line causes the Foo definition to be included, which means also including Foo.prototype.test = ... and (because rollup can't know that Object.create doesn't modify Foo.prototype) Bar.prototype = Object.create(Foo.prototype). So the second line, which causes the Bar declaration to be included, doesn't include the Bar.prototype = statement because it's already included.

Not immediately sure what a general solution looks like (the last time I tried to naively sort statements according to their original order, it ended badly), but I'll try a few things. Suggestions welcome!

@Rich-Harris
Copy link
Contributor

Fixed in 0.9.0 - have confirmed all the d3-shape tests now pass without the workaround

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

No branches or pull requests

2 participants