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

new Class with side effects erroneously removed #1233

Closed
kzc opened this issue Jan 6, 2017 · 5 comments
Closed

new Class with side effects erroneously removed #1233

kzc opened this issue Jan 6, 2017 · 5 comments

Comments

@kzc
Copy link
Contributor

kzc commented Jan 6, 2017

Input

$ cat new.js

class Foo {
    constructor() {
        console.log("Foo");
    }
}
new Foo;

Expected output

$ node new.js

Foo

rollup@0.41.0 output

$ bin/rollup new.js -f es | node

⚠️   Generated an empty bundle

Suggested fix

--- a/src/ast/nodes/NewExpression.js
+++ b/src/ast/nodes/NewExpression.js
@@ -2,7 +2,7 @@ import Node from '../Node.js';
 import callHasEffects from './shared/callHasEffects.js';
 
 export default class NewExpression extends Node {
-       hasEffects ( scope ) {
-               return callHasEffects( scope, this.callee, true );
+       hasEffects () {
+               return true;
        }
 }

Rollup output with fix

$ bin/rollup new.js -f es

class Foo {
    constructor() {
        console.log("Foo");
    }
}
new Foo;

$ bin/rollup new.js -f es | node

Foo

A few existing rollup tests with incorrect results would have to be updated.

@kzc
Copy link
Contributor Author

kzc commented Jan 6, 2017

Related bug: #980

@Rich-Harris
Copy link
Contributor

Ideally we'd be able to determine that the function to test for side-effects is the constructor. In the shorter term, what if we just include all new expressions where the callee is a class? #1234

@kzc
Copy link
Contributor Author

kzc commented Jan 6, 2017

The fix I suggested was a bit drastic because of the seriousness of the error. But if you want to refine it for side effects, by all means. The related bug #980 had a side effect in the super constructor.

Rich-Harris added a commit that referenced this issue Jan 6, 2017
include new expressions where callee is a class
@kzc
Copy link
Contributor Author

kzc commented Jan 6, 2017

what if we just include all new expressions where the callee is a class?

Even if the callee is a function we still want the new Func included if it has a side effect.

@Rich-Harris
Copy link
Contributor

That already happens, it's only classes that were behaving badly.

About to leave my desk so will cut a release with the fix and we can address the false positives another time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants