1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

FIXES #390: super() calls in constructor of classes that are defined as object properties

This commit is contained in:
Stan Angeloff 2010-05-25 09:15:46 +03:00
parent 0288dba46c
commit aba19841ee
3 changed files with 39 additions and 21 deletions

View file

@ -1,5 +1,5 @@
(function(){
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, CurryNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, IndexNode, LiteralNode, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, _a, compact, del, flatten, helpers, index_of, literal, merge, utility;
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, CurryNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, IndexNode, LiteralNode, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, _a, compact, del, flatten, helpers, include, index_of, literal, merge, utility;
var __extends = function(child, parent) {
var ctor = function(){ };
ctor.prototype = parent.prototype;
@ -31,6 +31,7 @@
flatten = _a.flatten;
merge = _a.merge;
del = _a.del;
include = _a.include;
index_of = _a.index_of;
//### BaseNode
// The **BaseNode** is the abstract base class for all nodes in the syntax tree.
@ -962,7 +963,10 @@
pvar = _e[0];
func = _e[1];
if (pvar && pvar.base.value === 'constructor' && func instanceof CodeNode) {
func.name = this.variable.compile(o);
func.body.push(new ReturnNode(literal('this')));
this.variable = new ValueNode(this.variable);
this.variable.namespaced = include(func.name, '.');
constructor = new AssignNode(this.variable, func);
} else {
if (pvar) {
@ -1049,7 +1053,7 @@
if (this.context === 'object') {
return ("" + name + ": " + val);
}
if (!(this.is_value() && this.variable.has_properties())) {
if (!(this.is_value() && (this.variable.has_properties() || this.variable.namespaced))) {
o.scope.find(name);
}
val = ("" + name + " = " + val);

View file

@ -14,7 +14,7 @@ else
Scope: this.Scope
# Import the helpers we plan to use.
{compact, flatten, merge, del, index_of}: helpers
{compact, flatten, merge, del, include, index_of}: helpers
#### BaseNode
@ -649,7 +649,10 @@ exports.ClassNode: class ClassNode extends BaseNode
for prop in @properties
[pvar, func]: [prop.variable, prop.value]
if pvar and pvar.base.value is 'constructor' and func instanceof CodeNode
func.name: @variable.compile(o)
func.body.push(new ReturnNode(literal('this')))
@variable: new ValueNode @variable
@variable.namespaced: include func.name, '.'
constructor: new AssignNode(@variable, func)
else
if pvar
@ -719,7 +722,7 @@ exports.AssignNode: class AssignNode extends BaseNode
@value.proto: proto if proto
val: @value.compile o
return "$name: $val" if @context is 'object'
o.scope.find name unless @is_value() and @variable.has_properties()
o.scope.find name unless @is_value() and (@variable.has_properties() or @variable.namespaced)
val: "$name = $val"
return "$@tab$val;" if stmt
if top then val else "($val)"

View file

@ -1,17 +1,28 @@
# Issue #380: problem with @ and instanceof
class ClassName
am_i: ->
@ instanceof ClassName
obj: new ClassName()
ok obj.am_i()
# Issue #383: Numbers that start with . not recognized
value: .25 + .75
ok value is 1
value: 0.0 + -.25 - -.75 + 0.0
ok value is 0.5
deepEqual [0..10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
deepEqual [0...10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Issue #380: problem with @ and instanceof
class ClassName
am_i: ->
@ instanceof ClassName
obj: new ClassName()
ok obj.am_i()
# Issue #383: Numbers that start with . not recognized
value: .25 + .75
ok value is 1
value: 0.0 + -.25 - -.75 + 0.0
ok value is 0.5
deepEqual [0..10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
deepEqual [0...10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Issue #390: super() calls in constructor of classes that are defined as object properties
class Hive
constructor: (name) -> @name: name
class Hive.Bee extends Hive
constructor: (name) -> super name
maya: new Hive.Bee('Maya')
ok maya.name is 'Maya'