diff --git a/src/ast/nodes/shared/callHasEffects.js b/src/ast/nodes/shared/callHasEffects.js index ce48fc0..2c37442 100644 --- a/src/ast/nodes/shared/callHasEffects.js +++ b/src/ast/nodes/shared/callHasEffects.js @@ -70,6 +70,11 @@ export default function callHasEffects ( scope, callee, isNew ) { if ( fnHasEffects( node, isNew && isES5Function( node ) ) ) return true; } + else if ( /Class/.test( node.type ) ) { + // TODO find constructor (may belong to a superclass) + return true; + } + else if ( isReference( node ) ) { const flattened = flatten( node ); const declaration = scope.findDeclaration( flattened.name ); diff --git a/test/form/class-constructor-side-effect/_config.js b/test/form/class-constructor-side-effect/_config.js new file mode 100644 index 0000000..5c8a4a0 --- /dev/null +++ b/test/form/class-constructor-side-effect/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'preserves side-effects in a class constructor (#1233)' +}; diff --git a/test/form/class-constructor-side-effect/_expected/amd.js b/test/form/class-constructor-side-effect/_expected/amd.js new file mode 100644 index 0000000..1b939aa --- /dev/null +++ b/test/form/class-constructor-side-effect/_expected/amd.js @@ -0,0 +1,11 @@ +define(function () { 'use strict'; + + class Foo { + constructor () { + console.log( 'Foo' ); + } + } + + new Foo; + +}); diff --git a/test/form/class-constructor-side-effect/_expected/cjs.js b/test/form/class-constructor-side-effect/_expected/cjs.js new file mode 100644 index 0000000..ad3db87 --- /dev/null +++ b/test/form/class-constructor-side-effect/_expected/cjs.js @@ -0,0 +1,9 @@ +'use strict'; + +class Foo { + constructor () { + console.log( 'Foo' ); + } +} + +new Foo; diff --git a/test/form/class-constructor-side-effect/_expected/es.js b/test/form/class-constructor-side-effect/_expected/es.js new file mode 100644 index 0000000..705e350 --- /dev/null +++ b/test/form/class-constructor-side-effect/_expected/es.js @@ -0,0 +1,7 @@ +class Foo { + constructor () { + console.log( 'Foo' ); + } +} + +new Foo; diff --git a/test/form/class-constructor-side-effect/_expected/iife.js b/test/form/class-constructor-side-effect/_expected/iife.js new file mode 100644 index 0000000..a047aa4 --- /dev/null +++ b/test/form/class-constructor-side-effect/_expected/iife.js @@ -0,0 +1,12 @@ +(function () { + 'use strict'; + + class Foo { + constructor () { + console.log( 'Foo' ); + } + } + + new Foo; + +}()); diff --git a/test/form/class-constructor-side-effect/_expected/umd.js b/test/form/class-constructor-side-effect/_expected/umd.js new file mode 100644 index 0000000..c4409fe --- /dev/null +++ b/test/form/class-constructor-side-effect/_expected/umd.js @@ -0,0 +1,15 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory() : + typeof define === 'function' && define.amd ? define(factory) : + (factory()); +}(this, (function () { 'use strict'; + + class Foo { + constructor () { + console.log( 'Foo' ); + } + } + + new Foo; + +}))); diff --git a/test/form/class-constructor-side-effect/main.js b/test/form/class-constructor-side-effect/main.js new file mode 100644 index 0000000..705e350 --- /dev/null +++ b/test/form/class-constructor-side-effect/main.js @@ -0,0 +1,7 @@ +class Foo { + constructor () { + console.log( 'Foo' ); + } +} + +new Foo;