From 117204a7846324b3d890bfe5b905bc153b21c398 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 21 Aug 2010 19:16:02 -0400 Subject: [PATCH] Revert "Issue #619. 'new' operator misbehavior." This reverts commit e7834de92974f997e692dd1c11f735a71078666f. --- lib/cake.js | 2 +- lib/command.js | 2 +- lib/grammar.js | 4 ++-- lib/nodes.js | 24 +++++++----------------- src/grammar.coffee | 4 ++-- src/nodes.coffee | 14 ++++++-------- test/test_classes.coffee | 29 +---------------------------- 7 files changed, 20 insertions(+), 59 deletions(-) diff --git a/lib/cake.js b/lib/cake.js index 9908284d..2fbf2d88 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 2e2b1b0d..eb67b4d6 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 415b7618..b58e536e 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 ed5579a9..793118b5 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, value; + var _b, _c, _d, _e, _f, _g, _h, arg, args, compilation; if (!(o.chainRoot)) { o.chainRoot = this; } @@ -504,17 +504,7 @@ } return _e; }).call(this); - 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); + compilation = this.isSuper ? this.compileSuper(args.join(', '), o) : ("" + (this.prefix()) + (this.variable.compile(o)) + "(" + (args.join(', ')) + ")"); } return compilation; }; @@ -886,7 +876,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; @@ -990,7 +980,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; @@ -1254,7 +1244,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 { @@ -1591,7 +1581,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; @@ -1640,7 +1630,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 fe4a7a94..eb33dc9e 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 0f8bedc7..00f3fb70 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -449,9 +449,7 @@ exports.CallNode = class CallNode extends BaseNode compilation = if @isSuper @compileSuper(args.join(', '), o) else - value = @variable.compile o - value = "(#{value})" if @isNew and @variable instanceof ValueNode and @variable.hasProperties() - "#{@prefix()}#{value}(#{ args.join(', ') })" + "#{@prefix()}#{@variable.compile(o)}(#{ args.join(', ') })" compilation # `super()` is converted into a call against the superclass's implementation @@ -762,7 +760,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 @@ -852,7 +850,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 @@ -1070,7 +1068,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}" @@ -1344,7 +1342,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 '' @@ -1374,7 +1372,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 1dae1ac7..ab9d7e58 100644 --- a/test/test_classes.coffee +++ b/test/test_classes.coffee @@ -234,31 +234,4 @@ obj = method: -> 'value' instance = new obj.klass -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 +ok instance.method() is 'value' \ No newline at end of file