From 6f47364392d2cc42836a382a57341ab92598f331 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 13 Nov 2010 18:02:50 -0500 Subject: [PATCH] done refactoring Class for now... --- Cakefile | 2 +- lib/nodes.js | 51 ++++++++++++++++++------------------------------ src/nodes.coffee | 31 ++++++++++++----------------- 3 files changed, 33 insertions(+), 51 deletions(-) diff --git a/Cakefile b/Cakefile index 7d2dd93f..9459a10c 100644 --- a/Cakefile +++ b/Cakefile @@ -149,7 +149,7 @@ runTests = (CoffeeScript) -> CoffeeScript.run code.toString(), {fileName} catch err failedTests += 1 - log "failed #{fileName}", red, '\n' + err.stack.toString() + log "failed #{fileName}", red, '\n' + err.stack?.toString() task 'test', 'run the CoffeeScript language test suite', -> diff --git a/lib/nodes.js b/lib/nodes.js index d9eb08a9..49232c5d 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -881,41 +881,28 @@ return _results; }; Class.prototype.walkBody = function(name) { - var exps, _fn, _len, _ref; - _ref = exps = this.body.expressions; - _fn = function(node, i) { - if (node instanceof Value && node.isObject(true)) { - return exps[i] = compact(this.addProperties(node, name)); - } else if (node instanceof Code) { - if (this.ctor) { - throw new Error('cannot define more than one constructor in a class'); - } - if (node.bound) { - throw new Error('cannot define a constructor as a bound function'); - } - this.ctor = node; - return exps[i] = null; - } else { - return node.traverseChildren(false, __bind(function(n2) { - var expr2, j, _len, _ref; - if (n2 instanceof Expressions) { - _ref = n2.expressions; - for (j = 0, _len = _ref.length; j < _len; j++) { - expr2 = _ref[j]; - if (expr2 instanceof Value && expr2.isObject(true)) { - n2.expressions[j] = compact(this.addProperties(expr2, name)); - } + return this.traverseChildren(false, __bind(function(child) { + var exps, i, node, _len, _ref; + if (child instanceof Expressions) { + _ref = exps = child.expressions; + for (i = 0, _len = _ref.length; i < _len; i++) { + node = _ref[i]; + if (node instanceof Value && node.isObject(true)) { + exps[i] = compact(this.addProperties(node, name)); + } else if (node instanceof Code) { + if (this.ctor) { + throw new Error('cannot define more than one constructor in a class'); } - return n2.expressions = flatten(n2.expressions); + if (node.bound) { + throw new Error('cannot define a constructor as a bound function'); + } + this.ctor = node; + exps[i] = null; } - }, this)); + } + return child.expressions = exps = compact(flatten(exps)); } - }; - for (i = 0, _len = _ref.length; i < _len; i++) { - node = _ref[i]; - _fn.call(this, node, i); - } - return this.body.expressions = exps = compact(flatten(exps)); + }, this)); }; Class.prototype.ensureConstructor = function(name) { if (!this.ctor) { diff --git a/src/nodes.coffee b/src/nodes.coffee index 6146c0e5..6e2bbb0b 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -712,24 +712,19 @@ exports.Class = class Class extends Base # Walk the body of the class, looking for prototype properties to be converted. walkBody: (name) -> - for node, i in exps = @body.expressions - if node instanceof Value and node.isObject(true) - exps[i] = compact @addProperties node, name - else if node instanceof Code - if @ctor - throw new Error 'cannot define more than one constructor in a class' - if node.bound - throw new Error 'cannot define a constructor as a bound function' - @ctor = node - exps[i] = null - else - node.traverseChildren false, (n2) => - if n2 instanceof Expressions - for expr2, j in n2.expressions - if expr2 instanceof Value and expr2.isObject(true) - n2.expressions[j] = compact @addProperties expr2, name - n2.expressions = flatten n2.expressions - @body.expressions = exps = compact flatten exps + @traverseChildren false, (child) => + if child instanceof Expressions + for node, i in exps = child.expressions + if node instanceof Value and node.isObject(true) + exps[i] = compact @addProperties node, name + else if node instanceof Code + if @ctor + throw new Error 'cannot define more than one constructor in a class' + if node.bound + throw new Error 'cannot define a constructor as a bound function' + @ctor = node + exps[i] = null + child.expressions = exps = compact flatten exps # Make sure that a constructor is defined for the class, and properly # configured.