Skip to content

Commit de0e3ba

Browse files
committed
Merge pull request #3237 from marchaefner/fixSuper
Fix super-related tagging of `Code` nodes
2 parents e019575 + aea0f25 commit de0e3ba

File tree

3 files changed

+52
-29
lines changed

3 files changed

+52
-29
lines changed

lib/coffee-script/nodes.js

+12-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

+19-21
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,10 @@ exports.Value = class Value extends Base
509509
isSplice: ->
510510
last(@properties) instanceof Slice
511511

512+
looksStatic: (className) ->
513+
@base.value is className and @properties.length and
514+
@properties[0].name?.value isnt 'prototype'
515+
512516
# The value can be unwrapped as its inner node, if there are no attached
513517
# properties.
514518
unwrap: ->
@@ -1036,8 +1040,6 @@ exports.Class = class Class extends Base
10361040
else
10371041
if assign.variable.this
10381042
func.static = yes
1039-
if func.bound
1040-
func.context = name
10411043
else
10421044
assign.variable = new Value(new Literal(name), [(new Access new Literal 'prototype'), new Access base])
10431045
if func instanceof Code and func.bound
@@ -1046,14 +1048,17 @@ exports.Class = class Class extends Base
10461048
assign
10471049
compact exprs
10481050

1049-
# Walk the body of the class, looking for prototype properties to be converted.
1051+
# Walk the body of the class, looking for prototype properties to be converted
1052+
# and tagging static assignments.
10501053
walkBody: (name, o) ->
10511054
@traverseChildren false, (child) =>
10521055
cont = true
10531056
return false if child instanceof Class
10541057
if child instanceof Block
10551058
for node, i in exps = child.expressions
1056-
if node instanceof Value and node.isObject(true)
1059+
if node instanceof Assign and node.variable.looksStatic name
1060+
node.value.static = yes
1061+
else if node instanceof Value and node.isObject(true)
10571062
cont = false
10581063
exps[i] = @addProperties node, name, o
10591064
child.expressions = exps = flatten exps
@@ -1164,8 +1169,8 @@ exports.Assign = class Assign extends Base
11641169
else
11651170
o.scope.find name
11661171
if @value instanceof Code and match = METHOD_DEF.exec name
1167-
@value.klass = match[1] if match[1]
1168-
@value.name = match[2] ? match[3] ? match[4] ? match[5]
1172+
@value.klass = match[1] if match[2]
1173+
@value.name = match[3] ? match[4] ? match[5]
11691174
val = @value.compileToFragments o, LEVEL_LIST
11701175
return (compiledName.concat @makeCode(": "), val) if @context is 'object'
11711176
answer = compiledName.concat @makeCode(" #{ @context or '=' } "), val
@@ -2141,21 +2146,14 @@ NUMBER = ///^[+-]?(?:
21412146
\d*\.?\d+ (?:e[+-]?\d+)? # decimal
21422147
)$///i
21432148

2144-
METHOD_DEF = ///
2145-
^
2146-
(?:
2147-
(#{IDENTIFIER_STR})
2148-
\.prototype
2149-
(?:
2150-
\.(#{IDENTIFIER_STR})
2151-
| \[("(?:[^\\"\r\n]|\\.)*"|'(?:[^\\'\r\n]|\\.)*')\]
2152-
| \[(0x[\da-fA-F]+ | \d*\.?\d+ (?:[eE][+-]?\d+)?)\]
2153-
)
2154-
)
2155-
|
2156-
(#{IDENTIFIER_STR})
2157-
$
2158-
///
2149+
METHOD_DEF = /// ^
2150+
(#{IDENTIFIER_STR})
2151+
(\.prototype)?
2152+
(?: \.(#{IDENTIFIER_STR})
2153+
| \[("(?:[^\\"\r\n]|\\.)*"|'(?:[^\\'\r\n]|\\.)*')\]
2154+
| \[(0x[\da-fA-F]+ | \d*\.?\d+ (?:[eE][+-]?\d+)?)\]
2155+
)
2156+
$ ///
21592157

21602158
# Is a literal value a string/regex?
21612159
IS_STRING = /^['"]/

test/classes.coffee

+21
Original file line numberDiff line numberDiff line change
@@ -807,3 +807,24 @@ test "#3063: Class bodies cannot contain pure statements", ->
807807
return if S.f
808808
@f: => this
809809
"""
810+
811+
test "#2949: super in static method with reserved name", ->
812+
class Foo
813+
@static: -> 'baz'
814+
815+
class Bar extends Foo
816+
@static: -> super
817+
818+
eq Bar.static(), 'baz'
819+
820+
test "#3232: super in static methods (not object-assigned)", ->
821+
class Foo
822+
@baz = -> true
823+
@qux = -> true
824+
825+
class Bar extends Foo
826+
@baz = -> super
827+
Bar.qux = -> super
828+
829+
ok Bar.baz()
830+
ok Bar.qux()

0 commit comments

Comments
 (0)