allowing classes to extend expressions; fixes #1482

This commit is contained in:
Michael Ficarra 2011-08-07 05:02:01 -04:00
parent 35a30fbd6d
commit 7cf5988099
6 changed files with 30 additions and 43 deletions

View File

@ -210,17 +210,17 @@
return new Class;
}), o('CLASS Block', function() {
return new Class(null, null, $2);
}), o('CLASS EXTENDS Value', function() {
}), o('CLASS EXTENDS Expression', function() {
return new Class(null, $3);
}), o('CLASS EXTENDS Value Block', function() {
}), o('CLASS EXTENDS Expression Block', function() {
return new Class(null, $3, $4);
}), o('CLASS SimpleAssignable', function() {
return new Class($2);
}), o('CLASS SimpleAssignable Block', function() {
return new Class($2, null, $3);
}), o('CLASS SimpleAssignable EXTENDS Value', function() {
}), o('CLASS SimpleAssignable EXTENDS Expression', function() {
return new Class($2, $4);
}), o('CLASS SimpleAssignable EXTENDS Value Block', function() {
}), o('CLASS SimpleAssignable EXTENDS Expression Block', function() {
return new Class($2, $4, $5);
})
],

View File

@ -231,7 +231,7 @@
} else if (top) {
node.front = true;
code = node.compile(o);
codes.push(node.isStatement(o) ? code : this.tab + code + ';');
codes.push(node.isStatement(o) ? code : "" + this.tab + code + ";");
} else {
codes.push(node.compile(o, LEVEL_LIST));
}
@ -354,7 +354,7 @@
}
};
Return.prototype.compileNode = function(o) {
return this.tab + ("return" + (this.expression ? ' ' + this.expression.compile(o, LEVEL_PAREN) : '') + ";");
return this.tab + ("return" + [this.expression ? " " + (this.expression.compile(o, LEVEL_PAREN)) : void 0] + ";");
};
return Return;
})();

File diff suppressed because one or more lines are too long

View File

@ -279,14 +279,14 @@ grammar =
# Class definitions have optional bodies of prototype property assignments,
# and optional references to the superclass.
Class: [
o 'CLASS', -> new Class
o 'CLASS Block', -> new Class null, null, $2
o 'CLASS EXTENDS Value', -> new Class null, $3
o 'CLASS EXTENDS Value Block', -> new Class null, $3, $4
o 'CLASS SimpleAssignable', -> new Class $2
o 'CLASS SimpleAssignable Block', -> new Class $2, null, $3
o 'CLASS SimpleAssignable EXTENDS Value', -> new Class $2, $4
o 'CLASS SimpleAssignable EXTENDS Value Block', -> new Class $2, $4, $5
o 'CLASS', -> new Class
o 'CLASS Block', -> new Class null, null, $2
o 'CLASS EXTENDS Expression', -> new Class null, $3
o 'CLASS EXTENDS Expression Block', -> new Class null, $3, $4
o 'CLASS SimpleAssignable', -> new Class $2
o 'CLASS SimpleAssignable Block', -> new Class $2, null, $3
o 'CLASS SimpleAssignable EXTENDS Expression', -> new Class $2, $4
o 'CLASS SimpleAssignable EXTENDS Expression Block', -> new Class $2, $4, $5
]
# Ordinary function invocation, or a chained series of calls.

View File

@ -222,7 +222,7 @@ exports.Block = class Block extends Base
else if top
node.front = true
code = node.compile o
codes.push if node.isStatement o then code else @tab + code + ';'
codes.push if node.isStatement o then code else "#{@tab}#{code};"
else
codes.push node.compile o, LEVEL_LIST
return codes.join '\n' if top
@ -328,7 +328,7 @@ exports.Return = class Return extends Base
if expr and expr not instanceof Return then expr.compile o, level else super o, level
compileNode: (o) ->
@tab + "return#{ if @expression then ' ' + @expression.compile(o, LEVEL_PAREN) else '' };"
@tab + "return#{[" #{@expression.compile o, LEVEL_PAREN}" if @expression]};"
#### Value

View File

@ -503,3 +503,10 @@ test "#1464: bound class methods should keep context", ->
test "#1009: classes with reserved words as determined names", ->
eq 'function', typeof (-> class @for).call {}
test "#1482: classes can extend expressions", ->
id = (x) -> x
nonce = {}
class A then nonce: nonce
class B extends id A
eq nonce, (new B).nonce