Skip to content

Commit 5d13d14

Browse files
author
Marc Häfner
committed
Closes #3008 -- Fix scope of constructor reference
* Make scope of `Code` nodes accessible (prior to `compileNode`) * Use correct scope for reference of external constructor. * Remove unreachable code.
1 parent 1df8abf commit 5d13d14

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

lib/coffee-script/nodes.js

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ exports.Class = class Class extends Base
10311031
if func instanceof Code
10321032
assign = @ctor = func
10331033
else
1034-
@externalCtor = o.scope.freeVariable 'class'
1034+
@externalCtor = o.classScope.freeVariable 'class'
10351035
assign = new Assign new Literal(@externalCtor), func
10361036
else
10371037
if assign.variable.this
@@ -1098,18 +1098,18 @@ exports.Class = class Class extends Base
10981098
lname = new Literal name
10991099
func = new Code [], Block.wrap [@body]
11001100
args = []
1101+
o.classScope = func.makeScope o.scope
11011102

11021103
@hoistDirectivePrologue()
11031104
@setContext name
11041105
@walkBody name, o
11051106
@ensureConstructor name
11061107
@addBoundFunctions o
11071108
@body.spaced = yes
1108-
@body.expressions.unshift @ctor unless @ctor instanceof Code
11091109
@body.expressions.push lname
11101110

11111111
if @parent
1112-
superClass = new Literal o.scope.freeVariable 'super', no
1112+
superClass = new Literal o.classScope.freeVariable 'super', no
11131113
@body.expressions.unshift new Extends lname, superClass
11141114
func.params.push new Param superClass
11151115
args.push @parent
@@ -1305,6 +1305,8 @@ exports.Code = class Code extends Base
13051305

13061306
jumps: NO
13071307

1308+
makeScope: (parentScope) -> new Scope parentScope, @body, this
1309+
13081310
# Compilation creates a new scope unless explicitly asked to share with the
13091311
# outer scope. Handles splat parameters in the parameter list by peeking at
13101312
# the JavaScript `arguments` object. If the function is bound with the `=>`
@@ -1320,7 +1322,7 @@ exports.Code = class Code extends Base
13201322
boundfunc.updateLocationDataIfMissing @locationData
13211323
return boundfunc.compileNode(o)
13221324

1323-
o.scope = new Scope o.scope, @body, this
1325+
o.scope = del(o, 'classScope') or @makeScope o.scope
13241326
o.scope.shared = del(o, 'sharedScope')
13251327
o.indent += TAB
13261328
delete o.bare

0 commit comments

Comments
 (0)