diff --git a/lib/coffee-script/nodes.js b/lib/coffee-script/nodes.js index ab54962e..5eeaa7c7 100644 --- a/lib/coffee-script/nodes.js +++ b/lib/coffee-script/nodes.js @@ -813,7 +813,7 @@ } if (this.isNew) { idt = this.tab + TAB; - return "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return typeof result === \"object\" ? result : child;\n" + this.tab + "})(" + (this.variable.compile(o, LEVEL_LIST)) + ", " + splatArgs + ", function() {})"; + return "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return typeof result === \"object\" && result !== null ? result : child;\n" + this.tab + "})(" + (this.variable.compile(o, LEVEL_LIST)) + ", " + splatArgs + ", function() {})"; } base = new Value(this.variable); if ((name = base.properties.pop()) && base.isComplex()) { diff --git a/src/nodes.coffee b/src/nodes.coffee index e90ba728..0037f690 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -587,7 +587,7 @@ exports.Call = class Call extends Base (function(func, args, ctor) { #{idt}ctor.prototype = func.prototype; #{idt}var child = new ctor, result = func.apply(child, args); - #{idt}return typeof result === "object" ? result : child; + #{idt}return typeof result === "object" && result !== null ? result : child; #{@tab}})(#{ @variable.compile o, LEVEL_LIST }, #{splatArgs}, function() {}) """ base = new Value @variable diff --git a/test/function_invocation.coffee b/test/function_invocation.coffee index d9018fe9..145f6ac1 100644 --- a/test/function_invocation.coffee +++ b/test/function_invocation.coffee @@ -392,6 +392,12 @@ test "#1011: passing a splat to a method of a number", -> eq '1011', (131.0).toString [5]... +test "splats and the `new` operator: functions that return `null` should produce their instance", -> + args = [] + child = new (constructor = -> null) args... + ok child instanceof constructor + + test "implicit return", -> eq ok, new ->