diff --git a/lib/cake.js b/lib/cake.js index 2fbf2d88..9908284d 100755 --- a/lib/cake.js +++ b/lib/cake.js @@ -43,7 +43,7 @@ CoffeeScript.run(fs.readFileSync('Cakefile').toString(), { fileName: 'Cakefile' }); - oparse = new optparse.OptionParser(switches); + oparse = new (optparse.OptionParser)(switches); if (!(args.length)) { return printTasks(); } diff --git a/lib/command.js b/lib/command.js index eb67b4d6..2e2b1b0d 100644 --- a/lib/command.js +++ b/lib/command.js @@ -219,7 +219,7 @@ }; parseOptions = function() { var o; - optionParser = new optparse.OptionParser(SWITCHES, BANNER); + optionParser = new (optparse.OptionParser)(SWITCHES, BANNER); o = (opts = optionParser.parse(process.argv.slice(2, process.argv.length))); o.compile || (o.compile = (!!o.output)); o.run = !(o.compile || o.print || o.lint); diff --git a/lib/grammar.js b/lib/grammar.js index b58e536e..415b7618 100644 --- a/lib/grammar.js +++ b/lib/grammar.js @@ -398,9 +398,9 @@ ], Loop: [ o("LOOP Block", function() { - return new WhileNode(new LiteralNode('true')).addBody($2); + return (new WhileNode(new LiteralNode('true'))).addBody($2); }), o("LOOP Expression", function() { - return new WhileNode(new LiteralNode('true')).addBody(Expressions.wrap([$2])); + return (new WhileNode(new LiteralNode('true'))).addBody(Expressions.wrap([$2])); }) ], For: [ diff --git a/lib/nodes.js b/lib/nodes.js index 793118b5..ed5579a9 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -481,7 +481,7 @@ })()); }; CallNode.prototype.compileNode = function(o) { - var _b, _c, _d, _e, _f, _g, _h, arg, args, compilation; + var _b, _c, _d, _e, _f, _g, _h, arg, args, compilation, value; if (!(o.chainRoot)) { o.chainRoot = this; } @@ -504,7 +504,17 @@ } return _e; }).call(this); - compilation = this.isSuper ? this.compileSuper(args.join(', '), o) : ("" + (this.prefix()) + (this.variable.compile(o)) + "(" + (args.join(', ')) + ")"); + compilation = (function() { + if (this.isSuper) { + return this.compileSuper(args.join(', '), o); + } else { + value = this.variable.compile(o); + if (this.isNew && this.variable instanceof ValueNode && this.variable.hasProperties()) { + value = ("(" + (value) + ")"); + } + return "" + (this.prefix()) + (value) + "(" + (args.join(', ')) + ")"; + } + }).call(this); } return compilation; }; @@ -876,7 +886,7 @@ })) + ';'; props = !props.empty() ? '\n' + props.compile(o) : ''; extension = extension ? '\n' + this.idt() + extension.compile(o) + ';' : ''; - returns = this.returns ? '\n' + new ReturnNode(this.variable).compile(o) : ''; + returns = this.returns ? '\n' + (new ReturnNode(this.variable)).compile(o) : ''; return construct + extension + props + returns; }; return ClassNode; @@ -980,7 +990,7 @@ } val = new ValueNode(literal(valVar), [new accessClass(idx)]); } - assigns.push(new AssignNode(obj, val).compile(o)); + assigns.push((new AssignNode(obj, val)).compile(o)); } code = assigns.join("\n"); return code; @@ -1244,7 +1254,7 @@ this.body = Expressions.wrap([new IfNode(this.guard, this.body)]); } if (this.returns) { - post = '\n' + new ReturnNode(literal(rvar)).compile(merge(o, { + post = '\n' + (new ReturnNode(literal(rvar))).compile(merge(o, { indent: this.idt() })); } else { @@ -1581,7 +1591,7 @@ }; ForNode.prototype.compileReturnValue = function(val, o) { if (this.returns) { - return '\n' + new ReturnNode(literal(val)).compile(o); + return '\n' + (new ReturnNode(literal(val))).compile(o); } if (val) { return '\n' + val; @@ -1630,7 +1640,7 @@ svar = scope.freeVariable(); sourcePart = ("" + (svar) + " = " + (this.source.compile(o)) + ";"); if (this.pattern) { - namePart = new AssignNode(this.name, literal("" + (svar) + "[" + (ivar) + "]")).compile(merge(o, { + namePart = (new AssignNode(this.name, literal("" + (svar) + "[" + (ivar) + "]"))).compile(merge(o, { indent: this.idt(1), top: true })) + '\n'; diff --git a/src/grammar.coffee b/src/grammar.coffee index eb33dc9e..fe4a7a94 100644 --- a/src/grammar.coffee +++ b/src/grammar.coffee @@ -429,8 +429,8 @@ grammar = ] Loop: [ - o "LOOP Block", -> new WhileNode(new LiteralNode 'true').addBody $2 - o "LOOP Expression", -> new WhileNode(new LiteralNode 'true').addBody Expressions.wrap [$2] + o "LOOP Block", -> (new WhileNode(new LiteralNode 'true')).addBody $2 + o "LOOP Expression", -> (new WhileNode(new LiteralNode 'true')).addBody Expressions.wrap [$2] ] # Array, object, and range comprehensions, at the most generic level. diff --git a/src/nodes.coffee b/src/nodes.coffee index 00f3fb70..0f8bedc7 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -449,7 +449,9 @@ exports.CallNode = class CallNode extends BaseNode compilation = if @isSuper @compileSuper(args.join(', '), o) else - "#{@prefix()}#{@variable.compile(o)}(#{ args.join(', ') })" + value = @variable.compile o + value = "(#{value})" if @isNew and @variable instanceof ValueNode and @variable.hasProperties() + "#{@prefix()}#{value}(#{ args.join(', ') })" compilation # `super()` is converted into a call against the superclass's implementation @@ -760,7 +762,7 @@ exports.ClassNode = class ClassNode extends BaseNode construct = @idt() + (new AssignNode(@variable, constructor)).compile(merge o, {sharedScope: constScope}) + ';' props = if !props.empty() then '\n' + props.compile(o) else '' extension = if extension then '\n' + @idt() + extension.compile(o) + ';' else '' - returns = if @returns then '\n' + new ReturnNode(@variable).compile(o) else '' + returns = if @returns then '\n' + (new ReturnNode(@variable)).compile(o) else '' construct + extension + props + returns #### AssignNode @@ -850,7 +852,7 @@ exports.AssignNode = class AssignNode extends BaseNode else idx = literal(if splat then "#{valVar}.length - #{olength - idx}" else idx) if typeof idx isnt 'object' val = new ValueNode(literal(valVar), [new accessClass(idx)]) - assigns.push(new AssignNode(obj, val).compile(o)) + assigns.push((new AssignNode(obj, val)).compile(o)) code = assigns.join("\n") code @@ -1068,7 +1070,7 @@ exports.WhileNode = class WhileNode extends BaseNode pre = "#{set}#{@tab}while (#{cond})" @body = Expressions.wrap([new IfNode(@guard, @body)]) if @guard if @returns - post = '\n' + new ReturnNode(literal(rvar)).compile(merge(o, indent: @idt())) + post = '\n' + (new ReturnNode(literal(rvar))).compile merge o, indent: @idt() else post = '' "#{pre} {\n#{ @body.compile(o) }\n#{@tab}}#{post}" @@ -1342,7 +1344,7 @@ exports.ForNode = class ForNode extends BaseNode this compileReturnValue: (val, o) -> - return '\n' + new ReturnNode(literal(val)).compile(o) if @returns + return '\n' + (new ReturnNode(literal(val))).compile(o) if @returns return '\n' + val if val '' @@ -1372,7 +1374,7 @@ exports.ForNode = class ForNode extends BaseNode svar = scope.freeVariable() sourcePart = "#{svar} = #{ @source.compile(o) };" if @pattern - namePart = new AssignNode(@name, literal("#{svar}[#{ivar}]")).compile(merge o, {indent: @idt(1), top: true}) + '\n' + namePart = (new AssignNode(@name, literal("#{svar}[#{ivar}]"))).compile(merge o, {indent: @idt(1), top: true}) + '\n' else namePart = "#{name} = #{svar}[#{ivar}]" if name unless @object diff --git a/test/test_classes.coffee b/test/test_classes.coffee index ab9d7e58..1dae1ac7 100644 --- a/test/test_classes.coffee +++ b/test/test_classes.coffee @@ -234,4 +234,31 @@ obj = method: -> 'value' instance = new obj.klass -ok instance.method() is 'value' \ No newline at end of file +ok instance.method() is 'value' + + +# Ensure that nested classes are safely wrapped in parentheses when instantiated +# to avoid JS problems with operator precedence: +class1 = -> + @name = 'class1' + this + +class1.class2 = -> + @name = 'class2' + this + +factory = (arg) -> + return { class2: class1.class2 } + +obj1 = new class1 +obj2_1 = new class1.class2 +obj2_2 = new factory('dummy').class2 +obj2_3 = new (factory('dummy')).class2 +obj2_4 = new (factory('dummy').class2) + +ok obj1.name is 'class1' +ok obj2_1.name is 'class2' +ok obj2_2.name is 'class2' +ok obj2_3.name is 'class2' +ok obj2_4.name is 'class2' +ok obj2_2.class2 is undefined