Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #841: bring back the extended hook #1960

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/coffee-script/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/nodes.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1860,7 +1860,7 @@ UTILITIES =
# Correctly set up a prototype chain for inheritance, including a reference # Correctly set up a prototype chain for inheritance, including a reference
# to the superclass for `super()` calls, and copies of any static properties. # to the superclass for `super()` calls, and copies of any static properties.
extends: -> """ extends: -> """
function(child, parent) { for (var key in parent) { if (#{utility 'hasProp'}.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; } function(child, parent) { for (var key in parent) { if (#{utility 'hasProp'}.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; if(typeof parent.extended == 'function') parent.extended(child); return child; }
""" """


# Create a function bound to the current value of "this". # Create a function bound to the current value of "this".
Expand Down
69 changes: 40 additions & 29 deletions test/classes.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -272,22 +272,22 @@ test "nothing classes", ->


c = class c = class
ok c instanceof Function ok c instanceof Function


test "classes with static-level implicit objects", -> test "classes with static-level implicit objects", ->

class A class A
@static = one: 1 @static = one: 1
two: 2 two: 2

class B class B
@static = one: 1, @static = one: 1,
two: 2 two: 2

eq A.static.one, 1 eq A.static.one, 1
eq A.static.two, undefined eq A.static.two, undefined
eq (new A).two, 2 eq (new A).two, 2

eq B.static.one, 1 eq B.static.one, 1
eq B.static.two, 2 eq B.static.two, 2
eq (new B).two, undefined eq (new B).two, undefined
Expand Down Expand Up @@ -546,61 +546,72 @@ test "#1598: super works for static methods too", ->
'pass? ' + super 'pass? ' + super


eq Child.method(), 'pass? yes' eq Child.method(), 'pass? yes'

test "#1842: Regression with bound functions within bound class methods", -> test "#1842: Regression with bound functions within bound class methods", ->

class Store class Store
@bound: => @bound: =>
do => do =>
eq this, Store eq this, Store

Store.bound() Store.bound()

# And a fancier case: # And a fancier case:

class Store class Store

eq this, Store eq this, Store

@bound: => @bound: =>
do => do =>
eq this, Store eq this, Store

@unbound: -> @unbound: ->
eq this, Store eq this, Store

instance: => instance: =>
ok this instanceof Store ok this instanceof Store

Store.bound() Store.bound()
Store.unbound() Store.unbound()
(new Store).instance() (new Store).instance()

test "#1876: Class @A extends A", -> test "#1876: Class @A extends A", ->
class A class A
class @A extends A class @A extends A

ok (new @A) instanceof A ok (new @A) instanceof A

test "#1813: Passing class definitions as expressions", -> test "#1813: Passing class definitions as expressions", ->
ident = (x) -> x ident = (x) -> x

result = ident class A then x = 1 result = ident class A then x = 1

eq result, A eq result, A

result = ident class B extends A result = ident class B extends A
x = 1 x = 1

eq result, B eq result, B

test "#494: Named classes", -> test "#494: Named classes", ->

class A class A
eq A.name, 'A' eq A.name, 'A'

class A.B class A.B
eq A.B.name, 'B' eq A.B.name, 'B'

class A.B["C"] class A.B["C"]
ok A.B.C.name isnt 'C' ok A.B.C.name isnt 'C'

test "#841: bring back the `extended` hook", ->
class A
@extended: (subclass) =>
subclass.extended = @extended
subclass.another = @extended
class B extends A
ok Object::hasOwnProperty.call B, 'extended'
eq B.extended, A.extended
ok Object::hasOwnProperty.call B, 'another'
eq B.another, A.extended