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

Fixes #3232 -- Tag all class properties static

(and remove duplicate `context` assigment)
This commit is contained in:
Marc Häfner 2013-11-12 17:00:20 +01:00
parent 138c25fe5f
commit aea0f2533b
3 changed files with 29 additions and 8 deletions

View file

@ -759,6 +759,11 @@
return last(this.properties) instanceof Slice;
};
Value.prototype.looksStatic = function(className) {
var _ref2;
return this.base.value === className && this.properties.length && ((_ref2 = this.properties[0].name) != null ? _ref2.value : void 0) !== 'prototype';
};
Value.prototype.unwrap = function() {
if (this.properties.length) {
return this;
@ -1446,9 +1451,6 @@
} else {
if (assign.variable["this"]) {
func["static"] = true;
if (func.bound) {
func.context = name;
}
} else {
assign.variable = new Value(new Literal(name), [new Access(new Literal('prototype')), new Access(base)]);
if (func instanceof Code && func.bound) {
@ -1477,7 +1479,9 @@
_ref2 = exps = child.expressions;
for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) {
node = _ref2[i];
if (node instanceof Value && node.isObject(true)) {
if (node instanceof Assign && node.variable.looksStatic(name)) {
node.value["static"] = true;
} else if (node instanceof Value && node.isObject(true)) {
cont = false;
exps[i] = _this.addProperties(node, name, o);
}

View file

@ -509,6 +509,10 @@ exports.Value = class Value extends Base
isSplice: ->
last(@properties) instanceof Slice
looksStatic: (className) ->
@base.value is className and @properties.length and
@properties[0].name?.value isnt 'prototype'
# The value can be unwrapped as its inner node, if there are no attached
# properties.
unwrap: ->
@ -1036,8 +1040,6 @@ exports.Class = class Class extends Base
else
if assign.variable.this
func.static = yes
if func.bound
func.context = name
else
assign.variable = new Value(new Literal(name), [(new Access new Literal 'prototype'), new Access base])
if func instanceof Code and func.bound
@ -1046,14 +1048,17 @@ exports.Class = class Class extends Base
assign
compact exprs
# Walk the body of the class, looking for prototype properties to be converted.
# Walk the body of the class, looking for prototype properties to be converted
# and tagging static assignments.
walkBody: (name, o) ->
@traverseChildren false, (child) =>
cont = true
return false if child instanceof Class
if child instanceof Block
for node, i in exps = child.expressions
if node instanceof Value and node.isObject(true)
if node instanceof Assign and node.variable.looksStatic name
node.value.static = yes
else if node instanceof Value and node.isObject(true)
cont = false
exps[i] = @addProperties node, name, o
child.expressions = exps = flatten exps

View file

@ -816,3 +816,15 @@ test "#2949: super in static method with reserved name", ->
@static: -> super
eq Bar.static(), 'baz'
test "#3232: super in static methods (not object-assigned)", ->
class Foo
@baz = -> true
@qux = -> true
class Bar extends Foo
@baz = -> super
Bar.qux = -> super
ok Bar.baz()
ok Bar.qux()