Skip to content

Fix super-related tagging of Code nodes #3237

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

Merged
merged 2 commits into from
Nov 15, 2013
Merged
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
20 changes: 12 additions & 8 deletions lib/coffee-script/nodes.js

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

40 changes: 19 additions & 21 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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: ->
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -1164,8 +1169,8 @@ exports.Assign = class Assign extends Base
else
o.scope.find name
if @value instanceof Code and match = METHOD_DEF.exec name
@value.klass = match[1] if match[1]
@value.name = match[2] ? match[3] ? match[4] ? match[5]
@value.klass = match[1] if match[2]
@value.name = match[3] ? match[4] ? match[5]
val = @value.compileToFragments o, LEVEL_LIST
return (compiledName.concat @makeCode(": "), val) if @context is 'object'
answer = compiledName.concat @makeCode(" #{ @context or '=' } "), val
Expand Down Expand Up @@ -2141,21 +2146,14 @@ NUMBER = ///^[+-]?(?:
\d*\.?\d+ (?:e[+-]?\d+)? # decimal
)$///i

METHOD_DEF = ///
^
(?:
(#{IDENTIFIER_STR})
\.prototype
(?:
\.(#{IDENTIFIER_STR})
| \[("(?:[^\\"\r\n]|\\.)*"|'(?:[^\\'\r\n]|\\.)*')\]
| \[(0x[\da-fA-F]+ | \d*\.?\d+ (?:[eE][+-]?\d+)?)\]
)
)
|
(#{IDENTIFIER_STR})
$
///
METHOD_DEF = /// ^
(#{IDENTIFIER_STR})
(\.prototype)?
(?: \.(#{IDENTIFIER_STR})
| \[("(?:[^\\"\r\n]|\\.)*"|'(?:[^\\'\r\n]|\\.)*')\]
| \[(0x[\da-fA-F]+ | \d*\.?\d+ (?:[eE][+-]?\d+)?)\]
)
$ ///

# Is a literal value a string/regex?
IS_STRING = /^['"]/
Expand Down
21 changes: 21 additions & 0 deletions test/classes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -807,3 +807,24 @@ test "#3063: Class bodies cannot contain pure statements", ->
return if S.f
@f: => this
"""

test "#2949: super in static method with reserved name", ->
class Foo
@static: -> 'baz'

class Bar extends Foo
@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()