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

Adding anonymous class support (gets a temporary variable for a name.) Issue #548

This commit is contained in:
Jeremy Ashkenas 2010-07-27 22:05:55 -04:00
parent cc7b0f2e8f
commit 8d544ce80b
6 changed files with 147 additions and 129 deletions

View file

@ -226,6 +226,8 @@
return new ClassNode($2, null, $4);
}), o("CLASS SimpleAssignable EXTENDS Value INDENT ClassBody OUTDENT", function() {
return new ClassNode($2, $4, $6);
}), o("CLASS INDENT ClassBody OUTDENT", function() {
return new ClassNode('__temp__', null, $3);
})
],
ClassAssign: [

View file

@ -94,7 +94,7 @@
};
BaseNode.prototype.containsPureStatement = function() {
return this.isPureStatement() || this.contains(function(n) {
return n.isPureStatement();
return n.isPureStatement && n.isPureStatement();
});
};
BaseNode.prototype.traverse = function(block) {
@ -756,6 +756,9 @@
};
ClassNode.prototype.compileNode = function(o) {
var _b, _c, _d, _e, access, applied, className, constScope, construct, constructor, extension, func, me, pname, prop, props, pvar, returns, val;
if (this.variable === '__temp__') {
this.variable = literal(o.scope.freeVariable());
}
extension = this.parent && new ExtendsNode(this.variable, this.parent);
props = new Expressions();
o.top = true;

File diff suppressed because one or more lines are too long

View file

@ -277,6 +277,7 @@ grammar = {
o "CLASS SimpleAssignable EXTENDS Value", -> new ClassNode $2, $4
o "CLASS SimpleAssignable INDENT ClassBody OUTDENT", -> new ClassNode $2, null, $4
o "CLASS SimpleAssignable EXTENDS Value INDENT ClassBody OUTDENT", -> new ClassNode $2, $4, $6
o "CLASS INDENT ClassBody OUTDENT", -> new ClassNode '__temp__', null, $3
]
# Assignments that can happen directly inside a class declaration.

View file

@ -104,7 +104,7 @@ exports.BaseNode = class BaseNode
# Convenience for the most common use of contains. Does the node contain
# a pure statement?
containsPureStatement: ->
@isPureStatement() or @contains (n) -> n.isPureStatement()
@isPureStatement() or @contains (n) -> n.isPureStatement and n.isPureStatement()
# Perform an in-order traversal of the AST. Crosses scope boundaries.
traverse: (block) -> @traverseChildren true, block
@ -688,6 +688,7 @@ exports.ClassNode = class ClassNode extends BaseNode
# equivalent syntax tree and compile that, in pieces. You can see the
# constructor, property assignments, and inheritance getting built out below.
compileNode: (o) ->
@variable = literal o.scope.freeVariable() if @variable is '__temp__'
extension = @parent and new ExtendsNode(@variable, @parent)
props = new Expressions
o.top = true

View file

@ -218,3 +218,12 @@ func class Test
ok (new Test).prop is 'value'
ok (new Test).prop2 is 'value2'
# Test anonymous classes.
obj =
klass: class
method: -> 'value'
instance = new obj.klass
ok instance.method() is 'value'